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