File tree 2 files changed +29
-1
lines changed
2 files changed +29
-1
lines changed Original file line number Diff line number Diff line change @@ -72,7 +72,9 @@ func ParsePKCS1PrivateKey(der []byte) (*rsa.PrivateKey, error) {
72
72
}
73
73
74
74
if priv .N .Sign () <= 0 || priv .D .Sign () <= 0 || priv .P .Sign () <= 0 || priv .Q .Sign () <= 0 ||
75
- priv .Dp .Sign () <= 0 || priv .Dq .Sign () <= 0 || priv .Qinv .Sign () <= 0 {
75
+ priv .Dp != nil && priv .Dp .Sign () <= 0 ||
76
+ priv .Dq != nil && priv .Dq .Sign () <= 0 ||
77
+ priv .Qinv != nil && priv .Qinv .Sign () <= 0 {
76
78
return nil , errors .New ("x509: private key contains zero or negative value" )
77
79
}
78
80
Original file line number Diff line number Diff line change @@ -59,6 +59,32 @@ func TestParsePKCS1PrivateKey(t *testing.T) {
59
59
if _ , err := ParsePKCS1PrivateKey (data ); err == nil {
60
60
t .Errorf ("parsing invalid private key did not result in an error" )
61
61
}
62
+
63
+ // A partial key without CRT values should still parse.
64
+ b , _ := asn1 .Marshal (struct {
65
+ Version int
66
+ N * big.Int
67
+ E int
68
+ D * big.Int
69
+ P * big.Int
70
+ Q * big.Int
71
+ }{
72
+ N : priv .N ,
73
+ E : priv .PublicKey .E ,
74
+ D : priv .D ,
75
+ P : priv .Primes [0 ],
76
+ Q : priv .Primes [1 ],
77
+ })
78
+ p2 , err := ParsePKCS1PrivateKey (b )
79
+ if err != nil {
80
+ t .Fatalf ("parsing partial private key resulted in an error: %v" , err )
81
+ }
82
+ if ! p2 .Equal (priv ) {
83
+ t .Errorf ("partial private key did not match original key" )
84
+ }
85
+ if p2 .Precomputed .Dp == nil || p2 .Precomputed .Dq == nil || p2 .Precomputed .Qinv == nil {
86
+ t .Errorf ("precomputed values not recomputed" )
87
+ }
62
88
}
63
89
64
90
func TestPKCS1MismatchPublicKeyFormat (t * testing.T ) {
You can’t perform that action at this time.
0 commit comments