From: Jacob Hoffman-Andrews Date: Tue, 18 Jun 2024 12:43:16 -0700 Subject: [PATCH] doc: note OpaqueKeyEncrypter can't be implemented (#129) Also, remove TestOpaqueKeyRoundtripJWE test case, which is expensive (because it incidentally uses the large default PBES2Count) and isn't testing reachable code. It can be restored if/when we fix the API for OpaqueKeyEncrypter so it can be implemented (and possibly pass along PBES2 parameters). Fixes #112, updates #114 Note: I tried simply exporting `encryptKey` as `EncryptKey` and adding parameters, but it wound up getting more involved because we would also have to export the return type `recipientInfo`, which in turn contains the unexported `*rawHeader`. For now this documents the status quo (OpaqueKeyEncrypter doesn't work) and speeds up our tests dramatically (11s on my machine). --- opaque.go | 3 +++ opaque_test.go | 55 ------------------------------------------------------- 2 files changed, 3 insertions(+), 55 deletions(-) diff --git a/opaque.go b/opaque.go index 68db085..4294272 100644 --- a/opaque.go +++ b/opaque.go @@ -83,6 +83,9 @@ func (o *opaqueVerifier) verifyPayload(payload []byte, signature []byte, alg Sig } // OpaqueKeyEncrypter is an interface that supports encrypting keys with an opaque key. +// +// Note: this cannot currently be implemented outside this package because of its +// unexported method. type OpaqueKeyEncrypter interface { // KeyID returns the kid KeyID() string diff --git a/opaque_test.go b/opaque_test.go index e3c31aa..275737f 100644 --- a/opaque_test.go +++ b/opaque_test.go @@ -286,61 +286,6 @@ func rtSerialize(t *testing.T, serializer func(*JSONWebSignature) (string, error return sig } -func TestOpaqueKeyRoundtripJWE(t *testing.T) { - keyAlgs := []KeyAlgorithm{ - ECDH_ES_A128KW, ECDH_ES_A192KW, ECDH_ES_A256KW, A128KW, A192KW, A256KW, - RSA1_5, RSA_OAEP, RSA_OAEP_256, A128GCMKW, A192GCMKW, A256GCMKW, - PBES2_HS256_A128KW, PBES2_HS384_A192KW, PBES2_HS512_A256KW, - } - encAlgs := []ContentEncryption{A128GCM, A192GCM, A256GCM, A128CBC_HS256, A192CBC_HS384, A256CBC_HS512} - kid := "test-kid" - - serializers := []func(*JSONWebEncryption) (string, error){ - func(obj *JSONWebEncryption) (string, error) { return obj.CompactSerialize() }, - func(obj *JSONWebEncryption) (string, error) { return obj.FullSerialize(), nil }, - } - - for _, alg := range keyAlgs { - for _, enc := range encAlgs { - for _, testKey := range generateTestKeys(alg, enc) { - for _, serializer := range serializers { - kew := makeOpaqueKeyEncrypter(t, testKey.enc, alg, kid) - encrypter, err := NewEncrypter( - enc, - Recipient{ - Algorithm: alg, - Key: kew, - }, - &EncrypterOptions{}, - ) - if err != nil { - t.Fatal(err, alg) - } - - jwe, err := encrypter.Encrypt([]byte("foo bar")) - if err != nil { - t.Fatal(err, alg) - } - - dw := makeOpaqueKeyDecrypter(t, testKey.dec, alg) - jwe = jweSerialize(t, serializer, jwe, dw) - if jwe.Header.KeyID != kid { - t.Errorf("expected jwe kid to equal %s but got %s", kid, jwe.Header.KeyID) - } - - out, err := jwe.Decrypt(dw) - if err != nil { - t.Fatal(err, out) - } - if string(out) != "foo bar" { - t.Errorf("expected decrypted jwe to equal %s but got %s", "foo bar", string(out)) - } - } - } - } - } -} - func jweSerialize(t *testing.T, serializer func(*JSONWebEncryption) (string, error), jwe *JSONWebEncryption, d OpaqueKeyDecrypter) *JSONWebEncryption { b, err := serializer(jwe) if err != nil {