gitee.com/lh-her-team/common@v1.5.1/crypto/x509/pkcs1.go (about)

     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 x509
     6  
     7  import (
     8  	"crypto/rsa"
     9  	"encoding/asn1"
    10  	"errors"
    11  	"math/big"
    12  )
    13  
    14  // pkcs1PrivateKey is a structure which mirrors the PKCS#1 ASN.1 for an RSA private key.
    15  type pkcs1PrivateKey struct {
    16  	Version int
    17  	N       *big.Int
    18  	E       int
    19  	D       *big.Int
    20  	P       *big.Int
    21  	Q       *big.Int
    22  	// We ignore these values, if present, because rsa will calculate them.
    23  	Dp               *big.Int                  `asn1:"optional"`
    24  	Dq               *big.Int                  `asn1:"optional"`
    25  	Qinv             *big.Int                  `asn1:"optional"`
    26  	AdditionalPrimes []pkcs1AdditionalRSAPrime `asn1:"optional,omitempty"`
    27  }
    28  
    29  type pkcs1AdditionalRSAPrime struct {
    30  	Prime *big.Int
    31  	// We ignore these values because rsa will calculate them.
    32  	Exp   *big.Int
    33  	Coeff *big.Int
    34  }
    35  
    36  // pkcs1PublicKey reflects the ASN.1 structure of a PKCS#1 public key.
    37  type pkcs1PublicKey struct {
    38  	N *big.Int
    39  	E int
    40  }
    41  
    42  // ParsePKCS1PrivateKey returns an RSA private key from its ASN.1 PKCS#1 DER encoded form.
    43  func ParsePKCS1PrivateKey(der []byte) (*rsa.PrivateKey, error) {
    44  	var priv pkcs1PrivateKey
    45  	rest, err := asn1.Unmarshal(der, &priv)
    46  	if len(rest) > 0 {
    47  		return nil, asn1.SyntaxError{Msg: "trailing data"}
    48  	}
    49  	if err != nil {
    50  		return nil, err
    51  	}
    52  	if priv.Version > 1 {
    53  		return nil, errors.New("x509: unsupported private key version")
    54  	}
    55  	if priv.N.Sign() <= 0 || priv.D.Sign() <= 0 || priv.P.Sign() <= 0 || priv.Q.Sign() <= 0 {
    56  		return nil, errors.New("x509: private key contains zero or negative value")
    57  	}
    58  	key := new(rsa.PrivateKey)
    59  	key.PublicKey = rsa.PublicKey{
    60  		E: priv.E,
    61  		N: priv.N,
    62  	}
    63  	key.D = priv.D
    64  	key.Primes = make([]*big.Int, 2+len(priv.AdditionalPrimes))
    65  	key.Primes[0] = priv.P
    66  	key.Primes[1] = priv.Q
    67  	for i, a := range priv.AdditionalPrimes {
    68  		if a.Prime.Sign() <= 0 {
    69  			return nil, errors.New("x509: private key contains zero or negative prime")
    70  		}
    71  		key.Primes[i+2] = a.Prime
    72  		// We ignore the other two values because rsa will calculate
    73  		// them as needed.
    74  	}
    75  	err = key.Validate()
    76  	if err != nil {
    77  		return nil, err
    78  	}
    79  	key.Precompute()
    80  	return key, nil
    81  }
    82  
    83  // MarshalPKCS1PrivateKey converts a private key to ASN.1 DER encoded form.
    84  func MarshalPKCS1PrivateKey(key *rsa.PrivateKey) []byte {
    85  	key.Precompute()
    86  	version := 0
    87  	if len(key.Primes) > 2 {
    88  		version = 1
    89  	}
    90  	priv := pkcs1PrivateKey{
    91  		Version: version,
    92  		N:       key.N,
    93  		E:       key.PublicKey.E,
    94  		D:       key.D,
    95  		P:       key.Primes[0],
    96  		Q:       key.Primes[1],
    97  		Dp:      key.Precomputed.Dp,
    98  		Dq:      key.Precomputed.Dq,
    99  		Qinv:    key.Precomputed.Qinv,
   100  	}
   101  	priv.AdditionalPrimes = make([]pkcs1AdditionalRSAPrime, len(key.Precomputed.CRTValues))
   102  	for i, values := range key.Precomputed.CRTValues {
   103  		priv.AdditionalPrimes[i].Prime = key.Primes[2+i]
   104  		priv.AdditionalPrimes[i].Exp = values.Exp
   105  		priv.AdditionalPrimes[i].Coeff = values.Coeff
   106  	}
   107  	b, _ := asn1.Marshal(priv)
   108  	return b
   109  }
   110  
   111  // ParsePKCS1PublicKey parses a PKCS#1 public key in ASN.1 DER form.
   112  func ParsePKCS1PublicKey(der []byte) (*rsa.PublicKey, error) {
   113  	var pub pkcs1PublicKey
   114  	rest, err := asn1.Unmarshal(der, &pub)
   115  	if err != nil {
   116  		return nil, err
   117  	}
   118  	if len(rest) > 0 {
   119  		return nil, asn1.SyntaxError{Msg: "trailing data"}
   120  	}
   121  	if pub.N.Sign() <= 0 || pub.E <= 0 {
   122  		return nil, errors.New("x509: public key contains zero or negative value")
   123  	}
   124  	if pub.E > 1<<31-1 {
   125  		return nil, errors.New("x509: public key contains large public exponent")
   126  	}
   127  	return &rsa.PublicKey{
   128  		E: pub.E,
   129  		N: pub.N,
   130  	}, nil
   131  }
   132  
   133  // MarshalPKCS1PublicKey converts an RSA public key to PKCS#1, ASN.1 DER form.
   134  func MarshalPKCS1PublicKey(key *rsa.PublicKey) []byte {
   135  	derBytes, _ := asn1.Marshal(pkcs1PublicKey{
   136  		N: key.N,
   137  		E: key.E,
   138  	})
   139  	return derBytes
   140  }
   141  
   142  // rsaPublicKey reflects the ASN.1 structure of a PKCS#1 public key.
   143  type rsaPublicKey struct {
   144  	N *big.Int
   145  	E int
   146  }