-
Notifications
You must be signed in to change notification settings - Fork 1.1k
PKCS#12 Feature #449
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
base: master
Are you sure you want to change the base?
PKCS#12 Feature #449
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| // Package pkcs12 implements the parsing and encoding of key and certificate files into a PKCS#12 file | ||
| package pkcs12 | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This could use a package-level comment. |
||
|
|
||
| import ( | ||
| "crypto/x509" | ||
| "encoding/base64" | ||
| "encoding/pem" | ||
|
|
||
| "github.com/AGWA-forks/go-pkcs12" | ||
| ) | ||
|
|
||
| // ParseAndEncode takes key, certificate, and optional password | ||
| // as []byte and parses them to get a suitable format to encode them | ||
| func ParseAndEncode(key, cert, password []byte) string { | ||
| var file string | ||
| certBlock, _ := pem.Decode(cert) | ||
| certBytes := certBlock.Bytes | ||
| certificate, err := x509.ParseCertificate(certBytes) | ||
| if err != nil { | ||
| return file | ||
| } | ||
|
|
||
| block, _ := pem.Decode(key) | ||
| privKey := block.Bytes | ||
| if block.Type == "RSA PRIVATE KEY" { | ||
| pkcsKey, err := x509.ParsePKCS1PrivateKey(privKey) | ||
| if err != nil { | ||
| return file | ||
| } | ||
| file = Encode(pkcsKey, certificate, password) | ||
| } else if block.Type == "EC PRIVATE KEY" { | ||
| ecKey, err := x509.ParseECPrivateKey(privKey) | ||
| if err != nil { | ||
| return file | ||
| } | ||
| file = Encode(ecKey, certificate, password) | ||
| } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What are the error cases here? |
||
|
|
||
| return file | ||
| } | ||
|
|
||
| // Encode is called by ParseAndEncode with a key, certificate, and optional password | ||
| // to call AGWA-forks's pkcs12 encode function and returns the pkcs12 file as a base64 encoded string | ||
| func Encode(privateKey interface{}, certificate *x509.Certificate, password []byte) string { | ||
| var none []*x509.Certificate | ||
| var data string | ||
| pfxData, err := pkcs12.Encode(privateKey, certificate, none, password) | ||
| if err != nil { | ||
| return data | ||
| } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure writing to "outfile.p12" is what you want here. The caller should get a byte slice containing the encoded data.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @kisom originally thought exporting the PKCS12 key might not be a good idea since it's the binary data. Or is there a different way of doing it that I'm missing? |
||
|
|
||
| data = base64.StdEncoding.EncodeToString(pfxData) | ||
|
|
||
| return data | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This output should also contain the base64-encoded PKCS#12 file if it was requested.