ssl SSLError outines:SSL_CTX_use_certificate_chain_file:PEM lib

19,471

Solution 1

I've not run across that specific error before in Python, but it looks very much like OpenSSL choked on your .pem file - I'd speculate that the .pem file you are using isn't the right format for what Python asked OpenSSL to do. I've opened one of my known good .pem files, replaced the my personally identifying information and public/private keys with garbage data so you can get a feel for what a non-password protected .pem should look like.

The PEM file contains both your public and private key. Never post them for others to look at as they then have the ability to see your private keys and could then impersonate you or your servers!

NOTE: The following code listing is an entirely faked, randomized set of data for demonstrating the .pem file format. This sample PEM would fail even the most casual of verifications given the not so hidden "THIS_IS_JUST_RANDOM_CHARACTERS" text where there should be actual key data instead of just random character data.

Bag Attributes
    friendlyName: Apple Development IOS Push Services: com.mycompany.myappidentifier
    localKeyID: 01 23 45 67 89 AB CD EF 01 23 45 67 89 AB CD EF 01 23 45 67 
subject=/UID=com.mycompany.myappidentifier/CN=Apple Development IOS Push Services: com.mycompany.myappidentifier/OU=ABC1234567/C=US
issuer=/C=US/O=Apple Inc./OU=Apple Worldwide Developer Relations/CN=Apple Worldwide Developer Relations Certification Authority
-----BEGIN CERTIFICATE-----
THIS_IS_JUST_RANDOM_CHARACTERS_90f0yGqSm7JmgJ0srvviZpn6cSMoCeymB
t449Y40dVKzDM809kpexmWuMrkjdnfaQmF15lYrpKOAu6t9sZPpFfIocovuW38pH
p4U9Rtt7TC4tVnMHYYk3Dsbu6lqcMeK9sp15Ffw8vcS9AHafNWeY9sjxe8oICcAt
mXV6RCcIjqADwf5yHS283SQ45K2DMQbOQ6ScMckLO1o6SC3VqWnkInMQPeVuLkbS
77BDaRF8X32pOLrwOKyn1CLJLvTDdOb9kWWBVSfvoZwCLinA99Pikc2Cmj0AUzPE
KHc4GvyqXzotjH9HsHnt8TNPEietXPZuILApztEEoAJwPhlJIiwdCkh66KlP7QJt
3l8iGCItGoMttokGGR99d7AaPbIwb7dZPjxc8TTBEoHXrHRrVCm4ogEazFiqfG5R
SCgwK4wyxtMxzgjaUJVq598i7QDmkcQxL8nPyqKv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2GaKNLMJm2c4B7saOyqLbthyI59YkyrM9hTQK3fGqQpKJosgEQyFg4KZrRx
NlkYidUDYy5htWp4kFLW9QmRgoCLtqZyNIOUbdE4dxntPCcGjU5VSGa0ofpxZaMr
pVLC1GGP4r02yP6aZj04njKJmNrvHEK9sr8S452UVwyRIkIfoiBlCkmmRCVnELa8
sraVxhpwmLIQC1E4DCIyFH6pPi7VYOh5pZewBlAiFyaHRThVKBSn11dzl714qyqy
zQaNjHXLP4oWSAEgPHtimCpLgRhhZO2neOwI7ptW7n3WncvW92Fs1Q3FaGujVw0x
NkpEf4pHfUT4ZK4UdsQzVokyMhptMg03ZLNQ7mkNhkrAPmR5Y7K2zDYBtQ45WULg
97mCUJ3TCMffVFd3szYF
-----END CERTIFICATE-----
Bag Attributes
    friendlyName: Common Name From Keychain Access CSR Wizard
    localKeyID: AB CD EF 01 23 45 67 89 AB CD EF 01 23 45 67 89 AB CD EF 01 
