Created
January 13, 2017 15:27
-
-
Save jorpic/182aebc8eaeebc269dab4e5b52ff3971 to your computer and use it in GitHub Desktop.
TLS certificate generation scripts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env bash | |
| # based on https://jamielinux.com/docs/openssl-certificate-authority/index.html | |
| # TODO: check if root | |
| set -e | |
| ROOT_STORE=/etc/ssl/caroperator-svc/root | |
| ROOT_KEY=$ROOT_STORE/private/ca.key | |
| ROOT_CERT=$ROOT_STORE/certs/ca.cert | |
| IMDT_STORE=/etc/ssl/caroperator-svc/intermediate | |
| IMDT_KEY=$IMDT_STORE/private/intermediate.key | |
| IMDT_CERT=$IMDT_STORE/certs/intermediate.cert | |
| IMDT_CSR=$ROOT_STORE/csr/intermedite.csr | |
| CA_CHAIN=$IMDT_STORE/certs/ca-chain.cert | |
| case $1 in | |
| init-ca) | |
| echo === Prepare the root directory | |
| mkdir -p $ROOT_STORE/{certs,crl,csr,newcerts,private} | |
| chmod 700 $ROOT_STORE/private | |
| touch $ROOT_STORE/index.txt | |
| echo 1000 > $ROOT_STORE/serial | |
| echo === Create the root key | |
| openssl genrsa -out $ROOT_KEY 4096 | |
| chmod 400 $ROOT_KEY | |
| echo === Create the root certificate | |
| openssl req -config conf/openssl-root.conf \ | |
| -new -x509 -days 7300 -extensions v3_ca \ | |
| -key $ROOT_KEY \ | |
| -out $ROOT_CERT | |
| chmod 444 $ROOT_CERT | |
| echo === Verify the root certificate | |
| openssl x509 -noout -text -in $ROOT_CERT | |
| echo === Prepare the intermediate directory | |
| mkdir -p $IMDT_STORE/{certs,crl,csr,newcerts,private} | |
| chmod 700 $IMDT_STORE/private | |
| touch $IMDT_STORE/index.txt | |
| echo 1000 > $IMDT_STORE/serial | |
| echo 1000 > $IMDT_STORE/crlnumber | |
| echo === Create the intermediate key | |
| openssl genrsa -out $IMDT_KEY 4096 | |
| chmod 400 $IMDT_KEY | |
| echo === Create the intermediate certificate | |
| openssl req -config conf/openssl-intermediate.conf \ | |
| -new \ | |
| -key $IMDT_KEY \ | |
| -out $IMDT_CSR | |
| openssl ca -config conf/openssl-root.conf \ | |
| -extensions v3_intermediate_ca \ | |
| -days 3650 -notext -md sha256 \ | |
| -in $IMDT_CSR \ | |
| -out $IMDT_CERT | |
| chmod 444 $IMDT_CERT | |
| echo === Verify the intermediate certificate | |
| openssl verify -CAfile $ROOT_CERT $IMDT_CERT | |
| echo === Create the certificate chain file | |
| cat $IMDT_CERT $ROOT_CERT > $CA_CHAIN | |
| chmod 444 $CA_CHAIN | |
| ;; | |
| gen-csr) | |
| CLIENT_NAME=$2 | |
| KEY=$IMDT_STORE/private/$CLIENT_NAME.key | |
| CSR=$IMDT_STORE/csr/$CLIENT_NAME.csr | |
| openssl genrsa -out $KEY 2048 | |
| chmod 400 $KEY | |
| openssl req -config conf/openssl-intermediate.conf \ | |
| -key $KEY \ | |
| -new -sha256 -out $CSR | |
| ;; | |
| sign-user-cert) | |
| CLIENT_NAME=$2 | |
| CSR=$IMDT_STORE/csr/$CLIENT_NAME.csr | |
| CERT=$IMDT_STORE/certs/$CLIENT_NAME.cert | |
| openssl ca -config conf/openssl-intermediate.conf \ | |
| -extensions usr_cert -days 375 -notext -md sha256 \ | |
| -in $CSR \ | |
| -out $CERT | |
| chmod 444 $CERT | |
| ;; | |
| *) | |
| echo "Usage:" | |
| echo " init-ca" | |
| echo " gen-csr <client-name>" | |
| echo " sign-user-cert <client-name>" | |
| exit 1 | |
| ;; | |
| esac |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| [ ca ] | |
| default_ca = CA_default | |
| [ CA_default ] | |
| # Directory and file locations. | |
| dir = /etc/ssl/caroperator-svc/intermediate | |
| certs = $dir/certs | |
| crl_dir = $dir/crl | |
| new_certs_dir = $dir/newcerts | |
| database = $dir/index.txt | |
| serial = $dir/serial | |
| RANDFILE = $dir/private/.rand | |
| # The root key and root certificate. | |
| private_key = $dir/private/intermediate.key | |
| certificate = $dir/certs/intermediate.cert | |
| # For certificate revocation lists. | |
| crlnumber = $dir/crlnumber | |
| crl = $dir/crl/intermediate.crl | |
| crl_extensions = crl_ext | |
| default_crl_days = 30 | |
| # SHA-1 is deprecated, so use SHA-2 instead. | |
| default_md = sha256 | |
| name_opt = ca_default | |
| cert_opt = ca_default | |
| default_days = 375 | |
| preserve = no | |
| policy = policy_loose | |
| [ policy_loose ] | |
| # Allow the intermediate CA to sign a more diverse range of certificates. | |
| # See the POLICY FORMAT section of the `ca` man page. | |
| countryName = optional | |
| stateOrProvinceName = optional | |
| localityName = optional | |
| organizationName = optional | |
| organizationalUnitName = optional | |
| commonName = supplied | |
| emailAddress = optional | |
| [ req ] | |
| # Options for the `req` tool (`man req`). | |
| default_bits = 2048 | |
| distinguished_name = req_distinguished_name | |
| string_mask = utf8only | |
| # SHA-1 is deprecated, so use SHA-2 instead. | |
| default_md = sha256 | |
| # Extension to add when the -x509 option is used. | |
| 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 | |
| countryName_default = RU | |
| stateOrProvinceName_default = Russian Federation | |
| localityName_default = | |
| 0.organizationName_default = RAMC | |
| organizationalUnitName_default = RAMC/CaRMa Caroperator CA | |
| commonName_default = RAMC/CaRMa Caroperator Intermediate CA | |
| emailAddress_default = [email protected] | |
| [ 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 | |
| [ v3_intermediate_ca ] | |
| # Extensions for a typical intermediate CA (`man x509v3_config`). | |
| subjectKeyIdentifier = hash | |
| authorityKeyIdentifier = keyid:always,issuer | |
| basicConstraints = critical, CA:true, pathlen:0 | |
| keyUsage = critical, digitalSignature, cRLSign, keyCertSign | |
| [ usr_cert ] | |
| # Extensions for client certificates (`man x509v3_config`). | |
| basicConstraints = CA:FALSE | |
| nsCertType = client, email | |
| nsComment = "OpenSSL Generated Client Certificate" | |
| subjectKeyIdentifier = hash | |
| authorityKeyIdentifier = keyid,issuer | |
| keyUsage = critical, nonRepudiation, digitalSignature, keyEncipherment | |
| extendedKeyUsage = clientAuth, emailProtection | |
| [ server_cert ] | |
| # Extensions for server certificates (`man x509v3_config`). | |
| basicConstraints = CA:FALSE | |
| nsCertType = server | |
| nsComment = "OpenSSL Generated Server Certificate" | |
| subjectKeyIdentifier = hash | |
| authorityKeyIdentifier = keyid,issuer:always | |
| keyUsage = critical, digitalSignature, keyEncipherment | |
| extendedKeyUsage = serverAuth | |
| [ crl_ext ] | |
| # Extension for CRLs (`man x509v3_config`). | |
| authorityKeyIdentifier=keyid:always | |
| [ ocsp ] | |
| # Extension for OCSP signing certificates (`man ocsp`). | |
| basicConstraints = CA:FALSE | |
| subjectKeyIdentifier = hash | |
| authorityKeyIdentifier = keyid,issuer | |
| keyUsage = critical, digitalSignature | |
| extendedKeyUsage = critical, OCSPSigning |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| [ ca ] | |
| default_ca = CA_default | |
| [ CA_default ] | |
| # Directory and file locations. | |
| dir = /etc/ssl/caroperator-svc/root | |
| certs = $dir/certs | |
| crl_dir = $dir/crl | |
| new_certs_dir = $dir/newcerts | |
| database = $dir/index.txt | |
| serial = $dir/serial | |
| RANDFILE = $dir/private/.rand | |
| # The root key and root certificate. | |
| private_key = $dir/private/ca.key | |
| certificate = $dir/certs/ca.cert | |
| # For certificate revocation lists. | |
| crlnumber = $dir/crlnumber | |
| crl = $dir/crl/ca.crl | |
| crl_extensions = crl_ext | |
| default_crl_days = 30 | |
| # SHA-1 is deprecated, so use SHA-2 instead. | |
| default_md = sha256 | |
| name_opt = ca_default | |
| cert_opt = ca_default | |
| default_days = 375 | |
| preserve = no | |
| policy = policy_strict | |
| [ policy_strict ] | |
| # The root CA should only sign intermediate certificates that match. | |
| # See the POLICY FORMAT section of `man ca`. | |
| countryName = match | |
| stateOrProvinceName = match | |
| organizationName = match | |
| organizationalUnitName = optional | |
| commonName = supplied | |
| emailAddress = optional | |
| [ req ] | |
| # Options for the `req` tool (`man req`). | |
| default_bits = 2048 | |
| distinguished_name = req_distinguished_name | |
| string_mask = utf8only | |
| # SHA-1 is deprecated, so use SHA-2 instead. | |
| default_md = sha256 | |
| # Extension to add when the -x509 option is used. | |
| 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 | |
| countryName_default = RU | |
| stateOrProvinceName_default = Russian Federation | |
| localityName_default = | |
| 0.organizationName_default = RAMC | |
| organizationalUnitName_default = RAMC/CaRMa Caroperator CA | |
| commonName_default = RAMC/CaRMa Caroperator Root CA | |
| emailAddress_default = [email protected] | |
| [ 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 | |
| [ v3_intermediate_ca ] | |
| # Extensions for a typical intermediate CA (`man x509v3_config`). | |
| subjectKeyIdentifier = hash | |
| authorityKeyIdentifier = keyid:always,issuer | |
| basicConstraints = critical, CA:true, pathlen:0 | |
| keyUsage = critical, digitalSignature, cRLSign, keyCertSign | |
| [ crl_ext ] | |
| # Extension for CRLs (`man x509v3_config`). | |
| authorityKeyIdentifier=keyid:always | |
| [ ocsp ] | |
| # Extension for OCSP signing certificates (`man ocsp`). | |
| basicConstraints = CA:FALSE | |
| subjectKeyIdentifier = hash | |
| authorityKeyIdentifier = keyid,issuer | |
| keyUsage = critical, digitalSignature | |
| extendedKeyUsage = critical, OCSPSigning |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment