Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Developer
Kiran_Snist
Posts: 174
Registered: ‎01-25-2011
My Device: Not Specified

Help required in adding ssl certificate

Hi,

 

I have been trying to add a ssl certificate in my application but i m not been successful, I m getting a certificate verification exception while running the application. i have used the foll code in the mainscreen to install the certificate

		
		X509Certificate ox509Certificate;

		try {
		    // Get an input stream for the certificate in a resource file
		    InputStream rs = getClass().getResourceAsStream("/inblrm14.pem");
		    
		    // PEM format is Base64 encoded
		    Base64InputStream b64is = new Base64InputStream(rs);

		    // Create the X509 certificate
		    ox509Certificate = new X509Certificate(b64is);

		    // Clean up.
		    b64is.close();
		    rs.close();

		    ox509Certificate.verify(ox509Certificate.getPublicKey());
		    System.out.println(Integer.toHexString(ox509Certificate.hashCode()));
		    // Add the certificate to the DeviceKeyStore
		    KeyStore ks = DeviceKeyStore.getInstance();
		  //check if certificate is not already in the DeviceKeyStore
		   if(!ks.isMember(ox509Certificate))
		    ks.set(null, ox509Certificate.getSubjectFriendlyName(), ox509Certificate, ox509Certificate.getStatus(),
		    		ks.getTicket());
		} catch (CertificateException ce) {
			Util.errorDialog(ce.toString());
		} catch (CryptoException crypt) {
			Util.errorDialog(crypt.toString());
		} catch (IOException ioe) {
			Util.errorDialog(ioe.toString());
		}
		
	

 I m getting that error when i m using verify method. 

 

Moreover i tried adding the certificate in android(to check the certificate correctness) n i got positive response. 

So, I wonder whether i m following the right procedure or not... can any one guide me through the process of adding the ssl certificate to the bb device

Please use plain text.
Contributor
kannappan88
Posts: 40
Registered: ‎10-10-2011
My Device: BB Curve&Bold

Re: Help required in adding ssl certificate

[ Edited ]
 
addCertToDeviceKeyStore(ReadFile("/ca.pem"));
 private Certificate ReadFile(String path) {
		try {
			InputStream is = getClass().getResourceAsStream(path);
			byte[] data = new byte[is.available()];
			is.read(data, 0, is.available());
			Certificate c = CertificateUtilities.readCertificateFile(null, data);		
			return c;
		} catch (Exception ioe) {
			ioe.printStackTrace();
		}
		return null;
	}

	public void addCertToDeviceKeyStore(Certificate certificate) {
		KeyStore keyStore = DeviceKeyStore.getInstance();
		// check if certificate is not already in the DeviceKeyStore
		if (!keyStore.isMember(certificate)) {
			try {
				String SFN = certificate.getSubjectFriendlyName();
				CertificateStatus CS = certificate.getStatus();
				keyStore.set(null, SFN, certificate, CS, keyStore.getTicket());
				
			} catch (Exception e) {
				
			}
		} else {
			System.out.println("Added certificate to Device Keystore");
			
		}		 
	}

 Try this code. 

Please use plain text.
Developer
Kiran_Snist
Posts: 174
Registered: ‎01-25-2011
My Device: Not Specified

Re: Help required in adding ssl certificate

Thanks a ton.. I had some progress using ur code snippet. but now the prob is that after the apps hits that part of code, I m asked to enter the key store password but i m not able to enter the password, my device got hanged. I m not able to move any further... Could u plz help me out in this issue... Thanks in advance!!

Please use plain text.