Blame SOURCES/Makefile.certificate

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