Examples of KeyStoreManager


Examples of org.wso2.carbon.core.util.KeyStoreManager

    try {
      if (keyStoreName == null) {
        throw new SecurityConfigException("Key Store name can't be null");
      }

      KeyStoreManager keyMan = KeyStoreManager.getInstance((UserRegistry)registry);
      KeyStore ks = keyMan.getKeyStore(keyStoreName);

      byte[] bytes = Base64.decode(certData);
      CertificateFactory factory = CertificateFactory.getInstance("X.509");
      X509Certificate cert;
            try {
                cert = (X509Certificate) factory
                    .generateCertificate(new ByteArrayInputStream(bytes));
            } catch (CertificateException e) {
                log.error(e.getMessage(), e);
                throw new SecurityConfigException("Invalid format of the provided certificate file");
            }

      if (ks.getCertificateAlias(cert) != null) {
        // We already have this certificate in the key store - ignore
        // adding it twice
        return;
      }

      // String alias = this.getAlias(cert);
      ks.setCertificateEntry(fileName, cert);

      keyMan.updateKeyStore(keyStoreName, ks);

    } catch (SecurityConfigException e) {
      throw e;
    } catch (Exception e) {
      log.error(e.getMessage(), e);
View Full Code Here

Examples of org.wso2.carbon.core.util.KeyStoreManager

    try {
      if (keyStoreName == null) {
        throw new SecurityConfigException("Key Store name can't be null");
      }

      KeyStoreManager keyMan = KeyStoreManager.getInstance((UserRegistry)registry);
      KeyStore ks = keyMan.getKeyStore(keyStoreName);

      byte[] bytes = Base64.decode(certData);
      CertificateFactory factory = CertificateFactory.getInstance("X.509");
      X509Certificate cert;
            try {
                cert = (X509Certificate) factory
                    .generateCertificate(new ByteArrayInputStream(bytes));
            } catch (Exception e) {
                throw new SecurityConfigException("Invalid format of the provided certificate file");
            }

      if (ks.getCertificateAlias(cert) != null) {
        // We already have this certificate in the key store - ignore
        // adding it twice
        return null;
      }
      alias = cert.getSubjectDN().getName();
      // String alias = this.getAlias(cert);
      ks.setCertificateEntry(alias, cert);

      keyMan.updateKeyStore(keyStoreName, ks);

      return alias;

    } catch (SecurityConfigException e) {
      throw e;
View Full Code Here

Examples of org.wso2.carbon.core.util.KeyStoreManager

    try {
      if (keyStoreName == null) {
        throw new SecurityConfigException("Key Store name can't be null");
      }

      KeyStoreManager keyMan = KeyStoreManager.getInstance((UserRegistry)registry);
      KeyStore ks = keyMan.getKeyStore(keyStoreName);

      if (ks.getCertificate(alias) == null) {
        return;
      }

      ks.deleteEntry(alias);
      keyMan.updateKeyStore(keyStoreName, ks);
    } catch (SecurityConfigException e) {
      throw e;
    } catch (Exception e) {
      log.error(e.getMessage(), e);
      throw new SecurityConfigException(e.getMessage(), e);
View Full Code Here

Examples of org.wso2.carbon.core.util.KeyStoreManager

    try {
      if (keyStoreName == null) {
        throw new Exception("keystore name cannot be null");
      }

      KeyStoreManager keyMan = KeyStoreManager.getInstance((UserRegistry)registry);
      KeyStore ks = keyMan.getKeyStore(keyStoreName);

      Enumeration<String> enm = ks.aliases();
      List<String> lst = new ArrayList<String>();
      while (enm.hasMoreElements()) {
        lst.add(enm.nextElement());
View Full Code Here

Examples of org.wso2.carbon.core.util.KeyStoreManager

      KeyStore keyStore;
      String keyStoreType;
      String privateKeyPassowrd;
      if (KeyStoreUtil.isPrimaryStore(keyStoreName)) {
        KeyStoreManager keyMan = KeyStoreManager.getInstance((UserRegistry)registry);
        keyStore = keyMan.getPrimaryKeyStore();
        ServerConfiguration serverConfig = ServerConfiguration.getInstance();
        keyStoreType = serverConfig
            .getFirstProperty(RegistryResources.SecurityManagement.SERVER_PRIMARY_KEYSTORE_TYPE);
        privateKeyPassowrd = serverConfig
            .getFirstProperty(RegistryResources.SecurityManagement.SERVER_PRIVATE_KEY_PASSWORD);
      } else {
        String path = SecurityConstants.KEY_STORES + "/" + keyStoreName;
        if (!registry.resourceExists(path)) {
          throw new SecurityConfigException("Key Store not found");
        }
        Resource resource = registry.get(path);
        KeyStoreManager manager = KeyStoreManager.getInstance((UserRegistry)registry);
        keyStore = manager.getKeyStore(keyStoreName);
        keyStoreType = resource.getProperty(SecurityConstants.PROP_TYPE);

        String encpass = resource.getProperty(SecurityConstants.PROP_PRIVATE_KEY_PASS);

        CryptoUtil util = CryptoUtil.getDefaultCryptoUtil();
View Full Code Here

Examples of org.wso2.carbon.core.util.KeyStoreManager

    try {

      for (int i = 0; i < keystores.length; i++) {
        if (KeyStoreUtil.isPrimaryStore(keystores[i].getKeyStoreName())) {
          KeyStoreManager keyMan = KeyStoreManager.getInstance((UserRegistry)registry);
          keyStore = keyMan.getPrimaryKeyStore();
          ServerConfiguration serverConfig = ServerConfiguration.getInstance();
          privateKeyPassowrd = serverConfig
              .getFirstProperty(RegistryResources.SecurityManagement.SERVER_PRIVATE_KEY_PASSWORD);
          return keyStore.getKey(alias, privateKeyPassowrd.toCharArray());
        }
View Full Code Here

Examples of org.wso2.carbon.core.util.KeyStoreManager

    private String generateKeyPair() throws Exception {

        String domainName = SAMLSSOUtil.getRealmService().getTenantManager().getDomain(registry.getTenantId());
        //load keystore
        X509V3CertificateGenerator v3CertGen = new X509V3CertificateGenerator();
        KeyStoreManager keyMan = KeyStoreManager.getInstance(null);
        KeyStore keyStore = keyMan.getKeyStore("userSSO.jks");

        //generate keypair
        KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
        keyPairGenerator.initialize(1024);
        KeyPair keyPair = keyPairGenerator.generateKeyPair();

        v3CertGen.setSerialNumber(BigInteger.valueOf(new SecureRandom().nextInt()));
        v3CertGen.setIssuerDN(new X509Principal("CN=" + domainName + ", OU=None, O=None L=None, C=None"));
        v3CertGen.setNotBefore(new Date(System.currentTimeMillis() - 1000L * 60 * 60 * 24 * 30));
        v3CertGen.setNotAfter(new Date(System.currentTimeMillis() + (1000L * 60 * 60 * 24 * 365 * 10)));
        v3CertGen.setSubjectDN(new X509Principal("CN=" + domainName + ", OU=None, O=None L=None, C=None"));
        v3CertGen.setPublicKey(keyPair.getPublic());
        v3CertGen.setSignatureAlgorithm("MD5WithRSAEncryption");
        X509Certificate PKCertificate = v3CertGen.generateX509Certificate(keyPair.getPrivate());

        //Generate the password
        SecureRandom random = new SecureRandom();
        String randString = new BigInteger(130, random).toString(12);
        String password = randString.substring(randString.length() - 10, randString.length());

        //add private key to KS
        keyStore.setKeyEntry(new Integer(registry.getTenantId()).toString(), keyPair.getPrivate(), password.toCharArray(),
                new java.security.cert.Certificate[]{PKCertificate});

        keyMan.updateKeyStore("userSSO.jks", keyStore);

        //save pub. key to a file
        String filePath = dumpPubCert(MessageContext.getCurrentMessageContext().getConfigurationContext()
                , PKCertificate.getEncoded(), null);

View Full Code Here

Examples of org.wso2.carbon.core.util.KeyStoreManager

      KeyStoreAdmin keyAdmin = new KeyStoreAdmin(IdentityTenantUtil.getRegistry(null, null));
      KeyStoreData[] keystores = keyAdmin.getKeyStores();
      if (keystores.length == 0 || keystores.length > 1) {
        throw new IdentityException("There should be only one keystore");
      }
      KeyStoreManager keyMan = KeyStoreManager.getInstance(null);
      KeyStore store = keyMan.getKeyStore(keystores[0].getKeyStoreName());
      Certificate[] certChain = store.getCertificateChain(alias);
      X509Certificate[] certs = new X509Certificate[certChain.length];
      for (int i = 0; i < certs.length; i++) {
        certs[i] = (X509Certificate) certChain[i];
      }
View Full Code Here

Examples of org.wso2.carbon.core.util.KeyStoreManager

      log.debug("Retreiving certificate to include in Mex for service " + serviceName);
    }

    try {
      KeyStoreData[] keystores = getServiceKeyStores(serviceName);
      KeyStoreManager keyMan = KeyStoreManager.getInstance(null);
      KeyStoreAdmin keyAdmin = new KeyStoreAdmin(IdentityTenantUtil.getRegistry(null, null));
      KeyStoreData privateStore = null;
      KeyStoreData keyStoreData = null;

      if (keystores != null && keystores.length > 0) {
        for (int i = 0; i < keystores.length; i++) {
          if (KeyStoreUtil.isPrimaryStore(keystores[i].getKeyStoreName())) {
            privateStore = keystores[i];
            break;
          }
        }
      }

      if (privateStore != null) {
        // policy has a private key store
        keyStoreData = keyAdmin.getKeystoreInfo(privateStore.getKeyStoreName());
        cert = getCertificate(privateStore.getKeyStoreName(), keyStoreData.getKey()
            .getAlias());
      } else {
        // this is for UT token policy
        ServerConfiguration config = ServerConfiguration.getInstance();
        String keyalias = config.getFirstProperty("Security.KeyStore.KeyAlias");
        KeyStore store = keyMan.getPrimaryKeyStore();
        cert = (X509Certificate) store.getCertificate(keyalias);
      }

    } catch (Exception e) {
      log.error("Error while retreiving certificate to include in Mex for service ", e);
View Full Code Here

Examples of org.wso2.carbon.core.util.KeyStoreManager

    if (log.isDebugEnabled()) {
      log.debug("Retreiving certificate for alias " + alias);
    }

    try {
      KeyStoreManager keyMan = KeyStoreManager.getInstance(null);
      KeyStore store = keyMan.getKeyStore(keyStoreName);
      return (X509Certificate) store.getCertificate(alias);
    } catch (Exception e) {
      log.error("Error while retreiving certificate for alias ", e);
      throw new IdentityException("Error while retreiving certificate for alias ", e);
    }
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.