Sharing only the public key Is secure? - security

I need to secure a REST request knowing who is doing it. I know the HMAC method but I wouldn't like not store the client private key on my server. This is my approach:
The SERVER creates a private and public key for the client
The SERVER sends the private key and a client id to the client
The CLIENT stores its private key
The SERVER stores only the public client key
The CLIENT makes a RESTful request by encrypting its client id with its private key (ecryptedData) and sends the pair clientID:encryptedData to the SERVER
The SERVER looks for the public key of the given client id and decrypt the encryptedData
The SERVER checks if the decrypted data contains the same client id. If the client id is the same then the SERVER trusts on the sender otherwise it rejects the request.
Maybe this method already exists but I don't know it.
Is it secure this method?
EDIT
I reformulate the question:
if I care only about who is the sender and not about what he is sending in a monodirectional communication (CLIENT -> SERVER), can I use RSA in this way?
ONE TIME
SERVER creates the RSA pair key for the client
SERVER stores the client public key (I doesn't matter if the key is stolen..it's public!)
SERVER sends the client private key to the client
DURING THE CLIENT -> SERVER COMMUNICATION
CLIENT encrypts a known word + timestamp (to prevent replay attack) by its private key ex. SIGNATURE = encrypt(RSA, 'FOO:1234234')
CLIENT sends the message with the API KEY and singature ex. 54545345:SIGNATURE
SERVER looks up the public key of the given API KEY
SERVER decrypts the message with the public key found
SERVER checks the correctness of the known word FOO and the timestamp
SERVER rejects the message if the previous step fails
Is it secure this method?
Thanks a lot!
aGO!

"SERVER sends the client private key to the client": this does not seem very secure.
If malicious clients intercept this communication, they can get client's private-key and can send messages as if they were sent by the actual client.
You should let the client generate the two keys, without sharing the private one.

It's not secure to send a Private Key to a user. Please take a look at the next approach:
The Client uses your or open-source (3rd-party) SDK (or crypto library) to generate a Key Pair (Private and a Public Key) - if you need, I can share some cool solution;
The Client stores a Private Key into local storage (that key can be based on a Client password);
The Client sends a request to your Server that contains his Public Key and some additional data about the Client (his Identity, name/email/etc.). This request will be encrypted with a Public Key of your server and signed with Client's Private Key;
The Server decrypts the encrypted request, authenticates/registers the Client and his Public Key and signature. Here you should have some Public Key Management (Keys Service).
At communication step, the Server and the Client use their Private Keys to decrypt and sign data / Public Keys to encrypt data and verify signatures.

Related

JWT with RSA public and private key

I don't understand why I should use the public key in signing JWT. The private key is there so that the JWT token cannot be forged, yes? But why additionally sign it with a public key? Are there any benefits? Because I don't understand it at all. After all, a JWT signed with a private key can be read without the public key. What is this public key for?
Signing a JWT means you take the cleartext, signing it with a key - either the private key from an RSA pair or a symmetric key, then add the signature to the JWT. The JWT itself is still readable without decrtypting the signature. But someone with the key can decrypt the signature and confirm the contents match the cleartext.
The advantage of using RSA over symmetric key is that anyone can verify the signature without them having to have a secret key. You can either pass the public key to the JWT recipient over a side channel, or if using OAuth2 it provides a URL to access public keys.
You would use the public key for encrypting, not signing. You encrypt with the recipient's public key so that only the recipient can decrypt it.

NodeJS SubtleCrypto - Multiple keyUsage for RSA Keys

I'm so sorry for the ambiguous title of this question, i'm not really sure how to phrase this.
I've generated a Public and Private key using SubtleCrypto in NodeJS like so:
const { publicKey, privateKey } = await subtle.generateKey({
name: 'RSA-OAEP',
4096,
new Uint8Array([1, 0, 1]),
'SHA-256',
}, true, ['encrypt', 'decrypt']);
And this works perfectly for one use case:
Public Key to Encrypt, Private Key to Decrypt.
However, the way I wish to implement RSA in my project is as such:
Client asks Server for a Public Key
Client encrypts payload using Public Key
Server decrypts payload using Private Key
Server encrypts response payload using Private Key
Client decrypts response payload using Public Key
When I try to perform Step 4, i encountered this error:
The requested operation is not valid for the provided key
Is there a way to specify that each key could be used for Encrypt & Decrypt?
Also if my implementation is completely wrong, i'm sorry for that.
Step 4, as described by you, is a signing operation. Signing is very different from encrypting data. For this to work, both, client and server would need their own keypair:
client: client public key & client private key
server: server public key & server private key
Client uploads its client public key
Client asks server for the server public key
Client encrypts payload using the server public key
Server decrypts payload using the server private key
Server encrypts response payload using client public key
Client decrypts response payload using client private key
Besides the possible huge computational workload of encrypting and decrypting large amount of data using RSA, what is the threat model here and what do you want to achieve? You should be very careful if you really want to deploy this into production, as you seem to be rather inexperienced with this topic (no offense here).

ADFS sso and using passport-saml Express "No decryption key for encrypted SAML response"

