-
Notifications
You must be signed in to change notification settings - Fork 9
/
rsa_public_test.go
237 lines (221 loc) · 6.88 KB
/
rsa_public_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
// Copyright 2024 Thales Group
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package gose
import (
"bytes"
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"testing"
"github.com/ThalesGroup/gose/jose"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestNewRsaPublicKeyImpl(t *testing.T) {
require.NotNil(t, rand.Reader)
rsaKey, err := rsa.GenerateKey(rand.Reader, 2048)
require.NoError(t, err)
require.Nil(t, rsaKey.Validate())
jwk, err := JwkFromPrivateKey(rsaKey, []jose.KeyOps{jose.KeyOpsDecrypt}, nil)
require.NoError(t, err)
k, err := NewRsaDecryptionKey(jwk)
require.NoError(t, err)
assert.NotNil(t, k)
}
func TestNewRsaPublicKeyImpl_FailsWithInvalidKeyType(t *testing.T) {
require.NotNil(t, rand.Reader)
ecdsaKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
require.NoError(t, err)
jwk, err := JwkFromPublicKey(&ecdsaKey.PublicKey, []jose.KeyOps{jose.KeyOpsVerify}, nil)
require.NoError(t, err)
_, err = NewRsaPublicKeyImpl(jwk)
require.Equal(t, ErrInvalidKeyType, err)
}
func TestNewRsaPublicMarshalSucceeds(t *testing.T) {
// Setup
require.NotNil(t, rand.Reader)
rsaKey, err := rsa.GenerateKey(rand.Reader, 2048)
require.NoError(t, err)
require.Nil(t, rsaKey.Validate())
jwk, err := JwkFromPublicKey(rsaKey.Public(), []jose.KeyOps{jose.KeyOpsVerify}, nil)
require.NoError(t, err)
k, err := NewVerificationKey(jwk)
require.NoError(t, err)
// Act
str, err := k.Marshal()
// Assert
require.NoError(t, err)
require.NotEmpty(t, str)
buf := bytes.NewReader([]byte(str))
jwkOut, err := LoadJwk(buf, nil)
require.Equal(t, jwk.Kid(), jwkOut.Kid())
require.Equal(t, jwk.Kty(), jwkOut.Kty())
require.Equal(t, jwk.Alg(), jwkOut.Alg())
require.Equal(t, jwk.Ops(), jwkOut.Ops())
goodRsaJwk := jwk.(*jose.PublicRsaKey)
marshalledRsaJwk := jwkOut.(*jose.PublicRsaKey)
require.Equal(t, goodRsaJwk.N, marshalledRsaJwk.N)
require.Equal(t, goodRsaJwk.E, marshalledRsaJwk.E)
}
func TestNewRsaPublicMarshalPemSucceeds(t *testing.T) {
// Setup
rsaKey, err := rsa.GenerateKey(rand.Reader, 2048)
require.NoError(t, err)
jwk, err := JwkFromPublicKey(rsaKey.Public(), []jose.KeyOps{jose.KeyOpsVerify}, nil)
require.NoError(t, err)
k, err := NewVerificationKey(jwk)
require.NoError(t, err)
// Act
str, err := k.MarshalPem()
// Assert
require.NoError(t, err)
require.NotEmpty(t, str)
block, overflow := pem.Decode([]byte(str))
require.Empty(t, overflow)
require.Equal(t, block.Type, rsaPublicKeyPemType)
recoveredKey, err := x509.ParsePKIXPublicKey(block.Bytes)
require.NoError(t, err)
recoveredRsaKey, ok := recoveredKey.(*rsa.PublicKey)
require.True(t, ok)
assert.Equal(t, recoveredRsaKey.E, rsaKey.E)
assert.Equal(t, recoveredRsaKey.N.Cmp(rsaKey.N), 0)
}
func TestNewRsaPublicFailsWhenRsaExponentIsInvalid(t *testing.T) {
// Setup
var jwk jose.PublicRsaKey
jwk.SetAlg(jose.AlgPS256)
jwk.SetOps([]jose.KeyOps{jose.KeyOpsVerify})
jwk.N.SetBytes([]byte("AABABB"))
jwk.E.SetBytes([]byte("====="))
// Act
k, err := NewVerificationKey(&jwk)
// Assert
require.Equal(t, ErrInvalidExponent, err)
require.Nil(t, k)
}
//
//// TODO: redo the test, as monkey patching doesn't seem to work here
//func TestNewVerifierVerifyPSSSucceeds(t *testing.T) {
// // Setup
// var jwk jose.PublicRsaKey
// jwk.SetAlg(jose.AlgPS256)
// jwk.SetOps([]jose.KeyOps{jose.KeyOpsVerify})
// jwk.N.SetBytes([]byte("ABCDEFGHIJKLMNOPQRSTUVWXYZ"))
// jwk.E.SetBytes([]byte("AQAB"))
//
// defer monkey.Patch(rsa.VerifyPSS,
// func(pub *rsa.PublicKey, hash crypto.Hash, hashed []byte, sig []byte, opts *rsa.PSSOptions) error {
// return nil
// }).Unpatch()
//
// k, err := NewVerificationKey(&jwk)
//
// // Assert
// require.Nil(t, err)
// require.NotNil(t, k)
//
// // Act
// result := k.Verify(jose.KeyOpsVerify, []byte("1234"), []byte("5678"))
//
// // Assert
// require.True(t, result)
//}
//
//func TestRsaPublicVerifyPkcs15Succeeds(t *testing.T) {
// // Setup
// var jwk jose.PublicRsaKey
// jwk.SetAlg(jose.AlgRS256)
// jwk.SetOps([]jose.KeyOps{jose.KeyOpsVerify})
// jwk.N.SetBytes([]byte("ABCDEFGHIJKLMNOPQRSTUVWXYZ"))
// jwk.E.SetBytes([]byte("AQAB"))
//
// defer monkey.Patch(rsa.VerifyPKCS1v15,
// func(pub *rsa.PublicKey, hash crypto.Hash, hashed []byte, sig []byte) error {
// return nil
// }).Unpatch()
//
// k, err := NewVerificationKey(&jwk)
//
// // Assert
// require.Nil(t, err)
// require.NotNil(t, k)
//
// // Act
// result := k.Verify(jose.KeyOpsVerify, []byte("1234"), []byte("5678"))
//
// // Assert
// require.True(t, result)
//}
//
//func TestRsaPublicVerifyFailsWhenPSSCryptoErrors(t *testing.T) {
// // Setup
// var jwk jose.PublicRsaKey
// jwk.SetAlg(jose.AlgPS256)
// jwk.SetOps([]jose.KeyOps{jose.KeyOpsVerify})
// jwk.N.SetBytes([]byte("ABCDEFGHIJKLMNOPQRSTUVWXYZ"))
// jwk.E.SetBytes([]byte("AQAB"))
//
// defer monkey.Patch(rsa.VerifyPSS,
// func(pub *rsa.PublicKey, hash crypto.Hash, hashed []byte, sig []byte, options *rsa.PSSOptions) error {
// return errors.New("Expected error")
// }).Unpatch()
//
// k, err := NewVerificationKey(&jwk)
//
// // Assert
// require.Nil(t, err)
// require.NotNil(t, k)
//
// // Act
// result := k.Verify(jose.KeyOpsVerify, []byte("1234"), []byte("5678"))
//
// // Assert
// require.False(t, result)
//}
//
//func TestRsaPublicVerifyFailsWhenPkcs15CryptoErrors(t *testing.T) {
// // Setup
// var jwk jose.PublicRsaKey
// jwk.SetAlg(jose.AlgPS256)
// jwk.SetOps([]jose.KeyOps{jose.KeyOpsVerify})
// jwk.N.SetBytes([]byte("ABCDEFGHIJKLMNOPQRSTUVWXYZ"))
// jwk.E.SetBytes([]byte("AQAB"))
//
// defer monkey.Patch(rsa.VerifyPKCS1v15,
// func(pub *rsa.PublicKey, hash crypto.Hash, hashed []byte, sig []byte) error {
// return errors.New("Expected error")
// }).Unpatch()
//
// k, err := NewVerificationKey(&jwk)
//
// // Assert
// require.Nil(t, err)
// require.NotNil(t, k)
//
// // Act
// result := k.Verify(jose.KeyOpsVerify, []byte("1234"), []byte("5678"))
//
// // Assert
// require.False(t, result)
//}