-
Notifications
You must be signed in to change notification settings - Fork 2
/
CVCCAGenerator.js
304 lines (235 loc) · 9.33 KB
/
CVCCAGenerator.js
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/**
* ---------
* |.##> <##.| Open Smart Card Development Platform (www.openscdp.org)
* |# #|
* |# #| Copyright (c) 1999-2018 CardContact Software & System Consulting
* |'##> <##'| Andreas Schwier, 32429 Minden, Germany (www.cardcontact.de)
* ---------
*
* This file is part of OpenSCDP.
*
* OpenSCDP is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* OpenSCDP is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with OpenSCDP; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* @fileoverview Script to generate a full reference EAC PKI
*/
CVCCA = require('scsh/eac/CVCCA').CVCCA;
PACE = require('scsh/eac/PACE').PACE;
CVCertificateStore = require('scsh/eac/CVCertificateStore').CVCertificateStore;
/**
* Generate a complete CVC PKI setup for testing purposes
*
* @param {Crypto} crypto the crypto provider to use
* @param {CVCertificateStore} certstore place to store keys and certificates
*/
function CVCCAGenerator(crypto, certstore) {
this.crypto = crypto;
this.certstore = certstore;
this.keyspec = new Key();
this.keyspec.setComponent(Key.ECC_CURVE_OID, new ByteString("brainpoolP256r1", OID));
this.taAlgorithmIdentifier = new ByteString("id-TA-ECDSA-SHA-256", OID);
this.verbose = false;
}
/**
* Log message
*
* @param {String} msg the message
*/
CVCCAGenerator.prototype.log = function(msg) {
if (this.verbose) {
GPSystem.trace(msg);
}
}
/**
* Create a CVCA at the given path and with the defined policy.
*
* <p>Calling this method a second time will create a link certificate.</p>
*
* @param {String} path a path of certificate holder names
* @param {Object} policy the certificate policy
*/
CVCCAGenerator.prototype.createCVCA = function(path, policy) {
var cvca = new CVCCA(this.crypto, this.certstore, null, null, path);
cvca.setKeySpec(this.keyspec, this.taAlgorithmIdentifier);
// Create a new request
var req = cvca.generateRequest(null, false);
this.log("Request: " + req);
this.log(req.getASN1());
assert(req.verifyWith(this.crypto, req.getPublicKey()));
var cert = cvca.generateCertificate(req, policy);
this.log("Certificate: " + cert);
this.log(cert.getASN1());
// Import certificate into store, making it the most current certificate
cvca.importCertificate(cert);
}
/**
* Create a DVCA at the given path and with the defined policy.
*
* @param {String} path a path of certificate holder names
* @param {Object} policy the certificate policy
*/
CVCCAGenerator.prototype.createDVCA = function(path, policy) {
var cvca = new CVCCA(this.crypto, this.certstore, null, null, CVCertificateStore.parentPathOf(path));
cvca.setKeySpec(this.keyspec, this.taAlgorithmIdentifier);
var dvca = new CVCCA(this.crypto, this.certstore, null, null, path);
dvca.setKeySpec(this.keyspec, this.taAlgorithmIdentifier);
// Create a new request
var req = dvca.generateRequest(null, false);
this.log("Request: " + req);
this.log(req.getASN1());
assert(req.verifyWith(this.crypto, req.getPublicKey()));
var cert = cvca.generateCertificate(req, policy);
this.log("Certificate: " + cert);
this.log(cert.getASN1());
// Import certificate into store, making it the most current certificate
dvca.importCertificate(cert);
}
/**
* Create a terminal at the given path and with the defined policy.
*
* @param {String} path a path of certificate holder names
* @param {Object} policy the certificate policy
*/
CVCCAGenerator.prototype.createTerminal = function(path, policy) {
var dvca = new CVCCA(this.crypto, this.certstore, null, null, CVCertificateStore.parentPathOf(path));
dvca.setKeySpec(this.keyspec, this.taAlgorithmIdentifier);
var term = new CVCCA(this.crypto, this.certstore, null, null, path);
term.setKeySpec(this.keyspec, this.taAlgorithmIdentifier);
// Create a new request
var req = term.generateRequest(null, false);
this.log("Request: " + req);
this.log(req.getASN1());
assert(req.verifyWith(this.crypto, req.getPublicKey()));
var cert = dvca.generateCertificate(req, policy);
this.log("Certificate: " + cert);
this.log(cert.getASN1());
// Import certificate into store, making it the most current certificate
term.importCertificate(cert);
}
CVCCAGenerator.CWD = GPSystem.mapFilename("", GPSystem.CWD);
/**
* Setup EAC PKI
*/
CVCCAGenerator.setup = function() {
var crypto = new Crypto();
var ss = new CVCertificateStore(CVCCAGenerator.CWD + "/cvc");
var g = new CVCCAGenerator(crypto, ss);
// g.verbose = true;
// Create CVCAs
var policy = { certificateValidityDays: 3650,
chatRoleOID: new ByteString("id-IS", OID),
chatRights: new ByteString("C3", HEX),
includeDomainParameter: true
};
g.createCVCA("/UTISCVCA", policy);
var policy = { certificateValidityDays: 3650,
chatRoleOID: new ByteString("id-AT", OID),
chatRights: new ByteString("FFFFFFFFFF", HEX),
includeDomainParameter: true
};
g.createCVCA("/UTATCVCA", policy);
var policy = { certificateValidityDays: 3650,
chatRoleOID: new ByteString("id-ST", OID),
chatRights: new ByteString("C3", HEX),
includeDomainParameter: true
};
g.createCVCA("/UTSTCVCA", policy);
// Create DVCAs
var policy = { certificateValidityDays: 3650,
chatRoleOID: new ByteString("id-IS", OID),
chatRights: new ByteString("83", HEX),
includeDomainParameter: false
};
g.createDVCA("/UTISCVCA/UTISDVCAOD", policy);
var policy = { certificateValidityDays: 3650,
chatRoleOID: new ByteString("id-IS", OID),
chatRights: new ByteString("43", HEX),
includeDomainParameter: false
};
g.createDVCA("/UTISCVCA/UTISDVCAOF", policy);
var policy = { certificateValidityDays: 3650,
chatRoleOID: new ByteString("id-AT", OID),
chatRights: new ByteString("BFFFFFFFFF", HEX),
includeDomainParameter: false
};
g.createDVCA("/UTATCVCA/UTATDVCAOD", policy);
var policy = { certificateValidityDays: 3650,
chatRoleOID: new ByteString("id-AT", OID),
chatRights: new ByteString("7FFFFFFFFF", HEX),
includeDomainParameter: false
};
g.createDVCA("/UTATCVCA/UTATDVCANO", policy);
var policy = { certificateValidityDays: 3650,
chatRoleOID: new ByteString("id-ST", OID),
chatRights: new ByteString("83", HEX),
includeDomainParameter: false
};
g.createDVCA("/UTSTCVCA/UTSTDVCAAB", policy);
var policy = { certificateValidityDays: 3650,
chatRoleOID: new ByteString("id-ST", OID),
chatRights: new ByteString("43", HEX),
includeDomainParameter: false
};
g.createDVCA("/UTSTCVCA/UTSTDVCACP", policy);
// Create terminals
var policy = { certificateValidityDays: 3650,
chatRoleOID: new ByteString("id-IS", OID),
chatRights: new ByteString("03", HEX),
includeDomainParameter: false
};
g.createTerminal("/UTISCVCA/UTISDVCAOD/UTTERM", policy);
var policy = { certificateValidityDays: 3650,
chatRoleOID: new ByteString("id-IS", OID),
chatRights: new ByteString("03", HEX),
includeDomainParameter: false
};
g.createTerminal("/UTISCVCA/UTISDVCAOF/UTTERM", policy);
var policy = { certificateValidityDays: 3650,
chatRoleOID: new ByteString("id-AT", OID),
chatRights: new ByteString("3FFFFFFFFF", HEX),
includeDomainParameter: false
};
g.createTerminal("/UTATCVCA/UTATDVCAOD/UTTERM", policy);
var sectorPublicKey1 = new Key(CVCCAGenerator.CWD + "/kp_puk_SectorKey1.xml");
sectorPublicKey1.setComponent(Key.ECC_CURVE_OID, sectorPublicKey1.getComponent(Key.ECC_CURVE_OID));
var encodedSectorPublicKey1 = PACE.encodePublicKey("id-RI-ECDH-SHA-256", sectorPublicKey1, true).getBytes();
var encodedSectorPublicKeyHash1 = crypto.digest(Crypto.SHA_256, encodedSectorPublicKey1);
var sectorPublicKey2 = new Key(CVCCAGenerator.CWD + "/kp_puk_SectorKey2.xml");
sectorPublicKey2.setComponent(Key.ECC_CURVE_OID, sectorPublicKey2.getComponent(Key.ECC_CURVE_OID));
var encodedSectorPublicKey2 = PACE.encodePublicKey("id-RI-ECDH-SHA-256", sectorPublicKey2, true).getBytes();
var encodedSectorPublicKeyHash2 = crypto.digest(Crypto.SHA_256, encodedSectorPublicKey2);
var sectorId = new ASN1(0x73,
new ASN1(ASN1.OBJECT_IDENTIFIER, new ByteString("id-sector", OID)),
new ASN1(0x80, encodedSectorPublicKeyHash1),
new ASN1(0x81, encodedSectorPublicKeyHash2)
);
var policy = { certificateValidityDays: 3650,
chatRoleOID: new ByteString("id-AT", OID),
chatRights: new ByteString("3FFFFFFFFF", HEX),
includeDomainParameter: false,
extensions: [ sectorId ]
};
g.createTerminal("/UTATCVCA/UTATDVCANO/UTTERM", policy);
var policy = { certificateValidityDays: 3650,
chatRoleOID: new ByteString("id-ST", OID),
chatRights: new ByteString("03", HEX),
includeDomainParameter: false
};
g.createTerminal("/UTSTCVCA/UTSTDVCAAB/UTTERM", policy);
var policy = { certificateValidityDays: 3650,
chatRoleOID: new ByteString("id-ST", OID),
chatRights: new ByteString("03", HEX),
includeDomainParameter: false
};
g.createTerminal("/UTSTCVCA/UTSTDVCACP/UTTERM", policy);
}