Add e-mail to CSR generated with keytool - ssl-certificate

As per this : http://en.wikipedia.org/wiki/Certificate_signing_request the CSR can contain an email address.
My question is how can I add such e-mail address field considering I`m generating the CSR with keytool ?
Thank you.

When generating the key pair you can pass the Distinguished Name as a parameter with the -dname flag and thus add the EMAILADDRESS attribute.
keytool -genkey -alias mydomain -keyalg RSA -keystore keystore.jks -keysize 2048 -dname "CN=Your Name, EMAILADDRESS=your.name#example.com, C=UK"

Related

Generate CSR without any extra attributes in distinguished name (DN) except common name and country

My domain provider only alowes me to set common name and country in CSR, but with keytool emty atributes will always be set to [Unknown].
Is there a poisibility to set these attributes emty?
Or can i generate CSR for glassfish with Openssl?
There is an option to supply the DN value as you wish, instead of the keytool constructing it for you.
You could use the -dname option, like this:
keytool -genkeypair -keystore test.jks -storepass password -dname 'CN=localhost,C=US' -keyalg rsa -keysize 2048

x509 certificate subject DN

I need to create a self signed certificate with my own the Subject DN. Example: I want to set username in Subject DN. I will retrieve this information using
X509Certificate.getSubjectDN().getName().
I was able to set the IssuserDN using -dname option of the keytool.
keytool -genkey -keyalg RSA -dname "CN=MyApp ACES CA 2, OU=MyApp Public Sector, O=MyApp, C=US" -alias selfsigned -keystore keystore.jks -storepass password -validity 360 -keysize 2048
getIssuerDN().getName()
getSubjectDN().getName()
prints following:
CN=MyApp ACES CA 2, OU=MyApp Public Sector, O=MyApp, C=US
CN=MyApp ACES CA 2, OU=MyApp Public Sector, O=MyApp, C=US
Not sure how to set the Subject DN using java keytool or openssl.
Thanks

Keytool - Import a certificate to the keystore

I am trying to import a certificate into a keystore.
Therefore i execute the following steps:
Create an empty keystore.jks file
Create the file contentent with the keytool:
keytool -genkey -alias server-alias -keyalg RSA -keypass changeit -storepass changeit -keystore keystore.jks
Add my certificate to that keystore:
keytool -import -v -trustcacerts -alias server-alias -file C:\server.cert -keystore keystore.jks -keypass changeit -storepass changeit
But I always get the error:
keytool error: java.lang.Exception: Public keys in reply and keystore don't match
What am I doing wrong here and how can I fix it?
In step two you always generate a new key, but if you want to import a certificate and it's private key into the keystore you have to use the key that matches the certificate.
Hence you need the key you created before someone signed your server certificate (the one stored in C:\server.cert).
The following steps have to be performed in the right order:
Create a random public/private key pair. The private key is usually saved password protected
Create a certificate request containing the public key from step 1
Send the certificate request to a trust center which signs it and sends you back the certificate (the server.cert you have)
Save the private key from step 1 together with the returned certificate
Therefore you are always trying to re-do step 1 and 4 without step 2 and 3. Thus you are getting that "don't match" error.
Just complete all steps 1-4 after another. Or use the saved private key that belongs to the server.cert certificate (hopefully you still have it).

How to add subject alernative name to ssl certs?

I'm using openssl to create self-signed certs. I'm getting this error with the certs I generated:
javax.net.ssl.SSLHandshakeException:
java.security.cert.CertificateException: No subject alternative names
present
Does anyone know how to specify "Subject alternative name" while creating a cert?
This is how I'm generating a keystore:
sudo $JAVA_HOME/bin/keytool -genkey -dname "CN=192.168.x.xxx, OU=I, O=I, L=T, ST=On, C=CA" -alias tomcat -validity 3650 -keyalg RSA -keystore /root/.keystore -keypass abcd -storepass abcd
To generate a key:
openssl s_client -connect 192.168.x.xxx:8443 2>/dev/null
Please help! Thanks!
Although this question was more specifically about IP addresses in Subject Alt. Names, the commands are similar (using DNS entries for a host name and IP entries for IP addresses).
To quote myself:
If you're using keytool, as of Java 7, keytool has an option to
include a Subject Alternative Name (see the table in the documentation
for -ext): you could use -ext san=dns:www.example.com or -ext
san=ip:10.0.0.1
Note that you only need Java 7's keytool to use this command. Once you've prepared your keystore, it should work with previous versions of Java.
(The rest of this answer also mentions how to do this with OpenSSL, but it doesn't seem to be what you're using.)
Both IP and DNS can be specified with the keytool additional argument -ext SAN=dns:abc.com,ip:1.1.1.1
Example:
keytool -genkeypair -keystore <keystore> -dname "CN=test, OU=Unknown, O=Unknown, L=Unknown, ST=Unknown, C=Unknown" -keypass <keypwd> -storepass <storepass> -keyalg RSA -alias unknown -ext SAN=dns:test.abc.com,ip:1.1.1.1
When generating CSR is possible to specify -ext attribute again to have it inserted in the CSR
keytool -certreq -file test.csr -keystore test.jks -alias testAlias -ext SAN=dns:test.example.com
complete example here: How to create CSR with SANs using keytool

Create X.509 Authorization Certificate

Can we programmatically create X.509 Certificate which contains arbitrary attributes e.g. Age, Gender, Designation etc.
I need to create Authorization Certificate, which client application will present to server application before executing the required function.
I am using Java and C#, so any solution/suggestion is good for me.
Have a look at this site for a start.
Create keystore
keytool -genkey -alias youralias -keyalg RSA -keypass yourpassword123 -keystore yourKeystoreRSAMD5 -storepass store123 -sigalg MD5WITHRSA -validity 9999 -dname "CN=Common Name, OU=XYZ, O=Your company name, L=Your Town, S=Region, C=Countryname"
Export cert file in PEM format
keytool -export -alias youralias -file rsamd5YourKey.cer -keystore yourKeystoreRSAMD5 -storepass store123