2017-03-02 3 views
0

J'ai généré un certificat RSA x509 utilisant le château gonflable en java. Le code est ci-dessous:Casting château gonflable X509Certificate à Java.security.cert.Certificate []

public static X509Certificate generateCert() 
{ 
    try 
    { 
     Security.addProvider(new BouncyCastleProvider()); 
     // generate a key pair 
     KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA", "BC"); 
     KeyPair keyPair = keyPairGenerator.generateKeyPair(); 

     // build a certificate generator 
     X509V3CertificateGenerator certGen = new X509V3CertificateGenerator(); 
     X500Principal dnName = new X500Principal("cn=example"); 
     // add some options 
     certGen.setSerialNumber(BigInteger.valueOf(System.currentTimeMillis())); 
     certGen.setSubjectDN(new X509Name("dc=name")); 
     certGen.setIssuerDN(dnName); // use the same 
     // yesterday 
     certGen.setNotBefore(new Date(System.currentTimeMillis() - 24 * 60 * 60 * 1000)); 
     // in 2 years 
     certGen.setNotAfter(new Date(System.currentTimeMillis() + 2 * 365 * 24 * 60 * 60 * 1000)); 
     certGen.setPublicKey(keyPair.getPublic()); 
     certGen.setSignatureAlgorithm("SHA256withRSA"); 
     certGen.addExtension(X509Extensions.ExtendedKeyUsage, true, new ExtendedKeyUsage(KeyPurposeId.id_kp_timeStamping)); 
     mCurrentRSAKeyPair = keyPair; 
     // finally, sign the certificate with the private key of the same KeyPair 
     X509Certificate cert = certGen.generate(keyPair.getPrivate(), "BC"); 

     return cert; 
    } 
    catch (Exception e) 
    { 
     e.printStackTrace(); 
     return null; 
    } 
} 

Je veux être en mesure de lancer le X509Cerificate retourné dans un certificat java.security.cert [] mais il dit qu'ils sont incompatibles. Je dois utiliser ce tableau de certificat pour un androïde keystore:

public RSA(char[] password) throws Exception 
{ 
    KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType()); 
    ks.load(null); 
    Enumeration<String> aliases = ks.aliases(); 
    if(!aliases.hasMoreElements()) 
    { 
     //mCurrentCertificate is the X509Certificate 
     mCurrentCertificate = generateCert(); 
     //Store the new keypair 
     FileInputStream fs = null; 
     ks.load(fs, password); 

     KeyStore.ProtectionParameter protParam = 
       new KeyStore.PasswordProtection(password); 

     Object cert = mCurrentCertificate.getEncoded(); 

     java.security.cert.Certificate[] myCert = (java.security.cert.Certificate[]) cert; //CAST HERE 

     KeyStore.PrivateKeyEntry pkEntry = 
       new KeyStore.PrivateKeyEntry(mCurrentRSAKeyPair.getPrivate(), 
         myCert); 

     ks.setEntry("UserKey", pkEntry, protParam); 
    } 
} 

Répondre

0

myCert est un tableau de certificats, et cert est un tableau d'octets (comme retourné par getEncoded()).

Vous devez mettre votre variable mCurrentCertificate dans un tableau:

java.security.cert.Certificate[] myCert = new java.security.cert.Certificate[] { (java.security.cert.Certificate) mCurrentCertificate}; 
// not sure if needs to cast mCurrentCertificate