| 1 |
From: Jacob Hoffman-Andrews <[email protected]> |
| 2 |
Date: Tue, 18 Jun 2024 12:43:16 -0700 |
| 3 |
Subject: [PATCH] doc: note OpaqueKeyEncrypter can't be implemented (#129) |
| 4 |
|
| 5 |
Also, remove TestOpaqueKeyRoundtripJWE test case, which is expensive |
| 6 |
(because it incidentally uses the large default PBES2Count) and isn't |
| 7 |
testing reachable code. It can be restored if/when we fix the API for |
| 8 |
OpaqueKeyEncrypter so it can be implemented (and possibly pass along |
| 9 |
PBES2 parameters). |
| 10 |
|
| 11 |
Fixes #112, updates #114 |
| 12 |
|
| 13 |
Note: I tried simply exporting `encryptKey` as `EncryptKey` and adding |
| 14 |
parameters, but it wound up getting more involved because we would also |
| 15 |
have to export the return type `recipientInfo`, which in turn contains |
| 16 |
the unexported `*rawHeader`. For now this documents the status quo |
| 17 |
(OpaqueKeyEncrypter doesn't work) and speeds up our tests dramatically |
| 18 |
(11s on my machine). |
| 19 |
--- |
| 20 |
opaque.go | 3 +++ |
| 21 |
opaque_test.go | 55 ------------------------------------------------------- |
| 22 |
2 files changed, 3 insertions(+), 55 deletions(-) |
| 23 |
|
| 24 |
diff --git a/opaque.go b/opaque.go |
| 25 |
index 68db085..4294272 100644 |
| 26 |
--- a/opaque.go |
| 27 |
+++ b/opaque.go |
| 28 |
@@ -83,6 +83,9 @@ func (o *opaqueVerifier) verifyPayload(payload []byte, signature []byte, alg Sig |
| 29 |
} |
| 30 |
|
| 31 |
// OpaqueKeyEncrypter is an interface that supports encrypting keys with an opaque key. |
| 32 |
+// |
| 33 |
+// Note: this cannot currently be implemented outside this package because of its |
| 34 |
+// unexported method. |
| 35 |
type OpaqueKeyEncrypter interface { |
| 36 |
// KeyID returns the kid |
| 37 |
KeyID() string |
| 38 |
diff --git a/opaque_test.go b/opaque_test.go |
| 39 |
index e3c31aa..275737f 100644 |
| 40 |
--- a/opaque_test.go |
| 41 |
+++ b/opaque_test.go |
| 42 |
@@ -286,61 +286,6 @@ func rtSerialize(t *testing.T, serializer func(*JSONWebSignature) (string, error |
| 43 |
return sig |
| 44 |
} |
| 45 |
|
| 46 |
-func TestOpaqueKeyRoundtripJWE(t *testing.T) { |
| 47 |
- keyAlgs := []KeyAlgorithm{ |
| 48 |
- ECDH_ES_A128KW, ECDH_ES_A192KW, ECDH_ES_A256KW, A128KW, A192KW, A256KW, |
| 49 |
- RSA1_5, RSA_OAEP, RSA_OAEP_256, A128GCMKW, A192GCMKW, A256GCMKW, |
| 50 |
- PBES2_HS256_A128KW, PBES2_HS384_A192KW, PBES2_HS512_A256KW, |
| 51 |
- } |
| 52 |
- encAlgs := []ContentEncryption{A128GCM, A192GCM, A256GCM, A128CBC_HS256, A192CBC_HS384, A256CBC_HS512} |
| 53 |
- kid := "test-kid" |
| 54 |
- |
| 55 |
- serializers := []func(*JSONWebEncryption) (string, error){ |
| 56 |
- func(obj *JSONWebEncryption) (string, error) { return obj.CompactSerialize() }, |
| 57 |
- func(obj *JSONWebEncryption) (string, error) { return obj.FullSerialize(), nil }, |
| 58 |
- } |
| 59 |
- |
| 60 |
- for _, alg := range keyAlgs { |
| 61 |
- for _, enc := range encAlgs { |
| 62 |
- for _, testKey := range generateTestKeys(alg, enc) { |
| 63 |
- for _, serializer := range serializers { |
| 64 |
- kew := makeOpaqueKeyEncrypter(t, testKey.enc, alg, kid) |
| 65 |
- encrypter, err := NewEncrypter( |
| 66 |
- enc, |
| 67 |
- Recipient{ |
| 68 |
- Algorithm: alg, |
| 69 |
- Key: kew, |
| 70 |
- }, |
| 71 |
- &EncrypterOptions{}, |
| 72 |
- ) |
| 73 |
- if err != nil { |
| 74 |
- t.Fatal(err, alg) |
| 75 |
- } |
| 76 |
- |
| 77 |
- jwe, err := encrypter.Encrypt([]byte("foo bar")) |
| 78 |
- if err != nil { |
| 79 |
- t.Fatal(err, alg) |
| 80 |
- } |
| 81 |
- |
| 82 |
- dw := makeOpaqueKeyDecrypter(t, testKey.dec, alg) |
| 83 |
- jwe = jweSerialize(t, serializer, jwe, dw) |
| 84 |
- if jwe.Header.KeyID != kid { |
| 85 |
- t.Errorf("expected jwe kid to equal %s but got %s", kid, jwe.Header.KeyID) |
| 86 |
- } |
| 87 |
- |
| 88 |
- out, err := jwe.Decrypt(dw) |
| 89 |
- if err != nil { |
| 90 |
- t.Fatal(err, out) |
| 91 |
- } |
| 92 |
- if string(out) != "foo bar" { |
| 93 |
- t.Errorf("expected decrypted jwe to equal %s but got %s", "foo bar", string(out)) |
| 94 |
- } |
| 95 |
- } |
| 96 |
- } |
| 97 |
- } |
| 98 |
- } |
| 99 |
-} |
| 100 |
- |
| 101 |
func jweSerialize(t *testing.T, serializer func(*JSONWebEncryption) (string, error), jwe *JSONWebEncryption, d OpaqueKeyDecrypter) *JSONWebEncryption { |
| 102 |
b, err := serializer(jwe) |
| 103 |
if err != nil { |