Blame SOURCES/Makefile.certificate

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