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.
14 // pkcs1PrivateKey is a structure which mirrors the PKCS#1 ASN.1 for an RSA private key.
15 type pkcs1PrivateKey
struct {
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"`
27 AdditionalPrimes
[]pkcs1AdditionalRSAPrime
`asn1:"optional,omitempty"`
30 type pkcs1AdditionalRSAPrime
struct {
33 // We ignore these values because rsa will calculate them.
38 // pkcs1PublicKey reflects the ASN.1 structure of a PKCS#1 public key.
39 type pkcs1PublicKey
struct {
44 // ParsePKCS1PrivateKey returns an RSA private key from its ASN.1 PKCS#1 DER encoded form.
45 func ParsePKCS1PrivateKey(der
[]byte) (*rsa
.PrivateKey
, error
) {
46 var priv pkcs1PrivateKey
47 rest
, err
:= asn1
.Unmarshal(der
, &priv
)
49 return nil, asn1
.SyntaxError
{Msg
: "trailing data"}
56 return nil, errors
.New("x509: unsupported private key version")
59 if priv
.N
.Sign() <= 0 || priv
.D
.Sign() <= 0 || priv
.P
.Sign() <= 0 || priv
.Q
.Sign() <= 0 {
60 return nil, errors
.New("x509: private key contains zero or negative value")
63 key
:= new(rsa
.PrivateKey
)
64 key
.PublicKey
= rsa
.PublicKey
{
70 key
.Primes
= make([]*big
.Int
, 2+len(priv
.AdditionalPrimes
))
71 key
.Primes
[0] = priv
.P
72 key
.Primes
[1] = priv
.Q
73 for i
, a
:= range priv
.AdditionalPrimes
{
74 if a
.Prime
.Sign() <= 0 {
75 return nil, errors
.New("x509: private key contains zero or negative prime")
77 key
.Primes
[i
+2] = a
.Prime
78 // We ignore the other two values because rsa will calculate
91 // MarshalPKCS1PrivateKey converts a private key to ASN.1 DER encoded form.
92 func MarshalPKCS1PrivateKey(key
*rsa
.PrivateKey
) []byte {
96 if len(key
.Primes
) > 2 {
100 priv
:= pkcs1PrivateKey
{
107 Dp
: key
.Precomputed
.Dp
,
108 Dq
: key
.Precomputed
.Dq
,
109 Qinv
: key
.Precomputed
.Qinv
,
112 priv
.AdditionalPrimes
= make([]pkcs1AdditionalRSAPrime
, len(key
.Precomputed
.CRTValues
))
113 for i
, values
:= range key
.Precomputed
.CRTValues
{
114 priv
.AdditionalPrimes
[i
].Prime
= key
.Primes
[2+i
]
115 priv
.AdditionalPrimes
[i
].Exp
= values
.Exp
116 priv
.AdditionalPrimes
[i
].Coeff
= values
.Coeff
119 b
, _
:= asn1
.Marshal(priv
)
123 // ParsePKCS1PublicKey parses a PKCS#1 public key in ASN.1 DER form.
124 func ParsePKCS1PublicKey(der
[]byte) (*rsa
.PublicKey
, error
) {
125 var pub pkcs1PublicKey
126 rest
, err
:= asn1
.Unmarshal(der
, &pub
)
131 return nil, asn1
.SyntaxError
{Msg
: "trailing data"}
134 if pub
.N
.Sign() <= 0 || pub
.E
<= 0 {
135 return nil, errors
.New("x509: public key contains zero or negative value")
138 return nil, errors
.New("x509: public key contains large public exponent")
141 return &rsa
.PublicKey
{
147 // MarshalPKCS1PublicKey converts an RSA public key to PKCS#1, ASN.1 DER form.
148 func MarshalPKCS1PublicKey(key
*rsa
.PublicKey
) []byte {
149 derBytes
, _
:= asn1
.Marshal(pkcs1PublicKey
{