1#!/bin/bash 2 3# Copyright (c) 2012 The Chromium Authors. All rights reserved. 4# Use of this source code is governed by a BSD-style license that can be 5# found in the LICENSE file. 6 7# This script generates certificates that can be used to test SSL client 8# authentication. Outputs for automated tests are stored in 9# net/data/ssl/certificates, but may be re-generated for manual testing. 10# 11# This script generates two chains of test client certificates: 12# 13# 1. A (end-entity) -> B -> C (self-signed root) 14# 2. D (end-entity) -> E -> C (self-signed root) 15# 16# In which A, B, C, D, and E all have distinct keypairs. Both client 17# certificates share the same root, but are issued by different 18# intermediates. The names of these intermediates are hardcoded within 19# unit tests, and thus should not be changed. 20 21try () { 22 echo "$@" 23 "$@" || exit 1 24} 25 26try rm -rf out 27try mkdir out 28 29echo Create the serial number files and indices. 30serial=1000 31for i in B C E 32do 33 try /bin/sh -c "echo $serial > out/$i-serial" 34 serial=$(expr $serial + 1) 35 touch out/$i-index.txt 36 touch out/$i-index.txt.attr 37done 38 39echo Generate the keys. 40for i in A B C D E 41do 42 try openssl genrsa -out out/$i.key 2048 43done 44 45echo Generate the C CSR 46COMMON_NAME="C Root CA" \ 47 CA_DIR=out \ 48 ID=C \ 49 try openssl req \ 50 -new \ 51 -key out/C.key \ 52 -out out/C.csr \ 53 -config client-certs.cnf 54 55echo C signs itself. 56COMMON_NAME="C Root CA" \ 57 CA_DIR=out \ 58 ID=C \ 59 try openssl x509 \ 60 -req -days 3650 \ 61 -in out/C.csr \ 62 -extensions ca_cert \ 63 -extfile client-certs.cnf \ 64 -signkey out/C.key \ 65 -out out/C.pem 66 67echo Generate the intermediates 68COMMON_NAME="B CA" \ 69 CA_DIR=out \ 70 ID=B \ 71 try openssl req \ 72 -new \ 73 -key out/B.key \ 74 -out out/B.csr \ 75 -config client-certs.cnf 76 77COMMON_NAME="C CA" \ 78 CA_DIR=out \ 79 ID=C \ 80 try openssl ca \ 81 -batch \ 82 -extensions ca_cert \ 83 -in out/B.csr \ 84 -out out/B.pem \ 85 -config client-certs.cnf 86 87COMMON_NAME="E CA" \ 88 CA_DIR=out \ 89 ID=E \ 90 try openssl req \ 91 -new \ 92 -key out/E.key \ 93 -out out/E.csr \ 94 -config client-certs.cnf 95 96COMMON_NAME="C CA" \ 97 CA_DIR=out \ 98 ID=C \ 99 try openssl ca \ 100 -batch \ 101 -extensions ca_cert \ 102 -in out/E.csr \ 103 -out out/E.pem \ 104 -config client-certs.cnf 105 106echo Generate the leaf certs 107for id in A D 108do 109 COMMON_NAME="Client Cert $id" \ 110 ID=$id \ 111 try openssl req \ 112 -new \ 113 -key out/$id.key \ 114 -out out/$id.csr \ 115 -config client-certs.cnf 116 # Store the private key also in PKCS#8 format. 117 try openssl pkcs8 \ 118 -topk8 -nocrypt \ 119 -in out/$id.key \ 120 -outform DER \ 121 -out out/$id.pk8 122done 123 124echo B signs A 125COMMON_NAME="B CA" \ 126 CA_DIR=out \ 127 ID=B \ 128 try openssl ca \ 129 -batch \ 130 -extensions user_cert \ 131 -in out/A.csr \ 132 -out out/A.pem \ 133 -config client-certs.cnf 134 135echo E signs D 136COMMON_NAME="E CA" \ 137 CA_DIR=out \ 138 ID=E \ 139 try openssl ca \ 140 -batch \ 141 -extensions user_cert \ 142 -in out/D.csr \ 143 -out out/D.pem \ 144 -config client-certs.cnf 145 146echo Package the client certs and private keys into PKCS12 files 147# This is done for easily importing all of the certs needed for clients. 148try /bin/sh -c "cat out/A.pem out/A.key out/B.pem out/C.pem > out/A-chain.pem" 149try /bin/sh -c "cat out/D.pem out/D.key out/E.pem out/C.pem > out/D-chain.pem" 150 151try openssl pkcs12 \ 152 -in out/A-chain.pem \ 153 -out client_1.p12 \ 154 -export \ 155 -passout pass:chrome 156 157try openssl pkcs12 \ 158 -in out/D-chain.pem \ 159 -out client_2.p12 \ 160 -export \ 161 -passout pass:chrome 162 163echo Package the client certs for unit tests 164try cp out/A.pem ../certificates/client_1.pem 165try cp out/A.key ../certificates/client_1.key 166try cp out/A.pk8 ../certificates/client_1.pk8 167try cp out/B.pem ../certificates/client_1_ca.pem 168 169try cp out/D.pem ../certificates/client_2.pem 170try cp out/D.key ../certificates/client_2.key 171try cp out/D.pk8 ../certificates/client_2.pk8 172try cp out/E.pem ../certificates/client_2_ca.pem 173