Key Attributes: <No Attributes>
-----BEGIN RSA PRIVATE KEY-----
THIS_IS_JUST_RANDOM_CHARACTERS_1YKVrMNhmg68Qt026SWMxtaKiVtHG2yYS
V3gdLgqujtp4rpWONYoLQKhlW923yzrxYuSUhC8LiNDlAjicrJXZVj4vYhwdydSS
QBnDaxc5U2NYev0tCEbPTpYDERAsdE7WTBwaTuEyWEoqTsPQLucohKY2E7UzoZcZ
BttZaX0vLy3W7y22dSKtIghJMEy8MENjeJkET6Xaf4ozC7DNr2g1dBICPT42Hx77
GagtxA2OyxW7qkB8GTnOncXWDbli1y5eYIjb0GZNQVSmGEBl5ykNvhbzpqc53nfN
NJNMO1mEIaDo3GZIVfv3gpiNGpaplofix2DZJOaQBh4s0X3gwc3Y9f1Ta39tYdVh
h04JCDdliIfbjp4T6pDJQeebaBPczOne2gFHMOHGlxcfYbNzlqgBhzdoqH25nKN4
hThTUQioisjUrUayiyPkMAJFdKn37iff1XHOmCFEJqL7eMayeyLk55hWLLBi4Q7I
wUH9rbR2LnfXdCf25jyRbZQx3uKhj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IVATE KEY-----

If your PEM has a different format (perhaps Binary? Missing the public or private key sections? Extra sections not present in this sample?) then you need to run a conversion or re-export your PEM from a PKCS#12 export from Keychain Access in OS X. The conversion step would vary based on what kind of file you actually have, so I can't give you any guidance there without additional information, but I can provide you with instructions for how to go about re-exporting a PEM from Keychain Access in OS X:

  1. Open Keychain Access.
  2. In the 'login' keychain, search the 'My Certificate' or 'All Items' category for 'Apple Production iOS Push Services: your.apps.app.id'
  3. Right-Click the item that is returned from that search and select the 'Export' option.
  4. Save the 'Personal Information Exchange (.p12)' file in an easily accessible location.
  5. Specify a password for the .p12 file that you can remember and follow any on-screen prompts to authenticate with Keychain to export data.
  6. Open Terminal and 'cd' into the directory containing the .p12 from the previous step.
  7. Perform the following command against the .p12 filename: openssl pkcs12 -in FILENAME.p12 -out FILENAME.pem -nodes
  8. When prompted, enter the .p12 file's password you created in step 5 and press enter.
  9. Move the PEM to your server and update any code references if necessary.

Again, this is all speculation based on the error code you listed in your question, however give this a shot and let us know how things go.

Solution 2

As far as I can tell, I got this same error because I neglected to use the -x509 flag in my openssl command. According to the documentation, without this command, openssl only generates a "certificate request", not an actual self-signed certificate, which is what I wanted.

openssl req -x509 -nodes -newkey rsa:2048 -keyout key.pem -out cert.pem -subj "/CN=*.example.com"
              ^
   (make sure you use this)

Please correct me if I'm wrong. I'm just putting this out here in case it helps someone.

Share:
19,471

Related videos on Youtube

el.severo
Author by

el.severo

Updated on September 14, 2022

Comments

  • el.severo
    el.severo over 1 year

    While trying to make a push test to my app I'm getting the following error:

    Traceback (most recent call last):
      File "test.py", line 16, in <module>
        wrapper.notify()
      File "/usr/local/lib/python2.7/dist-packages/APNSWrapper-0.6.1-py2.7.egg/APNSWrapper/notifications.py", line 194, in notify
        apnsConnection.connect(apnsHost, self.apnsPort)
      File "/usr/local/lib/python2.7/dist-packages/APNSWrapper-0.6.1-py2.7.egg/APNSWrapper/connection.py", line 215, in connect
        self.context().connect(host, port)
      File "/usr/local/lib/python2.7/dist-packages/APNSWrapper-0.6.1-py2.7.egg/APNSWrapper/connection.py", line 161, in connect
        self.connectionContext.connect((host, port))
      File "/usr/lib/python2.7/ssl.py", line 331, in connect
        self._real_connect(addr, False)
      File "/usr/lib/python2.7/ssl.py", line 314, in _real_connect
        self.ca_certs, self.ciphers)
    ssl.SSLError: [Errno 336445449] _ssl.c:365: error:140DC009:SSL routines:SSL_CTX_use_certificate_chain_file:PEM lib
    

    And my script looks like:

    #!/usr/bin/env python
    from APNSWrapper import *
    
    deviceToken = '****************************************************************'
    
    # create wrapper
    wrapper = APNSNotificationWrapper('cert.pem', True)
    
    # create message
    message = APNSNotification()
    message.token(deviceToken)
    message.badge(28)
    
    # add message to tuple and send it to APNS server
    wrapper.append(message)
    wrapper.notify()
    

    What is the cause of this error?