xref: /freebsd/crypto/openssl/doc/HOWTO/keys.txt (revision e71b70530d95c4f34d8bdbd78d1242df1ba4a945)
150ef0093SJacques Vidrine<DRAFT!>
250ef0093SJacques Vidrine			HOWTO keys
350ef0093SJacques Vidrine
450ef0093SJacques Vidrine1. Introduction
550ef0093SJacques Vidrine
650ef0093SJacques VidrineKeys are the basis of public key algorithms and PKI.  Keys usually
750ef0093SJacques Vidrinecome in pairs, with one half being the public key and the other half
850ef0093SJacques Vidrinebeing the private key.  With OpenSSL, the private key contains the
950ef0093SJacques Vidrinepublic key information as well, so a public key doesn't need to be
1050ef0093SJacques Vidrinegenerated separately.
1150ef0093SJacques Vidrine
1250ef0093SJacques VidrinePublic keys come in several flavors, using different cryptographic
1350ef0093SJacques Vidrinealgorithms.  The most popular ones associated with certificates are
1450ef0093SJacques VidrineRSA and DSA, and this HOWTO will show how to generate each of them.
1550ef0093SJacques Vidrine
1650ef0093SJacques Vidrine
1750ef0093SJacques Vidrine2. To generate a RSA key
1850ef0093SJacques Vidrine
1950ef0093SJacques VidrineA RSA key can be used both for encryption and for signing.
2050ef0093SJacques Vidrine
2150ef0093SJacques VidrineGenerating a key for the RSA algorithm is quite easy, all you have to
2250ef0093SJacques Vidrinedo is the following:
2350ef0093SJacques Vidrine
2450ef0093SJacques Vidrine  openssl genrsa -des3 -out privkey.pem 2048
2550ef0093SJacques Vidrine
2650ef0093SJacques VidrineWith this variant, you will be prompted for a protecting password.  If
2750ef0093SJacques Vidrineyou don't want your key to be protected by a password, remove the flag
2850ef0093SJacques Vidrine'-des3' from the command line above.
2950ef0093SJacques Vidrine
3050ef0093SJacques VidrineThe number 2048 is the size of the key, in bits.  Today, 2048 or
3150ef0093SJacques Vidrinehigher is recommended for RSA keys, as fewer amount of bits is
3250ef0093SJacques Vidrineconsider insecure or to be insecure pretty soon.
3350ef0093SJacques Vidrine
3450ef0093SJacques Vidrine
3550ef0093SJacques Vidrine3. To generate a DSA key
3650ef0093SJacques Vidrine
3780815a77SJung-uk KimA DSA key can be used for signing only.  It is important to
3880815a77SJung-uk Kimknow what a certificate request with a DSA key can really be used for.
3950ef0093SJacques Vidrine
4050ef0093SJacques VidrineGenerating a key for the DSA algorithm is a two-step process.  First,
4150ef0093SJacques Vidrineyou have to generate parameters from which to generate the key:
4250ef0093SJacques Vidrine
4350ef0093SJacques Vidrine  openssl dsaparam -out dsaparam.pem 2048
4450ef0093SJacques Vidrine
4550ef0093SJacques VidrineThe number 2048 is the size of the key, in bits.  Today, 2048 or
4650ef0093SJacques Vidrinehigher is recommended for DSA keys, as fewer amount of bits is
4750ef0093SJacques Vidrineconsider insecure or to be insecure pretty soon.
4850ef0093SJacques Vidrine
4950ef0093SJacques VidrineWhen that is done, you can generate a key using the parameters in
5050ef0093SJacques Vidrinequestion (actually, several keys can be generated from the same
5150ef0093SJacques Vidrineparameters):
5250ef0093SJacques Vidrine
5350ef0093SJacques Vidrine  openssl gendsa -des3 -out privkey.pem dsaparam.pem
5450ef0093SJacques Vidrine
5550ef0093SJacques VidrineWith this variant, you will be prompted for a protecting password.  If
5650ef0093SJacques Vidrineyou don't want your key to be protected by a password, remove the flag
5750ef0093SJacques Vidrine'-des3' from the command line above.
5850ef0093SJacques Vidrine
5950ef0093SJacques Vidrine
60*e71b7053SJung-uk Kim4. To generate an EC key
61*e71b7053SJung-uk Kim
62*e71b7053SJung-uk KimAn EC key can be used both for key agreement (ECDH) and signing (ECDSA).
63*e71b7053SJung-uk Kim
64*e71b7053SJung-uk KimGenerating a key for ECC is similar to generating a DSA key. These are
65*e71b7053SJung-uk Kimtwo-step processes. First, you have to get the EC parameters from which
66*e71b7053SJung-uk Kimthe key will be generated:
67*e71b7053SJung-uk Kim
68*e71b7053SJung-uk Kim  openssl ecparam -name prime256v1 -out prime256v1.pem
69*e71b7053SJung-uk Kim
70*e71b7053SJung-uk KimThe prime256v1, or NIST P-256, which stands for 'X9.62/SECG curve over
71*e71b7053SJung-uk Kima 256-bit prime field', is the name of an elliptic curve which generates the
72*e71b7053SJung-uk Kimparameters. You can use the following command to list all supported curves:
73*e71b7053SJung-uk Kim
74*e71b7053SJung-uk Kim  openssl ecparam -list_curves
75*e71b7053SJung-uk Kim
76*e71b7053SJung-uk KimWhen that is done, you can generate a key using the created parameters (several
77*e71b7053SJung-uk Kimkeys can be produced from the same parameters):
78*e71b7053SJung-uk Kim
79*e71b7053SJung-uk Kim  openssl genpkey -des3 -paramfile prime256v1.pem -out private.key
80*e71b7053SJung-uk Kim
81*e71b7053SJung-uk KimWith this variant, you will be prompted for a password to protect your key.
82*e71b7053SJung-uk KimIf you don't want your key to be protected by a password, remove the flag
83*e71b7053SJung-uk Kim'-des3' from the command line above.
84*e71b7053SJung-uk Kim
85*e71b7053SJung-uk KimYou can also directly generate the key in one step:
86*e71b7053SJung-uk Kim
87*e71b7053SJung-uk Kim  openssl ecparam -genkey -name prime256v1 -out private.key
88*e71b7053SJung-uk Kim
89*e71b7053SJung-uk Kimor
90*e71b7053SJung-uk Kim
91*e71b7053SJung-uk Kim  openssl genpkey -algorithm EC -pkeyopt ec_paramgen_curve:P-256
92*e71b7053SJung-uk Kim
93*e71b7053SJung-uk Kim
94*e71b7053SJung-uk Kim5. NOTE
95*e71b7053SJung-uk Kim
96*e71b7053SJung-uk KimIf you intend to use the key together with a server certificate,
97*e71b7053SJung-uk Kimit may be reasonable to avoid protecting it with a password, since
98*e71b7053SJung-uk Kimotherwise someone would have to type in the password every time the
99*e71b7053SJung-uk Kimserver needs to access the key.
100*e71b7053SJung-uk Kim
101*e71b7053SJung-uk KimFor X25519 and X448, it's treated as a distinct algorithm but not as one of
102*e71b7053SJung-uk Kimthe curves listed with 'ecparam -list_curves' option. You can use
103*e71b7053SJung-uk Kimthe following command to generate an X25519 key:
104*e71b7053SJung-uk Kim
105*e71b7053SJung-uk Kim  openssl genpkey -algorithm X25519 -out xkey.pem
106