Home | History | Annotate | Download | only in x509
      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 
     27 	AdditionalPrimes []pkcs1AdditionalRSAPrime `asn1:"optional,omitempty"`
     28 }
     29 
     30 type pkcs1AdditionalRSAPrime struct {
     31 	Prime *big.Int
     32 
     33 	// We ignore these values because rsa will calculate them.
     34 	Exp   *big.Int
     35 	Coeff *big.Int
     36 }
     37 
     38 // ParsePKCS1PrivateKey returns an RSA private key from its ASN.1 PKCS#1 DER encoded form.
     39 func ParsePKCS1PrivateKey(der []byte) (*rsa.PrivateKey, error) {
     40 	var priv pkcs1PrivateKey
     41 	rest, err := asn1.Unmarshal(der, &priv)
     42 	if len(rest) > 0 {
     43 		return nil, asn1.SyntaxError{Msg: "trailing data"}
     44 	}
     45 	if err != nil {
     46 		return nil, err
     47 	}
     48 
     49 	if priv.Version > 1 {
     50 		return nil, errors.New("x509: unsupported private key version")
     51 	}
     52 
     53 	if priv.N.Sign() <= 0 || priv.D.Sign() <= 0 || priv.P.Sign() <= 0 || priv.Q.Sign() <= 0 {
     54 		return nil, errors.New("x509: private key contains zero or negative value")
     55 	}
     56 
     57 	key := new(rsa.PrivateKey)
     58 	key.PublicKey = rsa.PublicKey{
     59 		E: priv.E,
     60 		N: priv.N,
     61 	}
     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 
     76 	err = key.Validate()
     77 	if err != nil {
     78 		return nil, err
     79 	}
     80 	key.Precompute()
     81 
     82 	return key, nil
     83 }
     84 
     85 // MarshalPKCS1PrivateKey converts a private key to ASN.1 DER encoded form.
     86 func MarshalPKCS1PrivateKey(key *rsa.PrivateKey) []byte {
     87 	key.Precompute()
     88 
     89 	version := 0
     90 	if len(key.Primes) > 2 {
     91 		version = 1
     92 	}
     93 
     94 	priv := pkcs1PrivateKey{
     95 		Version: version,
     96 		N:       key.N,
     97 		E:       key.PublicKey.E,
     98 		D:       key.D,
     99 		P:       key.Primes[0],
    100 		Q:       key.Primes[1],
    101 		Dp:      key.Precomputed.Dp,
    102 		Dq:      key.Precomputed.Dq,
    103 		Qinv:    key.Precomputed.Qinv,
    104 	}
    105 
    106 	priv.AdditionalPrimes = make([]pkcs1AdditionalRSAPrime, len(key.Precomputed.CRTValues))
    107 	for i, values := range key.Precomputed.CRTValues {
    108 		priv.AdditionalPrimes[i].Prime = key.Primes[2+i]
    109 		priv.AdditionalPrimes[i].Exp = values.Exp
    110 		priv.AdditionalPrimes[i].Coeff = values.Coeff
    111 	}
    112 
    113 	b, _ := asn1.Marshal(priv)
    114 	return b
    115 }
    116 
    117 // rsaPublicKey reflects the ASN.1 structure of a PKCS#1 public key.
    118 type rsaPublicKey struct {
    119 	N *big.Int
    120 	E int
    121 }
    122