Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can not understand how to generate acceptable pem cert #33

Closed
roma86 opened this issue Jul 22, 2016 · 10 comments
Closed

Can not understand how to generate acceptable pem cert #33

roma86 opened this issue Jul 22, 2016 · 10 comments
Labels

Comments

@roma86
Copy link

roma86 commented Jul 22, 2016

Hello. Thank you for the lib.

I use this command to convert p12 cer to pem

openssl pkcs12 -in aps.p12 -out aps.pem -nodes -clcerts

Which is works fine when i test it with ruby houston gem.

To test this lib i just use example code from readme, only path to the pem file is changed.

The result is error message

Cert Error:failed to parse PKCS1 private key

Which is referenced to apns2/certificate/certificate.go from this lib.

When it fail i tried to send passphrase as the last param into certificate.FromPemFile without success.

My questions are:

  • What is the correct way to generate p12 for this library?
  • Why same cert works with ruby lib and dose not with current one?

Thanks in advance.


go version go1.6.2 darwin/amd64

When i say works fine i mean push notifications are delivered to destination device.

@cenkbilgen
Copy link

As you guessed, it has to do with the format of the pem file generated by the way your calling openssl. See this related discussion: https://groups.google.com/forum/#!msg/golang-nuts/ZZ1Wt9d268Q/gQGh6e-wGAAJ.

To use the certificate you have, you need to modify certificates.go and rebuild. It worked for me by importing crypto/rsa and crypto/ecdsa then modifying ParsePrivateKey (

func parsePrivateKey(bytes []byte) (crypto.PrivateKey, error) {
) to try using x509.ParsePKCS8PrivateKey like in the link above, not just PKCS1. I can make a pull request out of it, but try that for now.

Alternatively and easier you may be able to get the certificate file in the right form for ParsePKCS1PrivateKey through openssl, nothing I tried seemed to work so can't help you there.

@sideshow sideshow added the bug label Jul 23, 2016
@sideshow
Copy link
Owner

sideshow commented Jul 23, 2016

Hey @cenkbilgen , good find. A pull request would be awesome!
@roma86 Since you have the p12 certificate, you can also use this directly instead, without needing to converti it to a pem.
Use the certificate.FromP12File() instead of the certificate.FromPemFile() method.

@roma86
Copy link
Author

roma86 commented Jul 24, 2016

@sideshow @cenkbilgen thanks

sideshow added a commit that referenced this issue Aug 2, 2016
@sideshow
Copy link
Owner

sideshow commented Aug 2, 2016

@cenkbilgen thanks again for the info.
@roma86 i've pushed back a fix on a branch called fix-parse-private-key. Any chance you could check this with your .pem to confirm it fixes the issue. Thanks

@roma86
Copy link
Author

roma86 commented Aug 2, 2016

@sideshow thank you. i will check it today and report you back.

@seenickcode
Copy link

Awesome thanks.

@credli
Copy link

credli commented Sep 7, 2016

@sideshow @seenickcode @roma86 @cenkbilgen this is to confirm that fix-parse-private-key is working for us in production mode.

@sideshow
Copy link
Owner

@credli Thanks for the info. Looks like i fixed your issue, but i was unable to decrypt @roma86 's private key, because if an issue with encrypted keys. Im looking at ways to resolve this hopefully without introducing another dependency

@c3mb0
Copy link
Contributor

c3mb0 commented Oct 10, 2016

I am not sure if this is related or fixed already, but I had problems with certificates that include full chain info (4 nodes instead of 2) for both pem and p12. For pem, a workable solution is to trim the certificate with file io since it's plain text (take the first and last node). For p12, convert the certificate to pem via this command:

openssl pkcs12 -in certName.p12 -clcerts -nokeys -passin pass:yourpassword -out certName.pem && openssl pkcs12 -in certName.p12 -nocerts -nodes -passin pass:yourpassword | openssl rsa >> certName.pem

This ensures a 2 node pem is outputted, whether the p12 is 4 or 2 node.

@sideshow
Copy link
Owner

Closing this as better docs will be added as part of this issue #54

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

6 participants