I am having this issue while logging ADFS SSO. "No decryption key for encrypted SAML response".
Login from another account is being successful. Someone can help me with this. I am using Express with passport-saml work.
This is code snap where I am stuck.
node_modules/passport-saml/lib/passport-saml/saml.js in null. at line 623:15
if (encryptedAssertions.length == 1) {
if (!self.options.decryptionPvk)
throw new Error('No decryption key for encrypted SAML response');
var encryptedAssertionXml = encryptedAssertions[0].toString();
How encryption works in SAML: identity provider encrypts some elements of the SAML response with service provider's public key. The service provider decrypts using the private key that corresponds to the public key used to encrypt. In other words, the service provider needs to own a keypair - private key and public key - for this use case to work.
The private key of the aforementioned keypair needs to be configured via decryptionPvk parameter in passport-saml. Since the assertion is encrypted yet no private key is found in decryptionPvk, passport-saml complains.
Either remove the encryption on ADFS side or provide the private key to passport-saml.

Why google Oauth verifyIdToken (javascript nodejs version) doesn't use client-secret?

I'm testing google singin for a SPA js+nodejs app.
I've added this:
<script src="https://apis.google.com/js/platform.js" async defer></script>
and these:
<meta name="google-signin-client_id" content="YOUR_CLIENT_ID.apps.googleusercontent.com">
<div class="g-signin2" data-onsuccess="onSignIn"></div>
in html5/js client side.
following this guide:
https://developers.google.com/identity/sign-in/web/sign-in
when the users authenticate the library gets the token and pass it to the server as explained here:
https://developers.google.com/identity/sign-in/web/backend-auth
on server side (nodejs) the token is verified using this function:
client.verifyIdToken(
token,
CLIENT_ID,
// Or, if multiple clients access the backend:
//[CLIENT_ID_1, CLIENT_ID_2, CLIENT_ID_3],
function(e, login) {
var payload = login.getPayload();
var userid = payload['sub'];
// If request specified a G Suite domain:
//var domain = payload['hd'];
});
MY QUESTION IS: when is the client_secret used? as I've used CLIENT_ID front end to get the auth token from google then I've used CLIENT_ID server side for token verification. I thought that the token could have been verified using client_secret (that is SECRET) known only server side so that no one else getting the token can auth that user.
What am I missing?
It appears the Client you have created is a Public client , The Client Secret is used in a Private Client .
Edit : I am sorry I used the term private client instead of Confidential client .
Basically we have 2 types of clients in Oauth2
Public Clients :- These are clients which don't need a client secret .
Private Clients :- These clients have a Client secret .
I cannot give you a very certain answer as to why you do not get to see your client-secret as I have not worked with these specific libraries before , however
it seems to me that may be you had a created a public client instead of a Confidential one .
I believe I have the answer,
See here: https://firebase.google.com/docs/auth/admin/verify-id-tokens
It explains how to check the signature of the JWT on your own (if you wanted) and this is also what the google-auth-library is doing. Inside the library, search for verifySignedJwtWithCertsAsync() inside of oauth2client.js. Google handles the signing of the JWT using their own private key during the federated sign in process. By the time the JWT is returned to you and sent to the auth library, it's already been signed. This is great because you never have to touch a private key. It's securely stored at Google, you just let Google handle that part. Then, when you send the JWT up to your server, the key id claim in the header lets the auth library know which public key to use to decode it. If the public key fails to decode it, then the authentication has failed.
Finally, ensure that the ID token was signed by the private key corresponding to the token's kid claim. Grab the public key from https://www.googleapis.com/robot/v1/metadata/x509/securetoken#system.gserviceaccount.com and use a JWT library to verify the signature. Use the value of max-age in the Cache-Control header of the response from that endpoint to know when to refresh the public keys.
If all the above verifications are successful, you can use the subject (sub) of the ID token as the uid of the corresponding user or device.

Hot to verify a public key's extensions before importing it to GnuPG?

How do I verify a user's extended public key file's integrity (when downloading through a connection that lacks confidentiality and authentication) when I have their previous (now expired) public key in my keyring? Is their expired key sufficient information to verify the extended key? Consider the below scenario:
I have Bob's trusted public key in my keyring.
Bob's key expired yesterday, so he extended his keypair and uploaded a new ascii-armoured public key to his website.
I downloaded Bob's new public key file over http, and I want to verify it.
Is the new public key file signed with his old key in a verifiable way? How would I verify the integrity of the new key file utilizing his existing (expired) key in my keyring?
For a general scenario with a new key pair: If either the key itself is signed by his old key (this is the usual way to do such key changes) and/or the key file you downloaded is signed by his old key, you can verify and validate the signature anyway: all that happens is GnuPG indicating that the key already expired.
But you wrote
Bob's key expired yesterday, so he extended his keypair and uploaded a new ascii-armoured public key to his website
Extending the key's validity does not produce a different key. They key is identified by the tuple of public key and creation timestamp, which is hashed together to the fingerprint of the key. Short and long key IDs are derived from that. If all he did is indeed extend the validity of the key, simply import the key. The signature and trust you issued on that key are still valid.
If you wish you can compare at least the long key ID before importing, run
gpg --keyid-format 0xlong [key-file]
and compare with the key already in your key chain.
Anyway: don't simply trust keys in your key chain, but use signatures and trust instead. Lots of mail clients automatically fetch keys to verify signatures, you might have fetched some (unvalidated) keys for reading signatures issued on other keys, ...

Resources