| 1 | // Copyright 2011 The Go Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | package x509scip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509`/ |
| 6 | |
| 7 | import ( |
| 8 | "errorsscip-go gomod github.com/golang/go/src go1.16 errors/" |
| 9 | "fmtscip-go gomod github.com/golang/go/src go1.16 fmt/" |
| 10 | |
| 11 | "github.com/zmap/zcrypto/encoding/asn1scip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/encoding/asn1`/" |
| 12 | "github.com/zmap/zcrypto/x509/pkixscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509/pkix`/" |
| 13 | ) |
| 14 | |
| 15 | // pkcs8 reflects an ASN.1, PKCS#8 PrivateKey. See |
| 16 | // ftp://ftp.rsasecurity.com/pub/pkcs/pkcs-8/pkcs-8v1_2.asn |
| 17 | // and RFC 5208. |
| 18 | type pkcs8scip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509`/pkcs8# struct { |
| 19 | Versionscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509`/pkcs8#Version. int |
| 20 | Algoscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509`/pkcs8#Algo. pkixscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509/pkix`/.AlgorithmIdentifierscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509/pkix`/AlgorithmIdentifier# |
| 21 | PrivateKeyscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509`/pkcs8#PrivateKey. []byte |
| 22 | // optional attributes omitted. |
| 23 | } |
| 24 | |
| 25 | // ParsePKCS8PrivateKey parses an unencrypted, PKCS#8 private key. |
| 26 | // See RFC 5208. |
| 27 | func ParsePKCS8PrivateKeyscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509`/ParsePKCS8PrivateKey().(der []byte) (key interface{}, err error) { |
| 28 | var privKey pkcs8scip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509`/pkcs8# |
| 29 | if _, err := asn1scip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/encoding/asn1`/.Unmarshalscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/encoding/asn1`/Unmarshal().(der, &privKey); err != nil { |
| 30 | return nil, err |
| 31 | } |
| 32 | switch { |
| 33 | case privKey.Algoscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509`/pkcs8#Algo..Algorithmscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509/pkix`/AlgorithmIdentifier#Algorithm..Equalscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/encoding/asn1`/ObjectIdentifier#Equal().(oidPublicKeyRSAscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509`/oidPublicKeyRSA.): |
| 34 | key, err = ParsePKCS1PrivateKeyscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509`/ParsePKCS1PrivateKey().(privKey.PrivateKeyscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509`/pkcs8#PrivateKey.) |
| 35 | if err != nil { |
| 36 | return nil, errorsscip-go gomod github.com/golang/go/src go1.16 errors/.Newscip-go gomod github.com/golang/go/src go1.16 errors/New().("x509: failed to parse RSA private key embedded in PKCS#8: " + err.Error()) |
| 37 | } |
| 38 | return key, nil |
| 39 | |
| 40 | case privKey.Algoscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509`/pkcs8#Algo..Algorithmscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509/pkix`/AlgorithmIdentifier#Algorithm..Equalscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/encoding/asn1`/ObjectIdentifier#Equal().(oidPublicKeyECDSAscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509`/oidPublicKeyECDSA.): |
| 41 | bytes := privKey.Algoscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509`/pkcs8#Algo..Parametersscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509/pkix`/AlgorithmIdentifier#Parameters..FullBytesscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/encoding/asn1`/RawValue#FullBytes. |
| 42 | namedCurveOID := new(asn1scip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/encoding/asn1`/.ObjectIdentifierscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/encoding/asn1`/ObjectIdentifier#) |
| 43 | if _, err := asn1scip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/encoding/asn1`/.Unmarshalscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/encoding/asn1`/Unmarshal().(bytes, namedCurveOID); err != nil { |
| 44 | namedCurveOID = nil |
| 45 | } |
| 46 | key, err = parseECPrivateKeyscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509`/parseECPrivateKey().(namedCurveOID, privKey.PrivateKeyscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509`/pkcs8#PrivateKey.) |
| 47 | if err != nil { |
| 48 | return nil, errorsscip-go gomod github.com/golang/go/src go1.16 errors/.Newscip-go gomod github.com/golang/go/src go1.16 errors/New().("x509: failed to parse EC private key embedded in PKCS#8: " + err.Error()) |
| 49 | } |
| 50 | return key, nil |
| 51 | |
| 52 | default: |
| 53 | return nil, fmtscip-go gomod github.com/golang/go/src go1.16 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.16 fmt/Errorf().("x509: PKCS#8 wrapping contained private key with unknown algorithm: %v", privKey.Algoscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509`/pkcs8#Algo..Algorithmscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509/pkix`/AlgorithmIdentifier#Algorithm.) |
| 54 | } |
| 55 | } |