| 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 | "crypto/rsascip-go gomod github.com/golang/go/src go1.16 `crypto/rsa`/" |
| 9 | "errorsscip-go gomod github.com/golang/go/src go1.16 errors/" |
| 10 | "math/bigscip-go gomod github.com/golang/go/src go1.16 `math/big`/" |
| 11 | |
| 12 | "github.com/zmap/zcrypto/encoding/asn1scip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/encoding/asn1`/" |
| 13 | ) |
| 14 | |
| 15 | // pkcs1PrivateKey is a structure which mirrors the PKCS#1 ASN.1 for an RSA private key. |
| 16 | type pkcs1PrivateKeyscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509`/pkcs1PrivateKey# struct { |
| 17 | Versionscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509`/pkcs1PrivateKey#Version. int |
| 18 | Nscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509`/pkcs1PrivateKey#N. *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# |
| 19 | Escip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509`/pkcs1PrivateKey#E. int |
| 20 | Dscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509`/pkcs1PrivateKey#D. *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# |
| 21 | Pscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509`/pkcs1PrivateKey#P. *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# |
| 22 | Qscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509`/pkcs1PrivateKey#Q. *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# |
| 23 | // We ignore these values, if present, because rsa will calculate them. |
| 24 | Dpscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509`/pkcs1PrivateKey#Dp. *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# `asn1:"optional"` |
| 25 | Dqscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509`/pkcs1PrivateKey#Dq. *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# `asn1:"optional"` |
| 26 | Qinvscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509`/pkcs1PrivateKey#Qinv. *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# `asn1:"optional"` |
| 27 | |
| 28 | AdditionalPrimesscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509`/pkcs1PrivateKey#AdditionalPrimes. []pkcs1AdditionalRSAPrimescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509`/pkcs1AdditionalRSAPrime# `asn1:"optional,omitempty"` |
| 29 | } |
| 30 | |
| 31 | type pkcs1AdditionalRSAPrimescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509`/pkcs1AdditionalRSAPrime# struct { |
| 32 | Primescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509`/pkcs1AdditionalRSAPrime#Prime. *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# |
| 33 | |
| 34 | // We ignore these values because rsa will calculate them. |
| 35 | Expscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509`/pkcs1AdditionalRSAPrime#Exp. *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# |
| 36 | Coeffscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509`/pkcs1AdditionalRSAPrime#Coeff. *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# |
| 37 | } |
| 38 | |
| 39 | // pkcs1PublicKey reflects the ASN.1 structure of a PKCS#1 public key. |
| 40 | type pkcs1PublicKeyscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509`/pkcs1PublicKey# struct { |
| 41 | Nscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509`/pkcs1PublicKey#N. *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# |
| 42 | Escip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509`/pkcs1PublicKey#E. int |
| 43 | } |
| 44 | |
| 45 | // ParsePKCS1PrivateKey returns an RSA private key from its ASN.1 PKCS#1 DER encoded form. |
| 46 | func ParsePKCS1PrivateKeyscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509`/ParsePKCS1PrivateKey().(der []byte) (*rsascip-go gomod github.com/golang/go/src go1.16 `crypto/rsa`/.PrivateKeyscip-go gomod github.com/golang/go/src go1.16 `crypto/rsa`/PrivateKey#, error) { |
| 47 | var priv pkcs1PrivateKeyscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509`/pkcs1PrivateKey# |
| 48 | rest, 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, &priv) |
| 49 | if len(rest) > 0 { |
| 50 | return nil, asn1scip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/encoding/asn1`/.SyntaxErrorscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/encoding/asn1`/SyntaxError#{Msgscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/encoding/asn1`/SyntaxError#Msg.: "trailing data"} |
| 51 | } |
| 52 | if err != nil { |
| 53 | return nil, err |
| 54 | } |
| 55 | |
| 56 | if priv.Versionscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509`/pkcs1PrivateKey#Version. > 1 { |
| 57 | 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: unsupported private key version") |
| 58 | } |
| 59 | |
| 60 | if priv.Nscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509`/pkcs1PrivateKey#N..Signscip-go gomod github.com/golang/go/src go1.16 `math/big`/Int#Sign().() <= 0 || priv.Dscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509`/pkcs1PrivateKey#D..Signscip-go gomod github.com/golang/go/src go1.16 `math/big`/Int#Sign().() <= 0 || priv.Pscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509`/pkcs1PrivateKey#P..Signscip-go gomod github.com/golang/go/src go1.16 `math/big`/Int#Sign().() <= 0 || priv.Qscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509`/pkcs1PrivateKey#Q..Signscip-go gomod github.com/golang/go/src go1.16 `math/big`/Int#Sign().() <= 0 { |
| 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: private key contains zero or negative value") |
| 62 | } |
| 63 | |
| 64 | key := new(rsascip-go gomod github.com/golang/go/src go1.16 `crypto/rsa`/.PrivateKeyscip-go gomod github.com/golang/go/src go1.16 `crypto/rsa`/PrivateKey#) |
| 65 | key.PublicKeyscip-go gomod github.com/golang/go/src go1.16 `crypto/rsa`/PrivateKey#PublicKey. = rsascip-go gomod github.com/golang/go/src go1.16 `crypto/rsa`/.PublicKeyscip-go gomod github.com/golang/go/src go1.16 `crypto/rsa`/PublicKey#{ |
| 66 | Escip-go gomod github.com/golang/go/src go1.16 `crypto/rsa`/PublicKey#E.: priv.Escip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509`/pkcs1PrivateKey#E., |
| 67 | Nscip-go gomod github.com/golang/go/src go1.16 `crypto/rsa`/PublicKey#N.: priv.Nscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509`/pkcs1PrivateKey#N., |
| 68 | } |
| 69 | |
| 70 | key.Dscip-go gomod github.com/golang/go/src go1.16 `crypto/rsa`/PrivateKey#D. = priv.Dscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509`/pkcs1PrivateKey#D. |
| 71 | key.Primesscip-go gomod github.com/golang/go/src go1.16 `crypto/rsa`/PrivateKey#Primes. = make([]*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#, 2+len(priv.AdditionalPrimesscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509`/pkcs1PrivateKey#AdditionalPrimes.)) |
| 72 | key.Primesscip-go gomod github.com/golang/go/src go1.16 `crypto/rsa`/PrivateKey#Primes.[0] = priv.Pscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509`/pkcs1PrivateKey#P. |
| 73 | key.Primesscip-go gomod github.com/golang/go/src go1.16 `crypto/rsa`/PrivateKey#Primes.[1] = priv.Qscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509`/pkcs1PrivateKey#Q. |
| 74 | for i, a := range priv.AdditionalPrimesscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509`/pkcs1PrivateKey#AdditionalPrimes. { |
| 75 | if a.Primescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509`/pkcs1AdditionalRSAPrime#Prime..Signscip-go gomod github.com/golang/go/src go1.16 `math/big`/Int#Sign().() <= 0 { |
| 76 | 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: private key contains zero or negative prime") |
| 77 | } |
| 78 | key.Primesscip-go gomod github.com/golang/go/src go1.16 `crypto/rsa`/PrivateKey#Primes.[i+2] = a.Primescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509`/pkcs1AdditionalRSAPrime#Prime. |
| 79 | // We ignore the other two values because rsa will calculate |
| 80 | // them as needed. |
| 81 | } |
| 82 | |
| 83 | err = key.Validatescip-go gomod github.com/golang/go/src go1.16 `crypto/rsa`/PrivateKey#Validate().() |
| 84 | if err != nil { |
| 85 | return nil, err |
| 86 | } |
| 87 | key.Precomputescip-go gomod github.com/golang/go/src go1.16 `crypto/rsa`/PrivateKey#Precompute().() |
| 88 | |
| 89 | return key, nil |
| 90 | } |
| 91 | |
| 92 | // MarshalPKCS1PrivateKey converts a private key to ASN.1 DER encoded form. |
| 93 | func MarshalPKCS1PrivateKeyscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509`/MarshalPKCS1PrivateKey().(key *rsascip-go gomod github.com/golang/go/src go1.16 `crypto/rsa`/.PrivateKeyscip-go gomod github.com/golang/go/src go1.16 `crypto/rsa`/PrivateKey#) []byte { |
| 94 | key.Precomputescip-go gomod github.com/golang/go/src go1.16 `crypto/rsa`/PrivateKey#Precompute().() |
| 95 | |
| 96 | version := 0 |
| 97 | if len(key.Primesscip-go gomod github.com/golang/go/src go1.16 `crypto/rsa`/PrivateKey#Primes.) > 2 { |
| 98 | version = 1 |
| 99 | } |
| 100 | |
| 101 | priv := pkcs1PrivateKeyscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509`/pkcs1PrivateKey#{ |
| 102 | Versionscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509`/pkcs1PrivateKey#Version.: version, |
| 103 | Nscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509`/pkcs1PrivateKey#N.: key.Nscip-go gomod github.com/golang/go/src go1.16 `crypto/rsa`/PublicKey#N., |
| 104 | Escip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509`/pkcs1PrivateKey#E.: key.PublicKeyscip-go gomod github.com/golang/go/src go1.16 `crypto/rsa`/PrivateKey#PublicKey..Escip-go gomod github.com/golang/go/src go1.16 `crypto/rsa`/PublicKey#E., |
| 105 | Dscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509`/pkcs1PrivateKey#D.: key.Dscip-go gomod github.com/golang/go/src go1.16 `crypto/rsa`/PrivateKey#D., |
| 106 | Pscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509`/pkcs1PrivateKey#P.: key.Primesscip-go gomod github.com/golang/go/src go1.16 `crypto/rsa`/PrivateKey#Primes.[0], |
| 107 | Qscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509`/pkcs1PrivateKey#Q.: key.Primesscip-go gomod github.com/golang/go/src go1.16 `crypto/rsa`/PrivateKey#Primes.[1], |
| 108 | Dpscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509`/pkcs1PrivateKey#Dp.: key.Precomputedscip-go gomod github.com/golang/go/src go1.16 `crypto/rsa`/PrivateKey#Precomputed..Dpscip-go gomod github.com/golang/go/src go1.16 `crypto/rsa`/PrecomputedValues#Dp., |
| 109 | Dqscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509`/pkcs1PrivateKey#Dq.: key.Precomputedscip-go gomod github.com/golang/go/src go1.16 `crypto/rsa`/PrivateKey#Precomputed..Dqscip-go gomod github.com/golang/go/src go1.16 `crypto/rsa`/PrecomputedValues#Dq., |
| 110 | Qinvscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509`/pkcs1PrivateKey#Qinv.: key.Precomputedscip-go gomod github.com/golang/go/src go1.16 `crypto/rsa`/PrivateKey#Precomputed..Qinvscip-go gomod github.com/golang/go/src go1.16 `crypto/rsa`/PrecomputedValues#Qinv., |
| 111 | } |
| 112 | |
| 113 | priv.AdditionalPrimesscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509`/pkcs1PrivateKey#AdditionalPrimes. = make([]pkcs1AdditionalRSAPrimescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509`/pkcs1AdditionalRSAPrime#, len(key.Precomputedscip-go gomod github.com/golang/go/src go1.16 `crypto/rsa`/PrivateKey#Precomputed..CRTValuesscip-go gomod github.com/golang/go/src go1.16 `crypto/rsa`/PrecomputedValues#CRTValues.)) |
| 114 | for i, values := range key.Precomputedscip-go gomod github.com/golang/go/src go1.16 `crypto/rsa`/PrivateKey#Precomputed..CRTValuesscip-go gomod github.com/golang/go/src go1.16 `crypto/rsa`/PrecomputedValues#CRTValues. { |
| 115 | priv.AdditionalPrimesscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509`/pkcs1PrivateKey#AdditionalPrimes.[i].Primescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509`/pkcs1AdditionalRSAPrime#Prime. = key.Primesscip-go gomod github.com/golang/go/src go1.16 `crypto/rsa`/PrivateKey#Primes.[2+i] |
| 116 | priv.AdditionalPrimesscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509`/pkcs1PrivateKey#AdditionalPrimes.[i].Expscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509`/pkcs1AdditionalRSAPrime#Exp. = values.Expscip-go gomod github.com/golang/go/src go1.16 `crypto/rsa`/CRTValue#Exp. |
| 117 | priv.AdditionalPrimesscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509`/pkcs1PrivateKey#AdditionalPrimes.[i].Coeffscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/x509`/pkcs1AdditionalRSAPrime#Coeff. = values.Coeffscip-go gomod github.com/golang/go/src go1.16 `crypto/rsa`/CRTValue#Coeff. |
| 118 | } |
| 119 | |
| 120 | b, _ := asn1scip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/encoding/asn1`/.Marshalscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/encoding/asn1`/Marshal().(priv) |
| 121 | return b |
| 122 | } |