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