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

ssl.SSLError when connecting using push certificates certificates #68

Closed adam-vioozer closed 10 years ago

adam-vioozer commented 10 years ago

Hi,

First - thanks for the much-needed package!

When I am trying to send a push notification via the packege, I get SSL errors:

p12 = '/path/to/dev.p12'
p12_nopass = '/path/to/dev_nopass.p12'
pem = '/path/to/dev.pem'
cer = '/path/to/aps_development.cer'     # From Apple's dev site

apns = APNs(use_sandbox=True,
        cert_file=pem,
        key_file=p12_nopass)

# Send a notification
# Dummy token, but it does not cause the error (SSL fails before token check)
token_hex = 'b5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32812f4850b87'
payload = Payload(alert="Hello World!", sound="default", badge=1)
apns.gateway_server.send_notification(token_hex, payload)

Which gives:

Traceback (most recent call last):
  File "apple_push_driver.py", line 18, in <module>
    apns.gateway_server.send_notification(token_hex, payload)
  File "/path_to/site-packages/apns.py", line 381, in send_notification
    self.write(self._get_notification(token_hex, payload))
  File "/path_to/apns.py", line 174, in write
    return self._connection().write(string)
  File "/path_to/apns.py", line 167, in _connection
    self._connect()
  File "/path_to/apns.py", line 151, in _connect
    self._ssl = wrap_socket(self._socket, self.key_file, self.cert_file)
  File "/path_to/ssl.py", line 387, in wrap_socket
    ciphers=ciphers)
  File "/path_to/ssl.py", line 141, in __init__
ciphers)
ssl.SSLError: [Errno 336265225] _ssl.c:351: error:140B0009:SSL routines:SSL_CTX_use_PrivateKey_file:PEM lib

I have posted the issue in StackOverflow, but thought it might be relevant here.

http://stackoverflow.com/questions/23457302/ssl-sslerror-when-sending-push-message-using-pyapns

CTassisF commented 10 years ago

If I'm not wrong you should convert all your files (.p12 private key and .cer public key) to PEM, then PyAPNs will work fine. PEM files are plain text and should start with "-----BEGIN RSA PRIVATE KEY-----" or "-----BEGIN CERTIFICATE-----".

adam-vioozer commented 10 years ago

True, that was the issue.

  1. Would you like me to add this to the documentation (a PR to README.md)?
  2. Do you think we could display a relevant error message if we catch this SSL error?