/
signaturealgorithm_test.go
133 lines (116 loc) · 5.22 KB
/
signaturealgorithm_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
/*
Copyright 2019-present Faye Amacker.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package webauthn
import (
"crypto"
"crypto/x509"
"testing"
)
func TestSignatureAlgorithm(t *testing.T) {
testCases := []struct {
coseAlg int
wantSigAlg x509.SignatureAlgorithm
wantPubKeyAlg x509.PublicKeyAlgorithm
wantHash crypto.Hash
wantIsRSA bool
wantIsRSAPSS bool
wantIsECDSA bool
}{
{COSEAlgES256, x509.ECDSAWithSHA256, x509.ECDSA, crypto.SHA256, false, false, true},
{COSEAlgES384, x509.ECDSAWithSHA384, x509.ECDSA, crypto.SHA384, false, false, true},
{COSEAlgES512, x509.ECDSAWithSHA512, x509.ECDSA, crypto.SHA512, false, false, true},
{COSEAlgPS256, x509.SHA256WithRSAPSS, x509.RSA, crypto.SHA256, true, true, false},
{COSEAlgPS384, x509.SHA384WithRSAPSS, x509.RSA, crypto.SHA384, true, true, false},
{COSEAlgPS512, x509.SHA512WithRSAPSS, x509.RSA, crypto.SHA512, true, true, false},
{COSEAlgRS1, x509.SHA1WithRSA, x509.RSA, crypto.SHA1, true, false, false},
{COSEAlgRS256, x509.SHA256WithRSA, x509.RSA, crypto.SHA256, true, false, false},
{COSEAlgRS384, x509.SHA384WithRSA, x509.RSA, crypto.SHA384, true, false, false},
{COSEAlgRS512, x509.SHA512WithRSA, x509.RSA, crypto.SHA512, true, false, false},
}
for _, tc := range testCases {
if sigAlg, err := CoseAlgToSignatureAlgorithm(tc.coseAlg); err != nil {
t.Errorf("SignatureAlgorithm(%d) returns error %q", tc.coseAlg, err)
} else {
if sigAlg.Algorithm != tc.wantSigAlg {
t.Errorf("SignatureAlgorithm(%d).Algorithm = %s, want %s", tc.coseAlg, sigAlg.Algorithm, tc.wantSigAlg)
}
if sigAlg.PublicKeyAlgorithm != tc.wantPubKeyAlg {
t.Errorf("SignatureAlgorithm(%d).PublicKeyAlgorithm = %s, want %s", tc.coseAlg, sigAlg.PublicKeyAlgorithm, tc.wantPubKeyAlg)
}
if sigAlg.Hash != tc.wantHash {
t.Errorf("SignatureAlgorithm(%d).Hash = %v, want %v", tc.coseAlg, sigAlg.Hash, tc.wantHash)
}
if sigAlg.IsRSA() != tc.wantIsRSA {
t.Errorf("SignatureAlgorithm(%d).IsRSA() = %t , want %t", tc.coseAlg, sigAlg.IsRSA(), tc.wantIsRSA)
}
if sigAlg.IsRSAPSS() != tc.wantIsRSAPSS {
t.Errorf("SignatureAlgorithm(%d).IsRSAPSS() = %t, want %t", tc.coseAlg, sigAlg.IsRSAPSS(), tc.wantIsRSAPSS)
}
if sigAlg.IsECDSA() != tc.wantIsECDSA {
t.Errorf("SignatureAlgorithm(%d).IsECDSA() = %t, want %t", tc.coseAlg, sigAlg.IsECDSA(), tc.wantIsECDSA)
}
if !signatureAlgorithmRegistered(tc.coseAlg) {
t.Errorf("COSEAlgorithmAvailable(%d) returns false, want true", tc.coseAlg)
}
}
}
}
/*
func TestRegisterAndUnregisterSignatureAlgorithm(t *testing.T) {
coseAlgRS1 := -65535 // RSASSA-PKCS1-v1_5 with SHA-1
RegisterSignatureAlgorithm(coseAlgRS1, x509.SHA1WithRSA, x509.RSA, crypto.SHA1)
if sigAlg, err := CoseAlgToSignatureAlgorithm(coseAlgRS1); err != nil {
t.Errorf("SignatureAlgorithm(%d) returns error %q", coseAlgRS1, err)
} else {
if sigAlg.Algorithm != x509.SHA1WithRSA {
t.Errorf("SignatureAlgorithm(%d).Algorithm = %s, want %s", coseAlgRS1, sigAlg.Algorithm, x509.SHA1WithRSA)
}
if sigAlg.PublicKeyAlgorithm != x509.RSA {
t.Errorf("SignatureAlgorithm(%d).PublicKeyAlgorithm = %s, want %s", coseAlgRS1, sigAlg.PublicKeyAlgorithm, x509.RSA)
}
if sigAlg.Hash != crypto.SHA1 {
t.Errorf("SignatureAlgorithm(%d).Hash = %v, want %v", coseAlgRS1, sigAlg.Hash, crypto.SHA1)
}
if !sigAlg.IsRSA() {
t.Errorf("SignatureAlgorithm(%d).IsRSA() = false, want true", coseAlgRS1)
}
if sigAlg.IsRSAPSS() {
t.Errorf("SignatureAlgorithm(%d).IsRSAPSS() = true, want false", coseAlgRS1)
}
if sigAlg.IsECDSA() {
t.Errorf("SignatureAlgorithm(%d).IsECDSA() = true, want false", coseAlgRS1)
}
}
if !signatureAlgorithmRegistered(coseAlgRS1) {
t.Errorf("COSEAlgorithmAvailable(%d) returns false, want true", coseAlgRS1)
}
UnregisterSignatureAlgorithm(coseAlgRS1)
if _, err := CoseAlgToSignatureAlgorithm(coseAlgRS1); err == nil {
t.Errorf("SignatureAlgorithm(%d) returns no error, want error containing substring \"unsupported COSE algorithm\"", coseAlgRS1)
}
if signatureAlgorithmRegistered(coseAlgRS1) {
t.Errorf("COSEAlgorithmAvailable(%d) returns true, want false", coseAlgRS1)
}
}
func TestUnregisteredSignatureAlgorithm(t *testing.T) {
coseAlgRS1 := -65535 // RSASSA-PKCS1-v1_5 with SHA-1
if _, err := CoseAlgToSignatureAlgorithm(coseAlgRS1); err == nil {
t.Errorf("SignatureAlgorithm(%d) returns no error, want error containing substring \"unsupported COSE algorithm\"", coseAlgRS1)
} else if !strings.Contains(err.Error(), "COSE algorithm -65535 is not registered") {
t.Errorf("SignatureAlgorithm(%d) returns error %q, want error containing substring \"unsupported COSE algorithm\"", coseAlgRS1, err)
}
if signatureAlgorithmRegistered(coseAlgRS1) {
t.Errorf("COSEAlgorithmAvailable(%d) returns true, want false", coseAlgRS1)
}
}
*/