What's the meaning of the "kid" claim in a JWT token? - token

I generated a JWT and there are some claims which I understand well, but there is a claim called kid in header. Does anyone know what it means?
I generated the token using auth0.com

kid is an optional header claim which holds a key identifier, particularly useful when you have multiple keys to sign the tokens and you need to look up the right one to verify the signature.
Once a signed JWT is a JWS, consider the definition from the RFC 7515:
4.1.4. "kid" (Key ID) Header Parameter
The kid (key ID) Header Parameter is a hint indicating which key
was used to secure the JWS. This parameter allows originators to
explicitly signal a change of key to recipients. The structure of the
kid value is unspecified. Its value MUST be a case-sensitive
string. Use of this Header Parameter is OPTIONAL.
When used with a JWK, the kid value is used to match a JWK kid
parameter value.

The kid (key ID) claim is an optional header claim, used to specify the key for validating the signature.
It is described here: http://self-issued.info/docs/draft-jones-json-web-token-01.html#ReservedHeaderParameterName

Related

Are encrypted strings URL Safe?

I have a need to encrypt a part of the URL e.g https://www.example.com/resource/{resourceid}.
The resourceid is the element that I want to encyrpt as it may be a sensitive piece of information.
Before returning this URL to the clients, we want to encrypt the resourceid and on the way back (from the client on a subsequent request) we will decrypt the resourceid before processing.
The original resourceid is a string (alphanumeric characters). They are not super-secret, the intent is to make them an "opaque" string before returning them to the caller.
I am unable to figure out if the result of an encryption (symmetric key is what I am after) is URL safe? I can encrypt the resource ID and then do a URL encoding on top and do the reverse on the way back.
However, if the encryption outcome is already URL safe, i won't need to worry about it. I am unable to locate any information that states whether the outcome of an encryption function is URL safe or not.
Any help or pointers would be helpful.

OAuth/OpenID Connect, how do I select a key from a JWK if the token doesn't have a kid in it's header?

Say I have two keys in my JWK but the token I am trying to verify does not have a kid in it's header. How do I determine which key to use?
There may be other methods in the token to identify the key such as a thumbmprint (x5t in a JWT) or the public key itself (if you can compare it to something exchanged out of band) or a URL where the key material can be retrieved (jwks_uri if exchanged out of band). If there's no such reference at all you may loop over all the keys that you know are valid keys for the sender and see if one of them can be used to verify the token.
The JWT token will contain 'alg' header and similar the JWKS has 'use' and 'alg' parameters. Instead of looping all the keys in the JWKS, you can compare JWT 'alg' header and JWKS alg to find out which key to use. In case if you have more than one key which matches both 'use' and 'alg' parameters in JWKS, loop only these matching keys to verify the token.

Add custom key/value to JWT token payload or user with keycloak

I have keycloak running in localhost.
I want to
add a key/value pair to the token payload
or add a key/value pair related to the user (payload again)
Can you suggest me a way to do this and a way to verify that it has been added?
(I guess with https://jwt.io/)
As mentioned above by Sébastien, a mapper should be added. So I have added a mapper user attribute and then I went to users->attributes and added the same key name with its value. I verified it afterwards, the key/value pair is included in the payload

Given an public key of type CKK_EC, is it possible to find the matching private key using C_FindObjects?

I have a serialized EC public key - its CKA_EC_PARAMS and CKA_EC_POINT. There's a matching private key on my token. Is there any way to find it?
With an RSA key, I can do a FindObjects with CKA_KEY_TYPE=CKK_PRIVATE_KEY and CKA_MODULUS=. Is there a way to do the same thing with EC keys? According to the PKCS#11 spec, CKA_EC_POINT isn't an attribute for EC Private Keys.
I have a token with support for EC at hand, and it seems that the only way to associate the private and public key will be through the CKA_ID value. No attribute available to test directly the key value.
Actually, even in the case of RSA that's the basic standard method to associate a private and a public key, they ought to be created with identical CKA_ID (that's what the Netscape browser originally did, and everyone copied on that).
They are even some buggy pkcs#11 implementations that won't allow you to read the CKA_MODULUS value of a RSA private key (this is definitevely a bug since the spec explicitly says this value ought to always be public, but it's just one of many bad things frequently happpening with pkcs#11). With them, CKA_ID is the only way even for RSA.

