| 1 | // Copyright 2012 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/ct/x509`/ |
| 6 | |
| 7 | import ( |
| 8 | "crypto/ecdsascip-go gomod github.com/golang/go/src go1.16 `crypto/ecdsa`/" |
| 9 | "crypto/ellipticscip-go gomod github.com/golang/go/src go1.16 `crypto/elliptic`/" |
| 10 | // START CT CHANGES |
| 11 | "github.com/zmap/zcrypto/ct/asn1scip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/asn1`/" |
| 12 | // START CT CHANGES |
| 13 | "errorsscip-go gomod github.com/golang/go/src go1.16 errors/" |
| 14 | "fmtscip-go gomod github.com/golang/go/src go1.16 fmt/" |
| 15 | "math/bigscip-go gomod github.com/golang/go/src go1.16 `math/big`/" |
| 16 | ) |
| 17 | |
| 18 | const ecPrivKeyVersionscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/ecPrivKeyVersion. = 1 |
| 19 | |
| 20 | // ecPrivateKey reflects an ASN.1 Elliptic Curve Private Key Structure. |
| 21 | // References: |
| 22 | // |
| 23 | // RFC5915 |
| 24 | // SEC1 - http://www.secg.org/download/aid-780/sec1-v2.pdf |
| 25 | // |
| 26 | // Per RFC5915 the NamedCurveOID is marked as ASN.1 OPTIONAL, however in |
| 27 | // most cases it is not. |
| 28 | type ecPrivateKeyscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/ecPrivateKey# struct { |
| 29 | Versionscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/ecPrivateKey#Version. int |
| 30 | PrivateKeyscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/ecPrivateKey#PrivateKey. []byte |
| 31 | NamedCurveOIDscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/ecPrivateKey#NamedCurveOID. asn1scip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/asn1`/.ObjectIdentifierscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/asn1`/ObjectIdentifier# `asn1:"optional,explicit,tag:0"` |
| 32 | PublicKeyscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/ecPrivateKey#PublicKey. asn1scip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/asn1`/.BitStringscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/asn1`/BitString# `asn1:"optional,explicit,tag:1"` |
| 33 | } |
| 34 | |
| 35 | // ParseECPrivateKey parses an ASN.1 Elliptic Curve Private Key Structure. |
| 36 | func ParseECPrivateKeyscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/ParseECPrivateKey().(der []byte) (key *ecdsascip-go gomod github.com/golang/go/src go1.16 `crypto/ecdsa`/.PrivateKeyscip-go gomod github.com/golang/go/src go1.16 `crypto/ecdsa`/PrivateKey#, err error) { |
| 37 | return parseECPrivateKeyscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/parseECPrivateKey().(nil, der) |
| 38 | } |
| 39 | |
| 40 | // MarshalECPrivateKey marshals an EC private key into ASN.1, DER format. |
| 41 | func MarshalECPrivateKeyscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/MarshalECPrivateKey().(key *ecdsascip-go gomod github.com/golang/go/src go1.16 `crypto/ecdsa`/.PrivateKeyscip-go gomod github.com/golang/go/src go1.16 `crypto/ecdsa`/PrivateKey#) ([]byte, error) { |
| 42 | oid, ok := oidFromNamedCurvescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/oidFromNamedCurve().(key.Curvescip-go gomod github.com/golang/go/src go1.16 `crypto/ecdsa`/PublicKey#Curve.) |
| 43 | if !ok { |
| 44 | 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: unknown elliptic curve") |
| 45 | } |
| 46 | return asn1scip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/asn1`/.Marshalscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/asn1`/Marshal().(ecPrivateKeyscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/ecPrivateKey#{ |
| 47 | Versionscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/ecPrivateKey#Version.: 1, |
| 48 | PrivateKeyscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/ecPrivateKey#PrivateKey.: key.Dscip-go gomod github.com/golang/go/src go1.16 `crypto/ecdsa`/PrivateKey#D..Bytesscip-go gomod github.com/golang/go/src go1.16 `math/big`/Int#Bytes().(), |
| 49 | NamedCurveOIDscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/ecPrivateKey#NamedCurveOID.: oid, |
| 50 | PublicKeyscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/ecPrivateKey#PublicKey.: asn1scip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/asn1`/.BitStringscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/asn1`/BitString#{Bytesscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/asn1`/BitString#Bytes.: ellipticscip-go gomod github.com/golang/go/src go1.16 `crypto/elliptic`/.Marshalscip-go gomod github.com/golang/go/src go1.16 `crypto/elliptic`/Marshal().(key.Curvescip-go gomod github.com/golang/go/src go1.16 `crypto/ecdsa`/PublicKey#Curve., key.Xscip-go gomod github.com/golang/go/src go1.16 `crypto/ecdsa`/PublicKey#X., key.Yscip-go gomod github.com/golang/go/src go1.16 `crypto/ecdsa`/PublicKey#Y.)}, |
| 51 | }) |
| 52 | } |
| 53 | |
| 54 | // parseECPrivateKey parses an ASN.1 Elliptic Curve Private Key Structure. |
| 55 | // The OID for the named curve may be provided from another source (such as |
| 56 | // the PKCS8 container) - if it is provided then use this instead of the OID |
| 57 | // that may exist in the EC private key structure. |
| 58 | func parseECPrivateKeyscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/parseECPrivateKey().(namedCurveOID *asn1scip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/asn1`/.ObjectIdentifierscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/asn1`/ObjectIdentifier#, der []byte) (key *ecdsascip-go gomod github.com/golang/go/src go1.16 `crypto/ecdsa`/.PrivateKeyscip-go gomod github.com/golang/go/src go1.16 `crypto/ecdsa`/PrivateKey#, err error) { |
| 59 | var privKey ecPrivateKeyscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/ecPrivateKey# |
| 60 | if _, err := asn1scip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/asn1`/.Unmarshalscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/asn1`/Unmarshal().(der, &privKey); err != nil { |
| 61 | 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: " + err.Error()) |
| 62 | } |
| 63 | if privKey.Versionscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/ecPrivateKey#Version. != ecPrivKeyVersionscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/ecPrivKeyVersion. { |
| 64 | 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: unknown EC private key version %d", privKey.Versionscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/ecPrivateKey#Version.) |
| 65 | } |
| 66 | |
| 67 | var curve ellipticscip-go gomod github.com/golang/go/src go1.16 `crypto/elliptic`/.Curvescip-go gomod github.com/golang/go/src go1.16 `crypto/elliptic`/Curve# |
| 68 | if namedCurveOID != nil { |
| 69 | curve = namedCurveFromOIDscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/namedCurveFromOID().(*namedCurveOID) |
| 70 | } else { |
| 71 | curve = namedCurveFromOIDscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/namedCurveFromOID().(privKey.NamedCurveOIDscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/ecPrivateKey#NamedCurveOID.) |
| 72 | } |
| 73 | if curve == nil { |
| 74 | 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: unknown elliptic curve") |
| 75 | } |
| 76 | |
| 77 | k := new(bigscip-go gomod github.com/golang/go/src go1.16 `math/big`/.Intscip-go gomod github.com/golang/go/src go1.16 `math/big`/Int#).SetBytesscip-go gomod github.com/golang/go/src go1.16 `math/big`/Int#SetBytes().(privKey.PrivateKeyscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/ecPrivateKey#PrivateKey.) |
| 78 | if k.Cmpscip-go gomod github.com/golang/go/src go1.16 `math/big`/Int#Cmp().(curve.Paramsscip-go gomod github.com/golang/go/src go1.16 `crypto/elliptic`/Curve#Params().().Nscip-go gomod github.com/golang/go/src go1.16 `crypto/elliptic`/CurveParams#N.) >= 0 { |
| 79 | 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: invalid elliptic curve private key value") |
| 80 | } |
| 81 | priv := new(ecdsascip-go gomod github.com/golang/go/src go1.16 `crypto/ecdsa`/.PrivateKeyscip-go gomod github.com/golang/go/src go1.16 `crypto/ecdsa`/PrivateKey#) |
| 82 | priv.Curvescip-go gomod github.com/golang/go/src go1.16 `crypto/ecdsa`/PublicKey#Curve. = curve |
| 83 | priv.Dscip-go gomod github.com/golang/go/src go1.16 `crypto/ecdsa`/PrivateKey#D. = k |
| 84 | priv.Xscip-go gomod github.com/golang/go/src go1.16 `crypto/ecdsa`/PublicKey#X., priv.Yscip-go gomod github.com/golang/go/src go1.16 `crypto/ecdsa`/PublicKey#Y. = curve.ScalarBaseMultscip-go gomod github.com/golang/go/src go1.16 `crypto/elliptic`/Curve#ScalarBaseMult().(privKey.PrivateKeyscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/ecPrivateKey#PrivateKey.) |
| 85 | |
| 86 | return priv, nil |
| 87 | } |