-
Notifications
You must be signed in to change notification settings - Fork 2
/
gentestkeys.js
184 lines (153 loc) · 7.92 KB
/
gentestkeys.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
/**
* ---------
* |.##> <##.| 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 Generate a set of test keys and save to GP profile
*/
X509CertificateGenerator = require('scsh/x509/X509CertificateGenerator').X509CertificateGenerator;
/**
* Write key profile
*
* @param {String} filename the absolute file name to write the file to
* @param {XML} xml the structure to write
*/
function writeXML(filename, xml) {
print("Writing " + filename + "...");
var fw = new java.io.FileWriter(filename);
fw.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
fw.write(xml.toXMLString());
fw.close();
}
/**
* Generate an ECC key pair on brainpoolP256r1 and save as GP key profile
*
* @param {String} name the name of the key
*/
function generateECCKeyPair(name) {
var curve = new ByteString("brainpoolP256r1", OID);
var keysize = 256;
var pubKey = new Key();
pubKey.setType(Key.PUBLIC);
pubKey.setComponent(Key.ECC_CURVE_OID, curve);
var priKey = new Key();
priKey.setType(Key.PRIVATE);
priKey.setComponent(Key.ECC_CURVE_OID, curve);
var crypto = new Crypto();
crypto.generateKeyPair(Crypto.EC, pubKey, priKey);
var gp = new Namespace("http://namespaces.globalplatform.org/systems-profiles/1.1.0");
var priKeyXML =
<gp:KeyProfile xmlns:gp={gp} UniqueID="2B0601040181C31F100006" ProfileVersion="1.1.0" ErrataVersion="0">
<gp:Description>{"PrK_" + name + " ECDSA Private Key"}</gp:Description>
<gp:Revisions arrayElement="Revision" arrayIndex="#">
<gp:Revision Version="1.0.0" Date="2011-11-11" Time="00:00:00" By="www.smartcard-hsm.org" Digest="00000000"/>
</gp:Revisions>
<gp:KeyInfo Name="ECPrivate" Type="PRIVATE" SubType="EC" Size={keysize} Mode="TEST"/>
<gp:Attribute Sensitive="false" Importable="true" Exportable="true"/>
<gp:Usage Encrypt="true" Decrypt="true" DecryptEncrypt="true" Sign="true" Verify="true" Wrap="true" Unwrap="true" UnwrapWrap="true" Derive="true"/>
<gp:Value Format="ECPRIVATE" arrayElement="Component" arrayIndex="#">
<gp:Component Name="ECC_CURVE_OID" Encoding="HEX" Value={curve.toString(HEX)}></gp:Component>
<gp:Component Name="ECC_D" Encoding="HEX" Value={priKey.getComponent(Key.ECC_D).toString(HEX)}></gp:Component>
</gp:Value>
</gp:KeyProfile>
var pubKeyXML =
<gp:KeyProfile xmlns:gp={gp} UniqueID="2B0601040181C31F100008" ProfileVersion="1.1.0" ErrataVersion="0">
<gp:Description>{"PuK_" + name + " ECDSA Public Key"}</gp:Description>
<gp:Revisions arrayElement="Revision" arrayIndex="#">
<gp:Revision Version="1.0.0" Date="2011-11-11" Time="00:00:00" By="www.smartcard-hsm.org" Digest="00000000"/>
</gp:Revisions>
<gp:KeyInfo Name="ECPublic" Type="PUBLIC" SubType="EC" Size={keysize} Mode="TEST"/>
<gp:Attribute Sensitive="false" Importable="true" Exportable="true"/>
<gp:Usage Encrypt="true" Decrypt="true" DecryptEncrypt="true" Sign="true" Verify="true" Wrap="true" Unwrap="true" UnwrapWrap="true" Derive="true"/>
<gp:Value Format="ECPUBLIC" arrayElement="Component" arrayIndex="#">
<gp:Component Name="ECC_CURVE_OID" Encoding="HEX" Value={curve.toString(HEX)}></gp:Component>
<gp:Component Name="ECC_QX" Encoding="HEX" Value={pubKey.getComponent(Key.ECC_QX).toString(HEX)}></gp:Component>
<gp:Component Name="ECC_QY" Encoding="HEX" Value={pubKey.getComponent(Key.ECC_QY).toString(HEX)}></gp:Component>
</gp:Value>
</gp:KeyProfile>
var fname = GPSystem.mapFilename("kp_prk_" + name + ".xml", GPSystem.CWD);
writeXML(fname, priKeyXML);
var fname = GPSystem.mapFilename("kp_puk_" + name + ".xml", GPSystem.CWD);
writeXML(fname, pubKeyXML);
}
/**
* Generate a sector key pair with the sector public key as PrK_Sector * PuK_Revocation
*
* @param {String} name the name of the key
*/
function generateSectorKeyPair(name) {
var curve = new ByteString("brainpoolP256r1", OID);
var keysize = 256;
var pubKey = new Key();
pubKey.setType(Key.PUBLIC);
pubKey.setComponent(Key.ECC_CURVE_OID, curve);
var priKey = new Key();
priKey.setType(Key.PRIVATE);
priKey.setComponent(Key.ECC_CURVE_OID, curve);
var crypto = new Crypto();
crypto.generateKeyPair(Crypto.EC, pubKey, priKey);
var revocationKey = new Key("kp_puk_RevocationKey.xml");
var inp = revocationKey.getComponent(Key.ECC_QX).concat(revocationKey.getComponent(Key.ECC_QY));
var puk = crypto.decrypt(priKey, Crypto.ECDHP, inp);
var len = priKey.getComponent(Key.ECC_P).length;
pubKey.setComponent(Key.ECC_QX, puk.left(len));
pubKey.setComponent(Key.ECC_QY, puk.right(len));
var gp = new Namespace("http://namespaces.globalplatform.org/systems-profiles/1.1.0");
var priKeyXML =
<gp:KeyProfile xmlns:gp={gp} UniqueID="2B0601040181C31F100006" ProfileVersion="1.1.0" ErrataVersion="0">
<gp:Description>{"PrK_" + name + " ECDSA Private Key"}</gp:Description>
<gp:Revisions arrayElement="Revision" arrayIndex="#">
<gp:Revision Version="1.0.0" Date="2011-11-11" Time="00:00:00" By="www.smartcard-hsm.org" Digest="00000000"/>
</gp:Revisions>
<gp:KeyInfo Name="ECPrivate" Type="PRIVATE" SubType="EC" Size={keysize} Mode="TEST"/>
<gp:Attribute Sensitive="false" Importable="true" Exportable="true"/>
<gp:Usage Encrypt="true" Decrypt="true" DecryptEncrypt="true" Sign="true" Verify="true" Wrap="true" Unwrap="true" UnwrapWrap="true" Derive="true"/>
<gp:Value Format="ECPRIVATE" arrayElement="Component" arrayIndex="#">
<gp:Component Name="ECC_CURVE_OID" Encoding="HEX" Value={curve.toString(HEX)}></gp:Component>
<gp:Component Name="ECC_D" Encoding="HEX" Value={priKey.getComponent(Key.ECC_D).toString(HEX)}></gp:Component>
</gp:Value>
</gp:KeyProfile>
var pubKeyXML =
<gp:KeyProfile xmlns:gp={gp} UniqueID="2B0601040181C31F100008" ProfileVersion="1.1.0" ErrataVersion="0">
<gp:Description>{"PuK_" + name + " ECDSA Public Key"}</gp:Description>
<gp:Revisions arrayElement="Revision" arrayIndex="#">
<gp:Revision Version="1.0.0" Date="2011-11-11" Time="00:00:00" By="www.smartcard-hsm.org" Digest="00000000"/>
</gp:Revisions>
<gp:KeyInfo Name="ECPublic" Type="PUBLIC" SubType="EC" Size={keysize} Mode="TEST"/>
<gp:Attribute Sensitive="false" Importable="true" Exportable="true"/>
<gp:Usage Encrypt="true" Decrypt="true" DecryptEncrypt="true" Sign="true" Verify="true" Wrap="true" Unwrap="true" UnwrapWrap="true" Derive="true"/>
<gp:Value Format="ECPUBLIC" arrayElement="Component" arrayIndex="#">
<gp:Component Name="ECC_CURVE_OID" Encoding="HEX" Value={curve.toString(HEX)}></gp:Component>
<gp:Component Name="ECC_QX" Encoding="HEX" Value={pubKey.getComponent(Key.ECC_QX).toString(HEX)}></gp:Component>
<gp:Component Name="ECC_QY" Encoding="HEX" Value={pubKey.getComponent(Key.ECC_QY).toString(HEX)}></gp:Component>
</gp:Value>
</gp:KeyProfile>
var fname = GPSystem.mapFilename("kp_prk_" + name + ".xml", GPSystem.CWD);
writeXML(fname, priKeyXML);
var fname = GPSystem.mapFilename("kp_puk_" + name + ".xml", GPSystem.CWD);
writeXML(fname, pubKeyXML);
}
generateECCKeyPair("GroupCAKey");
generateECCKeyPair("UniqueCAKey");
generateECCKeyPair("RevocationKey");
generateSectorKeyPair("SectorKey1");
generateSectorKeyPair("SectorKey2");
generateECCKeyPair("IDKey");