summaryrefslogtreecommitdiff
path: root/certs/makewinrtcerts.py
blob: 60a42ae36f0de6f21c82a03de44acef1d9140e3a (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
#!/usr/bin/env python
# **********************************************************************
#
# Copyright (c) 2003-2014 ZeroC, Inc. All rights reserved.
#
# This copy of Ice is licensed to you under the terms described in the
# ICE_LICENSE file included in this distribution.
#
# **********************************************************************

import os, sys, shutil, glob, socket

def usage():
    print("usage: " + sys.argv[0] + " [ip address]")
    sys.exit(1)

debug = True
ipAddress = None
if len(sys.argv) == 2:
    ipAddress = sys.argv[1]

if not ipAddress:
    try:
        ipAddress = socket.gethostbyname(socket.gethostname())
    except:
        ipAddress = "127.0.0.1"

cwd = os.getcwd()
if not os.path.exists("makewinrtcerts.py") or os.path.basename(cwd) != "certs":
    print("You must run this script from the certs demo directory")
    sys.exit(1)

if os.path.exists("winrt"):
    shutil.rmtree("winrt")
os.mkdir("winrt")

os.environ["ICE_CA_HOME"] = os.path.abspath("winrt")

os.chdir("winrt")

while True:
    print("The IP address used for the server certificate will be: " + ipAddress)

    sys.stdout.write("Do you want to keep this IP address? (y/n) [y]")
    sys.stdout.flush()
    input = sys.stdin.readline().strip()
    if input == 'n':
        sys.stdout.write("IP : ")
        sys.stdout.flush()
        ipAddress = sys.stdin.readline().strip()
    else:
        break

certs = "."
caHome = os.path.join(certs, "ca")

caKey = os.path.join(certs, "cakey.pem")
caCert = os.path.join(certs, "cacert.pem")

print("Generating new CA certificate and key...")
os.mkdir(caHome)

f = open(os.path.join(caHome, "serial"), "w")
f.write("01")
f.close()

f = open(os.path.join(caHome, "index.txt"), "w")
f.truncate(0)
f.close()

#
# Static configuration file data.
#
config = {\
"ca.cnf":"\
# **********************************************************************\n\
#\n\
# Copyright (c) 2003-2014 ZeroC, Inc. All rights reserved.\n\
#\n\
# This copy of Ice is licensed to you under the terms described in the\n\
# ICE_LICENSE file included in this distribution.\n\
#\n\
# **********************************************************************\n\
\n\
# Configuration file for the CA. This file is generated by iceca init.\n\
# DO NOT EDIT!\n\
\n\
###############################################################################\n\
###  Self Signed Root Certificate\n\
###############################################################################\n\
\n\
[ ca ]\n\
default_ca = ice\n\
\n\
[ ice ]\n\
default_days     = 1825    # How long certs are valid.\n\
default_md       = md5     # The Message Digest type.\n\
preserve         = no      # Keep passed DN ordering?\n\
\n\
[ req ]\n\
default_bits        = 2048\n\
default_keyfile     = $ENV::ICE_CA_HOME/ca/cakey.pem\n\
default_md          = md5\n\
prompt              = no\n\
distinguished_name  = dn\n\
x509_extensions     = extensions\n\
\n\
[ extensions ]\n\
basicConstraints = CA:true\n\
\n\
# PKIX recommendation.\n\
subjectKeyIdentifier = hash\n\
authorityKeyIdentifier = keyid:always,issuer:always\n\
\n\
[dn]\n\
countryName            = US\n\
stateOrProvinceName    = Florida\n\
localityName           = Palm Beach Gardens\n\
organizationName       = ZeroC, Inc.\n\
organizationalUnitName = Ice\n\
commonName             = ZeroC WinRT Test CA\n\
emailAddress           = info@zeroc.com\n\
",\
"server.cnf":"\
# **********************************************************************\n\
#\n\
# Copyright (c) 2003-2014 ZeroC, Inc. All rights reserved.\n\
#\n\
# This copy of Ice is licensed to you under the terms described in the\n\
# ICE_LICENSE file included in this distribution.\n\
#\n\
# **********************************************************************\n\
\n\
# Configuration file to sign a certificate. This file is generated by iceca init.\n\
# DO NOT EDIT!!\n\
\n\
[ ca ]\n\
default_ca = ice\n\
\n\
[ ice ]\n\
dir              = $ENV::ICE_CA_HOME/ca # Where everything is kept.\n\
private_key      = $dir/cakey.pem   # The CA Private Key.\n\
certificate      = $dir/cacert.pem  # The CA Certificate.\n\
database         = $dir/index.txt           # Database index file.\n\
new_certs_dir    = $dir                     # Default loc for new certs.\n\
serial           = $dir/serial              # The current serial number.\n\
certs            = $dir                     # Where issued certs are kept.\n\
RANDFILE         = $dir/.rand               # Private random number file.\n\
default_days     = 1825                     # How long certs are valid.\n\
default_md       = md5                      # The Message Digest type.\n\
preserve         = yes                      # Keep passed DN ordering?\n\
\n\
policy           = ca_policy\n\
x509_extensions  = certificate_extensions\n\
\n\
[ certificate_extensions ]\n\
basicConstraints = CA:false\n\
\n\
# PKIX recommendation.\n\
subjectKeyIdentifier = hash\n\
authorityKeyIdentifier = keyid:always,issuer:always\n\
subjectAltName         = DNS:%s, IP:%s\n\
\n\
[ ca_policy ]\n\
countryName            = match\n\
stateOrProvinceName    = match\n\
organizationName       = match\n\
organizationalUnitName = optional\n\
emailAddress           = optional\n\
commonName             = supplied\n\
\n\
[ req ]\n\
default_bits        = 1024\n\
default_md          = md5\n\
prompt              = no\n\
distinguished_name  = dn\n\
x509_extensions     = extensions\n\
\n\
[ extensions ]\n\
basicConstraints = CA:false\n\
\n\
# PKIX recommendation.\n\
subjectKeyIdentifier = hash\n\
authorityKeyIdentifier = keyid:always,issuer:always\n\
keyUsage = nonRepudiation, digitalSignature, keyEncipherment\n\
\n\
[dn]\n\
countryName            = US\n\
stateOrProvinceName    = Florida\n\
localityName           = Palm Beach Gardens\n\
organizationName       = ZeroC, Inc.\n\
organizationalUnitName = Ice\n\
commonName             = %s\n\
emailAddress           = info@zeroc.com\n\
" % (ipAddress, ipAddress, ipAddress) }

sys.stdout.write("Generating configuration files... ")

for file in ["ca.cnf", "server.cnf"]:
    sys.stdout.write(" " + file)
    sys.stdout.flush()
    cnf = open(os.path.join(caHome, file), "w")
    cnf.write(config[file])
    cnf.close()
    print("")

config = os.path.join(caHome, "ca.cnf")
cmd = "openssl req -config " + config + " -x509 -days 1825 -newkey rsa:1024 -out " + \
    os.path.join(caHome, "cacert.pem") + " -outform PEM -nodes"
if debug:
    print("[debug]", cmd)
os.system(cmd)
shutil.copyfile(os.path.join(caHome, "cakey.pem"), caKey)
shutil.copyfile(os.path.join(caHome, "cacert.pem"), caCert)

cmd = "openssl x509 -in " + caCert + " -outform DER -out " + os.path.join(certs, "cacert.der")
os.system(cmd)

#
# C++ server RSA certificate and key.
#
cppServerCert = os.path.join(certs, "s_rsa1024_pub.pem")
cppServerKey = os.path.join(certs, "s_rsa1024_priv.pem")
print("Generating new C++ server RSA certificate and key...")

if os.path.exists(cppServerCert):
    os.remove(cppServerCert)
if os.path.exists(cppServerKey):
    os.remove(cppServerKey)
    
serial = os.path.join(caHome, "serial")
f = open(serial, "r")
serialNum = f.read().strip()
f.close()

tmpKey = os.path.join(caHome, serialNum + "_key.pem")
tmpCert = os.path.join(caHome, serialNum + "_cert.pem")
req = os.path.join(caHome, "req.pem")
config = os.path.join(caHome, "server.cnf")
cmd = "openssl req -config " + config + " -newkey rsa:1024 -nodes -keyout " + tmpKey + " -keyform PEM" + \
    " -out " + req
if debug:
    print("[debug]", cmd)
os.system(cmd)

cmd = "openssl ca -config " + config + " -batch -in " + req
if debug:
    print("[debug]", cmd)
os.system(cmd)
shutil.move(os.path.join(caHome, serialNum + ".pem"), tmpCert)
shutil.copyfile(tmpKey, cppServerKey)
shutil.copyfile(tmpCert, cppServerCert)
os.remove(req)

#
# .NET server RSA certificate and key.
#
csServer = os.path.join(certs, "s_rsa1024.pfx")
print("Generating new .NET server RSA certificate and key...")
cmd = "openssl pkcs12 -in " + cppServerCert + " -inkey " + cppServerKey + " -export -out " + csServer + \
    " -certpbe PBE-SHA1-RC4-40 -keypbe PBE-SHA1-RC4-40 -passout pass:password"
if debug:
    print("[debug]", cmd)
os.system(cmd)

#
# Done.
#
print("Done.")
os.chdir("..")