TestEncryptedKeyRefusesBlockCipher checks that an EncryptedKey naming a block cipher is refused, so that a captured content key cannot be nested inside an attacker's EncryptedKey to decrypt ciphertext of the attacker's choosing under it (a CBC padding oracle).
(t *testing.T)
| 29 | // inside an attacker's EncryptedKey to decrypt ciphertext of the attacker's |
| 30 | // choosing under it (a CBC padding oracle). |
| 31 | func TestEncryptedKeyRefusesBlockCipher(t *testing.T) { |
| 32 | saved := RandReader |
| 33 | RandReader = rand.Reader |
| 34 | t.Cleanup(func() { RandReader = saved }) |
| 35 | |
| 36 | cert, key := loadTestKeyPair(t) |
| 37 | e := OAEP() |
| 38 | e.BlockCipher = AES128CBC |
| 39 | legit, err := e.Encrypt(cert, []byte("<x/>"), nil) |
| 40 | assert.NilError(t, err) |
| 41 | |
| 42 | // The legitimate message decrypts. |
| 43 | _, err = Decrypt(key, legit) |
| 44 | assert.NilError(t, err) |
| 45 | |
| 46 | captured := legit.FindElement("./KeyInfo/EncryptedKey") |
| 47 | assert.Assert(t, captured != nil) |
| 48 | |
| 49 | attackerBlocks := base64.StdEncoding.EncodeToString(make([]byte, 32)) |
| 50 | |
| 51 | // An attacker's EncryptedKey (aes128-cbc) wrapping the captured key. |
| 52 | evilKey := etree.NewElement("xenc:EncryptedKey") |
| 53 | evilKey.CreateAttr("xmlns:xenc", "http://www.w3.org/2001/04/xmlenc#") |
| 54 | evilKey.CreateElement("xenc:EncryptionMethod").CreateAttr("Algorithm", AES128CBC.Algorithm()) |
| 55 | evilKey.CreateElement("ds:KeyInfo").AddChild(captured.Copy()) |
| 56 | evilKey.CreateElement("xenc:CipherData").CreateElement("xenc:CipherValue").SetText(attackerBlocks) |
| 57 | |
| 58 | t.Run("EncryptedKey passed to Decrypt", func(t *testing.T) { |
| 59 | _, err := Decrypt(key, evilKey.Copy()) |
| 60 | var target ErrKeyAlgorithmNotAllowed |
| 61 | assert.Assert(t, errors.As(err, &target), "got %v", err) |
| 62 | }) |
| 63 | |
| 64 | t.Run("EncryptedKey nested in EncryptedData", func(t *testing.T) { |
| 65 | for _, bc := range []BlockCipher{AES128CBC, AES128GCM} { |
| 66 | data := etree.NewElement("xenc:EncryptedData") |
| 67 | data.CreateAttr("xmlns:xenc", "http://www.w3.org/2001/04/xmlenc#") |
| 68 | data.CreateElement("xenc:EncryptionMethod").CreateAttr("Algorithm", bc.Algorithm()) |
| 69 | data.CreateElement("ds:KeyInfo").AddChild(evilKey.Copy()) |
| 70 | data.CreateElement("xenc:CipherData").CreateElement("xenc:CipherValue").SetText(attackerBlocks) |
| 71 | |
| 72 | _, err := Decrypt(key, data) |
| 73 | var target ErrKeyAlgorithmNotAllowed |
| 74 | assert.Assert(t, errors.As(err, &target), "%s: got %v", bc.Algorithm(), err) |
| 75 | } |
| 76 | }) |
| 77 | } |
nothing calls this directly
no test coverage detected