public static string EncodeBytes(byte[] payload, object key, JwsAlgorithm algorithm, IDictionary <string, object> extraHeaders = null) { if (payload == null) { throw new ArgumentNullException("payload"); } if (extraHeaders == null) { extraHeaders = new Dictionary <string, object>() { { "typ", "JWT" } }; } Dictionary <string, object> strs = new Dictionary <string, object>() { { "alg", JWT.JwsAlgorithms[algorithm] } }; Dictionaries.Append <string, object>(strs, extraHeaders); byte[] bytes = Encoding.UTF8.GetBytes(JWT.jsMapper.Serialize(strs)); byte[] numArray = Encoding.UTF8.GetBytes(Compact.Serialize(new byte[][] { bytes, payload })); //SHA256 sha = SHA256.Create(); //byte[] numArray1 = sha.ComputeHash(numArray); byte[] numArray1 = JWT.HashAlgorithms[algorithm].Sign(numArray, key); return(Compact.Serialize(new byte[][] { bytes, payload, numArray1 })); }
private static byte[] DecodeBytes(string token, object key = null, JwsAlgorithm?jwsAlg = null, JweAlgorithm?jweAlg = null, JweEncryption?jweEnc = null) { Ensure.IsNotEmpty(token, "Incoming token expected to be in compact serialization form, not empty, whitespace or null."); byte[][] parts = Compact.Parse(token); if (parts.Length == 5) //encrypted JWT { return(DecryptBytes(parts, key, jweAlg, jweEnc)); } else { //signed or plain JWT byte[] header = parts[0]; byte[] payload = parts[1]; byte[] signature = parts[2]; byte[] securedInput = Encoding.UTF8.GetBytes(Compact.Serialize(header, payload)); var headerData = jsMapper.Parse <Dictionary <string, object> >(Encoding.UTF8.GetString(header)); var algorithm = (string)headerData["alg"]; if (jwsAlg != null && (JwsAlgorithm)jwsAlg != GetHashAlgorithm(algorithm)) { throw new InvalidAlgorithmException("The algorithm type passed to the Decode method did not match the algorithm type in the header."); } if (!HashAlgorithms[GetHashAlgorithm(algorithm)].Verify(signature, securedInput, key)) { throw new IntegrityException("Invalid signature."); } return(payload); } }
private static byte[] DecryptBytes(byte[][] parts, object key, JweAlgorithm?jweAlg, JweEncryption?jweEnc) { byte[] numArray = parts[0]; byte[] numArray1 = parts[1]; byte[] numArray2 = parts[2]; byte[] numArray3 = parts[3]; byte[] numArray4 = parts[4]; IDictionary <string, object> strs = JWT.jsMapper.Parse <Dictionary <string, object> >(Encoding.UTF8.GetString(numArray)); IKeyManagement item = JWT.KeyAlgorithms[JWT.GetJweAlgorithm((string)strs["alg"])]; IJweAlgorithm jweAlgorithm = JWT.EncAlgorithms[JWT.GetJweEncryption((string)strs["enc"])]; if (jweAlg.HasValue && jweAlg.Value != JWT.GetJweAlgorithm((string)strs["alg"])) { throw new InvalidAlgorithmException("The algorithm type passed to the Decrypt method did not match the algorithm type in the header."); } if (jweEnc.HasValue && jweEnc.Value != JWT.GetJweEncryption((string)strs["enc"])) { throw new InvalidAlgorithmException("The encryption type passed to the Decrypt method did not match the encryption type in the header."); } byte[] numArray5 = item.Unwrap(numArray1, key, jweAlgorithm.KeySize, strs); byte[] bytes = Encoding.UTF8.GetBytes(Compact.Serialize(new byte[][] { numArray })); byte[] numArray6 = jweAlgorithm.Decrypt(bytes, numArray5, numArray2, numArray3, numArray4); if (strs.ContainsKey("zip")) { numArray6 = JWT.CompressionAlgorithms[JWT.GetJweCompression((string)strs["zip"])].Decompress(numArray6); } return(numArray6); }
/// <summary> /// Encodes given json string to JWT token and sign it using given algorithm. /// </summary> /// <param name="payload">json string to encode (not null or whitespace)</param> /// <param name="key">key for signing, suitable for provided JWS algorithm, can be null.</param> /// <returns>JWT in compact serialization form, digitally signed.</returns> public static string Encode(string payload, object key, JwsAlgorithm algorithm, IDictionary <string, object> extraHeaders = null) { Ensure.IsNotEmpty(payload, "Payload expected to be not empty, whitespace or null."); if (extraHeaders == null) //allow overload, but keep backward compatible defaults { extraHeaders = new Dictionary <string, object> { { "typ", "JWT" } }; } var jwtHeader = new Dictionary <string, object> { { "alg", JwsAlgorithms[algorithm] } }; Dictionaries.Append(jwtHeader, extraHeaders); byte[] headerBytes = Encoding.UTF8.GetBytes(jsMapper.Serialize(jwtHeader)); byte[] payloadBytes = Encoding.UTF8.GetBytes(payload); var bytesToSign = Encoding.UTF8.GetBytes(Compact.Serialize(headerBytes, payloadBytes)); byte[] signature = HashAlgorithms[algorithm].Sign(bytesToSign, key); return(Compact.Serialize(headerBytes, payloadBytes, signature)); }
/// <summary> /// Encodes given binary data to JWT token and sign it using given algorithm. /// </summary> /// <param name="payload">Binary data to encode (not null)</param> /// <param name="key">key for signing, suitable for provided JWS algorithm, can be null.</param> /// <returns>JWT in compact serialization form, digitally signed.</returns> public static string EncodeBytes(byte[] payload, object key, JwsAlgorithm algorithm, IDictionary <string, object> extraHeaders = null) { if (payload == null) { throw new ArgumentNullException(nameof(payload)); } if (extraHeaders == null) //allow overload, but keep backward compatible defaults { extraHeaders = new Dictionary <string, object> { { "typ", "JWT" } }; } var jwtHeader = new Dictionary <string, object> { { "alg", JwsAlgorithms[algorithm] } }; Dictionaries.Append(jwtHeader, extraHeaders); byte[] headerBytes = Encoding.UTF8.GetBytes(jsMapper.Serialize(jwtHeader)); var bytesToSign = Encoding.UTF8.GetBytes(Compact.Serialize(headerBytes, payload)); byte[] signature = HashAlgorithms[algorithm].Sign(bytesToSign, key); return(Compact.Serialize(headerBytes, payload, signature)); }
public static string EncodeBytes(byte[] payload, object key, JweAlgorithm alg, JweEncryption enc, JweCompression?compression = null, IDictionary <string, object> extraHeaders = null) { if (payload == null) { throw new ArgumentNullException("payload"); } IKeyManagement item = JWT.KeyAlgorithms[alg]; IJweAlgorithm jweAlgorithm = JWT.EncAlgorithms[enc]; IDictionary <string, object> strs = new Dictionary <string, object>() { { "alg", JWT.JweAlgorithms[alg] }, { "enc", JWT.JweEncryptionMethods[enc] } }; Dictionaries.Append <string, object>(strs, extraHeaders); byte[][] numArray = item.WrapNewKey(jweAlgorithm.KeySize, key, strs); byte[] numArray1 = numArray[0]; byte[] numArray2 = numArray[1]; if (compression.HasValue) { strs["zip"] = JWT.JweCompressionMethods[compression.Value]; payload = JWT.CompressionAlgorithms[compression.Value].Compress(payload); } byte[] bytes = Encoding.UTF8.GetBytes(JWT.jsMapper.Serialize(strs)); byte[] bytes1 = Encoding.UTF8.GetBytes(Compact.Serialize(new byte[][] { bytes })); byte[][] numArray3 = jweAlgorithm.Encrypt(bytes1, payload, numArray1); return(Compact.Serialize(new byte[][] { bytes, numArray2, numArray3[0], numArray3[1], numArray3[2] })); }
private static JweToken ParseCompact(string jwe) { var parts = Compact.Iterate(jwe); var protectedHeaderBytes = parts.Next(); byte[] encryptedCek = parts.Next(); var iv = parts.Next(); var ciphertext = parts.Next(); var authTag = parts.Next(); var recipients = new List <JweRecipient>(); recipients.Add(new JweRecipient(encryptedCek, new Dictionary <string, object>())); return(new JweToken( protectedHeaderBytes: protectedHeaderBytes, unprotectedHeader: null, recipients: recipients, aad: null, iv: iv, ciphertext: ciphertext, authTag: authTag, encoding: SerializationMode.Compact)); }
/// <summary> /// Encodes given binary data to JWT token and applies requested encryption/compression algorithms. /// </summary> /// <param name="payload">Binary data to encode (not null)</param> /// <param name="key">key for encryption, suitable for provided JWS algorithm, can be null.</param> /// <returns>JWT in compact serialization form, encrypted and/or compressed.</returns> public static string EncodeBytes(byte[] payload, object key, JweAlgorithm alg, JweEncryption enc, JweCompression?compression = null, IDictionary <string, object> extraHeaders = null) { if (payload == null) { throw new ArgumentNullException(nameof(payload)); } IKeyManagement keys = KeyAlgorithms[alg]; IJweAlgorithm _enc = EncAlgorithms[enc]; IDictionary <string, object> jwtHeader = new Dictionary <string, object> { { "alg", JweAlgorithms[alg] }, { "enc", JweEncryptionMethods[enc] } }; Dictionaries.Append(jwtHeader, extraHeaders); byte[][] contentKeys = keys.WrapNewKey(_enc.KeySize, key, jwtHeader); byte[] cek = contentKeys[0]; byte[] encryptedCek = contentKeys[1]; if (compression.HasValue) { jwtHeader["zip"] = JweCompressionMethods[compression.Value]; payload = CompressionAlgorithms[compression.Value].Compress(payload); } byte[] header = Encoding.UTF8.GetBytes(jsMapper.Serialize(jwtHeader)); byte[] aad = Encoding.UTF8.GetBytes(Compact.Serialize(header)); byte[][] encParts = _enc.Encrypt(aad, payload, cek); return(Compact.Serialize(header, encryptedCek, encParts[0], encParts[1], encParts[2])); }
/// <summary> /// Encodes given json string to JWT token and applies requested encryption/compression algorithms. /// Json string to encode will be obtained via configured IJsonMapper implementation. /// </summary> /// <param name="payload">json string to encode (not null or whitespace)</param> /// <param name="key">key for encryption, suitable for provided JWS algorithm, can be null.</param> /// <returns>JWT in compact serialization form, encrypted and/or compressed.</returns> public static string Encode(string payload, object key, JweAlgorithm alg, JweEncryption enc, JweCompression?compression = null) { Ensure.IsNotEmpty(payload, "Payload expected to be not empty, whitespace or null."); IKeyManagement keys = KeyAlgorithms[alg]; IJweAlgorithm _enc = EncAlgorithms[enc]; var jwtHeader = new Dictionary <string, object> { { "alg", JweAlgorithms[alg] }, { "enc", JweEncryptionMethods[enc] } }; byte[][] contentKeys = keys.WrapNewKey(_enc.KeySize, key, jwtHeader); byte[] cek = contentKeys[0]; byte[] encryptedCek = contentKeys[1]; byte[] plainText = Encoding.UTF8.GetBytes(payload); if (compression.HasValue) { jwtHeader["zip"] = JweCompressionMethods[compression.Value]; plainText = CompressionAlgorithms[compression.Value].Compress(plainText); } byte[] header = Encoding.UTF8.GetBytes(jsMapper.Serialize(jwtHeader)); byte[] aad = Encoding.UTF8.GetBytes(Compact.Serialize(header)); byte[][] encParts = _enc.Encrypt(aad, plainText, cek); return(Compact.Serialize(header, encryptedCek, encParts[0], encParts[1], encParts[2])); }
public byte[] Unwrap(byte[] encryptedCek, object key, int cekSizeBits, IDictionary <string, object> header) { var sharedPassphrase = Ensure.Type <string>(key, "Pbse2HmacShaKeyManagementWithAesKeyWrap management algorithm expectes key to be string."); byte[] sharedKey = Encoding.UTF8.GetBytes(sharedPassphrase); Ensure.Contains(header, new[] { "p2c" }, "Pbse2HmacShaKeyManagementWithAesKeyWrap algorithm expects 'p2c' param in JWT header, but was not found"); Ensure.Contains(header, new[] { "p2s" }, "Pbse2HmacShaKeyManagementWithAesKeyWrap algorithm expects 'p2s' param in JWT header, but was not found"); byte[] algId = Encoding.UTF8.GetBytes((string)header["alg"]); int iterationCount = (int)header["p2c"]; byte[] saltInput = Compact.Base64UrlDecode((string)header["p2s"]); byte[] salt = Arrays.Concat(algId, Arrays.Zero, saltInput); byte[] kek; using (var prf = PRF) { kek = PBKDF2.DeriveKey(sharedKey, salt, iterationCount, keyLengthBits, prf); } return(aesKW.Unwrap(encryptedCek, kek, cekSizeBits, header)); }
/// <summary> /// Decodes JWT token by performining necessary decompression/decryption and signature verification as defined in JWT token header. /// Resulting json string is returned untouched (e.g. no parsing or mapping) /// </summary> /// <param name="token">JWT token in compact serialization form.</param> /// <param name="key">key for decoding suitable for JWT algorithm used, can be null.</param> /// <returns>decoded json string</returns> /// <exception cref="IntegrityException">if signature valdation failed</exception> /// <exception cref="EncryptionException">if JWT token can't be decrypted</exception> /// <exception cref="InvalidAlgorithmException">if JWT signature,encryption or compression algorithm is not supported</exception> public static string Decode(string token, object key = null) { Ensure.IsNotEmpty(token, "Incoming token expected to be in compact serialization form, not empty, whitespace or null."); byte[][] parts = Compact.Parse(token); string json; if (parts.Length == 5) //encrypted JWT { json = Decrypt(parts, key); } else { //signed or plain JWT byte[] header = parts[0]; byte[] payload = parts[1]; byte[] signature = parts[2]; byte[] securedInput = Encoding.UTF8.GetBytes(Compact.Serialize(header, payload)); var headerData = jsMapper.Parse <Dictionary <string, object> >(Encoding.UTF8.GetString(header, 0, payload.Length)); var algorithm = (string)headerData["alg"]; if (!HashAlgorithms[GetHashAlgorithm(algorithm)].Verify(signature, securedInput, key)) { throw new IntegrityException("Invalid signature."); } json = Encoding.UTF8.GetString(payload, 0, payload.Length); } return(json); }
public byte[][] WrapNewKey(int cekSizeBits, object key, IDictionary <string, object> header) { var sharedPassphrase = Ensure.Type <string>(key, "Pbse2HmacShaKeyManagementWithAesKeyWrap management algorithm expectes key to be string."); byte[] sharedKey = Encoding.UTF8.GetBytes(sharedPassphrase); byte[] algId = Encoding.UTF8.GetBytes((string)header["alg"]); int iterationCount = 8192; byte[] saltInput = Arrays.Random(96); //12 bytes header["p2c"] = iterationCount; header["p2s"] = Compact.Base64UrlEncode(saltInput); byte[] salt = Arrays.Concat(algId, Arrays.Zero, saltInput); byte[] kek; using (var prf = PRF) { kek = PBKDF2.DeriveKey(sharedKey, salt, iterationCount, keyLengthBits, prf); } return(aesKW.WrapNewKey(cekSizeBits, kek, header)); }
/// <summary> /// Encodes given binary data to JWT token and sign it using given algorithm. /// </summary> /// <param name="payload">Binary data to encode (not null)</param> /// <param name="key">key for signing, suitable for provided JWS algorithm, can be null.</param> /// <param name="settings">optional settings to override global DefaultSettings</param> /// <returns>JWT in compact serialization form, digitally signed.</returns> public static string EncodeBytes(byte[] payload, object key, JwsAlgorithm algorithm, IDictionary <string, object> extraHeaders = null, JwtSettings settings = null) { if (payload == null) { throw new ArgumentNullException(nameof(payload)); } if (extraHeaders == null) //allow overload, but keep backward compatible defaults { extraHeaders = new Dictionary <string, object> { { "typ", "JWT" } }; } var jwtHeader = new Dictionary <string, object> { { "alg", JwsAlgorithms[algorithm] } }; Dictionaries.Append(jwtHeader, extraHeaders); byte[] headerBytes = Encoding.UTF8.GetBytes(GetSettings(settings).JsonMapper.Serialize(jwtHeader)); var bytesToSign = Encoding.UTF8.GetBytes(Compact.Serialize(headerBytes, payload)); var jwsAlgorithm = GetSettings(settings).Jws(algorithm); if (jwsAlgorithm == null) { throw new JoseException(string.Format("Unsupported JWS algorithm requested: {0}", algorithm)); } byte[] signature = jwsAlgorithm.Sign(bytesToSign, key); return(Compact.Serialize(headerBytes, payload, signature)); }
private static string Decrypt(byte[][] parts, object key) { byte[] header = parts[0]; byte[] encryptedCek = parts[1]; byte[] iv = parts[2]; byte[] cipherText = parts[3]; byte[] authTag = parts[4]; IDictionary <string, object> jwtHeader = jsMapper.Parse <Dictionary <string, object> >(Encoding.UTF8.GetString(header, 0, header.Length)); IKeyManagement keys = KeyAlgorithms[GetJweAlgorithm((string)jwtHeader["alg"])]; IJweAlgorithm enc = EncAlgorithms[GetJweEncryption((string)jwtHeader["enc"])]; byte[] cek = keys.Unwrap(encryptedCek, key, enc.KeySize, jwtHeader); byte[] aad = Encoding.UTF8.GetBytes(Compact.Serialize(header)); byte[] plainText = enc.Decrypt(aad, cek, iv, cipherText, authTag); if (jwtHeader.ContainsKey("zip")) { plainText = CompressionAlgorithms[GetJweCompression((string)jwtHeader["zip"])].Decompress(plainText); } return(Encoding.UTF8.GetString(plainText, 0, plainText.Length)); }
private static byte[] DecryptBytes(byte[][] parts, object key, JweAlgorithm?jweAlg, JweEncryption?jweEnc) { byte[] header = parts[0]; byte[] encryptedCek = parts[1]; byte[] iv = parts[2]; byte[] cipherText = parts[3]; byte[] authTag = parts[4]; IDictionary <string, object> jwtHeader = jsMapper.Parse <Dictionary <string, object> >(Encoding.UTF8.GetString(header)); IKeyManagement keys = KeyAlgorithms[GetJweAlgorithm((string)jwtHeader["alg"])]; IJweAlgorithm enc = EncAlgorithms[GetJweEncryption((string)jwtHeader["enc"])]; if (jweAlg != null && (JweAlgorithm)jweAlg != GetJweAlgorithm((string)jwtHeader["alg"])) { throw new InvalidAlgorithmException("The algorithm type passed to the Decrypt method did not match the algorithm type in the header."); } if (jweEnc != null && (JweEncryption)jweEnc != GetJweEncryption((string)jwtHeader["enc"])) { throw new InvalidAlgorithmException("The encryption type passed to the Decrypt method did not match the encryption type in the header."); } byte[] cek = keys.Unwrap(encryptedCek, key, enc.KeySize, jwtHeader); byte[] aad = Encoding.UTF8.GetBytes(Compact.Serialize(header)); byte[] plainText = enc.Decrypt(aad, cek, iv, cipherText, authTag); if (jwtHeader.ContainsKey("zip")) { plainText = CompressionAlgorithms[GetJweCompression((string)jwtHeader["zip"])].Decompress(plainText); } return(plainText); }
private static byte[] securedInput(byte[] header, byte[] payload, bool b64) { return(b64 ? Encoding.UTF8.GetBytes(Compact.Serialize(header, payload)) : Arrays.Concat(Encoding.UTF8.GetBytes(Compact.Serialize(header)), Encoding.UTF8.GetBytes("."), payload)); }
private static byte[] DecryptBytes(byte[][] parts, object key, JweAlgorithm?jweAlg, JweEncryption?jweEnc, JwtSettings settings = null) { byte[] header = parts[0]; byte[] encryptedCek = parts[1]; byte[] iv = parts[2]; byte[] cipherText = parts[3]; byte[] authTag = parts[4]; JwtSettings jwtSettings = GetSettings(settings); IDictionary <string, object> jwtHeader = jwtSettings.JsonMapper.Parse <Dictionary <string, object> >(Encoding.UTF8.GetString(header)); JweAlgorithm headerAlg = jwtSettings.JwaAlgorithmFromHeader((string)jwtHeader["alg"]); JweEncryption headerEnc = jwtSettings.JweAlgorithmFromHeader((string)jwtHeader["enc"]); IKeyManagement keys = jwtSettings.Jwa(headerAlg); IJweAlgorithm enc = jwtSettings.Jwe(headerEnc); if (keys == null) { throw new JoseException(string.Format("Unsupported JWA algorithm requested: {0}", headerAlg)); } if (enc == null) { throw new JoseException(string.Format("Unsupported JWE algorithm requested: {0}", headerEnc)); } if (jweAlg != null && (JweAlgorithm)jweAlg != headerAlg) { throw new InvalidAlgorithmException("The algorithm type passed to the Decrypt method did not match the algorithm type in the header."); } if (jweEnc != null && (JweEncryption)jweEnc != headerEnc) { throw new InvalidAlgorithmException("The encryption type passed to the Decrypt method did not match the encryption type in the header."); } byte[] cek = keys.Unwrap(encryptedCek, key, enc.KeySize, jwtHeader); byte[] aad = Encoding.UTF8.GetBytes(Compact.Serialize(header)); byte[] plainText = enc.Decrypt(aad, cek, iv, cipherText, authTag); if (jwtHeader.ContainsKey("zip")) { var alg = (string)jwtHeader["zip"]; var compression = jwtSettings.Compression(GetJweCompression(alg)); if (compression == null) { throw new JoseException(string.Format("Unsupported compressions algorithm requested: {0}", alg)); } plainText = compression.Decompress(plainText); } return(plainText); }
public static byte[] PayloadBytes(string token) { byte[][] numArray = Compact.Parse(token); if ((int)numArray.Length > 3) { throw new JoseException("Getting payload for encrypted tokens is not supported. Please use Jose.JWT.Decode() method instead."); } return(numArray[1]); }
private static byte[] DecodeBytes(string token, object key = null, JwsAlgorithm?expectedJwsAlg = null, JweAlgorithm?expectedJweAlg = null, JweEncryption?expectedJweEnc = null, JwtSettings settings = null, byte[] payload = null) { Ensure.IsNotEmpty(token, "Incoming token expected to be in compact serialization form, not empty, whitespace or null."); var parts = Compact.Iterate(token); if (parts.Count == 5) //encrypted JWT { return(JWE.Decrypt(token, key, expectedJweAlg, expectedJweEnc, settings).PlaintextBytes); } else { //signed or plain JWT var jwtSettings = GetSettings(settings); byte[] header = parts.Next(); var headerData = jwtSettings.JsonMapper.Parse <Dictionary <string, object> >(Encoding.UTF8.GetString(header)); bool b64 = true; object value; if (headerData.TryGetValue("b64", out value)) { b64 = (bool)value; } byte[] contentPayload = parts.Next(b64); byte[] signature = parts.Next(); var effectivePayload = payload ?? contentPayload; var algorithm = (string)headerData["alg"]; var jwsAlgorithm = jwtSettings.JwsAlgorithmFromHeader(algorithm); if (expectedJwsAlg != null && expectedJwsAlg != jwsAlgorithm) { throw new InvalidAlgorithmException( "The algorithm type passed to the Decode method did not match the algorithm type in the header."); } var jwsAlgorithmImpl = jwtSettings.Jws(jwsAlgorithm); if (jwsAlgorithmImpl == null) { throw new JoseException(string.Format("Unsupported JWS algorithm requested: {0}", algorithm)); } if (!jwsAlgorithmImpl.Verify(signature, securedInput(header, effectivePayload, b64), key)) { throw new IntegrityException("Invalid signature."); } return(effectivePayload); } }
/// <summary> /// Parses signed JWT token, extracts and returns payload part as string /// This method is NOT supported for encrypted JWT tokens. /// This method is NOT performing integrity checking. /// </summary> /// <param name="token">signed JWT token</param> /// <returns>unmarshalled payload</returns> /// <exception cref="JoseException">if encrypted JWT token is provided</exception> public static string Payload(string token) { byte[][] parts = Compact.Parse(token); if (parts.Length > 3) { throw new JoseException( "Getting payload for encrypted tokens is not supported. Please use Jose.JWT.Decode() method instead."); } return(Encoding.UTF8.GetString(parts[1])); }
/// <summary> /// Parses signed JWT token, extracts and returns payload part as binary data. /// This method is NOT supported for encrypted JWT tokens. /// This method is NOT performing integrity checking. /// </summary> /// <param name="token">signed JWT token</param> /// <param name="settings">optional settings to override global DefaultSettings</param> /// <returns>unmarshalled payload</returns> /// <exception cref="JoseException">if encrypted JWT token is provided</exception> public static byte[] PayloadBytes(string token, JwtSettings settings = null) { byte[][] parts = Compact.Parse(token); if (parts.Length > 3) { throw new JoseException( "Getting payload for encrypted tokens is not supported. Please use Jose.JWT.Decode() method instead."); } return(parts[1]); }
public byte[] Unwrap(byte[] encryptedCek, object key, int cekSizeBits, IDictionary <string, object> header) { byte[] sharedKey = Ensure.Type <byte[]>(key, "AesGcmKeyWrapManagement alg expectes key to be byte[] array."); Ensure.BitSize(sharedKey, keyLengthBits, string.Format("AesGcmKeyWrapManagement management algorithm expected key of size {0} bits, but was given {1} bits", keyLengthBits, sharedKey.Length * 8)); Ensure.Contains(header, new[] { "iv" }, "AesGcmKeyWrapManagement algorithm expects 'iv' param in JWT header, but was not found"); Ensure.Contains(header, new[] { "tag" }, "AesGcmKeyWrapManagement algorithm expects 'tag' param in JWT header, but was not found"); byte[] iv = Compact.Base64UrlDecode((string)header["iv"]); byte[] authTag = Compact.Base64UrlDecode((string)header["tag"]); return(AesGcm.Decrypt(sharedKey, iv, null, encryptedCek, authTag)); }
/// <summary> /// Serialize token according to serialization mode /// </summary> public string AsString(IJsonMapper mapper = null) { if (Encoding == SerializationMode.Compact) { return(Compact.Serialize(ProtectedHeaderBytes, Recipients[0].EncryptedCek, Iv, Ciphertext, AuthTag)); } var json = new Dictionary <string, object>() { { "ciphertext", Base64Url.Encode(Ciphertext) }, { "protected", Base64Url.Encode(ProtectedHeaderBytes) }, { "iv", Base64Url.Encode(Iv) }, { "tag", Base64Url.Encode(AuthTag) } }; if (Aad != null) { json["aad"] = Base64Url.Encode(Aad); } if (UnprotectedHeader != null) { json["unprotected"] = UnprotectedHeader; } if (Recipients.Count == 1) { json["header"] = Recipients[0].Header; json["encrypted_key"] = Base64Url.Encode(Recipients[0].EncryptedCek); } else { var recipientList = new List <object>(); foreach (var recipient in Recipients) { recipientList.Add( new Dictionary <string, object> { { "header", recipient.Header }, { "encrypted_key", Base64Url.Encode(recipient.EncryptedCek) } } ); } json["recipients"] = recipientList; } return(mapper.Serialize(json)); }
private static byte[] DecodeBytes(string token, object key = null, JwsAlgorithm?expectedJwsAlg = null, JweAlgorithm?expectedJweAlg = null, JweEncryption?expectedJweEnc = null, JwtSettings settings = null) { Ensure.IsNotEmpty(token, "Incoming token expected to be in compact serialization form, not empty, whitespace or null."); byte[][] parts = Compact.Parse(token); if (parts.Length == 5) //encrypted JWT { return(DecryptBytes(parts, key, expectedJweAlg, expectedJweEnc, settings)); } else { //signed or plain JWT byte[] header = parts[0]; byte[] payload = parts[1]; byte[] signature = parts[2]; string[] jwtSections = token.Split('.'); StringBuilder originalToken = new StringBuilder(); originalToken.Append(jwtSections[0]); originalToken.Append("."); originalToken.Append(jwtSections[1]); byte[] securedInput = Encoding.UTF8.GetBytes(originalToken.ToString()); var jwtSettings = GetSettings(settings); var headerData = jwtSettings.JsonMapper.Parse <Dictionary <string, object> >(Encoding.UTF8.GetString(header)); var algorithm = (string)headerData["alg"]; var jwsAlgorithm = jwtSettings.JwsAlgorithmFromHeader(algorithm); if (expectedJwsAlg != null && expectedJwsAlg != jwsAlgorithm) { throw new InvalidAlgorithmException( "The algorithm type passed to the Decode method did not match the algorithm type in the header."); } var jwsAlgorithmImpl = jwtSettings.Jws(jwsAlgorithm); if (jwsAlgorithmImpl == null) { throw new JoseException(string.Format("Unsupported JWS algorithm requested: {0}", algorithm)); } if (!jwsAlgorithmImpl.Verify(signature, securedInput, key)) { throw new IntegrityException("Invalid signature."); } return(payload); } }
public byte[][] WrapNewKey(int cekSizeBits, object key, IDictionary <string, object> header) { byte[] sharedKey = Ensure.Type <byte[]>(key, "AesGcmKeyWrapManagement alg expectes key to be byte[] array."); Ensure.BitSize(sharedKey, keyLengthBits, string.Format("AesGcmKeyWrapManagement management algorithm expected key of size {0} bits, but was given {1} bits", keyLengthBits, sharedKey.Length * 8)); byte[] iv = Arrays.Random(96); byte[] cek = Arrays.Random(cekSizeBits); byte[][] cipherAndTag = AesGcm.Encrypt(sharedKey, iv, null, cek); header["iv"] = Compact.Base64UrlEncode(iv); header["tag"] = Compact.Base64UrlEncode(cipherAndTag[1]); return(new[] { cek, cipherAndTag[0] }); }
/// <summary> /// Encodes given binary data to JWT token and sign it using given algorithm. /// </summary> /// <param name="payload">Binary data to encode (not null)</param> /// <param name="key">key for signing, suitable for provided JWS algorithm, can be null.</param> /// <param name="algorithm">JWT algorithm to be used.</param> /// <param name="extraHeaders">optional extra headers to pass along with the payload.</param> /// <param name="settings">optional settings to override global DefaultSettings</param> /// <param name="options">additional encoding options</param> /// <returns>JWT in compact serialization form, digitally signed.</returns> public static string EncodeBytes(byte[] payload, object key, JwsAlgorithm algorithm, IDictionary <string, object> extraHeaders = null, JwtSettings settings = null, JwtOptions options = null) { if (payload == null) { throw new ArgumentNullException(nameof(payload)); } var jwtSettings = GetSettings(settings); var jwtOptions = options ?? JwtOptions.Default; var jwtHeader = new Dictionary <string, object> { { "alg", jwtSettings.JwsHeaderValue(algorithm) } }; if (extraHeaders == null) //allow overload, but keep backward compatible defaults { extraHeaders = new Dictionary <string, object> { { "typ", "JWT" } }; } if (!jwtOptions.EncodePayload) { jwtHeader["b64"] = false; jwtHeader["crit"] = Collections.Union(new[] { "b64" }, Dictionaries.Get <object>(extraHeaders, "crit")); } Dictionaries.Append(jwtHeader, extraHeaders); byte[] headerBytes = Encoding.UTF8.GetBytes(jwtSettings.JsonMapper.Serialize(jwtHeader)); var jwsAlgorithm = jwtSettings.Jws(algorithm); if (jwsAlgorithm == null) { throw new JoseException(string.Format("Unsupported JWS algorithm requested: {0}", algorithm)); } byte[] signature = jwsAlgorithm.Sign(securedInput(headerBytes, payload, jwtOptions.EncodePayload), key); byte[] payloadBytes = jwtOptions.DetachPayload ? new byte[0] : payload; return(jwtOptions.EncodePayload ? Compact.Serialize(headerBytes, payloadBytes, signature) : Compact.Serialize(headerBytes, Encoding.UTF8.GetString(payloadBytes), signature)); }
public virtual byte[] NewKey(int keyLength, object key, IDictionary <string, object> header) { var recieverPubKey = Ensure.Type <CngKey>(key, "EcdhKeyManagement alg expects key to be of CngKey type."); EccKey ephemeral = EccKey.Generate(recieverPubKey); IDictionary <string, object> epk = new Dictionary <string, object>(); epk["kty"] = "EC"; epk["x"] = Compact.Base64UrlEncode(ephemeral.X); epk["y"] = Compact.Base64UrlEncode(ephemeral.Y); epk["crv"] = Curve(recieverPubKey); header["epk"] = epk; return(DeriveKey(header, keyLength, recieverPubKey, ephemeral.Key)); }
/// <summary> /// Encodes given json string to JWT token and sign it using given algorithm. /// </summary> /// <param name="payload">json string to encode (not null or whitespace)</param> /// <param name="key">key for signing, suitable for provided JWS algorithm, can be null.</param> /// <returns>JWT in compact serialization form, digitally signed.</returns> public static string Encode(string payload, object key, JwsAlgorithm algorithm) { Ensure.IsNotEmpty(payload, "Payload expected to be not empty, whitespace or null."); var jwtHeader = new Dictionary <string, object> { { "typ", "JWT" }, { "alg", JwsAlgorithms[algorithm] } }; byte[] headerBytes = Encoding.UTF8.GetBytes(jsMapper.Serialize(jwtHeader)); byte[] payloadBytes = Encoding.UTF8.GetBytes(payload); var bytesToSign = Encoding.UTF8.GetBytes(Compact.Serialize(headerBytes, payloadBytes)); byte[] signature = HashAlgorithms[algorithm].Sign(bytesToSign, key); return(Compact.Serialize(headerBytes, payloadBytes, signature)); }
/// <summary> /// Encodes given binary data to JWT token and applies requested encryption/compression algorithms. /// </summary> /// <param name="payload">Binary data to encode (not null)</param> /// <param name="key">key for encryption, suitable for provided JWS algorithm, can be null.</param> /// <param name="alg">JWT algorithm to be used.</param> /// <param name="enc">encryption algorithm to be used.</param> /// <param name="compression">optional compression type to use.</param> /// <param name="extraHeaders">optional extra headers to pass along with the payload.</param> /// <param name="settings">optional settings to override global DefaultSettings</param> /// <returns>JWT in compact serialization form, encrypted and/or compressed.</returns> public static string EncodeBytes(byte[] payload, object key, JweAlgorithm alg, JweEncryption enc, JweCompression?compression = null, IDictionary <string, object> extraHeaders = null, JwtSettings settings = null) { if (payload == null) { // https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/nameof // nameof is not defined prior to c# 6.0 spec // throw new ArgumentNullException(nameof(payload)); throw new ArgumentNullException("payload is null"); } JwtSettings jwtSettings = GetSettings(settings); IKeyManagement keys = jwtSettings.Jwa(alg); IJweAlgorithm _enc = jwtSettings.Jwe(enc); if (keys == null) { throw new JoseException(string.Format("Unsupported JWA algorithm requested: {0}", alg)); } if (_enc == null) { throw new JoseException(string.Format("Unsupported JWE algorithm requested: {0}", enc)); } IDictionary <string, object> jwtHeader = new Dictionary <string, object> { { "alg", jwtSettings.JwaHeaderValue(alg) }, { "enc", jwtSettings.JweHeaderValue(enc) } }; Dictionaries.Append(jwtHeader, extraHeaders); byte[][] contentKeys = keys.WrapNewKey(_enc.KeySize, key, jwtHeader); byte[] cek = contentKeys[0]; byte[] encryptedCek = contentKeys[1]; if (compression.HasValue) { jwtHeader["zip"] = jwtSettings.CompressionHeader(compression.Value); payload = jwtSettings.Compression(compression.Value).Compress(payload); } byte[] header = Encoding.UTF8.GetBytes(jwtSettings.JsonMapper.Serialize(jwtHeader)); byte[] aad = Encoding.UTF8.GetBytes(Compact.Serialize(header)); byte[][] encParts = _enc.Encrypt(aad, payload, cek); return(Compact.Serialize(header, encryptedCek, encParts[0], encParts[1], encParts[2])); }
/// <summary> /// Parses signed JWT token, extracts and returns payload part as binary data. /// This method is NOT supported for encrypted JWT tokens. /// This method is NOT performing integrity checking. /// </summary> /// <param name="token">signed JWT token</param> /// <param name="settings">optional settings to override global DefaultSettings</param> /// <returns>unmarshalled payload</returns> /// <exception cref="JoseException">if encrypted JWT token is provided</exception> public static byte[] PayloadBytes(string token, JwtSettings settings = null) { byte[][] parts = Compact.Parse(token); if (parts.Length < 3) { throw new JoseException( "The given token doesn't follow JWT format and must contains at least three parts."); } if (parts.Length > 3) { throw new JoseException( "Getting payload for encrypted tokens is not supported. Please use Jose.JWT.Decode() method instead."); } return(parts[1]); }