-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrypto_test.go
203 lines (162 loc) · 7.17 KB
/
crypto_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
package webidrsa
import (
"crypto/rand"
"crypto/rsa"
"crypto/sha1"
"encoding/base64"
"testing"
"github.com/stretchr/testify/assert"
)
var (
testPriv *rsa.PrivateKey
testPub *rsa.PublicKey
privKey = []byte(`-----BEGIN RSA PRIVATE KEY-----
MIICXgIBAAKBgQDCFENGw33yGihy92pDjZQhl0C36rPJj+CvfSC8+q28hxA161QF
NUd13wuCTUcq0Qd2qsBe/2hFyc2DCJJg0h1L78+6Z4UMR7EOcpfdUE9Hf3m/hs+F
UR45uBJeDK1HSFHD8bHKD6kv8FPGfJTotc+2xjJwoYi+1hqp1fIekaxsyQIDAQAB
AoGBAJR8ZkCUvx5kzv+utdl7T5MnordT1TvoXXJGXK7ZZ+UuvMNUCdN2QPc4sBiA
QWvLw1cSKt5DsKZ8UETpYPy8pPYnnDEz2dDYiaew9+xEpubyeW2oH4Zx71wqBtOK
kqwrXa/pzdpiucRRjk6vE6YY7EBBs/g7uanVpGibOVAEsqH1AkEA7DkjVH28WDUg
f1nqvfn2Kj6CT7nIcE3jGJsZZ7zlZmBmHFDONMLUrXR/Zm3pR5m0tCmBqa5RK95u
412jt1dPIwJBANJT3v8pnkth48bQo/fKel6uEYyboRtA5/uHuHkZ6FQF7OUkGogc
mSJluOdc5t6hI1VsLn0QZEjQZMEOWr+wKSMCQQCC4kXJEsHAve77oP6HtG/IiEn7
kpyUXRNvFsDE0czpJJBvL/aRFUJxuRK91jhjC68sA7NsKMGg5OXb5I5Jj36xAkEA
gIT7aFOYBFwGgQAQkWNKLvySgKbAZRTeLBacpHMuQdl1DfdntvAyqpAZ0lY0RKmW
G6aFKaqQfOXKCyWoUiVknQJAXrlgySFci/2ueKlIE1QqIiLSZ8V8OlpFLRnb1pzI
7U1yQXnTAEFYM560yJlzUpOb1V4cScGd365tiSMvxLOvTA==
-----END RSA PRIVATE KEY-----`)
pubKey = []byte(`-----BEGIN RSA PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDCFENGw33yGihy92pDjZQhl0C3
6rPJj+CvfSC8+q28hxA161QFNUd13wuCTUcq0Qd2qsBe/2hFyc2DCJJg0h1L78+6
Z4UMR7EOcpfdUE9Hf3m/hs+FUR45uBJeDK1HSFHD8bHKD6kv8FPGfJTotc+2xjJw
oYi+1hqp1fIekaxsyQIDAQAB
-----END RSA PUBLIC KEY-----`)
pubT = "RSAPublicKey"
pubN = "c2144346c37df21a2872f76a438d94219740b7eab3c98fe0af7d20bcfaadbc871035eb5405354775df0b824d472ad10776aac05eff6845c9cd83089260d21d4befcfba67850c47b10e7297dd504f477f79bf86cf85511e39b8125e0cad474851c3f1b1ca0fa92ff053c67c94e8b5cfb6c63270a188bed61aa9d5f21e91ac6cc9"
pubE = "65537"
)
func init() {
testPriv, _ = rsa.GenerateKey(rand.Reader, rsaBits)
testPub = &testPriv.PublicKey
}
func TestNewRandomID(t *testing.T) {
assert.Equal(t, randLength, len(NewRandomID()))
}
func TestSignaturesRSA(t *testing.T) {
h := `WebID-RSA source="https://deiu.me/Private/", username="https://deiu.me/profile#me", nonce="MTQzODc4MzA5NXxtS1dYcVd4bGRjVXQ2bFVEMXk2NE5KMDU1TFB3Nk9qM2FmMWduMk4tdl9tWDdvZXBtdUJSa1ZMRHE4WWZ1dUE0RlNGeDl0OGt6SGZnbkpZbW5CWE96TUxRamJ6a3xCC-Ik7gERpCBc__l2OK0DxVxyIiLTDVZ7rLIib2MNSQ==", sig="qiTKnXaXgMfGEA2LLCqhFWiB+6T9gXvLR6nO2dCvk71nBoK3MiwLxbsF83uKT81ur9SucDJ2fmjLKPbP9o7NrkYrM45rkPJsXHjbAzHDw2DftKLez5DF70HtDa1rEaUEF1mLrNMGfL4VYea5z15lNNNiDKaJpCwhgeHNB1x2qNY="`
_toSign := `https://deiu.me/Private/https://deiu.me/profile#meMTQzODc4MzA5NXxtS1dYcVd4bGRjVXQ2bFVEMXk2NE5KMDU1TFB3Nk9qM2FmMWduMk4tdl9tWDdvZXBtdUJSa1ZMRHE4WWZ1dUE0RlNGeDl0OGt6SGZnbkpZbW5CWE96TUxRamJ6a3xCC-Ik7gERpCBc__l2OK0DxVxyIiLTDVZ7rLIib2MNSQ==`
_sig := `qiTKnXaXgMfGEA2LLCqhFWiB+6T9gXvLR6nO2dCvk71nBoK3MiwLxbsF83uKT81ur9SucDJ2fmjLKPbP9o7NrkYrM45rkPJsXHjbAzHDw2DftKLez5DF70HtDa1rEaUEF1mLrNMGfL4VYea5z15lNNNiDKaJpCwhgeHNB1x2qNY=`
p, err := ParseAuthorizationHeader(h)
assert.NoError(t, err)
assert.Equal(t, _sig, p.Signature)
parserPem, perr := ParsePublicPEMKey([]byte(""))
assert.Error(t, perr)
assert.Nil(t, parserPem)
parserPem, perr = ParsePublicPEMKey([]byte(`-----BEGIN RSA PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDCFENGw33yGihy92pDjZQhl0C3
oYi+1hqp1fIekaxsyQIDAQAB
-----END RSA PUBLIC KEY-----`))
assert.Error(t, perr)
assert.Nil(t, parserPem)
parserPem, perr = ParsePublicPEMKey([]byte(`-----BEGIN RSA FUNKY KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDCFENGw33yGihy92pDjZQhl0C3
-----END RSA FUNKY KEY-----`))
assert.Error(t, perr)
assert.Nil(t, parserPem)
parserPem, perr = ParsePublicPEMKey(pubKey)
assert.NoError(t, perr)
parser, perr := ParsePublicKeyNE(pubT, "", pubE)
assert.Nil(t, parser)
assert.Error(t, perr)
parser, perr = ParsePublicKeyNE(pubT, pubN, "a")
assert.Nil(t, parser)
assert.Error(t, perr)
parser, perr = ParsePublicKeyNE("DSAPublicKey", pubN, pubE)
assert.Nil(t, parser)
assert.Error(t, perr)
parser, perr = ParsePublicKeyNE(pubT, pubN, pubE)
assert.NoError(t, perr)
signer, err := ParsePrivatePEMKey([]byte(""))
assert.Error(t, err)
assert.Nil(t, signer)
signer, err = ParsePrivatePEMKey([]byte(`-----BEGIN RSA FUNKY KEY-----
MIICXgIBAAKBgQDCFENGw33yGihy92pDjZQhl0C36rPJj+CvfSC8+q28hxA161QF
NUd13wuCTUcq0Qd2qsBe/2hFyc2DCJJg0h1L78+6Z4UMR7EOcpfdUE9Hf3m/hs+F
UR45uBJeDK1HSFHD8bHKD6kv8FPGfJTotc+2xjJwoYi+1hqp1fIekaxsyQIDAQAB
AoGBAJR8ZkCUvx5kzv+utdl7T5MnordT1TvoXXJGXK7ZZ+UuvMNUCdN2QPc4sBiA
QWvLw1cSKt5DsKZ8UETpYPy8pPYnnDEz2dDYiaew9+xEpubyeW2oH4Zx71wqBtOK
kqwrXa/pzdpiucRRjk6vE6YY7EBBs/g7uanVpGibOVAEsqH1AkEA7DkjVH28WDUg
f1nqvfn2Kj6CT7nIcE3jGJsZZ7zlZmBmHFDONMLUrXR/Zm3pR5m0tCmBqa5RK95u
412jt1dPIwJBANJT3v8pnkth48bQo/fKel6uEYyboRtA5/uHuHkZ6FQF7OUkGogc
mSJluOdc5t6hI1VsLn0QZEjQZMEOWr+wKSMCQQCC4kXJEsHAve77oP6HtG/IiEn7
kpyUXRNvFsDE0czpJJBvL/aRFUJxuRK91jhjC68sA7NsKMGg5OXb5I5Jj36xAkEA
gIT7aFOYBFwGgQAQkWNKLvySgKbAZRTeLBacpHMuQdl1DfdntvAyqpAZ0lY0RKmW
G6aFKaqQfOXKCyWoUiVknQJAXrlgySFci/2ueKlIE1QqIiLSZ8V8OlpFLRnb1pzI
7U1yQXnTAEFYM560yJlzUpOb1V4cScGd365tiSMvxLOvTA==
-----END RSA FUNKY KEY-----`))
assert.Error(t, err)
assert.Nil(t, signer)
signer, err = ParsePrivatePEMKey([]byte(`-----BEGIN RSA PRIVATE KEY-----
MIICXgIBAAKBgQDCFENGw33yGihy92pDjZQhl0C36rPJj+CvfSC8+q28hxA161QF
kpyUXRNvFsDE0czpJJBvL/aRFUJxuRK91jhjC68sA7NsKMGg5OXb5I5Jj36xAkEA
gIT7aFOYBFwGgQAQkWNKLvySgKbAZRTeLBacpHMuQdl1DfdntvAyqpAZ0lY0RKmW
G6aFKaqQfOXKCyWoUiVknQJAXrlgySFci/2ueKlIE1QqIiLSZ8V8OlpFLRnb1pzI
7U1yQXnTAEFYM560yJlzUpOb1V4cScGd365tiSMvxLOvTA==
-----END RSA PRIVATE KEY-----`))
assert.Error(t, err)
assert.Nil(t, signer)
signer, err = ParsePrivatePEMKey(privKey)
assert.NoError(t, err)
toSign := p.Source + p.Username + p.Nonce
assert.Equal(t, _toSign, toSign)
claim := sha1.Sum([]byte(toSign))
signed, err := signer.Sign(claim[:])
assert.NoError(t, err)
b64Sig := base64.StdEncoding.EncodeToString(signed)
assert.Equal(t, p.Signature, b64Sig)
// println(p.Source, p.Username, p.Nonce, p.Signature)
sig, err := base64.StdEncoding.DecodeString(p.Signature)
assert.NoError(t, err)
err = parser.Verify(claim[:], sig)
assert.NoError(t, err)
sig, err = base64.StdEncoding.DecodeString(_sig)
assert.NoError(t, err)
err = parserPem.Verify(claim[:], sig)
assert.NoError(t, err)
err = parser.Verify(claim[:], sig)
assert.NoError(t, err)
}
func TestSignAndVerify(t *testing.T) {
toSign := "some string"
claim := sha1.Sum([]byte(toSign))
signer, err := ParsePrivatePEMKey(privKey)
assert.NoError(t, err)
signed, err := signer.Sign(claim[:])
assert.NoError(t, err)
sig := base64.URLEncoding.EncodeToString(signed)
assert.NotEmpty(t, sig)
parser, perr := ParsePublicPEMKey(pubKey)
assert.NoError(t, perr)
err = parser.Verify(claim[:], signed)
assert.NoError(t, err)
// check with ParsePublicKey
parser, err = ParsePublicKeyNE(pubT, pubN, pubE)
assert.NoError(t, perr)
err = parser.Verify(claim[:], signed)
assert.NoError(t, err)
// check with parse rsa.PublicKey
signer, err = ParsePrivateKey(testPriv)
assert.NoError(t, err)
signed, err = signer.Sign(claim[:])
assert.NoError(t, err)
sig = base64.StdEncoding.EncodeToString(signed)
assert.NotEmpty(t, sig)
parser, perr = ParsePublicKey(testPub)
assert.NoError(t, perr)
err = parser.Verify(claim[:], signed)
assert.NoError(t, err)
_, err = newSignerFromKey(testPub)
assert.Error(t, err)
_, err = newVerifierFromKey(testPriv)
assert.Error(t, err)
}