e809e7
UTF8 := $(shell locale -c LC_CTYPE -k | grep -q charmap.*UTF-8 && echo -utf8)
abe623
DAYS=365
abe623
KEYLEN=2048
abe623
TYPE=rsa:$(KEYLEN)
6536aa
EXTRA_FLAGS=
6536aa
ifdef SERIAL
6536aa
	EXTRA_FLAGS+=-set_serial $(SERIAL)
6536aa
endif
e809e7
cvsdist e21e18
.PHONY: usage
cvsdist e21e18
.SUFFIXES: .key .csr .crt .pem
cvsdist e21e18
.PRECIOUS: %.key %.csr %.crt %.pem
cvsdist e21e18
cvsdist e21e18
usage:
cvsdist e21e18
	@echo "This makefile allows you to create:"
cvsdist e21e18
	@echo "  o public/private key pairs"
cvsdist e21e18
	@echo "  o SSL certificate signing requests (CSRs)"
cvsdist e21e18
	@echo "  o self-signed SSL test certificates"
cvsdist e21e18
	@echo
cvsdist e21e18
	@echo "To create a key pair, run \"make SOMETHING.key\"."
cvsdist e21e18
	@echo "To create a CSR, run \"make SOMETHING.csr\"."
cvsdist e21e18
	@echo "To create a test certificate, run \"make SOMETHING.crt\"."
cvsdist e21e18
	@echo "To create a key and a test certificate in one file, run \"make SOMETHING.pem\"."
cvsdist e21e18
	@echo
cvsdist e21e18
	@echo "To create a key for use with Apache, run \"make genkey\"."
cvsdist e21e18
	@echo "To create a CSR for use with Apache, run \"make certreq\"."
cvsdist e21e18
	@echo "To create a test certificate for use with Apache, run \"make testcert\"."
cvsdist e21e18
	@echo
6536aa
	@echo "To create a test certificate with serial number other than random, add SERIAL=num"
abe623
	@echo "You can also specify key length with KEYLEN=n and expiration in days with DAYS=n"
6536aa
	@echo "Any additional options can be passed to openssl req via EXTRA_FLAGS"
c9c6ff
	@echo
cvsdist e21e18
	@echo Examples:
cvsdist e21e18
	@echo "  make server.key"
cvsdist e21e18
	@echo "  make server.csr"
cvsdist e21e18
	@echo "  make server.crt"
cvsdist e21e18
	@echo "  make stunnel.pem"
cvsdist e21e18
	@echo "  make genkey"
cvsdist e21e18
	@echo "  make certreq"
cvsdist e21e18
	@echo "  make testcert"
c9c6ff
	@echo "  make server.crt SERIAL=1"
6536aa
	@echo "  make stunnel.pem EXTRA_FLAGS=-sha384"
6536aa
	@echo "  make testcert DAYS=600"
cvsdist e21e18
cvsdist e21e18
%.pem:
cvsdist e21e18
	umask 77 ; \
cvsdist e21e18
	PEM1=`/bin/mktemp /tmp/openssl.XXXXXX` ; \
cvsdist e21e18
	PEM2=`/bin/mktemp /tmp/openssl.XXXXXX` ; \
6536aa
	/usr/bin/openssl req $(UTF8) -newkey $(TYPE) -keyout $$PEM1 -nodes -x509 -days $(DAYS) -out $$PEM2 $(EXTRA_FLAGS) ; \
cvsdist e21e18
	cat $$PEM1 >  $@ ; \
cvsdist e21e18
	echo ""    >> $@ ; \
cvsdist e21e18
	cat $$PEM2 >> $@ ; \
cvsdist e21e18
	$(RM) $$PEM1 $$PEM2
cvsdist e21e18
cvsdist e21e18
%.key:
cvsdist e21e18
	umask 77 ; \
abe623
	/usr/bin/openssl genrsa -aes128 $(KEYLEN) > $@
cvsdist e21e18
cvsdist e21e18
%.csr: %.key
cvsdist e21e18
	umask 77 ; \
e809e7
	/usr/bin/openssl req $(UTF8) -new -key $^ -out $@
cvsdist e21e18
cvsdist e21e18
%.crt: %.key
cvsdist e21e18
	umask 77 ; \
6536aa
	/usr/bin/openssl req $(UTF8) -new -key $^ -x509 -days $(DAYS) -out $@ $(EXTRA_FLAGS)
cvsdist e21e18
4e6a92
TLSROOT=/etc/pki/tls
4e6a92
KEY=$(TLSROOT)/private/localhost.key
4e6a92
CSR=$(TLSROOT)/certs/localhost.csr
4e6a92
CRT=$(TLSROOT)/certs/localhost.crt
cvsdist e21e18
cvsdist e21e18
genkey: $(KEY)
cvsdist e21e18
certreq: $(CSR)
cvsdist e21e18
testcert: $(CRT)
cvsdist e21e18
cvsdist e21e18
$(CSR): $(KEY)
cvsdist e21e18
	umask 77 ; \
e809e7
	/usr/bin/openssl req $(UTF8) -new -key $(KEY) -out $(CSR)
cvsdist e21e18
cvsdist e21e18
$(CRT): $(KEY)
cvsdist e21e18
	umask 77 ; \
6536aa
	/usr/bin/openssl req $(UTF8) -new -key $(KEY) -x509 -days $(DAYS) -out $(CRT) $(EXTRA_FLAGS)