Blame SOURCES/Makefile.certificate

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