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