36
36
import java .util .zip .ZipEntry ;
37
37
import java .util .zip .ZipInputStream ;
38
38
import java .util .Base64 ;
39
+
40
+ // PKCS7 encoding classes appear to be available only for internal use
41
+ // https://github.com/processing/processing-android/issues/496#issuecomment-449693146
39
42
import sun .security .pkcs .SignerInfo ;
40
43
import sun .security .x509 .AlgorithmId ;
41
44
import sun .security .x509 .X500Name ;
42
45
import sun .security .pkcs .PKCS7 ;
43
46
import sun .security .pkcs .ContentInfo ;
44
47
48
+ // Possible replacement using Bouncy Castle
49
+ //import org.spongycastle.asn1.x509.X509Name;
50
+ //import org.spongycastle.jce.X509Principal;
51
+ //import org.spongycastle.jce.provider.BouncyCastleProvider;
52
+ //import org.spongycastle.x509.X509V3CertificateGenerator;
53
+
54
+
45
55
/**
46
56
* Created by ibziy_000 on 17.08.2014.
47
57
*/
@@ -287,4 +297,54 @@ public int size() {
287
297
return count ;
288
298
}
289
299
}
300
+
301
+ /*
302
+ // key generation using Bouncy Castle from APDE
303
+ protected void writeKey(File keystoreFile, char[] keystorePassword, String alias, char[] password, int validity, String name, String orgUnit, String org, String city, String state, String country) {
304
+ try {
305
+ Security.addProvider(new BouncyCastleProvider());
306
+
307
+ KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
308
+ SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
309
+ keyGen.initialize(1024, random);
310
+ KeyPair pair = keyGen.generateKeyPair();
311
+
312
+ X509V3CertificateGenerator v3CertGen = new X509V3CertificateGenerator();
313
+
314
+ X509Principal principal = new X509Principal("CN=" + formatDN(name) + ", OU=" + formatDN(orgUnit) + ", O=" + formatDN(org)
315
+ + ", L=" + formatDN(city) + ", ST=" + formatDN(state) + ", C=" + formatDN(country));
316
+
317
+ int serial = new SecureRandom().nextInt();
318
+
319
+ v3CertGen.setSerialNumber(BigInteger.valueOf(serial < 0 ? -1 * serial : serial));
320
+ v3CertGen.setIssuerDN(principal);
321
+ v3CertGen.setNotBefore(new Date(System.currentTimeMillis()));
322
+ v3CertGen.setNotAfter(new Date(System.currentTimeMillis() + (1000L * 60 * 60 * 24 * 365 * validity))); //TODO Doesn't take leap days / years into account...
323
+ v3CertGen.setSubjectDN(principal);
324
+ v3CertGen.setPublicKey(pair.getPublic());
325
+ v3CertGen.setSignatureAlgorithm("MD5WithRSAEncryption");
326
+
327
+ X509Certificate pkCertificate = v3CertGen.generateX509Certificate(pair.getPrivate());
328
+
329
+ keystore.setKeyEntry(alias, pair.getPrivate(), password, new Certificate[] {pkCertificate});
330
+
331
+ //Write the new key to the keystore
332
+ writeKeystore(keystoreFile, keystorePassword);
333
+
334
+ //Reload the keystore so that the new key will appear
335
+ loadAliases((ArrayList<String>) loadKeystore(keystoreFile, keystorePassword).extra());
336
+ } catch (NoSuchAlgorithmException e) {
337
+ e.printStackTrace();
338
+ } catch (KeyStoreException e) {
339
+ e.printStackTrace();
340
+ } catch (InvalidKeyException e) {
341
+ e.printStackTrace();
342
+ } catch (SecurityException e) {
343
+ e.printStackTrace();
344
+ } catch (SignatureException e) {
345
+ e.printStackTrace();
346
+ }
347
+ }
348
+ */
349
+
290
350
}
0 commit comments