djacobs / PyAPNs

Python library for interacting with the Apple Push Notification service (APNs)
http://pypi.python.org/pypi/apns/
MIT License
1.22k stars 374 forks source link

Certificate problem #187

Closed thomashrabe closed 7 years ago

thomashrabe commented 7 years ago

Hi, I know this might be due to lack of better knowledge, but I seem not to be able to get this running. Can you please help me setting up the certificates?

I exported the Apple push certificate and the private key into cert.p12 and key.p12 respectively on my mac. Then I ran

openssl pkcs12 -nocerts -out key.pem -in key.p12 
openssl pkcs12 -clcerts -nokeys -out cert.pem -in cert.p12 

and uploaded them to server

Here's my modified apns-send.py

certificate_file = '/path/to/apnsCerts/cert.pem' key_file = '/path/to/apnsCerts/key.p12'

I try

./apns-send.py -p fc0112d3936f738d9d4c197c50dbf80304ab13fca4ab19d539ecacf65ce58b34 -m 'Hello World'

yet it fails with

Traceback (most recent call last):
  File "./apns-send.py", line 45, in <module>
    apns.gateway_server.send_notification(options.push_token, payload)
  File "/buyo/push/ios/apns.py", line 543, in send_notification
    self.write(self._get_notification(token_hex, payload))
  File "/buyo/push/ios/apns.py", line 267, in write
    return self._connection().write(string)
  File "/buyo/push/ios/apns.py", line 248, in _connection
    self._connect()
  File "/buyo/push/ios/apns.py", line 224, in _connect
    self._ssl = wrap_socket(self._socket, self.key_file, self.cert_file)
  File "/usr/lib/python2.7/ssl.py", line 487, in wrap_socket
    ciphers=ciphers)
  File "/usr/lib/python2.7/ssl.py", line 241, in __init__
    ciphers)
ssl.SSLError: [Errno 336265218] _ssl.c:355: error:140B0002:SSL routines:SSL_CTX_use_PrivateKey_file:system lib

I really appreciate any help / suggestions where it could fail. Thanks heaps in advance!

thomashrabe commented 7 years ago
openssl pkcs12 -in cert.p12 -out cert.pem -clcerts -nokeys 
openssl pkcs12 -in key.p12  -out key.pem -nocerts  
openssl rsa -in key.pem -out keyNoPasswd.pem

cat keyNoPasswd.pem > mergedPushCertificate.pem
cat cert.pem >> mergedPushCertificate.pem

Use mergedPushCertificate.pem for both cert and key parameters of the main script. Probably would be worth adding this into some kind of documentation...