Skip to content

Commit a71502d

Browse files
committed
fix(#36): fixed importKey, added unit tests, copyright notice updates
1 parent c7b923a commit a71502d

21 files changed

+123
-27
lines changed

.github/workflows/test.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2023-2024 ARMORTAL TECHNOLOGIES PTY LTD
1+
# Copyright 2023-2025 ARMORTAL TECHNOLOGIES PTY LTD
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2023-2024 ARMORTAL TECHNOLOGIES PTY LTD
1+
# Copyright 2023-2025 ARMORTAL TECHNOLOGIES PTY LTD
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.

.vscode/launch.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2023-2024 ARMORTAL TECHNOLOGIES PTY LTD
1+
// Copyright 2023-2025 ARMORTAL TECHNOLOGIES PTY LTD
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.

algorithm.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2023-2024 ARMORTAL TECHNOLOGIES PTY LTD
1+
// Copyright 2023-2025 ARMORTAL TECHNOLOGIES PTY LTD
22

33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.

algorithms/ecdsa/ecdsa.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2023-2024 ARMORTAL TECHNOLOGIES PTY LTD
1+
// Copyright 2023-2025 ARMORTAL TECHNOLOGIES PTY LTD
22

33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.

algorithms/ecdsa/ecdsa_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2023-2024 ARMORTAL TECHNOLOGIES PTY LTD
1+
// Copyright 2023-2025 ARMORTAL TECHNOLOGIES PTY LTD
22

33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.

algorithms/hmac/hmac.go

+10-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2023-2024 ARMORTAL TECHNOLOGIES PTY LTD
1+
// Copyright 2023-2025 ARMORTAL TECHNOLOGIES PTY LTD
22

33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -297,8 +297,10 @@ func importKeyFromJsonWebKey(keyData *webcrypto.JsonWebKey, params *ImportParams
297297

298298
// If usages is non-empty and the use field of jwk is present and is not "sign", then throw a DataError.
299299
if len(usages) != 0 {
300-
if keyData.Use != "sign" {
301-
return nil, webcrypto.NewError(webcrypto.ErrDataError, "use must be 'sign'")
300+
if keyData.Use != "" {
301+
if keyData.Use != "sign" {
302+
return nil, webcrypto.NewError(webcrypto.ErrDataError, "use must be 'sign'")
303+
}
302304
}
303305
}
304306

@@ -316,12 +318,13 @@ func importKeyFromJsonWebKey(keyData *webcrypto.JsonWebKey, params *ImportParams
316318
return nil, webcrypto.NewError(webcrypto.ErrDataError, "k length cannot be less than hash length")
317319
}
318320

319-
if params.Length != uint64(length) {
320-
return nil, webcrypto.NewError(webcrypto.ErrDataError, "length provided does not match key length")
321+
// If the params length is specified, we'll check and ensure the key provided matches the length
322+
if params.Length != 0 {
323+
if params.Length != uint64(length) {
324+
return nil, webcrypto.NewError(webcrypto.ErrDataError, "length provided does not match key length")
325+
}
321326
}
322327

323-
params.Length = uint64(length)
324-
325328
if keyData.Ext != extractable {
326329
return nil, webcrypto.NewError(webcrypto.ErrDataError, "ext in key does not match value provided")
327330
}

algorithms/hmac/hmac_test.go

+94-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2023-2024 ARMORTAL TECHNOLOGIES PTY LTD
1+
// Copyright 2023-2025 ARMORTAL TECHNOLOGIES PTY LTD
22

33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -19,6 +19,7 @@ package hmac
1919
import (
2020
"bytes"
2121
"encoding/hex"
22+
"encoding/json"
2223
"reflect"
2324
"testing"
2425

@@ -130,6 +131,98 @@ func TestImportKey(t *testing.T) {
130131

131132
}
132133

134+
func Test_ImportKey_JsonWebKey(t *testing.T) {
135+
t.Run("import no use", func(t *testing.T) {
136+
k := `{"kty":"oct","key_ops":["sign","verify"],"alg":"HS256","ext":true,"k":"31G2ai1-ZfKsfQfNEQNga9H90J3q8pSHCBc9jcxM7IUzGwzofZJrNgCmE7lXOyR-_BxlA0NthOYT11NwRMOu1w"}`
137+
var jwk webcrypto.JsonWebKey
138+
if err := json.Unmarshal([]byte(k), &jwk); err != nil {
139+
t.Errorf("failed to unmarshal json: %s", err.Error())
140+
}
141+
142+
_, err := subtle.ImportKey(webcrypto.Jwk, &jwk, &webcrypto.Algorithm{
143+
Name: "HMAC",
144+
Params: &ImportParams{
145+
Hash: "SHA-256",
146+
},
147+
}, true, []webcrypto.KeyUsage{webcrypto.Sign, webcrypto.Verify})
148+
if err != nil {
149+
t.Errorf("failed to import key: %s", err.Error())
150+
}
151+
})
152+
153+
t.Run("import valid use", func(t *testing.T) {
154+
k := `{"kty":"oct","use":"sign","key_ops":["sign","verify"],"alg":"HS256","ext":true,"k":"31G2ai1-ZfKsfQfNEQNga9H90J3q8pSHCBc9jcxM7IUzGwzofZJrNgCmE7lXOyR-_BxlA0NthOYT11NwRMOu1w"}`
155+
var jwk webcrypto.JsonWebKey
156+
if err := json.Unmarshal([]byte(k), &jwk); err != nil {
157+
t.Errorf("failed to unmarshal json: %s", err.Error())
158+
}
159+
160+
_, err := subtle.ImportKey(webcrypto.Jwk, &jwk, &webcrypto.Algorithm{
161+
Name: "HMAC",
162+
Params: &ImportParams{
163+
Hash: "SHA-256",
164+
},
165+
}, true, []webcrypto.KeyUsage{webcrypto.Sign, webcrypto.Verify})
166+
if err != nil {
167+
t.Errorf("failed to import key: %s", err.Error())
168+
}
169+
})
170+
171+
t.Run("import invalid use", func(t *testing.T) {
172+
k := `{"kty":"oct","use":"enc","key_ops":["sign","verify"],"alg":"HS256","ext":true,"k":"31G2ai1-ZfKsfQfNEQNga9H90J3q8pSHCBc9jcxM7IUzGwzofZJrNgCmE7lXOyR-_BxlA0NthOYT11NwRMOu1w"}`
173+
var jwk webcrypto.JsonWebKey
174+
if err := json.Unmarshal([]byte(k), &jwk); err != nil {
175+
t.Errorf("failed to unmarshal json: %s", err.Error())
176+
}
177+
178+
_, err := subtle.ImportKey(webcrypto.Jwk, &jwk, &webcrypto.Algorithm{
179+
Name: "HMAC",
180+
Params: &ImportParams{
181+
Hash: "SHA-256",
182+
},
183+
}, true, []webcrypto.KeyUsage{webcrypto.Sign, webcrypto.Verify})
184+
if err == nil {
185+
t.Error("importKey should have returned error")
186+
}
187+
})
188+
189+
t.Run("import invalid key_ops", func(t *testing.T) {
190+
k := `{"kty":"oct","key_ops":["encrypt","verify"],"alg":"HS256","ext":true,"k":"31G2ai1-ZfKsfQfNEQNga9H90J3q8pSHCBc9jcxM7IUzGwzofZJrNgCmE7lXOyR-_BxlA0NthOYT11NwRMOu1w"}`
191+
var jwk webcrypto.JsonWebKey
192+
if err := json.Unmarshal([]byte(k), &jwk); err != nil {
193+
t.Errorf("failed to unmarshal json: %s", err.Error())
194+
}
195+
196+
_, err := subtle.ImportKey(webcrypto.Jwk, &jwk, &webcrypto.Algorithm{
197+
Name: "HMAC",
198+
Params: &ImportParams{
199+
Hash: "SHA-256",
200+
},
201+
}, true, []webcrypto.KeyUsage{webcrypto.Sign, webcrypto.Verify})
202+
if err == nil {
203+
t.Error("importKey should have returned error")
204+
}
205+
})
206+
207+
t.Run("import invalid key length", func(t *testing.T) {
208+
k := `{"kty":"oct","key_ops":["sign","verify"],"alg":"HS256","ext":true,"k":"VrmFU2huAL6phqi_vvGPvItpX2cJFy6rzjEQpjMqKA0"}`
209+
var jwk webcrypto.JsonWebKey
210+
if err := json.Unmarshal([]byte(k), &jwk); err != nil {
211+
t.Errorf("failed to unmarshal json: %s", err.Error())
212+
}
213+
214+
_, err := subtle.ImportKey(webcrypto.Jwk, &jwk, &webcrypto.Algorithm{
215+
Name: "HMAC",
216+
Params: &ImportParams{
217+
Hash: "SHA-256",
218+
},
219+
}, true, []webcrypto.KeyUsage{webcrypto.Sign, webcrypto.Verify})
220+
if err == nil {
221+
t.Error("importKey should have returned error")
222+
}
223+
})
224+
}
225+
133226
func TestSign(t *testing.T) {
134227
raw, err := hex.DecodeString(rawHexKey)
135228
if err != nil {

algorithms/rsa/rsa.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2023-2024 ARMORTAL TECHNOLOGIES PTY LTD
1+
// Copyright 2023-2025 ARMORTAL TECHNOLOGIES PTY LTD
22

33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.

algorithms/rsa/rsa_oaep.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2023-2024 ARMORTAL TECHNOLOGIES PTY LTD
1+
// Copyright 2023-2025 ARMORTAL TECHNOLOGIES PTY LTD
22

33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.

algorithms/rsa/rsa_oaep_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2023-2024 ARMORTAL TECHNOLOGIES PTY LTD
1+
// Copyright 2023-2025 ARMORTAL TECHNOLOGIES PTY LTD
22

33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.

algorithms/sha/sha.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2023-2024 ARMORTAL TECHNOLOGIES PTY LTD
1+
// Copyright 2023-2025 ARMORTAL TECHNOLOGIES PTY LTD
22

33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.

algorithms/sha/sha_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2023-2024 ARMORTAL TECHNOLOGIES PTY LTD
1+
// Copyright 2023-2025 ARMORTAL TECHNOLOGIES PTY LTD
22

33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.

crypto.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2023-2024 ARMORTAL TECHNOLOGIES PTY LTD
1+
// Copyright 2023-2025 ARMORTAL TECHNOLOGIES PTY LTD
22

33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.

crypto_key.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2023-2024 ARMORTAL TECHNOLOGIES PTY LTD
1+
// Copyright 2023-2025 ARMORTAL TECHNOLOGIES PTY LTD
22

33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.

errors.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2023-2024 ARMORTAL TECHNOLOGIES PTY LTD
1+
// Copyright 2023-2025 ARMORTAL TECHNOLOGIES PTY LTD
22

33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.

format.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2023-2024 ARMORTAL TECHNOLOGIES PTY LTD
1+
// Copyright 2023-2025 ARMORTAL TECHNOLOGIES PTY LTD
22

33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2023-2024 ARMORTAL TECHNOLOGIES PTY LTD
1+
// Copyright 2023-2025 ARMORTAL TECHNOLOGIES PTY LTD
22

33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.

subtle.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2023-2024 ARMORTAL TECHNOLOGIES PTY LTD
1+
// Copyright 2023-2025 ARMORTAL TECHNOLOGIES PTY LTD
22

33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.

util/util.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2023-2024 ARMORTAL TECHNOLOGIES PTY LTD
1+
// Copyright 2023-2025 ARMORTAL TECHNOLOGIES PTY LTD
22

33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.

util/util_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2023-2024 ARMORTAL TECHNOLOGIES PTY LTD
1+
// Copyright 2023-2025 ARMORTAL TECHNOLOGIES PTY LTD
22

33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.

0 commit comments

Comments
 (0)