1 /**
  2  *  ---------
  3  * |.##> <##.|  Open Smart Card Development Platform (www.openscdp.org)
  4  * |#       #|  
  5  * |#       #|  Copyright (c) 1999-2009 CardContact Software & System Consulting
  6  * |'##> <##'|  Andreas Schwier, 32429 Minden, Germany (www.cardcontact.de)
  7  *  --------- 
  8  *
  9  *  This file is part of OpenSCDP.
 10  *
 11  *  OpenSCDP is free software; you can redistribute it and/or modify
 12  *  it under the terms of the GNU General Public License version 2 as
 13  *  published by the Free Software Foundation.
 14  *
 15  *  OpenSCDP is distributed in the hope that it will be useful,
 16  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 18  *  GNU General Public License for more details.
 19  *
 20  *  You should have received a copy of the GNU General Public License
 21  *  along with OpenSCDP; if not, write to the Free Software
 22  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 23  *
 24  * @fileoverview Generate Document Signer key pair and save to GP profile
 25  */
 26 
 27  
 28 load("tools/x509certificategenerator.js");
 29 
 30 
 31 /**
 32  * Write key profile
 33  * 
 34  * @param {String} filename the absolute file name to write the file to
 35  * @param {XML} xml the structure to write 
 36  */
 37 function writeXML(filename, xml) {
 38 	print("Writing " + filename + "...");
 39 	var fw = new java.io.FileWriter(filename);
 40 	fw.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
 41 	fw.write(xml.toXMLString());
 42 	fw.close();
 43 }
 44 
 45 
 46 
 47 /**
 48  * Generate an ECC key pair on brainpoolP256r1 and save as GP key profile
 49  * 
 50  * @param name the name of the key
 51  */
 52 function generateECCKeyPair(name) {
 53 	var curve = new ByteString("brainpoolP256r1", OID);
 54 	var keysize = 256;
 55 
 56 	var pubKey = new Key();
 57 	pubKey.setType(Key.PUBLIC);
 58 	pubKey.setComponent(Key.ECC_CURVE_OID, curve);
 59 
 60 	var priKey = new Key();
 61 	priKey.setType(Key.PRIVATE);
 62 	priKey.setComponent(Key.ECC_CURVE_OID, curve);
 63 
 64 	var crypto = new Crypto();
 65 	crypto.generateKeyPair(Crypto.EC, pubKey, priKey);
 66 
 67 	var gp = new Namespace("http://namespaces.globalplatform.org/systems-profiles/1.1.0");
 68 
 69 	var priKeyXML = 
 70 		<gp:KeyProfile xmlns:gp={gp} UniqueID="2B0601040181C31F100006" ProfileVersion="1.1.0" ErrataVersion="0">
 71 			<gp:Description>{"PrK_" + name + " ECDSA Private Key"}</gp:Description>
 72 			<gp:Revisions arrayElement="Revision" arrayIndex="#">
 73 				<gp:Revision Version="1.0.0" Date="2011-11-11" Time="00:00:00" By="www.smartcard-hsm.org" Digest="00000000"/>
 74 			</gp:Revisions>
 75 			<gp:KeyInfo Name="ECPrivate" Type="PRIVATE" SubType="EC" Size={keysize} Mode="TEST"/>
 76 			<gp:Attribute Sensitive="false" Importable="true" Exportable="true"/>
 77 			<gp:Usage Encrypt="true" Decrypt="true" DecryptEncrypt="true" Sign="true" Verify="true" Wrap="true" Unwrap="true" UnwrapWrap="true" Derive="true"/>
 78 			<gp:Value Format="ECPRIVATE" arrayElement="Component" arrayIndex="#">
 79 				<gp:Component Name="ECC_CURVE_OID" Encoding="HEX" Value={curve.toString(HEX)}></gp:Component>
 80 				<gp:Component Name="ECC_D" Encoding="HEX" Value={priKey.getComponent(Key.ECC_D).toString(HEX)}></gp:Component>
 81 			</gp:Value>
 82 		</gp:KeyProfile>
 83 		
 84 	var pubKeyXML =
 85 		<gp:KeyProfile xmlns:gp={gp} UniqueID="2B0601040181C31F100008" ProfileVersion="1.1.0" ErrataVersion="0">
 86 			<gp:Description>{"PuK_" + name + " ECDSA Public Key"}</gp:Description>
 87 			<gp:Revisions arrayElement="Revision" arrayIndex="#">
 88 				<gp:Revision Version="1.0.0" Date="2011-11-11" Time="00:00:00" By="www.smartcard-hsm.org" Digest="00000000"/>
 89 			</gp:Revisions>
 90 			<gp:KeyInfo Name="ECPublic" Type="PUBLIC" SubType="EC" Size={keysize} Mode="TEST"/>
 91 			<gp:Attribute Sensitive="false" Importable="true" Exportable="true"/>
 92 			<gp:Usage Encrypt="true" Decrypt="true" DecryptEncrypt="true" Sign="true" Verify="true" Wrap="true" Unwrap="true" UnwrapWrap="true" Derive="true"/>
 93 			<gp:Value Format="ECPUBLIC" arrayElement="Component" arrayIndex="#">
 94 				<gp:Component Name="ECC_CURVE_OID" Encoding="HEX" Value={curve.toString(HEX)}></gp:Component>
 95 				<gp:Component Name="ECC_QX" Encoding="HEX" Value={pubKey.getComponent(Key.ECC_QX).toString(HEX)}></gp:Component>
 96 				<gp:Component Name="ECC_QY" Encoding="HEX" Value={pubKey.getComponent(Key.ECC_QY).toString(HEX)}></gp:Component>
 97 			</gp:Value>
 98 		<