示例#1
0
// ParsePrivateKeyPEM parses and returns a PEM-encoded private
// key. The private key may be either an unencrypted PKCS#8, PKCS#1,
// or elliptic private key.
func ParsePrivateKeyPEM(keyPEM []byte) (key crypto.Signer, err error) {
	keyDER, err := GetKeyDERFromPEM(keyPEM)
	if err != nil {
		return nil, err
	}

	return derhelpers.ParsePrivateKeyDER(keyDER)
}
示例#2
0
文件: pkcs12.go 项目: JoeHorn/boulder
// Take in either two or one safeBags and return the certificates and or
// Private key within the bags
func parseBags(bags []safeBag, password []byte) (certs []*x509.Certificate, key crypto.Signer, err error) {
	for _, bag := range bags {
		bagid := bag.ID.String()
		switch bagid {
		case certBagID:
			var CertBag certBag
			if _, err = asn1.Unmarshal(bag.Value.Bytes, &CertBag); err != nil {
				return nil, nil, cferr.Wrap(cferr.CertificateError, cferr.ParseFailed, err)
			}
			certs, err = x509.ParseCertificates(CertBag.Data)

			if err != nil {
				return nil, nil, cferr.Wrap(cferr.CertificateError, cferr.ParseFailed, err)
			}

		case pkcs8ShroudedBagID:
			var pkinfo encryptedPrivateKeyInfo
			if _, err := asn1.Unmarshal(bag.Value.Bytes, &pkinfo); err != nil {
				return nil, nil, cferr.Wrap(cferr.CertificateError, cferr.ParseFailed, err)
			}
			pkDecrypted, err := decrypt(pkinfo.AlgorithmIdentifier, pkinfo.EncryptedData, password)
			if err != nil {
				return nil, nil, cferr.Wrap(cferr.CertificateError, cferr.ParseFailed, err)
			}
			// Checking if private key data has been properly decoded
			var rv asn1.RawValue
			if _, err = asn1.Unmarshal(pkDecrypted, &rv); err != nil {
				return nil, nil, cferr.Wrap(cferr.CertificateError, cferr.ParseFailed, err)
			}
			if key, err = derhelpers.ParsePrivateKeyDER(pkDecrypted); err != nil {
				return nil, nil, err
			}

		default:
			return nil, nil, cferr.Wrap(cferr.CertificateError, cferr.ParseFailed, errors.New("Only support for certificate bags and PKCS #8 Shrouded Bags"))
		}
	}
	return certs, key, nil
}