author | Oleksandr Gavenko <gavenkoa@gmail.com> |
Tue, 17 Nov 2020 19:49:45 +0200 | |
changeset 2462 | 9b3b29e0b467 |
parent 2453 | 5bb8692c080a |
child 2492 | bd3d45148652 |
permissions | -rw-r--r-- |
2450
3e1990dc6ac8
Generate a self-signed certificate.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
1 |
|
3e1990dc6ac8
Generate a self-signed certificate.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
2 |
========== |
3e1990dc6ac8
Generate a self-signed certificate.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
3 |
SSL, TLS |
3e1990dc6ac8
Generate a self-signed certificate.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
4 |
========== |
3e1990dc6ac8
Generate a self-signed certificate.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
5 |
.. contents:: |
3e1990dc6ac8
Generate a self-signed certificate.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
6 |
:local: |
3e1990dc6ac8
Generate a self-signed certificate.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
7 |
|
2451 | 8 |
Generate private keys |
9 |
===================== |
|
10 |
||
11 |
Generate RSA key (last argument is a key bit size):: |
|
12 |
||
13 |
openssl genrsa -des3 -out my.key -passout pass:123456 2048 |
|
14 |
||
15 |
Generate DSA key:: |
|
16 |
||
17 |
openssl gendsa -out my.key -passout pass:123456 <(openssl dsaparam 512) |
|
18 |
||
2453
5bb8692c080a
Show info about PKCS#12 store.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2451
diff
changeset
|
19 |
Select DSA curve:: |
5bb8692c080a
Show info about PKCS#12 store.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2451
diff
changeset
|
20 |
|
5bb8692c080a
Show info about PKCS#12 store.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2451
diff
changeset
|
21 |
openssl ecparam -list_curves |
5bb8692c080a
Show info about PKCS#12 store.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2451
diff
changeset
|
22 |
|
2450
3e1990dc6ac8
Generate a self-signed certificate.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
23 |
Generate a self-signed certificate |
3e1990dc6ac8
Generate a self-signed certificate.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
24 |
================================== |
3e1990dc6ac8
Generate a self-signed certificate.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
25 |
|
3e1990dc6ac8
Generate a self-signed certificate.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
26 |
``openssl`` allows to generate self-signed certificate by a single command (``-newkey`` |
3e1990dc6ac8
Generate a self-signed certificate.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
27 |
instructs to generate a private key and ``-x509`` instructs to issue a self-signed |
3e1990dc6ac8
Generate a self-signed certificate.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
28 |
certificate instead of a signing request):: |
3e1990dc6ac8
Generate a self-signed certificate.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
29 |
|
3e1990dc6ac8
Generate a self-signed certificate.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
30 |
openssl req -x509 -newkey rsa:4096 \ |
3e1990dc6ac8
Generate a self-signed certificate.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
31 |
-keyout my.key -passout pass:123456 -out my.crt \ |
3e1990dc6ac8
Generate a self-signed certificate.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
32 |
-days 365 \ |
3e1990dc6ac8
Generate a self-signed certificate.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
33 |
-subj /CN=localhost/O=home/C=US/emailAddress=me@mail.internal \ |
3e1990dc6ac8
Generate a self-signed certificate.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
34 |
-addext "subjectAltName = DNS:localhost,DNS:web.internal,email:me@mail.internal" \ |
3e1990dc6ac8
Generate a self-signed certificate.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
35 |
-addext keyUsage=digitalSignature -addext extendedKeyUsage=serverAuth |
3e1990dc6ac8
Generate a self-signed certificate.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
36 |
|
3e1990dc6ac8
Generate a self-signed certificate.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
37 |
You can generate a private key and construct a self-signing certificate in separate steps:: |
3e1990dc6ac8
Generate a self-signed certificate.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
38 |
|
3e1990dc6ac8
Generate a self-signed certificate.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
39 |
openssl genrsa -out my.key -passout pass:123456 2048 |
3e1990dc6ac8
Generate a self-signed certificate.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
40 |
|
3e1990dc6ac8
Generate a self-signed certificate.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
41 |
openssl req -x509 \ |
3e1990dc6ac8
Generate a self-signed certificate.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
42 |
-key my.key -passin pass:123456 -out my.csr \ |
3e1990dc6ac8
Generate a self-signed certificate.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
43 |
-days 3650 \ |
3e1990dc6ac8
Generate a self-signed certificate.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
44 |
-subj /CN=localhost/O=home/C=US/emailAddress=me@mail.internal \ |
3e1990dc6ac8
Generate a self-signed certificate.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
45 |
-addext "subjectAltName = DNS:localhost,DNS:web.internal,email:me@mail.internal" \ |
3e1990dc6ac8
Generate a self-signed certificate.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
46 |
-addext keyUsage=digitalSignature -addext extendedKeyUsage=serverAuth |
3e1990dc6ac8
Generate a self-signed certificate.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
47 |
|
3e1990dc6ac8
Generate a self-signed certificate.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
48 |
Review the resulting certificate:: |
3e1990dc6ac8
Generate a self-signed certificate.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
49 |
|
3e1990dc6ac8
Generate a self-signed certificate.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
50 |
openssl x509 -text -noout -in my.crt |
3e1990dc6ac8
Generate a self-signed certificate.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
51 |
|
3e1990dc6ac8
Generate a self-signed certificate.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
52 |
.. note:: |
3e1990dc6ac8
Generate a self-signed certificate.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
53 |
With ``openssl`` we can add an extra step: |
3e1990dc6ac8
Generate a self-signed certificate.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
54 |
|
3e1990dc6ac8
Generate a self-signed certificate.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
55 |
* generate private key (``openssl genrsa``) |
3e1990dc6ac8
Generate a self-signed certificate.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
56 |
* generate CSR (``openssl req -new``) |
3e1990dc6ac8
Generate a self-signed certificate.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
57 |
* sign CSR with private key (``openssl x509``) |
3e1990dc6ac8
Generate a self-signed certificate.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
58 |
|
3e1990dc6ac8
Generate a self-signed certificate.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
59 |
The problem here is that ``openssl x509`` doesn't support ``-addext`` like option so we |
3e1990dc6ac8
Generate a self-signed certificate.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
60 |
need to craft a config file... Of cause with Bash syntax ``<(...)`` we can add required |
3e1990dc6ac8
Generate a self-signed certificate.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
61 |
extensions:: |
3e1990dc6ac8
Generate a self-signed certificate.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
62 |
|
3e1990dc6ac8
Generate a self-signed certificate.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
63 |
openssl genrsa -out my.key -passout pass:123456 2048 |
3e1990dc6ac8
Generate a self-signed certificate.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
64 |
|
3e1990dc6ac8
Generate a self-signed certificate.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
65 |
openssl req -new \ |
3e1990dc6ac8
Generate a self-signed certificate.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
66 |
-key my.key -passin pass:123456 -out my.csr \ |
3e1990dc6ac8
Generate a self-signed certificate.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
67 |
-subj /CN=localhost/O=home/C=US/emailAddress=me@mail.internal |
3e1990dc6ac8
Generate a self-signed certificate.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
68 |
|
3e1990dc6ac8
Generate a self-signed certificate.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
69 |
openssl x509 -req \ |
3e1990dc6ac8
Generate a self-signed certificate.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
70 |
-in my.csr -signkey my.key -passin pass:123456 -out my.crt \ |
3e1990dc6ac8
Generate a self-signed certificate.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
71 |
-days 3650 -CAcreateserial \ |
3e1990dc6ac8
Generate a self-signed certificate.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
72 |
-extensions v3_ca \ |
3e1990dc6ac8
Generate a self-signed certificate.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
73 |
-extfile <( \ |
3e1990dc6ac8
Generate a self-signed certificate.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
74 |
echo "[v3_ca]"; \ |
3e1990dc6ac8
Generate a self-signed certificate.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
75 |
echo "extendedKeyUsage=serverAuth"; \ |
3e1990dc6ac8
Generate a self-signed certificate.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
76 |
echo "subjectAltName=DNS:localhost,DNS:web.internal,email:me@mail.internal") |
3e1990dc6ac8
Generate a self-signed certificate.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
77 |
|
3e1990dc6ac8
Generate a self-signed certificate.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
78 |
Java ``keytool`` creates PKCS#12 store:: |
3e1990dc6ac8
Generate a self-signed certificate.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
79 |
|
3e1990dc6ac8
Generate a self-signed certificate.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
80 |
keytool -genkeypair -keystore my.p12 -alias master \ |
3e1990dc6ac8
Generate a self-signed certificate.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
81 |
-storetype pkcs12 -keyalg RSA -keysize 2048 -validity 3650 \ |
3e1990dc6ac8
Generate a self-signed certificate.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
82 |
-storepass 123456 \ |
3e1990dc6ac8
Generate a self-signed certificate.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
83 |
-dname "CN=localhost,O=home,C=US" \ |
3e1990dc6ac8
Generate a self-signed certificate.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
84 |
-ext 'san=dns:localhost,dns:web.internal,email:me@mail.internal' |
3e1990dc6ac8
Generate a self-signed certificate.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
85 |
|
2453
5bb8692c080a
Show info about PKCS#12 store.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2451
diff
changeset
|
86 |
View the keystore:: |
5bb8692c080a
Show info about PKCS#12 store.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2451
diff
changeset
|
87 |
|
5bb8692c080a
Show info about PKCS#12 store.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2451
diff
changeset
|
88 |
keytool -list -v -keystore my.p12 -storepass 123456 |
5bb8692c080a
Show info about PKCS#12 store.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2451
diff
changeset
|
89 |
|
2450
3e1990dc6ac8
Generate a self-signed certificate.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
90 |
To export the self-signed certificate:: |
3e1990dc6ac8
Generate a self-signed certificate.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
91 |
|
3e1990dc6ac8
Generate a self-signed certificate.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
92 |
keytool -exportcert -keystore my.p12 -file my.crt \ |
3e1990dc6ac8
Generate a self-signed certificate.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
93 |
-alias master -rfc -storepass 123456 |
3e1990dc6ac8
Generate a self-signed certificate.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
94 |
|
3e1990dc6ac8
Generate a self-signed certificate.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
95 |
Review the resulting certificate:: |
3e1990dc6ac8
Generate a self-signed certificate.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
96 |
|
3e1990dc6ac8
Generate a self-signed certificate.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
97 |
keytool -printcert -file my.crt |
3e1990dc6ac8
Generate a self-signed certificate.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
98 |
|
3e1990dc6ac8
Generate a self-signed certificate.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
99 |
https://stackoverflow.com/questions/10175812/how-to-create-a-self-signed-certificate-with-openssl/64733092#64733092 |
3e1990dc6ac8
Generate a self-signed certificate.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
100 |
How to create a self-signed certificate with OpenSSL. |
3e1990dc6ac8
Generate a self-signed certificate.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
101 |
|
3e1990dc6ac8
Generate a self-signed certificate.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
102 |
Verify self-signed certificate |
3e1990dc6ac8
Generate a self-signed certificate.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
103 |
============================== |
3e1990dc6ac8
Generate a self-signed certificate.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
104 |
|
3e1990dc6ac8
Generate a self-signed certificate.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
105 |
Use a private key and corresponding self-signed certificate to launch a server:: |
3e1990dc6ac8
Generate a self-signed certificate.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
106 |
|
3e1990dc6ac8
Generate a self-signed certificate.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
107 |
openssl s_server -accept 8000 -www -key my.key -cert my.crt |
3e1990dc6ac8
Generate a self-signed certificate.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
108 |
|
3e1990dc6ac8
Generate a self-signed certificate.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
109 |
Clients should use self-signed certificate for verification:: |
3e1990dc6ac8
Generate a self-signed certificate.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
110 |
|
3e1990dc6ac8
Generate a self-signed certificate.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
111 |
echo | openssl s_client -servername localhost -connect localhost:8000 -CAfile my.crt |
3e1990dc6ac8
Generate a self-signed certificate.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
112 |
|
3e1990dc6ac8
Generate a self-signed certificate.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
113 |
curl -v --cacert my.crt https://localhost:8000 |
3e1990dc6ac8
Generate a self-signed certificate.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
114 |
|
3e1990dc6ac8
Generate a self-signed certificate.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
115 |
There is no certificate chain so the check is trivial for self-signed certificates... |
2451 | 116 |
|
117 |
PKCS#12 stores |
|
118 |
============== |
|
119 |
||
120 |
PKCS#12 store keeps private keys and certificates, to combine a private key and certificates into the store:: |
|
121 |
||
122 |
openssl pkcs12 -export -in my.crt -inkey my.key -certfile other.crt -out my.p12 -name master |
|
123 |
||
2453
5bb8692c080a
Show info about PKCS#12 store.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2451
diff
changeset
|
124 |
Show info about PKCS#12 store:: |
5bb8692c080a
Show info about PKCS#12 store.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2451
diff
changeset
|
125 |
|
5bb8692c080a
Show info about PKCS#12 store.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2451
diff
changeset
|
126 |
openssl pkcs12 -info -in certtool-srv.p12 -passin pass:123456 -nodes |
5bb8692c080a
Show info about PKCS#12 store.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2451
diff
changeset
|
127 |
keytool -list -v -keystore my.p12 -storepass 123456 |
5bb8692c080a
Show info about PKCS#12 store.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2451
diff
changeset
|
128 |
|
2451 | 129 |
To export a private key to PKCS#8 format (has header ``BEGIN PRIVATE KEY`` or ``BEGIN ENCRYPTED |
130 |
PRIVATE KEY``):: |
|
131 |
||
132 |
openssl pkcs12 -info -nocerts -in my.p12 -passin pass:123456 -nodes |
|
133 |
||
134 |
To extract private key and convert to PKCS#1 format (has header ``BEGIN RSA PRIVATE KEY`` or ``BEGIN |
|
135 |
DSA PRIVATE KEY``):: |
|
136 |
||
137 |
openssl pkcs12 -info -nocerts -in my.p12 -passin pass:123456 -nodes | openssl rsa |
|
138 |
||
139 |
To show private key info:: |
|
140 |
||
141 |
openssl pkcs12 -info -nocerts -in my.p12 -passin pass:123456 -nodes | openssl rsa -text -noout |
|
142 |
||
2453
5bb8692c080a
Show info about PKCS#12 store.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2451
diff
changeset
|
143 |
To show certificate info:: |
2451 | 144 |
|
145 |
openssl pkcs12 -info -nokeys -in my.p12 -passin pass:123456 |
|
146 |
openssl pkcs12 -info -nokeys -in my.p12 -passin pass:123456 | openssl x509 -text -noout |
|
147 |