-
Notifications
You must be signed in to change notification settings - Fork 2
/
gendocsignerkeys.js
136 lines (111 loc) · 5.21 KB
/
gendocsignerkeys.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
/**
* ---------
* |.##> <##.| 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 Document Signer key pair 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 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);
var x = new X509CertificateGenerator(this.crypto);
x.reset();
x.setSerialNumber(new ByteString("01", HEX));
x.setSignatureAlgorithm(Crypto.ECDSA_SHA256);
var issuer = { C:"DE", O:"CardContact", CN:"CardContact Document Signer" };
x.setIssuer(issuer);
x.setNotBefore("111111111111Z");
x.setNotAfter( "411111111111Z");
var subject = issuer;
x.setSubject(subject);
x.setPublicKey(pubKey);
x.addKeyUsageExtension( X509CertificateGenerator.digitalSignature );
x.addSubjectKeyIdentifierExtension();
var cert = x.generateX509Certificate(priKey);
cert.verifyWith(cert);
var filename = GPSystem.mapFilename("C_" + name + ".cer", GPSystem.CWD);
print("Writing " + filename);
var file = new java.io.FileOutputStream(filename);
file.write(cert.getBytes());
file.close();
}
generateECCKeyPair("DocSigner");