1 /**
  2  *  ---------
  3  * |.##> <##.|  Open Smart Card Development Platform (www.openscdp.org)
  4  * |#       #|  
  5  * |#       #|  Copyright (c) 1999-2010 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 Example black list generator
 25  */
 26 
 27 load("BlackListGenerator.js");
 28 
 29 load("tools/x509certificategenerator.js");
 30 
 31 
 32 /*
 33  * Write a byte string object to file
 34  *
 35  * The filename is mapped to the location of the script
 36  *
 37  * name		Name of file
 38  * content	ByteString content for file
 39  *
 40  */
 41 function writeFileOnDisk(name, content) {
 42 
 43 	print("Writing " + filename);
 44 
 45 	var file = new java.io.FileOutputStream(filename);
 46 	file.write(content);
 47 	file.close();
 48 }
 49 
 50 
 51 
 52 // Create the crypto object
 53 var crypto = new Crypto();
 54 
 55 // Generate an asymmetric 2048 bit key pair and a self signed certificate for Alice
 56 print("Generating key pair and self-signed certificate for Alice...\n");
 57 
 58 var privKeyA = new Key();
 59 privKeyA.setType(Key.PRIVATE);
 60 
 61 var pubKeyA = new Key();
 62 pubKeyA.setType(Key.PUBLIC);
 63 pubKeyA.setSize(2048);
 64 	
 65 crypto.generateKeyPair(Crypto.RSA, pubKeyA, privKeyA);
 66 	
 67 var x = new X509CertificateGenerator(crypto);
 68 
 69 x.reset();
 70 x.setSerialNumber(new ByteString("01", HEX));
 71 x.setSignatureAlgorithm(Crypto.RSA);
 72 var issuer = { C:"UT", O:"ACME Corporation", CN:"Test-CA" };
 73 x.setIssuer(issuer);
 74 x.setNotBefore("060825120000Z");
 75 x.setNotAfter("160825120000Z");
 76 var subject = { C:"UT", O:"Utopia CA", OU:"ACME Corporation", CN:"Alice" };
 77 x.setSubject(subject);
 78 x.setPublicKey(pubKeyA);
 79 x.addKeyUsageExtension(	X509CertificateGenerator.digitalSignature |
 80 							X509CertificateGenerator.keyCertSign |
 81 							X509CertificateGenerator.cRLSign );
 82 							
 83 x.addBasicConstraintsExtension(true, 0);
 84 x.addSubjectKeyIdentifierExtension();
 85 x.addAuthorityKeyIdentifierExtension(pubKeyA);
 86 
 87 var certA = x.generateX509Certificate(privKeyA);
 88 
 89 
 90 // Define how many elements should be added to the added/removed lists
 91 var numberOfEntries = 10;
 92 print("Creating list for " + numberOfEntries + " entries\n");
 93 
 94 // Generate black list with added items
 95 generator = new BlackListGenerator();
 96 
 97 // Set black list version
 98 var version = new ByteString("00", HEX);
 99 generator.setVersion(version);
100 
101 // Set black list type
102 generator.setType(BlackListGenerator.ADDED_LIST);
103 var listID = new ByteString("01", HEX); 
104 generator.setListID(listID);
105 
106 // Define some random value for the sector ID
107 var sector_A = crypto.generateRandom(32);
108 
109 var sectorSpecificIDs_A = new Array();
110 
111 // Create sector specific entries at random and add them to the list
112 for (var i = 0; i < numberOfEntries; i++) {
113 	sectorSpecificIDs_A[i] = crypto.generateRandom(32);
114 }
115 
116 // Add the complete details to the list
117 generator.addBlackListDetails(sector_A, sectorSpecificIDs_A);
118 
119 
120 // Create a second sector ID at random
121 var sector_B = crypto.generateRandom(32);
122 
123 var sectorSpecificIDs_B = new Array();
124 
125 // Create entries to the added list
126 for (var i = 0; i < numberOfEntries; i++) {
127 	sectorSpecificIDs_B[i] = crypto.generateRandom(32);
128 }
129 
130 generator.addBlackListDetails(sector_B, sectorSpecificIDs_B);
131 
132 var blackList = generator.generateBlackList();
133 var bl_added = new ASN1(blackList);
134 print(bl_added);
135 print("Total bytes: " + blackList.length);
136 
137 // Construct and create the CMS signed data object
138 var cmsGenerator = new CMSGenerator(CMSGenerator.TYPE_SIGNED_DATA);
139 cmsGenerator.setDataContent(blackList);
140 cmsGenerator.addSigner(privKeyA, certA, new ByteString("id-sha1", OID), true);
141 
142 var contentOID = new ByteString("0.4.0.127.0.7.3.2.2", OID);
143 var cms = cmsGenerator.generate(contentOID);
144 
145 // Map filename
146 var filename = GPSystem.mapFilename("blacklist.bin", GPSystem.USR);
147 
148 writeFileOnDisk(filename, cms);
149