This repository has been archived by the owner on Aug 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
sign_verify_test.go
262 lines (205 loc) · 7.39 KB
/
sign_verify_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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
package cose
import (
"crypto"
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"fmt"
"github.com/stretchr/testify/assert"
"testing"
)
func TestSignErrors(t *testing.T) {
assert := assert.New(t)
msg := NewSignMessage()
msg.Payload = []byte("payload to sign")
signer, err := NewSigner(ES256, nil)
assert.Nil(err, fmt.Sprintf("Error creating signer %s", err))
sig := NewSignature()
sig.Headers.Protected[algTag] = -41 // RSAES-OAEP w/ SHA-256 from [RFC8230]
sig.Headers.Protected[kidTag] = 1
msg.Signatures = []Signature{}
err = msg.Sign(rand.Reader, []byte(""), []Signer{*signer})
assert.Equal(ErrNoSignatures, err)
msg.Signatures = nil
err = msg.Sign(rand.Reader, []byte(""), []Signer{*signer})
assert.Equal(ErrNilSignatures, err)
// check that it creates the signatures array from nil
msg.AddSignature(sig)
assert.Equal(len(msg.Signatures), 1)
msg.Signatures[0].Headers = nil
err = msg.Sign(rand.Reader, []byte(""), []Signer{*signer})
assert.Equal(ErrNilSigHeader, err)
msg.Signatures = nil
msg.AddSignature(sig)
msg.Signatures[0].Headers.Protected = nil
err = msg.Sign(rand.Reader, []byte(""), []Signer{*signer})
assert.Equal(ErrNilSigProtectedHeaders, err)
msg.Signatures = nil
sig.Headers.Protected = map[interface{}]interface{}{}
sig.Headers.Protected[algTag] = -41 // RSAES-OAEP w/ SHA-256 from [RFC8230]
sig.Headers.Protected[kidTag] = 1
sig.SignatureBytes = []byte("already signed")
msg.AddSignature(sig)
assert.Equal(len(msg.Signatures), 1)
assert.NotNil(msg.Signatures[0].Headers)
err = msg.Sign(rand.Reader, []byte(""), []Signer{})
assert.Equal("0 signers for 1 signatures", err.Error())
err = msg.Sign(rand.Reader, []byte(""), []Signer{*signer})
assert.Equal("SignMessage signature 0 already has signature bytes", err.Error())
msg.Signatures[0].SignatureBytes = nil
err = msg.Sign(rand.Reader, []byte(""), []Signer{*signer})
assert.Equal(ErrUnavailableHashFunc, err)
msg.Signatures[0].Headers.Protected[algTag] = ES256.Value
signer.alg = ES256
signer.PrivateKey = dsaPrivateKey
err = msg.Sign(rand.Reader, []byte(""), []Signer{*signer})
assert.Equal(ErrUnknownPrivateKeyType, err)
signer.alg = PS256
err = msg.Sign(rand.Reader, []byte(""), []Signer{*signer})
assert.Equal("Signer of type PS256 cannot generate a signature of type ES256", err.Error())
msg.Signatures[0].Headers.Protected[algTag] = -9000
err = msg.Sign(rand.Reader, []byte(""), []Signer{*signer})
assert.Equal("Algorithm with value -9000 not found", err.Error())
msg.Signatures[0].Headers.Protected[algTag] = 1
err = msg.Sign(rand.Reader, []byte(""), []Signer{*signer})
assert.Equal(ErrInvalidAlg, err)
delete(msg.Signatures[0].Headers.Protected, algTag)
err = msg.Sign(rand.Reader, []byte(""), []Signer{*signer})
assert.Equal(ErrAlgNotFound, err)
}
func TestSignatureEqual(t *testing.T) {
assert := assert.New(t)
var s1, s2 *Signature = nil, nil
assert.Equal(s1.Equal(s2), true)
s1 = &Signature{}
s2 = s1
assert.Equal(s1.Equal(s2), true)
s1.SignatureBytes = []byte("123")
assert.Equal(s1.Equal(s2), true)
s2 = &Signature{SignatureBytes: []byte("000")}
assert.Equal(s1.Equal(s2), false)
s2.SignatureBytes = s1.SignatureBytes
assert.Equal(s1.Equal(s2), true)
s1.Headers = &Headers{
Protected: map[interface{}]interface{}{algTag: -41}, // RSAES-OAEP w/ SHA-256 from [RFC8230]
}
assert.Equal(s1.Equal(s2), false)
s2.Headers = s1.Headers
assert.Equal(s1.Equal(s2), true)
}
func TestSignatureDecodeErrors(t *testing.T) {
assert := assert.New(t)
var (
s *Signature = nil
result interface{}
)
assert.Panics(func () { s.Decode(result) })
s = &Signature{}
result = 5
assert.Panics(func () { s.Decode(result) })
s = &Signature{}
result = []interface{}{1, 2}
assert.Panics(func () { s.Decode(result) })
s = &Signature{}
result = []interface{}{
[]byte("\xA0"),
map[interface{}]interface{}{},
[]byte(""),
}
assert.Panics(func () { s.Decode(result) })
s.Headers = &Headers{}
result = []interface{}{
[]byte("\xA0"),
map[interface{}]interface{}{},
-1,
}
assert.Panics(func () { s.Decode(result) })
}
func TestSignMessageSignatureDigest(t *testing.T) {
assert := assert.New(t)
var (
external = []byte("")
hashFunc = crypto.SHA256
signature *Signature = nil
msg *SignMessage = nil
digest []byte
err error
)
digest, err = msg.signatureDigest(external, signature, hashFunc)
assert.Equal(err.Error(), "Cannot compute signatureDigest on nil SignMessage")
assert.Equal(len(digest), 0)
msg = &SignMessage{}
digest, err = msg.signatureDigest(external, signature, hashFunc)
assert.Equal(err.Error(), "Cannot compute signatureDigest on nil SignMessage.Signatures")
assert.Equal(len(digest), 0)
msg.AddSignature(&Signature{
Headers: nil,
SignatureBytes: []byte("123"),
})
signature = &Signature{
Headers: nil,
SignatureBytes: nil,
}
digest, err = msg.signatureDigest(external, signature, hashFunc)
assert.Equal(err.Error(), "SignMessage.Signatures does not include the signature to digest")
assert.Equal(len(digest), 0)
msg = NewSignMessage()
signature = NewSignature()
signature.Headers.Protected[algTag] = ES256
msg.Signatures = []Signature{*signature}
digest, err = msg.signatureDigest(nil, signature, hashFunc)
assert.Equal(err, nil, "signatureDigest does not accept nil external")
}
func TestVerifyErrors(t *testing.T) {
assert := assert.New(t)
msg := NewSignMessage()
msg.Payload = []byte("payload to sign")
sig := NewSignature()
sig.Headers.Protected[algTag] = -41 // RSAES-OAEP w/ SHA-256 from [RFC8230]
sig.Headers.Protected[kidTag] = 1
signer, err := NewSigner(ES256, nil)
assert.Nil(err, "Error creating signer")
verifier := signer.Verifier()
verifiers := []Verifier{*verifier}
payload := []byte("")
msg.Signatures = []Signature{}
assert.Nil(msg.Verify(payload, verifiers))
msg.Signatures = nil
assert.Nil(msg.Verify(payload, verifiers))
msg.AddSignature(sig)
msg.Signatures[0].Headers.Protected = nil
assert.Equal(ErrNilSigProtectedHeaders, msg.Verify(payload, verifiers))
msg.Signatures[0].Headers = nil
assert.Equal(ErrNilSigHeader, msg.Verify(payload, verifiers))
sig = NewSignature()
sig.Headers.Protected[algTag] = -41 // RSAES-OAEP w/ SHA-256 from [RFC8230]
sig.Headers.Protected[kidTag] = 1
msg.Signatures[0] = *sig
assert.Equal("SignMessage signature 0 missing signature bytes to verify", msg.Verify(payload, verifiers).Error())
msg.Signatures[0].Headers.Protected[algTag] = -41 // RSAES-OAEP w/ SHA-256 from [RFC8230]
msg.Signatures[0].Headers.Protected[kidTag] = 1
msg.Signatures[0].SignatureBytes = []byte("already signed")
assert.Equal(ErrUnavailableHashFunc, msg.Verify(payload, verifiers))
msg.Signatures[0].Headers.Protected[algTag] = 1
assert.Equal(ErrInvalidAlg, msg.Verify(payload, verifiers))
msg.Signatures[0].Headers.Protected[algTag] = -7 // ECDSA w/ SHA-256 from [RFC8152]
assert.Equal("Wrong number of signatures 1 and verifiers 0", msg.Verify(payload, []Verifier{}).Error())
verifiers = []Verifier{
Verifier{
PublicKey: &ecdsa.PublicKey{
Curve: elliptic.P384(),
X: FromBase64Int("usWxHK2PmfnHKwXPS54m0kTcGJ90UiglWiGahtagnv8"),
Y: FromBase64Int("IBOL-C3BttVivg-lSreASjpkttcsz-1rb7btKLv8EX4"),
},
Alg: ES256,
},
}
assert.Equal("Expected 256 bit key, got 384 bits instead", msg.Verify(payload, verifiers).Error())
verifiers = []Verifier{
Verifier{
PublicKey: ecdsaPrivateKey.Public(),
Alg: ES256,
},
}
assert.Equal("invalid signature length: 14", msg.Verify(payload, verifiers).Error())
}