improved certificates
[tlspool] / testdata / Makefile
1 #
2 # testdata/Makefile -- for TLS Pool developers
3 #
4 # This file creates elements in the testdata/ directory used for testing
5 # and is in fact a requisite for tool/testcli, tool/testsrv, tool/testpeer.
6 #
7 # Targets of interest:
8 #  * all -- makes sure you can go ahead
9 #  * rebuild-pkcs11 -- wipes your PKCS #11 token (really!) and starts it fresh
10 #  * rebuild-cert -- wipes your certificate files and rebuilds them
11 #  * rebuild-pgp -- wipes your OpenPGP public keys and rebuilds them
12 #  * rebuild-db -- wipes your public credentials databases and refills them
13 # The last four should be run in order; later ones may depend on predecessors.
14 #
15 # From: Rick van Rein <rick@openfortress.nl>
16 #
17
18
19 #
20 # The following numbered keys are created here:
21 #
22 # 1. test client OpenPGP key: testcli@tlspool.arpa2.lab
23 # 2. test server OpenPGP key: testsrv@tlspool.arpa2.lab
24 # 3. test client certificate: testcli@tlspool.arpa2.lab
25 # 4. test server certificate: testsrv@tlspool.arpa2.lab
26 # 5. test CA certificate: testca@tlspool.arpa2.lab
27 # 6. test on-the-fly signing CA certificate: flying-signer@tlspool.arpa2.lab
28 # 7. test server certificate: tlspool.arpa2.lab
29 #
30
31
32 #
33 # Setting for the configuration file tlspool.conf
34 # Setting of the number of RSA private key bits (radically ignoring 2^n trends)
35 #
36 CONFFILE ?= $(shell pwd)/../etc/tlspool.conf
37 RSABITS ?= 2048
38 PGPRSABITS ?= 2048
39
40 #
41 # The directory with tools, defaulting to ../tool in the git base
42 # Note that testdata is meant for developers, so assuming git is usually the best
43 #
44 TOOLDIR ?= $(shell pwd)/../build/tool
45
46 #
47 # Load a few things from tlspool.conf; these are assumed present while testing
48 #
49 P11PIN=$(shell sed < $(CONFFILE) -n 's/^pkcs11_pin //p')
50 P11LIB=$(shell sed < $(CONFFILE) -n 's/^pkcs11_path //p')
51 P11URI=$(shell sed < $(CONFFILE) -n 's/^pkcs11_token pkcs11:/pkcs11:/p')
52 DMNUSR=$(shell sed < $(CONFFILE) -n 's/^daemon_user //p')
53 DMNGRP=$(shell sed < $(CONFFILE) -n 's/^daemon_group //p')
54 BDBENV=$(shell sed < $(CONFFILE) -n 's/^dbenv_dir //p')
55
56 #
57 # Embellish p11tool command; if fixed, provide the PKCS #11 PIN automatically
58 #
59 ifeq ($(P11PIN),)
60 P11TOOL=p11tool --provider $(P11LIB) --login
61 CERTTOOL=certtool --provider $(P11LIB)
62 PGPTOOL=$(TOOLDIR)/pgp11-genkey
63 else
64 P11TOOL=GNUTLS_PIN=$(P11PIN) p11tool --provider $(P11LIB) --login
65 CERTTOOL=GNUTLS_PIN=$(P11PIN) certtool --provider $(P11LIB)
66 PGPTOOL=GNUTLS_PIN=$(P11PIN) $(TOOLDIR)/pgp11-genkey
67 endif
68
69 #
70 # Establish which private keys need to be generated on the PKCS #11 token
71 #
72
73 PRIVKEY1=$(shell $(P11TOOL) --list-privkeys '$(P11URI)' | sed -e '/object=obj1label/!d' -e 's/^[ \t]*URL: //')
74 ifeq ($(PRIVKEY1),)
75 PRIVKEYGEN += privkey1
76 PRIVKEY1=$(P11URI);id=%30%31;object=obj1label;type=private
77 endif
78
79 PRIVKEY2=$(shell $(P11TOOL) --list-privkeys '$(P11URI)' | sed -e '/object=obj2label/!d' -e 's/^[ \t]*URL: //')
80 ifeq ($(PRIVKEY2),)
81 PRIVKEYGEN += privkey2
82 PRIVKEY2=$(P11URI);id=%30%32;object=obj2label;type=private
83 endif
84
85 PRIVKEY3=$(shell $(P11TOOL) --list-privkeys '$(P11URI)' | sed -e '/object=obj3label/!d' -e 's/^[ \t]*URL: //')
86 ifeq ($(PRIVKEY3),)
87 PRIVKEYGEN += privkey3
88 PRIVKEY3=$(P11URI);id=%30%33;object=obj3label;type=private
89 endif
90
91 PRIVKEY4=$(shell $(P11TOOL) --list-privkeys '$(P11URI)' | sed -e '/object=obj4label/!d' -e 's/^[ \t]*URL: //')
92 ifeq ($(PRIVKEY4),)
93 PRIVKEYGEN += privkey4
94 PRIVKEY4=$(P11URI);id=%30%34;object=obj4label;type=private
95 endif
96
97 PRIVKEY5=$(shell $(P11TOOL) --list-privkeys '$(P11URI)' | sed -e '/object=obj5label/!d' -e 's/^[ \t]*URL: //')
98 ifeq ($(PRIVKEY5),)
99 PRIVKEYGEN += privkey5
100 PRIVKEY5=$(P11URI);id=%30%35;object=obj5label;type=private
101 endif
102
103 PRIVKEY6=$(shell $(P11TOOL) --list-privkeys '$(P11URI)' | sed -e '/object=obj6label/!d' -e 's/^[ \t]*URL: //')
104 ifeq ($(PRIVKEY6),)
105 PRIVKEYGEN += privkey6
106 PRIVKEY6=$(P11URI);id=%30%36;object=obj6label;type=private
107 endif
108
109 PRIVKEY7=$(shell $(P11TOOL) --list-privkeys '$(P11URI)' | sed -e '/object=obj7label/!d' -e 's/^[ \t]*URL: //')
110 ifeq ($(PRIVKEY7),)
111 PRIVKEYGEN += privkey7
112 PRIVKEY7=$(P11URI);id=%30%37;object=obj7label;type=private
113 endif
114
115 PRIVKEY8=$(shell $(P11TOOL) --list-privkeys '$(P11URI)' | sed -e '/object=obj8label/!d' -e 's/^[ \t]*URL: //')
116 ifeq ($(PRIVKEY8),)
117 PRIVKEYGEN += privkey8
118 PRIVKEY8=$(P11URI);id=%30%38;object=obj8label;type=private
119 endif
120
121
122 #
123 # General rules for cleaning and filling (together, rebuilding) parts
124 #
125
126 TARGET_PKCS11=$(PRIVKEYGEN)
127 TARGET_CERT=tlspool-test-client-cert.der tlspool-test-server-cert.der tlspool-test-ca-cert.der tlspool-test-flying-signer.der tlspool-test-webhost-cert.der tlspool-test-playground-cert.der tlspool-test-srp
128 TARGET_PGP=tlspool-test-client-pubkey.pgp tlspool-test-server-pubkey.pgp
129 TARGET_DB=localid.db disclose.db trust.db
130 TARGET_DBE=tlspool.env
131
132 .PHONY: all rebuild-pkcs11 rebuild-cert rebuild-pgp rebuild-db
133 .PHONY:       clean-pkcs11   clean-cert   clean-pgp   clean-db
134 .PHONY:      refill-pkcs11  refill-cert  refill-pgp  refill-db
135
136 all: fill-pkcs11 fill-cert fill-pgp fill-db
137
138 rebuild-pkcs11: clean-pkcs11 fill-pkcs11
139         #
140         # You should continue with "make rebuild-cert rebuild-pgp rebuild-db"
141         #
142
143 rebuild-cert: clean-cert fill-cert
144         #
145         # You should continue with "make rebuild-db"
146         #
147
148 rebuild-pgp: clean-pgp fill-pgp
149         #
150         # You should continue with "make rebuild-db"
151         #
152
153 rebuild-db: clean-db fill-db
154
155 clean-pkcs11:
156         #
157         # WARNING -- PROCEED WITH CARE
158         #
159         # About to wipe your PKCS #11 object store.
160         # If this is unintended, stop now.
161         #
162         $(P11TOOL) --initialize '$(P11URI)'
163
164 fill-pkcs11: $(PRIVKEYGEN)
165
166 clean-cert:
167         rm -f $(TARGET_CERT)
168
169 fill-cert: $(TARGET_CERT)
170
171 clean-pgp:
172         rm -f $(TARGET_PGP)
173
174 fill-pgp: $(TARGET_PGP)
175
176 clean-db:
177         if pidof tlspool ; then echo First stop TLS Pool ; exit 1 ; fi
178         rm -f $(TARGET_DB)
179         mkdir -p $(TARGET_DBE)
180         rm -f $(TARGET_DBE)/*
181         rmdir $(TARGET_DBE)
182
183 fill-db: $(TARGET_DBE) $(TARGET_DB)
184
185
186 #
187 # Rule for private key generation on the PKCS #11 token
188 #
189 # Old: Generate test keys externally and import using SoftHSM-specific tool:
190 #
191 # openssl pkcs8 -topk8 -in tlspool-test-client-key.pem -out tlspool-test-client-key-pkcs8.pem -inform pem -outform pem -nocrypt
192 # openssl pkcs8 -topk8 -in tlspool-test-server-key.pem -out tlspool-test-server-key-pkcs8.pem -inform pem -outform pem -nocrypt
193 #
194 # softhsm-util --import tlspool-test-client-key-pkcs8.pem --slot 0 --label 'TLS Pool testdata' --id '6f626a336964'
195 # softhsm-util --import tlspool-test-server-key-pkcs8.pem --slot 0 --label 'TLS Pool testdata' --id '6f626a346964'
196 #
197 # Could alternatively do:
198 #
199 # $(P11TOOL) --initialize '$(P11URI)'
200 # $(P11TOOL) --generate-rsa --bits $(RSABITS) --label objXlabel --id objXid --outfile xxx.pem '$(P11URI)'
201 # ...
202
203 .PHONY: privkey1 privkey2 privkey3 privkey4 privkey5 privkey6 privkey7 privkey8
204
205 privkey1:
206         @echo 'Generating private key #1 on PKCS #11 token'
207         $(P11TOOL) --generate-rsa --bits $(PGPRSABITS) --label=obj1label --id=3031 --outfile=/dev/null '$(P11URI)'
208
209 privkey2:
210         @echo 'Generating private key #2 on PKCS #11 token'
211         $(P11TOOL) --generate-rsa --bits $(PGPRSABITS) --label=obj2label --id=3032 --outfile=/dev/null '$(P11URI)'
212
213 privkey3:
214         @echo 'Generating private key #3 on PKCS #11 token'
215         $(P11TOOL) --generate-rsa --bits $(RSABITS) --label=obj3label --id=3033 --outfile=/dev/null '$(P11URI)'
216
217 privkey4:
218         @echo 'Generating private key #4 on PKCS #11 token'
219         $(P11TOOL) --generate-rsa --bits $(RSABITS) --label=obj4label --id=3034 --outfile=/dev/null '$(P11URI)'
220
221 privkey5:
222         @echo 'Generating private key #5 on PKCS #11 token'
223         $(P11TOOL) --generate-rsa --bits $(RSABITS) --label=obj5label --id=3035 --outfile=/dev/null '$(P11URI)'
224
225 privkey6:
226         @echo 'Generating private key #6 on PKCS #11 token'
227         $(P11TOOL) --generate-rsa --bits $(RSABITS) --label=obj6label --id=3036 --outfile=/dev/null '$(P11URI)'
228
229 privkey7:
230         @echo 'Generating private key #7 on PKCS #11 token'
231         $(P11TOOL) --generate-rsa --bits $(RSABITS) --label=obj7label --id=3037 --outfile=/dev/null '$(P11URI)'
232
233 privkey8:
234         @echo 'Generating private key #8 on PKCS #11 token'
235         $(P11TOOL) --generate-rsa --bits $(RSABITS) --label=obj8label --id=3038 --outfile=/dev/null '$(P11URI)'
236
237
238 #
239 # Produce binary DER certificates (without going through the textual "PEM" form)
240 #
241
242 # Key 1: OpenPGP Client Certificate
243 tlspool-test-client-pubkey.pgp:
244         echo Using PRIVKEY1, '$(PRIVKEY1)'
245         $(PGPTOOL) $(P11LIB) '$(PRIVKEY1)' 'OpenPGP Test Client <testcli@tlspool.arpa2.lab>' $@
246         $(CERTTOOL) --pgp-certificate-info --infile $@ --inraw --outfile $(@:.pgp=.asc)
247
248 # Key 2: OpenPGP Server Certificate
249 tlspool-test-server-pubkey.pgp:
250         echo Using PRIVKEY2, '$(PRIVKEY2)'
251         $(PGPTOOL) $(P11LIB) '$(PRIVKEY2)' 'OpenPGP Test Server <testsrv@tlspool.arpa2.lab>' $@
252         $(CERTTOOL) --pgp-certificate-info --infile $@ --inraw --outfile $(@:.pgp=.asc)
253
254 # Key 3: X.509 Client Certificate
255 tlspool-test-client-cert.der: tlspool-test-client-cert.template tlspool-test-ca-cert.der
256         echo Using PRIVKEY3, '$(PRIVKEY3)'
257         $(CERTTOOL) --outfile $@ --outder --generate-certificate --load-ca-certificate=tlspool-test-ca-cert.pem --load-ca-privkey='$(PRIVKEY5)' --load-privkey='$(PRIVKEY3)' --template=$<
258         $(CERTTOOL) --certificate-info --infile $@ --inder --outfile $(@:.der=.pem)
259
260 # Key 4: X.509 Server Certificate with user@ domain name
261 tlspool-test-server-cert.der: tlspool-test-server-cert.template tlspool-test-ca-cert.der
262         echo Using PRIVKEY4, '$(PRIVKEY4)'
263         $(CERTTOOL) --outfile $@ --outder --generate-certificate --load-ca-certificate=tlspool-test-ca-cert.pem --load-ca-privkey='$(PRIVKEY5)' --load-privkey='$(PRIVKEY4)' --template=$<
264         $(CERTTOOL) --certificate-info --infile $@ --inder --outfile $(@:.der=.pem)
265
266 # Key 5: Test CA (for chained certificates)
267 tlspool-test-ca-cert.der: tlspool-test-ca-cert.template
268         echo Using PRIVKEY5, '$(PRIVKEY5)'
269         $(CERTTOOL) --outfile $@ --outder --generate-self-signed --load-privkey='$(PRIVKEY5)' --template=$<
270         $(CERTTOOL) --certificate-info --infile $@ --inder --outfile $(@:.der=.pem)
271
272 #TODO# # Based on key 5: certificate chain
273 #TODO# tlspool-test-server-certchain.der: tlspool-test-server-cert.der tlspool-test-ca-cert.der
274 #TODO#  cat > $@ tlspool-test-server-cert.der tlspool-test-ca-cert.der
275
276 # Key 6: Flying Signer CA (loaded into TLS Pool and automated)
277
278 tlspool-test-flying-signer.der: tlspool-test-flying-signer.template
279         echo Using PRIVKEY6, '$(PRIVKEY6)'
280         $(CERTTOOL) --outfile $@ --outder --generate-self-signed --load-privkey='$(PRIVKEY6)' --template=$<
281         $(CERTTOOL) --certificate-info --infile $@ --inder --outfile $(@:.der=.pem)
282
283 # Key 7: X.509 Server Certificate with just a host name
284 tlspool-test-webhost-cert.der: tlspool-test-webhost-cert.template tlspool-test-ca-cert.der
285         echo Using PRIVKEY7, '$(PRIVKEY7)'
286         $(CERTTOOL) --outfile $@ --outder --generate-certificate --load-ca-certificate=tlspool-test-ca-cert.pem --load-ca-privkey='$(PRIVKEY5)' --load-privkey='$(PRIVKEY7)' --template=$<
287
288 # Key 8: X.509 Server Certificate with just a host name
289 tlspool-test-playground-cert.der: tlspool-test-playground-cert.template tlspool-test-ca-cert.der
290         echo Using PRIVKEY8, '$(PRIVKEY8)'
291         $(CERTTOOL) --outfile $@ --outder --generate-certificate --load-ca-certificate=tlspool-test-ca-cert.pem --load-ca-privkey='$(PRIVKEY5)' --load-privkey='$(PRIVKEY8)' --template=$<
292
293
294 # Turn a .der into a .keyid
295 %.keyid: %.der
296         $(CERTTOOL) --inraw --infile $< -i | sed -e '1,/Public Key ID:/d' -e '/Public key.s random art:/,$$d' -e 's/[ \t]*//' | sed -n -e 's/^sha1://' -e '/^[^:]*$$/p' > $@
297
298 #
299 # SRP credentials are loaded from fixed paths ../testdata/tlspool-test-srp.* for now
300 # Yes, this is ugly, we're still hoping to use SRP #11 instead, as defined on
301 # https://github.com/arpa2/srp-pkcs11
302 #
303
304 tlspool-test-srp:
305         chown $(DMNUSR):$(DMNGRP) $@.conf $@.passwd
306
307
308 #
309 # Create localid.db from scratch
310 #
311
312 tlspool.env:
313         mkdir -p $@
314         chown $(DMNUSR):$(DMNGRP) $@
315
316 localid.db: tlspool.env
317         $(TOOLDIR)/tlspool-localid-set $(CONFFILE) testcli@tlspool.arpa2.lab OpenPGP,client '$(PRIVKEY1)' tlspool-test-client-pubkey.pgp
318         $(TOOLDIR)/tlspool-localid-set $(CONFFILE) testsrv@tlspool.arpa2.lab OpenPGP,server '$(PRIVKEY2)' tlspool-test-server-pubkey.pgp
319         $(TOOLDIR)/tlspool-localid-set $(CONFFILE) testcli@tlspool.arpa2.lab x.509,client '$(PRIVKEY3)' tlspool-test-client-cert.der
320         $(TOOLDIR)/tlspool-localid-set $(CONFFILE) testsrv@tlspool.arpa2.lab x.509,server '$(PRIVKEY4)' tlspool-test-server-cert.der
321         $(TOOLDIR)/tlspool-localid-set $(CONFFILE) testcli@tlspool.arpa2.lab kerberos,client,server 'pkcs11:some;place' /dev/null
322         $(TOOLDIR)/tlspool-localid-set $(CONFFILE) testsrv@tlspool.arpa2.lab kerberos,client,server 'pkcs11:some;place' /dev/null
323         #REALISTIC-BUT-NOT-YET# $(TOOLDIR)/tlspool-localid-set $(CONFFILE) testcli@tlspool.arpa2.lab valexp,client,server 'tI&' /dev/null
324         #REALISTIC-BUT-NOT-YET# $(TOOLDIR)/tlspool-localid-set $(CONFFILE) testsrv@tlspool.arpa2.lab valexp,client,server 'It&' /dev/null
325         $(TOOLDIR)/tlspool-localid-set $(CONFFILE) testcli@tlspool.arpa2.lab valexp,client,server '1' /dev/null
326         $(TOOLDIR)/tlspool-localid-set $(CONFFILE) testsrv@tlspool.arpa2.lab valexp,client,server '1' /dev/null
327         $(TOOLDIR)/tlspool-localid-set $(CONFFILE) tlspool.arpa2.lab x.509,server,client '$(PRIVKEY7)' tlspool-test-webhost-cert.der
328         $(TOOLDIR)/tlspool-localid-set $(CONFFILE) playground.arpa2.lab x.509,server,client '$(PRIVKEY8)' tlspool-test-playground-cert.der
329         chown $(DMNUSR):$(DMNGRP) $(BDBENV)/* $@
330
331 disclose.db: tlspool.env localid.db
332         $(TOOLDIR)/tlspool-disclose-set $(CONFFILE) @.arpa2.lab testcli@tlspool.arpa2.lab testsrv@tlspool.arpa2.lab
333         $(TOOLDIR)/tlspool-disclose-set $(CONFFILE) . tlspool.arpa2.lab
334         chown $(DMNUSR):$(DMNGRP) $(BDBENV)/* $@
335
336 trust.db: tlspool.env tlspool-test-ca-cert.der tlspool-test-ca-cert.keyid tlspool-test-flying-signer.der tlspool-test-flying-signer.keyid
337         $(TOOLDIR)/tlspool-trust-set $(CONFFILE) x509,client,server `cat tlspool-test-ca-cert.keyid` 1 tlspool-test-ca-cert.der
338         $(TOOLDIR)/tlspool-trust-set $(CONFFILE) x509,client,server `cat tlspool-test-flying-signer.keyid` 1 tlspool-test-flying-signer.der
339         chown $(DMNUSR):$(DMNGRP) $(BDBENV)/* $@
340
341 clean: clean-db
342         rm -f *.der *.pgp
343         rm -f *.pem *.asc
344
345 anchors: trust.db
346         @$(foreach rootca,$(shell ls -1 $(shell pwd)/trust-anchors/*.pem),$(CERTTOOL) --certificate-info --infile "$(rootca)" --outder --outfile "$(rootca:.pem=.der)" && ) echo 'Converted all root CA .pem to .der'
347         @$(foreach rootca,$(shell ls -1 $(shell pwd)/trust-anchors/*.der),$(CERTTOOL) --inraw --infile "$(rootca)" -i | sed -e '1,/Public Key ID:/d' -e '/Public key.s random art:/,$$d' -e 's/[ \t]*//' > "$(rootca:.der=.keyid)" && ) echo 'Converted all root CA .der to .keyid'
348         @$(foreach rootca,$(shell ls -1 $(shell pwd)/trust-anchors/*.der),$(TOOLDIR)/set_trust $(CONFFILE) x509,client,server `cat "$(rootca:.der=.keyid)"` 1 "$(rootca)" && ) echo 'Imported  all root CA .der into trust.db'
349
350 anew: clean all
351