-
Notifications
You must be signed in to change notification settings - Fork 0
/
sign_test.go
133 lines (112 loc) · 3.66 KB
/
sign_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
package okms
import (
"context"
"crypto"
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/rsa"
"encoding/base64"
"strings"
"testing"
"github.com/google/uuid"
"github.com/ovh/okms-sdk-go/mocks"
"github.com/ovh/okms-sdk-go/types"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
)
func TestSigner_RSA(t *testing.T) {
api := mocks.NewAPIMock(t)
client := Client{api}
keyId := uuid.New()
format := types.Jwk
pKey, err := rsa.GenerateKey(rand.Reader, 2048)
require.NoError(t, err)
jwk, err := types.NewJsonWebKey(pKey, []types.CryptographicUsages{types.Sign, types.Verify}, keyId.String())
require.NoError(t, err)
api.EXPECT().GetServiceKey(mock.Anything, keyId, &format).
Return(&types.GetServiceKeyResponse{
Attributes: &map[string]interface{}{"state": "active"},
Keys: &[]types.JsonWebKey{jwk},
}, nil).
Once()
signer, err := client.NewSigner(context.Background(), keyId)
require.NoError(t, err)
require.Equal(t, pKey.Public(), signer.Public())
tcs := []struct {
alg types.DigitalSignatureAlgorithms
hash crypto.SignerOpts
}{
{alg: types.RS256, hash: crypto.SHA256},
{alg: types.RS384, hash: crypto.SHA384},
{alg: types.RS512, hash: crypto.SHA512},
{alg: types.PS256, hash: &rsa.PSSOptions{Hash: crypto.SHA256}},
{alg: types.PS384, hash: &rsa.PSSOptions{Hash: crypto.SHA384}},
{alg: types.PS512, hash: &rsa.PSSOptions{Hash: crypto.SHA512}},
}
for _, tc := range tcs {
t.Run(string(tc.alg), func(t *testing.T) {
hf := tc.hash.HashFunc().New()
hf.Write([]byte("the message"))
digest := hf.Sum(nil)
var rawsig []byte
var err error
if strings.HasPrefix(string(tc.alg), "RS") {
rawsig, err = rsa.SignPKCS1v15(rand.Reader, pKey, tc.hash.HashFunc(), digest)
} else {
rawsig, err = rsa.SignPSS(rand.Reader, pKey, tc.hash.HashFunc(), digest, tc.hash.(*rsa.PSSOptions))
}
require.NoError(t, err)
api.EXPECT().Sign(mock.Anything, keyId, tc.alg, true, mock.Anything).
Return(base64.StdEncoding.EncodeToString(rawsig), nil).
Once()
sig, err := signer.Sign(rand.Reader, digest, tc.hash)
require.NoError(t, err)
require.Equal(t, rawsig, sig)
})
}
}
func TestSigner_ECDSA(t *testing.T) {
api := mocks.NewAPIMock(t)
client := Client{api}
keyId := uuid.New()
format := types.Jwk
pKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
require.NoError(t, err)
jwk, err := types.NewJsonWebKey(pKey, []types.CryptographicUsages{types.Sign, types.Verify}, keyId.String())
require.NoError(t, err)
api.EXPECT().GetServiceKey(mock.Anything, keyId, &format).
Return(&types.GetServiceKeyResponse{
Attributes: &map[string]interface{}{"state": "active"},
Keys: &[]types.JsonWebKey{jwk},
}, nil).
Once()
signer, err := client.NewSigner(context.Background(), keyId)
require.NoError(t, err)
require.Equal(t, pKey.Public(), signer.Public())
tcs := []struct {
alg types.DigitalSignatureAlgorithms
hash crypto.SignerOpts
}{
{alg: types.ES256, hash: crypto.SHA256},
{alg: types.ES384, hash: crypto.SHA384},
{alg: types.ES512, hash: crypto.SHA512},
}
for _, tc := range tcs {
t.Run(string(tc.alg), func(t *testing.T) {
hf := tc.hash.HashFunc().New()
hf.Write([]byte("the message"))
digest := hf.Sum(nil)
r, s, err := ecdsa.Sign(rand.Reader, pKey, digest)
require.NoError(t, err)
rawsig := r.Bytes()
rawsig = append(rawsig, s.Bytes()...)
api.EXPECT().Sign(mock.Anything, keyId, tc.alg, true, digest).
Return(base64.StdEncoding.EncodeToString(rawsig), nil).
Once()
sig, err := signer.Sign(rand.Reader, digest, tc.hash)
require.NoError(t, err)
require.True(t, ecdsa.VerifyASN1(&pKey.PublicKey, digest, sig))
})
}
}