Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 2nd, 2012  |  syntax: None  |  size: 1.93 KB  |  hits: 22  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Install SSL Certificate with BlackBerry App
  2. X509Certificate _x509;
  3.  
  4. try {
  5.     // Get an input stream for the certificate in a resource file
  6.     InputStream rs = getClass().getResourceAsStream("/certificate.pem");
  7.  
  8.     // PEM format is Base64 encoded
  9.     Base64InputStream b64is = new Base64InputStream(rs);
  10.  
  11.     // Create the X509 certificate
  12.     _x509 = new X509Certificate(b64is);
  13.  
  14.     // Clean up.
  15.     b64is.close();
  16.     rs.close();
  17.  
  18.     // if the certificate is self signed this will perform a
  19.     // verfication check. For non-self signed certificates
  20.     // one could provide the signer's certificate in another
  21.     // resource file and validate it with that public key. Other
  22.     // versions of verify will verify it with a certificate in
  23.     // a keystore, but then we wouldn't need to do all this.
  24.     _x509.verify(_x509.getPublicKey());
  25.     System.out.println(_x509.getSubjectFriendlyName());
  26.     System.out.println(Integer.toHexString(_x509.hashCode()));
  27.  
  28.     // Add the certificate to the DeviceKeyStore
  29.     KeyStore ks = DeviceKeyStore.getInstance();
  30.  
  31.     // Associated data is set to null, but can be used if there is associated
  32.     // data known. You can use _x509.getStatus() instead of encoding the GOOD
  33.     // constant, but if the device can not find a revokation or validation list
  34.     // it will set the status to UNKNOWN which will confuse users. ks.getTicket()
  35.     // will prompt the user for permission for the program to access the key store.
  36.     // This may also cause the system to ask the user to set a password, unfortunately
  37.     // I can't remember, but I don't think it will if there is no private key in the
  38.     // certificate.
  39.     ks.set(null, _x509.getSubjectFriendlyName(), _x509, CertificateStatus.GOOD,
  40.        ks.getTicket() );
  41. } catch (CertificateException ce) {
  42.     System.out.println(ce.toString());
  43. } catch (CryptoException crypt) {
  44.     System.out.println(crypt);
  45. } catch (IOException ioe) {
  46.     System.out.println(ioe.toString());
  47. }