summaryrefslogtreecommitdiff
path: root/gen.sh
blob: 877eca34dc47a4454d1914d79839d8886558f88d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#!/bin/bash

# http://stackoverflow.com/questions/21297139/how-do-you-sign-certificate-signing-request-with-your-certification-authority


function create_ca() {

    cat > openssl-ca.cnf <<EOF
HOME            = .
RANDFILE        = .rnd

####################################################################

[ ca ]

default_ca = CA_default      # The default ca section

[ CA_default ]

default_days     = 3650      # how long to certify for
default_crl_days = 30        # how long before next CRL
default_md       = sha256    # use public key default MD
preserve         = no        # keep passed DN ordering

x509_extensions  = ca_extensions    # The extensions to add to the cert

email_in_dn     = no         # Don't concat the email in the DN
copy_extensions = copy       # Required to copy SANs from CSR to cert

certificate   = cacert.pem   # The CA certifcate
private_key   = cakey.pem    # The CA private key
new_certs_dir = .            # Location for new certs after signing
database      = index.txt    # Database index file
serial        = serial.txt   # The current serial number

unique_subject = no          # Set to 'no' to allow creation of
                             # several certificates with same subject.

####################################################################

[ req ]

default_bits       = 4096
default_keyfile    = cakey.pem
distinguished_name = ca_distinguished_name
x509_extensions    = ca_extensions
string_mask        = utf8only

####################################################################

[ ca_distinguished_name ]

countryName                    = Country Name (2 letter code)
countryName_default            = FR

stateOrProvinceName            = State or Province Name (full name)
stateOrProvinceName_default    = IDF

localityName                   = Locality Name (eg, city)
localityName_default           = Paris

organizationName               = Organization Name (eg, company)
organizationName_default       = Fsociety

organizationalUnitName         = Organizational Unit (eg, division)
organizationalUnitName_default = CA Research Department

commonName                     = Common Name (e.g. server FQDN or YOUR name)
commonName_default             = Test CA

emailAddress                   = Email Address
emailAddress_default           = ca@example.com

####################################################################

[ ca_extensions ]

subjectKeyIdentifier   = hash
authorityKeyIdentifier = keyid:always, issuer
basicConstraints       = critical, CA:true
keyUsage               = keyCertSign, cRLSign

[ signing_policy ]

countryName            = optional
stateOrProvinceName    = optional
localityName           = optional
organizationName       = optional
organizationalUnitName = optional
commonName             = supplied
emailAddress           = optional

####################################################################

[ signing_req ]

subjectKeyIdentifier   = hash
authorityKeyIdentifier = keyid, issuer

basicConstraints = CA:FALSE
keyUsage         = digitalSignature, keyEncipherment

EOF

    openssl req -x509 -config openssl-ca.cnf -newkey rsa:4096 -sha256 -nodes -keyout cakey.pem -out cacert.pem -outform PEM

    if [ -f cacert.pem ]; then

        ln -s cacert.pem `openssl x509 -hash -noout -in cacert.pem`.0

        #openssl x509 -purpose -in cacert.pem -inform PEM | less

        touch index.txt
        echo '01' > serial.txt

    fi

}


function create_server() {

    cat > openssl-server.cnf <<EOF
HOME            = .
RANDFILE        = .rnd

####################################################################

[ req ]

default_bits       = 2048
default_keyfile    = serverkey.pem
distinguished_name = server_distinguished_name
req_extensions     = server_req_extensions
string_mask        = utf8only

####################################################################

[ server_distinguished_name ]

countryName                    = Country Name (2 letter code)
countryName_default            = FR

stateOrProvinceName            = State or Province Name (full name)
stateOrProvinceName_default    = IDF

localityName                   = Locality Name (eg, city)
localityName_default           = Paris

organizationName               = Organization Name (eg, company)
organizationName_default       = Server Organization

organizationalUnitName         = Organizational Unit (eg, division)
organizationalUnitName_default = Server Research Department

commonName                     = Common Name (e.g. server FQDN or YOUR name)
commonName_default             = ServerSide Test

emailAddress                   = Email Address
emailAddress_default           = server@example.com

####################################################################

[ server_req_extensions ]

subjectKeyIdentifier = hash
basicConstraints     = CA:FALSE
keyUsage             = digitalSignature, keyEncipherment
nsComment            = "OpenSSL Generated Certificate"

EOF

    openssl req -config openssl-server.cnf -newkey rsa:2048 -sha256 -nodes -out serverkey.pem -outform PEM

    # openssl req -text -noout -verify -in serverkey.pem | less

    openssl ca -config openssl-ca.cnf -policy signing_policy -extensions signing_req -out servercert.pem -infiles serverkey.pem

}


function create_client() {

    cat > openssl-client.cnf <<EOF
HOME            = .
RANDFILE        = .rnd

####################################################################

[ req ]

default_bits       = 2048
default_keyfile    = clientkey.pem
distinguished_name = client_distinguished_name
req_extensions     = client_req_extensions
string_mask        = utf8only

####################################################################

[ client_distinguished_name ]

countryName                    = Country Name (2 letter code)
countryName_default            = FR

stateOrProvinceName            = State or Province Name (full name)
stateOrProvinceName_default    = IDF

localityName                   = Locality Name (eg, city)
localityName_default           = Paris

organizationName               = Organization Name (eg, company)
organizationName_default       = Client Organization

organizationalUnitName         = Organizational Unit (eg, division)
organizationalUnitName_default = Client Research Department

commonName                     = Common Name (e.g. client FQDN or YOUR name)
commonName_default             = ClientSide Test

emailAddress                   = Email Address
emailAddress_default           = client@example.com

####################################################################

[ client_req_extensions ]

subjectKeyIdentifier = hash
basicConstraints     = CA:FALSE
keyUsage             = digitalSignature, keyEncipherment
nsComment            = "OpenSSL Generated Certificate"

EOF

    openssl req -config openssl-client.cnf -newkey rsa:2048 -sha256 -nodes -out clientkey.pem -outform PEM

    # openssl req -text -noout -verify -in clientkey.pem | less

    openssl ca -config openssl-ca.cnf -policy signing_policy -extensions signing_req -out clientcert.pem -infiles clientkey.pem

}


function clean() {

    rm -f .rnd

    rm -f index.txt* serial.txt*

    rm -f *.pem *.cnf *.0

}


case $1 in

    ca)
        create_ca
        ;;

    server)
        create_server
        ;;

    client)
        create_client
        ;;

    clean)
        clean
        ;;

    *)
        echo "Usage: $0 <ca|server|client|clean>"
        exit 1
        ;;

esac