| 1 |
ROOT_CA_PASSWORD=ssl_ca_pwd |
| 2 |
PK8_PASSWORD=sslpwd |
| 3 |
P12_PASSWORD=sslpwd |
| 4 |
SERVER_CRT_DIR=server/ |
| 5 |
|
| 6 |
all : $(SERVER_CRT_DIR)root.key $(SERVER_CRT_DIR)root.crt $(SERVER_CRT_DIR)server.crt goodroot.crt goodclient badclient |
| 7 |
|
| 8 |
goodclient: goodclient.crt goodclient.p12 goodclient.pk8 |
| 9 |
|
| 10 |
badclient: badclient.crt badclient.p12 |
| 11 |
|
| 12 |
.PHONY: clean |
| 13 |
clean: |
| 14 |
@echo Removing certificate files |
| 15 |
@rm -f *.crt *.key *.csr *.srl *.p12 |
| 16 |
@rm -f *.pk8 |
| 17 |
@rm -rf $(SERVER_CRT_DIR)*.crt $(SERVER_CRT_DIR)*.key $(SERVER_CRT_DIR)*.csr $(SERVER_CRT_DIR)*.srl $(SERVER_CRT_DIR)*.p12 $(SERVER_CRT_DIR)*.pk8 |
| 18 |
@echo |
| 19 |
|
| 20 |
%.pk8 : %.key |
| 21 |
@echo Converting key to DER PKCS#8 format $@ |
| 22 |
openssl pkcs8 -topk8 -nocrypt -inform PEM -outform DER -in $< -out $@ |
| 23 |
|
| 24 |
%.p12 : %.crt |
| 25 |
@echo Exporting certificate $@ |
| 26 |
# These clients are signed directly by the root CA, so the .p12 needs no extra chain. |
| 27 |
# With intermediate CAs, add `-certfile <intermediate-chain>.crt` so the server can build the chain. |
| 28 |
openssl pkcs12 -export -in $< -inkey $*.key -out $@ -name user -CAfile $(SERVER_CRT_DIR)root.crt -caname local -passout pass:$(P12_PASSWORD) |
| 29 |
|
| 30 |
%root.key : |
| 31 |
@echo Generating CA key $@ |
| 32 |
mkdir -p $(*D) |
| 33 |
openssl genrsa -aes256 -passout pass:$(ROOT_CA_PASSWORD) -out $@ 4096 |
| 34 |
@echo |
| 35 |
|
| 36 |
goodroot.crt : $(SERVER_CRT_DIR) |
| 37 |
cp $(SERVER_CRT_DIR)root.crt goodroot.crt |
| 38 |
|
| 39 |
%root.crt : %root.key |
| 40 |
@echo Creating root certificate $@ |
| 41 |
openssl req -x509 -new -nodes -key $< -passin pass:$(ROOT_CA_PASSWORD) -sha256 -days 3650 -out $@ -subj "/C=US/ST=CA/O=PgJdbc test/CN=root certificate" |
| 42 |
@echo |
| 43 |
|
| 44 |
|
| 45 |
$(SERVER_CRT_DIR)server.crt : $(SERVER_CRT_DIR)root.key $(SERVER_CRT_DIR)root.crt |
| 46 |
$(eval $@_CERT_FILE := $(SERVER_CRT_DIR)server) |
| 47 |
@echo Creating good client certificate $@ |
| 48 |
openssl genrsa -out $($@_CERT_FILE).key 2048 |
| 49 |
openssl req -new -sha256 -key $($@_CERT_FILE).key -passin pass:$(ROOT_CA_PASSWORD) -subj "/C=US/ST=CA/O=PgJdbc tests/CN=localhost" -out $($@_CERT_FILE).csr |
| 50 |
openssl x509 -req -in $($@_CERT_FILE).csr -CA $(SERVER_CRT_DIR)root.crt -CAkey $(SERVER_CRT_DIR)root.key -passin pass:$(ROOT_CA_PASSWORD) -CAcreateserial -out $($@_CERT_FILE).crt -days 3650 -sha256 |
| 51 |
@rm $($@_CERT_FILE).csr |
| 52 |
@echo |
| 53 |
|
| 54 |
goodclient.crt goodclient.key : $(SERVER_CRT_DIR)root.key $(SERVER_CRT_DIR)root.crt |
| 55 |
$(eval $@_CERT_FILE := goodclient) |
| 56 |
@echo Creating good client certificate $@ |
| 57 |
openssl genrsa -out $($@_CERT_FILE).key 2048 |
| 58 |
# CN=test has to match user name |
| 59 |
openssl req -new -sha256 -key $($@_CERT_FILE).key -subj "/C=US/ST=CA/O=PgJdbc tests/CN=test" -out $($@_CERT_FILE).csr |
| 60 |
openssl x509 -req -in $($@_CERT_FILE).csr -CA $(SERVER_CRT_DIR)root.crt -CAkey $(SERVER_CRT_DIR)root.key -passin pass:$(ROOT_CA_PASSWORD) -CAcreateserial -out $($@_CERT_FILE).crt -days 3650 -sha256 |
| 61 |
@rm $($@_CERT_FILE).csr |
| 62 |
@echo |
| 63 |
|
| 64 |
badclient.crt badclient.key : badroot.key badroot.crt |
| 65 |
$(eval $@_CERT_FILE := badclient) |
| 66 |
@echo Creating bad client certificate $@ |
| 67 |
openssl genrsa -out $($@_CERT_FILE).key 2048 |
| 68 |
# CN=test has to match user name |
| 69 |
openssl req -new -sha256 -key $($@_CERT_FILE).key -subj "/C=US/ST=CA/O=PgJdbc tests/CN=test" -out $($@_CERT_FILE).csr |
| 70 |
openssl x509 -req -in $($@_CERT_FILE).csr -CA badroot.crt -CAkey badroot.key -passin pass:$(ROOT_CA_PASSWORD) -CAcreateserial -out $($@_CERT_FILE).crt -days 3650 -sha256 |
| 71 |
@rm $($@_CERT_FILE).csr |
| 72 |
@echo |