1

I have a JWE and I want to decrypt the Content Encryption Key (cek) with openssl (and other command line)

here my JWE:

eyJlcGsiOnsia3R5IjoiRUMiLCJjcnYiOiJQLTI1NiIsIngiOiJHQ2wtLWxRSGI3TktZVTNqWHBLVklfQllhVGxBTFQ1SkZQZGwzc2JCOW1ZIiwieSI6IkFEUlgyNVBCU2xaSkU3OWRyRVQwQVJ0UnFaQWtVSU1OdDlhYTJiYmpCWVkifSwiZW5jIjoiQTEyOEdDTSIsImFsZyI6IkVDREgtRVMrQTEyOEtXIn0.IFiUOn11TxTgnYWCA2rJLTYV3_r2n_qW.[IV].[Payload].[Sign]

So now I have retrieved this elements:

The Ephemeral Public Key (EPK):

-----BEGIN PUBLIC KEY-----
MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEGCl++lQHb7NKYU3jXpKVI/BYaTlA
LT5JFPdl3sbB9mYANFfbk8FKVkkTv12sRPQBG1GpkCRQgw231prZtuMFhg==
-----END PUBLIC KEY-----

The private key:

-----BEGIN EC PRIVATE KEY-----
MEECAQAwEwYHKoZIzj0CAQYIKoZIzj0DAQcEJzAlAgEBBCC0pi7An729X2hjD01f
TwwrlDzFIZ1XaNpJjqM01hyVuA==
-----END EC PRIVATE KEY-----

The shared key from private key and the EPK (base 64):

5gMHliQ850OswmbhgSK/QR6QU43sTGfXxbQDPhgK0Qw=

The Key Encryption Key (kek) in base 64:

jy5VPMvuVJTxTwtf1rhUKA==

The JWE header decrypted:

{
  "epk": {
    "kty": "EC",
    "crv": "P-256",
    "x": "GCl--lQHb7NKYU3jXpKVI_BYaTlALT5JFPdl3sbB9mY", 
    "y": "ADRX25PBSlZJE79drET0ARtRqZAkUIMNt9aa2bbjBYY"
  },
  "enc": "A128GCM",
  "alg": "ECDH-ES+A128KW"
}

Now I try to decrypt the CEK: IFiUOn11TxTgnYWCA2rJLTYV3_r2n_qW . But I have some errors.

here the command:

openssl enc -nosalt -aes128-wrap -base64 -k $(base64 kek) -d -in encrypted-cek.64 -out cek

with base64 kek equals to jy5VPMvuVJTxTwtf1rhUKA==

and encrypted-cek.64 contains IFiUOn11TxTgnYWCA2rJLTYV3/r2n/qW (I have converted the cek from base64url to base64)

Here the error:

Error setting cipher id-aes128-wrap
140295367438784:error:0607B0AA:digital envelope routines:EVP_CipherInit_ex:wrap mode not allowed:../crypto/evp/evp_enc.c:160:

What I have forgotten.

lecogiteur
  • 307
  • 1
  • 7
  • 16
  • 1
    (1) commandline `enc` normally does **password-based** encryption: the value given by any of `-k` `-kfile` `-pass` or typed to the prompt is **not the key** but a password which is processed by a PBKDF (Password Based Key Derivation Function) to produce the actual key (and where relevant also the IV). `enc` _can_ use a raw key specified _in hex_ with `-K` _uppercase_. (2) more seriously, as the error tells you, the EVP routines in their default mode, used by `enc`, do not accept keywrap algorithms. AFAICS you'll have to write a program to do these calls with CTX flag WRAP_ALLOW turned on. – dave_thompson_085 Apr 16 '19 at 06:48
  • I understand what you say. But there is really no way to do that in command line (normally I have the password). It's strange to must do a program for activate key wrapping. Is it possible without openssl. Is it exist already a program which o that – lecogiteur Apr 16 '19 at 14:54
  • 1
    I'm pretty sure _openssl_ commandline can't do it based on `grep WRAP_ALLOW apps/*.c` failing. There are at least thousands of programs and/or libraries that use openssl libs, and even more that do crypto other ways without openssl, and I can't speak for all of them. There are certainly many implementations of JW[TESK+=-@#$%&] and I'd expect plenty of them do ECDH-ES+A128KW + A128GCM -- but probably all at once, not in pieces you access separately. – dave_thompson_085 Apr 17 '19 at 07:26

0 Answers0