xref: /freebsd/crypto/openssl/doc/HOWTO/keys.txt (revision b077aed33b7b6aefca7b17ddb250cf521f938613)
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
17*b077aed3SPierre Pronchery2. To generate an RSA key
1850ef0093SJacques Vidrine
19*b077aed3SPierre ProncheryAn 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
60e71b7053SJung-uk Kim4. To generate an EC key
61e71b7053SJung-uk Kim
62e71b7053SJung-uk KimAn EC key can be used both for key agreement (ECDH) and signing (ECDSA).
63e71b7053SJung-uk Kim
64e71b7053SJung-uk KimGenerating a key for ECC is similar to generating a DSA key. These are
65e71b7053SJung-uk Kimtwo-step processes. First, you have to get the EC parameters from which
66e71b7053SJung-uk Kimthe key will be generated:
67e71b7053SJung-uk Kim
68e71b7053SJung-uk Kim  openssl ecparam -name prime256v1 -out prime256v1.pem
69e71b7053SJung-uk Kim
70e71b7053SJung-uk KimThe prime256v1, or NIST P-256, which stands for 'X9.62/SECG curve over
71e71b7053SJung-uk Kima 256-bit prime field', is the name of an elliptic curve which generates the
72e71b7053SJung-uk Kimparameters. You can use the following command to list all supported curves:
73e71b7053SJung-uk Kim
74e71b7053SJung-uk Kim  openssl ecparam -list_curves
75e71b7053SJung-uk Kim
76e71b7053SJung-uk KimWhen that is done, you can generate a key using the created parameters (several
77e71b7053SJung-uk Kimkeys can be produced from the same parameters):
78e71b7053SJung-uk Kim
79e71b7053SJung-uk Kim  openssl genpkey -des3 -paramfile prime256v1.pem -out private.key
80e71b7053SJung-uk Kim
81e71b7053SJung-uk KimWith this variant, you will be prompted for a password to protect your key.
82e71b7053SJung-uk KimIf you don't want your key to be protected by a password, remove the flag
83e71b7053SJung-uk Kim'-des3' from the command line above.
84e71b7053SJung-uk Kim
85e71b7053SJung-uk KimYou can also directly generate the key in one step:
86e71b7053SJung-uk Kim
87e71b7053SJung-uk Kim  openssl ecparam -genkey -name prime256v1 -out private.key
88e71b7053SJung-uk Kim
89e71b7053SJung-uk Kimor
90e71b7053SJung-uk Kim
91e71b7053SJung-uk Kim  openssl genpkey -algorithm EC -pkeyopt ec_paramgen_curve:P-256
92e71b7053SJung-uk Kim
93e71b7053SJung-uk Kim
94e71b7053SJung-uk Kim5. NOTE
95e71b7053SJung-uk Kim
96e71b7053SJung-uk KimIf you intend to use the key together with a server certificate,
97e71b7053SJung-uk Kimit may be reasonable to avoid protecting it with a password, since
98e71b7053SJung-uk Kimotherwise someone would have to type in the password every time the
99e71b7053SJung-uk Kimserver needs to access the key.
100e71b7053SJung-uk Kim
101e71b7053SJung-uk KimFor X25519 and X448, it's treated as a distinct algorithm but not as one of
102e71b7053SJung-uk Kimthe curves listed with 'ecparam -list_curves' option. You can use
103e71b7053SJung-uk Kimthe following command to generate an X25519 key:
104e71b7053SJung-uk Kim
105e71b7053SJung-uk Kim  openssl genpkey -algorithm X25519 -out xkey.pem
106