11PASSWORD =changeit
22KEYSTORE =keystore.jks
33HOSTNAME =localhost
4+ CLIENTNAME =cid
45# CN = Common Name
56# OU = Organization Unit
67# O = Organization Name
@@ -11,14 +12,14 @@ HOSTNAME=localhost
1112DNAME_CA='CN =Baeldung CA,OU=baeldung.com,O=Baeldung,L=SomeCity,ST=SomeState,C=CC'
1213# For server certificates, the Common Name (CN) must be the hostname
1314DNAME_HOST='CN=$(HOSTNAME),OU=baeldung.com,O=Baeldung,L=SomeCity,ST=SomeState,C =CC'
15+ DNAME_CLIENT='CN=$(CLIENTNAME),OU=baeldung.com,O=Baeldung,L=SomeCity,ST=SomeState,C =CC'
1416TRUSTSTORE =truststore.jks
15- CLIENTNAME =cid
1617
1718all : clean create-keystore add-host create-truststore add-client
1819
1920create-keystore :
2021 # Generate a certificate authority (CA)
21- keytool -genkey -alias ca \
22+ keytool -genkey -alias ca -ext BC=ca:true \
2223 -keyalg RSA -keysize 4096 -sigalg SHA512withRSA -keypass $(PASSWORD ) \
2324 -validity 3650 -dname $(DNAME_CA ) \
2425 -keystore $(KEYSTORE ) -storepass $(PASSWORD )
@@ -30,7 +31,7 @@ add-host:
3031 -validity 3650 -dname $(DNAME_HOST ) \
3132 -keystore $(KEYSTORE ) -storepass $(PASSWORD )
3233 # Generate a host certificate signing request
33- keytool -certreq -alias $(HOSTNAME ) \
34+ keytool -certreq -alias $(HOSTNAME ) -ext BC=ca:true \
3435 -keyalg RSA -keysize 4096 -sigalg SHA512withRSA \
3536 -validity 3650 -file " $( HOSTNAME) .csr" \
3637 -keystore $(KEYSTORE ) -storepass $(PASSWORD )
@@ -44,21 +45,25 @@ add-host:
4445 -file " $( HOSTNAME) .crt" \
4546 -keystore $(KEYSTORE ) -storepass $(PASSWORD )
4647
47- create-truststore :
48- # Export certificate authority into truststore
49- keytool -export -alias ca -file ca.crt \
48+ export-authority :
49+ # Export certificate authority
50+ keytool -export -alias ca -file ca.crt -rfc \
5051 -keystore $(KEYSTORE ) -storepass $(PASSWORD )
52+
53+
54+ create-truststore : export-authority
55+ # Import certificate authority into a new truststore
5156 keytool -import -trustcacerts -noprompt -alias ca -file ca.crt \
5257 -keystore $(TRUSTSTORE ) -storepass $(PASSWORD )
5358
5459add-client :
5560 # Generate client certificate
5661 keytool -genkey -alias $(CLIENTNAME ) \
5762 -keyalg RSA -keysize 4096 -sigalg SHA512withRSA -keypass $(PASSWORD ) \
58- -validity 3650 -dname $(DNAME_HOST ) \
63+ -validity 3650 -dname $(DNAME_CLIENT ) \
5964 -keystore $(TRUSTSTORE ) -storepass $(PASSWORD )
6065 # Generate a host certificate signing request
61- keytool -certreq -alias $(CLIENTNAME ) \
66+ keytool -certreq -alias $(CLIENTNAME ) -ext BC=ca:true \
6267 -keyalg RSA -keysize 4096 -sigalg SHA512withRSA \
6368 -validity 3650 -file " $( CLIENTNAME) .csr" \
6469 -keystore $(TRUSTSTORE ) -storepass $(PASSWORD )
@@ -73,4 +78,5 @@ add-client:
7378 -keystore $(TRUSTSTORE ) -storepass $(PASSWORD )
7479
7580clean :
76- rm -f $(KEYSTORE ) * .csr * .crt $(TRUSTSTORE )
81+ # Remove generated artifacts
82+ find . ! -name Makefile -type f -exec rm -f {} \;
0 commit comments