public X509Certificate generateX509Certificate() {

		Date targetDate1 = new Date();
		Calendar calendar1 = Calendar.getInstance();
		calendar1.setTime(targetDate1);
		calendar1.add(Calendar.DATE, -1);

		Date targetDate2 = new Date();
		Calendar calendar2 = Calendar.getInstance();
		calendar2.setTime(targetDate2);
		calendar2.add(Calendar.YEAR, 2);

		// yesterday
		Date validityBeginDate = new Date(targetDate1.getTime());
		// in 2 years
		Date validityEndDate = new Date(targetDate2.getTime());

		Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());

		try {
			KeyPairGenerator kpGen = KeyPairGenerator.getInstance(
					AppConfigurations.RSA, AppConfigurations.PROVIDER);
			kpGen.initialize(1024, new SecureRandom());
			KeyPair pair = kpGen.generateKeyPair();

			// Generate self-signed certificate
			X500Principal principal = new X500Principal(
					"O=WSO2, OU=Mobile, C=LK");

			BigInteger serial = BigInteger.valueOf(System.currentTimeMillis());

			X509v3CertificateBuilder certGen = new JcaX509v3CertificateBuilder(
					principal, serial, validityBeginDate, validityEndDate,
					principal, pair.getPublic());
			ContentSigner sigGen;

			sigGen = new JcaContentSignerBuilder(AppConfigurations.SHA256_RSA)
					.setProvider(AppConfigurations.PROVIDER).build(
							pair.getPrivate());
			X509Certificate cert = new JcaX509CertificateConverter()
					.setProvider(AppConfigurations.PROVIDER).getCertificate(
							certGen.build(sigGen));

			// cert.checkValidity();

			cert.verify(cert.getPublicKey());

			return cert;

		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		} catch (NoSuchProviderException e) {
			e.printStackTrace();
		} catch (OperatorCreationException e) {
			e.printStackTrace();
		} catch (CertificateExpiredException e) {
			e.printStackTrace();
		} catch (CertificateNotYetValidException e) {
			e.printStackTrace();
		} catch (CertificateException e) {
			e.printStackTrace();
		} catch (InvalidKeyException e) {
			e.printStackTrace();
		} catch (SignatureException e) {
			e.printStackTrace();
		}

		return null;
	}