Blame SOURCES/Makefile.certificate

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