WinAPI -> CryptoAPI -> RSA, encrypt with private, decrypt with public

Good day.
I need to teach Windows CryptoAPI to encrypt the message with private (not public) part of the key, and decrypt with public. This is necessary to give users information, that they can read, but can't change.
How it works now:
I get the context
CryptAcquireContext(#Prov, PAnsiChar(containerName), nil, PROV_RSA_FULL, 0)
generate a key pair
CryptGenKey(Prov, CALG_RSA_KEYX, CRYPT_EXPORTABLE, #key)
Encrypt (and the problem is here. "key" - a keypair, and the function uses its public part);
CryptEncrypt(key, 0, true, 0, #res[1], #strLen, buffSize)
Decrypt (the same problem here, it uses the private part of the key)
CryptDecrypt(key, 0, true, 0, #res[1], #buffSize)
Thank you for your attention / help.
Update
Yes, I could use a digital signature and other metods...
The problem is that I need to encrypt one database field and make sure that no one but me can change it. It will be possible to read this field only with the help of my program (till someone decompiles it and get public key). This could be done with symmetrical key and digital signatures, but then i will need to create another field and store another key and so on...
I do hope that we can somehow teach WIN API to do as I want. I know that i can do so with RSA, and I hope that somehow WinAPI supports this feature.
Strictly speaking, when "signing" a message:
the person with the private key decrypts the hash with their private key.
they then send that "decrypted" hash along with the message.
the receiver then encrypts the signature with the public key
If the "encrypted" hash matches the hash of the original message, you know the message has not been altered, and was sent by the person with the private key. The following pseudo-code represents the signing algorithm:
//Person with private key generating message and signature
originalHash = GenerateHashOfMessage(message);
signature = RsaDecrypt(originalHash, privateKey);
//Receiver validating signed message
hash = GenerateHashOfMessage(message);
originalHash = RsaEncrypt(signature, publicKey);
messageValid = (hash == originalHash);
This same mechanism can be used to accomplish what you want. Except you don't care about hashes, you just want to encrypt some (small) amount of data:
//Person with private key
cipherText = RsaDecrypt(plainText, privateKey);
//Person with public key
plainText = RsaEncrypt(cipherText, publicKey);
i'll leave the CryptoAPI calls as an excercise - since i'm still trying to figure out Microsoft's Crypto API.
Encrypting data with the private key and decrypting it with the public key isn't supported because anyone with the "published" public key could decrypt it. What's the value in encrypting it then?
If you want to verify that data hasn't been changed, you will want to sign the data instead. Signing encrypts a hash of the data with the private key. Look at the signing functions.
You may be able to trick out the signing functions to do what you want. I've done this with other implementations, but I haven't tried with the Microsoft CryptoAPI.
Also, note that with RSA encryption, the plain text message cannot be longer than the key. So, if you are using a 2048 bit key, you can only encrypt a message body of up to 256 bytes (minus a few for overhead).
Consider using asymmetric encryption just to pass a symmetric key, and use the symmetric key to encrypt and decrypt any size data.
Update
You may be able to use the CryptSignHash() function for this. Normally, this is used to "sign" a hash, but you can put any data you want into the hash:
Set the hash value in the hash object by using the HP_HASHVAL value of
the dwParam parameter in CryptSetHashParam.
You might be limited to so many bytes if the input is expected to be a SHA1 hash value.
Alternatively, you may wish to consider using OpenSSL. If I recall correctly, it's pretty straight forward to use its RSA signing functions to encrypt with the private key.
Also, I accomplished the same thing using the old (freeware) version of SecureBlackbox. You may be able to find the old free version, but it's not Unicode friendly, so you'll have some conversion to do if you're using a new Delphi. I've done this in the past also, so it's not too difficult.
You may also consider trying out the current SecureBlackbox and purchase it if it works for you.
Otherwise, as you stated, sign it to detect tampering, and encrypt it with a symmetric key that only the program knows in order to obfuscate it.
If they crack your code, anything's fair game anyway.

Resources