Blame SOURCES/Makefile.certificate

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