OLD | NEW |
1 // Copyright 2011 The Go Authors. All rights reserved. | 1 // Copyright 2011 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 package x509 | 5 package x509 |
6 | 6 |
7 import ( | 7 import ( |
8 "crypto/rsa" | 8 "crypto/rsa" |
9 "encoding/asn1" | 9 "encoding/asn1" |
10 "errors" | 10 "errors" |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
45 } | 45 } |
46 if err != nil { | 46 if err != nil { |
47 return | 47 return |
48 } | 48 } |
49 | 49 |
50 if priv.Version > 1 { | 50 if priv.Version > 1 { |
51 return nil, errors.New("x509: unsupported private key version") | 51 return nil, errors.New("x509: unsupported private key version") |
52 } | 52 } |
53 | 53 |
54 if priv.N.Sign() <= 0 || priv.D.Sign() <= 0 || priv.P.Sign() <= 0 || pri
v.Q.Sign() <= 0 { | 54 if priv.N.Sign() <= 0 || priv.D.Sign() <= 0 || priv.P.Sign() <= 0 || pri
v.Q.Sign() <= 0 { |
55 » » return nil, errors.New("private key contains zero or negative va
lue") | 55 » » return nil, errors.New("x509: private key contains zero or negat
ive value") |
56 } | 56 } |
57 | 57 |
58 key = new(rsa.PrivateKey) | 58 key = new(rsa.PrivateKey) |
59 key.PublicKey = rsa.PublicKey{ | 59 key.PublicKey = rsa.PublicKey{ |
60 E: priv.E, | 60 E: priv.E, |
61 N: priv.N, | 61 N: priv.N, |
62 } | 62 } |
63 | 63 |
64 key.D = priv.D | 64 key.D = priv.D |
65 key.Primes = make([]*big.Int, 2+len(priv.AdditionalPrimes)) | 65 key.Primes = make([]*big.Int, 2+len(priv.AdditionalPrimes)) |
66 key.Primes[0] = priv.P | 66 key.Primes[0] = priv.P |
67 key.Primes[1] = priv.Q | 67 key.Primes[1] = priv.Q |
68 for i, a := range priv.AdditionalPrimes { | 68 for i, a := range priv.AdditionalPrimes { |
69 if a.Prime.Sign() <= 0 { | 69 if a.Prime.Sign() <= 0 { |
70 » » » return nil, errors.New("private key contains zero or neg
ative prime") | 70 » » » return nil, errors.New("x509: private key contains zero
or negative prime") |
71 } | 71 } |
72 key.Primes[i+2] = a.Prime | 72 key.Primes[i+2] = a.Prime |
73 // We ignore the other two values because rsa will calculate | 73 // We ignore the other two values because rsa will calculate |
74 // them as needed. | 74 // them as needed. |
75 } | 75 } |
76 | 76 |
77 err = key.Validate() | 77 err = key.Validate() |
78 if err != nil { | 78 if err != nil { |
79 return nil, err | 79 return nil, err |
80 } | 80 } |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
113 | 113 |
114 b, _ := asn1.Marshal(priv) | 114 b, _ := asn1.Marshal(priv) |
115 return b | 115 return b |
116 } | 116 } |
117 | 117 |
118 // rsaPublicKey reflects the ASN.1 structure of a PKCS#1 public key. | 118 // rsaPublicKey reflects the ASN.1 structure of a PKCS#1 public key. |
119 type rsaPublicKey struct { | 119 type rsaPublicKey struct { |
120 N *big.Int | 120 N *big.Int |
121 E int | 121 E int |
122 } | 122 } |
OLD | NEW |