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