Add a certificate generator makefile/config

Update XOS core certificates

Change-Id: I1e4cda425704e724b9b494609f2e7777424c1951
diff --git a/scripts/pki/.gitignore b/scripts/pki/.gitignore
new file mode 100644
index 0000000..6ec615a
--- /dev/null
+++ b/scripts/pki/.gitignore
@@ -0,0 +1,5 @@
+*.pem
+*.key
+*.csr
+root_ca
+helm_xos_pki.yaml
diff --git a/scripts/pki/Makefile b/scripts/pki/Makefile
new file mode 100644
index 0000000..a21f042
--- /dev/null
+++ b/scripts/pki/Makefile
@@ -0,0 +1,110 @@
+# Copyright 2017-present Open Networking Foundation
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# XOS pki makefile
+# Configuration is also given in xos-pki.cnf
+
+SHELL = bash -eu -o pipefail
+
+# parameters
+KEY_SIZE         ?= 2048
+EXPIRATION_DAYS  ?= 366
+OPENSSL_CNF      ?= xos-pki.cnf
+
+# utility/validation targets
+
+help:
+	@echo "Usually you want to run 'make helm_xos_pki.yaml'"
+
+validate:
+	 openssl verify -verbose -purpose sslserver -CAfile xos-CA.pem xos-core.crt
+
+printca: xos-CA.pem
+	openssl x509 -in $< -text -noout
+
+printkey: xos-core.key
+	openssl rsa -in $< -check
+
+printcsr: xos-core.csr
+	openssl req -in $< -text -noout -verify
+
+printpem: xos-core.pem
+	openssl x509 -in $< -text -noout
+
+all_certs: xos-core.pem
+
+helm_xos_pki.yaml: xos-CA.pem xos-core.pem xos-core.key
+	@echo "Creating helm compatible YAML file containing certs"
+	@echo "---" > $@
+	@echo "# Certificates can be regenerated with scripts/pki/Makefile" >> $@
+	@echo "# Created on: `date -u`, good for $(EXPIRATION_DAYS) days" >> $@
+	@echo "ca_cert_chain: |" >> $@
+	@cat xos-CA.pem | base64 -b 72 | sed 's/^/  /' >> $@
+	@echo "secrets:" >> $@
+	@echo "  core_api_cert: |" >> $@
+	@cat xos-core.pem | base64 -b 72 | sed 's/^/    /' >> $@
+	@echo "  core_api_key: |" >> $@
+	@cat xos-core.key | base64 -b 72 | sed 's/^/    /' >> $@
+
+clean:
+	rm -rf root_ca *.pem *.key *.csr helm_xos_pki.yaml
+
+# CA creation
+root_ca:
+	mkdir -p root_ca/private root_ca/newcerts
+	chmod 700 root_ca/private
+	echo 1000 > root_ca/serial
+	touch root_ca/index.txt
+
+root_ca/private/ca_root_phrase: root_ca
+	@echo "TestingXOSRootCAPassPhrase" > root_ca/private/ca_root_phrase
+
+root_ca/private/ca_key.pem: root_ca root_ca/private/ca_root_phrase
+	@echo "## Creating CA private key, $@"
+	openssl genrsa -aes256 \
+	  -passout file:root_ca/private/ca_root_phrase \
+	  -out root_ca/private/ca_key.pem $(KEY_SIZE)
+
+xos-CA.pem: xos-pki.cnf root_ca/private/ca_key.pem
+	@echo "## Creating self-signed CA public key: $@"
+	openssl req -config $(OPENSSL_CNF) \
+	  -new -x509 -days $(EXPIRATION_DAYS) -sha256 \
+	  -extensions v3_ca \
+	  -key root_ca/private/ca_key.pem \
+	  -passin file:root_ca/private/ca_root_phrase \
+	  -subj "/C=US/ST=California/L=Menlo Park/O=ONF/OU=Testing Only/CN=CORD Test Root CA" \
+	  -out $@
+
+# cert creation
+.PRECIOUS: %.key %.csr # don't delete intermediate files
+
+%.key:
+	@echo "## Creating server private key: $@"
+	openssl genrsa -out $@ $(KEY_SIZE)
+
+%.csr: %.key $(OPENSSL_CNF)
+	@echo "## Creating signing request $@ from $<"
+	openssl req -config $(OPENSSL_CNF) \
+	  -new -sha256 -key $< \
+	  -subj "/C=US/ST=California/L=Menlo Park/O=ONF/OU=Testing Only/CN=$*" \
+	  -out $@
+
+%.pem: %.csr xos-CA.pem $(OPENSSL_CNF)
+	@echo "## Signing voltha.csr to create signed public key: voltha.crt"
+	openssl ca -config $(OPENSSL_CNF) \
+	  -batch -days $(EXPIRATION_DAYS) -md sha256 \
+	  -passin file:root_ca/private/ca_root_phrase \
+	  -extensions $* \
+	  -in $< \
+	  -out $@
diff --git a/scripts/pki/README.md b/scripts/pki/README.md
new file mode 100644
index 0000000..302b3e1
--- /dev/null
+++ b/scripts/pki/README.md
@@ -0,0 +1,7 @@
+# XOS certificate generation
+
+Run `make` on a system with the `openssl` cli tool installed to see options.
+
+Most likely you'll want to run `make helm_xos_pki.yaml` to generate a helm
+values file with base64 encoded certificates in it.
+
diff --git a/scripts/pki/xos-pki.cnf b/scripts/pki/xos-pki.cnf
new file mode 100644
index 0000000..5349054
--- /dev/null
+++ b/scripts/pki/xos-pki.cnf
@@ -0,0 +1,88 @@
+# Copyright 2017-present Open Networking Foundation
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+[ ca ]
+default_ca  = CA_default
+
+[ CA_default ]
+dir               = ./root_ca
+certs             = $dir/certs
+crl_dir           = $dir/crl
+new_certs_dir     = $dir/newcerts
+database          = $dir/index.txt
+serial            = $dir/serial
+
+private_key       = $dir/private/ca_key.pem
+certificate       = xos-CA.pem
+
+# Make new requests easier to sign - allow two subjects with same name
+# (Or revoke the old certificate first.)
+unique_subject    = no
+preserve          = no
+
+# for CA that signs client certs
+policy            = policy_loose
+
+[ policy_loose ]
+# Allow the to sign more types of certs
+countryName             = optional
+stateOrProvinceName     = optional
+localityName            = optional
+organizationName        = optional
+organizationalUnitName  = optional
+commonName              = supplied
+emailAddress            = optional
+
+[ req ]
+default_bits         = 2048
+default_days         = 366
+default_md           = sha256
+distinguished_name   = req_distinguished_name
+string_mask          = utf8only
+x509_extensions      = v3_ca
+
+[ req_distinguished_name ]
+# See <https://en.wikipedia.org/wiki/Certificate_signing_request>.
+countryName                     = Country Name (2 letter code)
+stateOrProvinceName             = State or Province Name
+localityName                    = Locality Name
+0.organizationName              = Organization Name
+organizationalUnitName          = Organizational Unit Name
+commonName                      = Common Name
+emailAddress                    = Email Address
+
+# Defaults DN
+countryName_default             = US
+stateOrProvinceName_default     = California
+localityName_default            = Menlo Park
+0.organizationName_default      = ONF
+organizationalUnitName_default  = Testing Only
+commonName                      = CORD Testing
+emailAddress_default            = do-not-reply@opencord.org
+
+[ v3_ca ]
+# Extensions for a typical CA (`man x509v3_config`).
+subjectKeyIdentifier = hash
+authorityKeyIdentifier = keyid:always,issuer
+basicConstraints = critical, CA:TRUE
+keyUsage = critical, digitalSignature, cRLSign, keyCertSign
+
+# Extensions for certificates (`man x509v3_config`).
+[ xos-core ]
+subjectKeyIdentifier = hash
+authorityKeyIdentifier = keyid,issuer:always
+basicConstraints = CA:FALSE
+keyUsage = critical, digitalSignature, keyEncipherment
+extendedKeyUsage = serverAuth
+subjectAltName = 'DNS:xos-core, DNS:xos-core.default, DNS:xos-core.default.svc.cluster.local'