PKI.ipynb 10.8 KB
Newer Older
Tobias Hofmann's avatar
Tobias Hofmann committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Introduction\n",
    "\n",
    "Jupyter Notebook detailing how to create a PKI with Root CA and intermediate CA. The scripts used are loaded from the git reposoitory https://gitlab.com/tobias.a.hofmann/pki\n",
    "\n",
    "The notebook will create a root CA, intermediate CA, sign the certificates and create a new certificate for a server named blog.itsfullofstars.de. \n",
    "\n",
    "# Start\n",
    "\n",
    "Setup the files and scripts by cloning the repostiroy https://gitlab.com/tobias.a.hofmann/pki\n",
    "\n",
    "Clone git repo."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!git clone https://gitlab.com/tobias.a.hofmann/pki"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Root CA\n",
    "\n",
    "In this step, a root CA will be created. This will be an \"offline\" CA. It will serve to sign the intermediate CA and not sign any server, user or other certificates. As the root CA is at the top of the trust, its certificate will be self signed. First, a CSR and private key will be created.\n",
    "\n",
    "1. Create private key\n",
    "\n",
    "The passphare for reading the private key is test123.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!openssl genrsa -aes256 -passout pass:test123 -out pki/rootca/private/ca.itsfullofstars.de.key.pem 8192"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2. Sign request\n",
    "\n",
    "Self sign the CA request and create CA certificate. The certificate will be valid for 10 years (3650 days) and issued to the subject: \n",
    "\n",
    "* C=DE\n",
    "* ST=BW\n",
    "* L=Karlsruhe\n",
    "* O=Demo\n",
    "* CN=ca.itsfullofstars.de"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%env ROOTCA=pki/rootca\n",
    "!openssl req -config pki/rootca/openssl_root.cnf -new -x509 -sha512 -extensions v3_ca -passin pass:test123 -key pki/rootca/private/ca.itsfullofstars.de.key.pem -out pki/rootca/certs/ca.itsfullofstars.de.crt.pem -days 3650 -set_serial 0 -subj \"/C=DE/ST=BW/L=Karlsruhe/O=Demo/CN=ca.itsfullofstars.de\" "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Intermediate CA\n",
    "After creating the root CA the intermediate CA will be created. The intermediate CA request will be signed by the root CA. This is the CA that users will interact with to request user or server certificates. \n",
    "\n",
    "1. Create a signing request and private key.\n",
    "\n",
    "The certificate will be issued to subject:\n",
    "\n",
    "* C=DE\n",
    "* ST=BW\n",
    "* L=Karlsruhe\n",
    "* O=Demo\n",
    "* CN=int.itsfullofstars.de"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%env INTCA=pki/intermediate\n",
    "!openssl req -config pki/intermediate/openssl_intermediate.cnf -passout pass:abc123 -new -newkey rsa:8192 -keyout pki/intermediate/private/int.itsfullofstars.de.key.pem -out pki/rootca/csr/int.itsfullofstars.de.csr -subj \"/C=DE/ST=BW/L=Karlsruhe/O=Demo/CN=int.itsfullofstars.de\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2. Sign request\n",
    "\n",
    "Send intermediate CA signing request to root CA and sign it. The intermediate CA certificate will be valid for 10 years (3650 days)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%env ROOTCA=pki/rootca\n",
    "!openssl ca -batch -config pki/rootca/openssl_root.cnf -extensions v3_intermediate_ca -days 3650 -passin pass:test123 -notext -md sha512 -cert pki/rootca/certs/ca.itsfullofstars.de.crt.pem -keyfile pki/rootca/private/ca.itsfullofstars.de.key.pem -in pki/rootca/csr/int.itsfullofstars.de.csr -outdir pki/rootca/newcerts -out pki/rootca/certs/int.itsfullofstars.de.crt.pem"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3. Copy signed intermediate cert\n",
    "\n",
    "In the last step, the intermediate certificate was signed by the root CA. Now the certificate needs to be copied to intermediate CA public cert folder. After this is done, the intermediate CA can use the certificate. Please note: when the CSR was created, a private key for the certificate was also created and saved in the private folder of the CA."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!cp pki/rootca/certs/int.itsfullofstars.de.crt.pem pki/intermediate/certs/"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## (Optional) Create CA Chain\n",
    "Create a certificate file that contains the Root CA and intermediate CA certificate."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!cat pki/intermediate/certs/int.itsfullofstars.de.crt.pem pki/rootca/certs/ca.itsfullofstars.de.crt.pem > pki/intermediate/certs/chain.itsfullofstars.crt.pem"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Create server certificate\n",
    "The above commands created a complete public key infratructure with one root CA and one intermediate CA. The intermediate CA is used to sign certificate requests submitted by users. This is what the following steps are doing: \n",
    "\n",
    "* Create a private key & signing request\n",
    "* Let intermediate CA sign the request\n",
    "\n",
    "1. Create a request for a server certificate. \n",
    "\n",
    "Create the cert request and new private key. The subject of the new server is:\n",
    "\n",
    "* C=DE\n",
    "* ST=BW\n",
    "* L=Karlsruhe\n",
    "* O=Demo\n",
    "* CN=blog.itsfullofstars.de\n",
    "\n",
    "The CN part defines that the server will be available under the DNS blog.itsfullofstars.de. This is what browsers will check the server certificate for when connecting to it. If there is a mismatch between CN and DNS name, the browser will reject the certificate and show an error message."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%env INTCA=pki/intermediate\n",
    "!openssl req -out pki/intermediate/csr/blog.itsfullofstars.de.csr.pem -newkey rsa:4096 -nodes -keyout pki/private/blog.itsfullofstars.de.key.pem -config pki/intermediate/openssl_intermediate.cnf -subj \"/C=DE/ST=BW/L=Karlsruhe/O=Demo/CN=blog.itsfullofstars.de\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2. Sign request\n",
    "\n",
    "The intermediate CA will sign the server certificate request. The validity of the certificate must be less than the validity of the CA, therefore it cannot be 3650 days, but 3050.\n",
    "\n",
    "**Note** This behaviour can be changed in the CA configuration.\n",
    "\n",
    "After the server certificate is signed, it is copied to the user directory. In real live, the user would download it or receive it via E-Mail."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%env INTCA=pki/intermediate\n",
    "!openssl ca -batch -config pki/intermediate/openssl_intermediate.cnf -extensions server_cert -days 3050 -passin pass:abc123 -notext -md sha512 -in pki/intermediate/csr/blog.itsfullofstars.de.csr.pem -out pki/intermediate/certs/blog.itsfullofstars.de.crt.pem\n",
    "!cp pki/intermediate/certs/blog.itsfullofstars.de.crt.pem ./"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Validate server certificate\n",
    "\n",
    "There are several ways to validate a certificate. One is to read its content and check that the values a matching, like the subject line or issuer. \n",
    "\n",
    "## Manually\n",
    "The following command reads the PEM certificate and makes it human readable: openssl x509 -text\n",
    "\n",
    "* In the line Issuer you can see the name of the CA that issued the certificate (int.itsfullofstars.de)\n",
    "* The subject line contains the name of the server C = DE, ST = BW, L = Karlsruhe, O = Demo, CN = blog.itsfullofstars.de\n",
    "* X509v3 extensions states that it's a sever certificate and not a CA\n",
    "* In the extensions the root CA is listed: DirName:/C=DE/ST=BW/L=Karlsruhe/O=Demo/CN=ca.itsfullofstars.de"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!openssl x509 -text -in blog.itsfullofstars.de.crt.pem "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## OpenSSL verify\n",
    "Instead of reading the information manually you can use openssl to validate the certificate. The option verify is used to do this. Using verify, openssl will check the server certificate against a provided list of CA certificates and see if everything matches. This is where the previous (optional) created certificate chain is useful: you pass the chain that contains the root CA and intermediate CA certificates as a single parameter value in -CAfile."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!openssl verify -CAfile pki/intermediate/certs/chain.itsfullofstars.crt.pem blog.itsfullofstars.de.crt.pem"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# PEM to CRT\n",
    "Often you may see a requirement to provide the certificate that is in PEM format as a CRT file. You can use OpenSSL to transform a certificate from PEM to CRT."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!openssl x509 -outform der -in blog.itsfullofstars.de.crt.pem -out blog.itsfullofstars.de.crt"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Cleanup\n",
    "Last step is to clean up the resources by deleting the created files. This way you can simply redo or restart the notebook and start creating your PKI from scratch."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!rm -rf pki\n",
    "!rm blog.itsfullofstars.de.crt\n",
    "!rm blog.itsfullofstars.de.crt.pem"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}