Blame SOURCES/Makefile.certificate

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