Home | History | Annotate | Download | only in scripts
      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 
     21 try () {
     22   echo "$@"
     23   "$@" || exit 1
     24 }
     25 
     26 try rm -rf out
     27 try mkdir out
     28 
     29 echo Create the serial number files and indices.
     30 serial=1000
     31 for i in B C E
     32 do
     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
     37 done
     38 
     39 echo Generate the keys.
     40 for i in A B C D E
     41 do
     42   try openssl genrsa -out out/$i.key 2048
     43 done
     44 
     45 echo Generate the C CSR
     46 COMMON_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 
     55 echo C signs itself.
     56 COMMON_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 
     67 echo Generate the intermediates
     68 COMMON_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 
     77 COMMON_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 
     87 COMMON_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 
     96 COMMON_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 
    106 echo Generate the leaf certs
    107 for id in A D
    108 do
    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
    122 done
    123 
    124 echo B signs A
    125 COMMON_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 
    135 echo E signs D
    136 COMMON_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 
    146 echo Package the client certs and private keys into PKCS12 files
    147 # This is done for easily importing all of the certs needed for clients.
    148 try /bin/sh -c "cat out/A.pem out/A.key out/B.pem out/C.pem > out/A-chain.pem"
    149 try /bin/sh -c "cat out/D.pem out/D.key out/E.pem out/C.pem > out/D-chain.pem"
    150 
    151 try openssl pkcs12 \
    152   -in out/A-chain.pem \
    153   -out client_1.p12 \
    154   -export \
    155   -passout pass:chrome
    156 
    157 try openssl pkcs12 \
    158   -in out/D-chain.pem \
    159   -out client_2.p12 \
    160   -export \
    161   -passout pass:chrome
    162 
    163 echo Package the client certs for unit tests
    164 try cp out/A.pem ../certificates/client_1.pem
    165 try cp out/A.key ../certificates/client_1.key
    166 try cp out/A.pk8 ../certificates/client_1.pk8
    167 try cp out/B.pem ../certificates/client_1_ca.pem
    168 
    169 try cp out/D.pem ../certificates/client_2.pem
    170 try cp out/D.key ../certificates/client_2.key
    171 try cp out/D.pk8 ../certificates/client_2.pk8
    172 try cp out/E.pem ../certificates/client_2_ca.pem
    173