java.security.KeyStore.Entry Java Examples

The following examples show how to use java.security.KeyStore.Entry. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PicketBoxSecurityVault.java    From tomcat-vault with Apache License 2.0 6 votes vote down vote up
private void checkAndConvertKeyStoreToJCEKS(String keystoreURL) throws Exception {
    if (keystore.getType().equalsIgnoreCase("JKS")) {

        // backup original keystore file
        copyFile(new File(keystoreURL), new File(keystoreURL + ".original"));

        KeyStore jceks = KeyStoreUtil.createKeyStore("JCEKS", keyStorePWD);

        Enumeration<String> aliases = keystore.aliases();
        while (aliases.hasMoreElements()) {
            String entryAlias = aliases.nextElement();
            KeyStore.PasswordProtection p = new KeyStore.PasswordProtection(keyStorePWD);
            KeyStore.Entry e = keystore.getEntry(entryAlias, p);
            jceks.setEntry(entryAlias, e, p);
        }
        keystore = jceks;
        keyStoreType = "JCEKS"; // after conversion we have to change keyStoreType to the one we really have
        saveKeyStoreToFile(keystoreURL);
        log.info(sm.getString("picketBoxSecurityVault.keyStoreConvertedToJCEKS", KEYSTORE_URL));
    }
}
 
Example #2
Source File: KeyStoreMaterialsProvider.java    From aws-dynamodb-encryption-java with Apache License 2.0 6 votes vote down vote up
public CurrentMaterials(Entry encryptionEntry, Entry signingEntry) {
    super();
    this.encryptionEntry = encryptionEntry;
    this.signingEntry = signingEntry;

    if (encryptionEntry instanceof SecretKeyEntry) {
        if (signingEntry instanceof SecretKeyEntry) {
            this.symRawMaterials = new SymmetricRawMaterials(
                    ((SecretKeyEntry) encryptionEntry).getSecretKey(),
                    ((SecretKeyEntry) signingEntry).getSecretKey(),
                    description);
        } else {
            this.symRawMaterials = new SymmetricRawMaterials(
                    ((SecretKeyEntry) encryptionEntry).getSecretKey(),
                    entry2Pair(signingEntry),
                    description);
        }
    } else {
        this.symRawMaterials = null;
    }
}
 
Example #3
Source File: KeyStoreMaterialsProvider.java    From aws-dynamodb-encryption-java with Apache License 2.0 6 votes vote down vote up
private static KeyPair entry2Pair(Entry entry) {
    PublicKey pub = null;
    PrivateKey priv = null;

    if (entry instanceof PrivateKeyEntry) {
        PrivateKeyEntry pk = (PrivateKeyEntry) entry;
        if (pk.getCertificate() != null) {
            pub = pk.getCertificate().getPublicKey();
        }
        priv = pk.getPrivateKey();
    } else if (entry instanceof TrustedCertificateEntry) {
        TrustedCertificateEntry tc = (TrustedCertificateEntry) entry;
        pub = tc.getTrustedCertificate().getPublicKey();
    } else {
        throw new IllegalArgumentException(
                "Only entry types PrivateKeyEntry and TrustedCertificateEntry are supported.");
    }
    return new KeyPair(pub, priv);
}
 
Example #4
Source File: KeyStoreMaterialsProvider.java    From aws-dynamodb-encryption-java with Apache License 2.0 6 votes vote down vote up
public CurrentMaterials(Entry encryptionEntry, Entry signingEntry) {
    super();
    this.encryptionEntry = encryptionEntry;
    this.signingEntry = signingEntry;

    if (encryptionEntry instanceof SecretKeyEntry) {
        if (signingEntry instanceof SecretKeyEntry) {
            this.symRawMaterials = new SymmetricRawMaterials(
                    ((SecretKeyEntry) encryptionEntry).getSecretKey(),
                    ((SecretKeyEntry) signingEntry).getSecretKey(),
                    description);
        } else {
            this.symRawMaterials = new SymmetricRawMaterials(
                    ((SecretKeyEntry) encryptionEntry).getSecretKey(),
                    entry2Pair(signingEntry),
                    description);
        }
    } else {
        this.symRawMaterials = null;
    }
}
 
Example #5
Source File: KeyStoreMaterialsProvider.java    From aws-dynamodb-encryption-java with Apache License 2.0 6 votes vote down vote up
private static KeyPair entry2Pair(Entry entry) {
    PublicKey pub = null;
    PrivateKey priv = null;

    if (entry instanceof PrivateKeyEntry) {
        PrivateKeyEntry pk = (PrivateKeyEntry) entry;
        if (pk.getCertificate() != null) {
            pub = pk.getCertificate().getPublicKey();
        }
        priv = pk.getPrivateKey();
    } else if (entry instanceof TrustedCertificateEntry) {
        TrustedCertificateEntry tc = (TrustedCertificateEntry) entry;
        pub = tc.getTrustedCertificate().getPublicKey();
    } else {
        throw new IllegalArgumentException(
                "Only entry types PrivateKeyEntry and TrustedCertificateEntry are supported.");
    }
    return new KeyPair(pub, priv);
}
 
Example #6
Source File: AbstractKeyStoreTokenConnection.java    From dss with GNU Lesser General Public License v2.1 6 votes vote down vote up
private DSSPrivateKeyEntry getDSSPrivateKeyEntry(KeyStore keyStore, String alias, PasswordProtection passwordProtection) {
	try {
		if (keyStore.isKeyEntry(alias)) {
			final Entry entry = keyStore.getEntry(alias, passwordProtection);
			if (entry instanceof PrivateKeyEntry) {
				PrivateKeyEntry pke = (PrivateKeyEntry) entry;
				return new KSPrivateKeyEntry(alias, pke);
			} else {
				LOG.warn("Skipped entry (unsupported class : {})", entry.getClass().getSimpleName());
			}
		} else {
			LOG.debug("No related/supported key found for alias '{}'", alias);
		}
	} catch (GeneralSecurityException e) {
		throw new DSSException("Unable to retrieve key from keystore", e);
	}
	return null;
}
 
Example #7
Source File: PicketBoxSecurityVault.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
private void checkAndConvertKeyStoreToJCEKS(String keystoreURL) throws Exception {
   if (keystore.getType().equalsIgnoreCase("JKS")) {

      // backup original keystore file
      copyFile(new File(keystoreURL), new File(keystoreURL + ".original"));

      KeyStore jceks = KeyStoreUtil.createKeyStore("JCEKS", keyStorePWD);
      
      Enumeration<String> aliases = keystore.aliases();
      while (aliases.hasMoreElements()) {
         String entryAlias = aliases.nextElement();
         KeyStore.PasswordProtection p = new KeyStore.PasswordProtection(keyStorePWD);
         KeyStore.Entry e = keystore.getEntry(entryAlias, p);
         jceks.setEntry(entryAlias, e, p);
      }
      keystore = jceks;
      keyStoreType = "JCEKS"; // after conversion we have to change keyStoreType to the one we really have
      saveKeyStoreToFile(keystoreURL);
      PicketBoxLogger.LOGGER.keyStoreConvertedToJCEKS(KEYSTORE_URL);
   }
}
 
Example #8
Source File: CopyKeyTask.java    From development with Apache License 2.0 5 votes vote down vote up
private Entry loadEntry(final EntryDescriptor descr) throws IOException,
        GeneralSecurityException {
    final KeyStore keystore = loadKeyStore(descr);
    final Entry entry = keystore.getEntry(descr.getAlias(),
            createProtection(descr));
    if (entry == null) {
        throw new BuildException(String.format(
                "No entry %s found in keystore %s.", descr.getAlias(),
                descr.getKeystore()));
    }
    return entry;
}
 
Example #9
Source File: SignTask.java    From development with Apache License 2.0 5 votes vote down vote up
private PrivateKeyEntry loadCAKeyEntry() throws IOException,
        GeneralSecurityException {
    final KeyStore keystore = loadKeyStore();
    final Entry entry = keystore.getEntry(this.alias,
            new PasswordProtection(this.password.toCharArray()));
    return (PrivateKeyEntry) entry;
}
 
Example #10
Source File: KeyStoreProvider.java    From aws-encryption-sdk-java with Apache License 2.0 5 votes vote down vote up
private JceMasterKey internalGetMasterKey(final String provider, final String keyId) {
    final Entry entry;
    try {
        entry = keystore_.getEntry(keyId, keystore_.isKeyEntry(keyId) ? protection_ : null);
    } catch (NoSuchAlgorithmException | UnrecoverableEntryException | KeyStoreException e) {
        throw new UnsupportedProviderException(e);
    }
    if (entry == null) {
        throw new NoSuchMasterKeyException();
    }
    if (entry instanceof SecretKeyEntry) {
        final SecretKeyEntry skEntry = (SecretKeyEntry) entry;
        if (!skEntry.getSecretKey().getAlgorithm().equals(keyAlgorithm_)) {
            return null;
        }
        return JceMasterKey.getInstance(skEntry.getSecretKey(), provider, keyId, wrappingAlgorithm_);
    } else if (entry instanceof PrivateKeyEntry) {
        final PrivateKeyEntry pkEntry = (PrivateKeyEntry) entry;
        if (!pkEntry.getPrivateKey().getAlgorithm().equals(keyAlgorithm_)) {
            return null;
        }
        return JceMasterKey.getInstance(pkEntry.getCertificate().getPublicKey(), pkEntry.getPrivateKey(), provider,
                keyId, wrappingAlgorithm_);
    } else if (entry instanceof TrustedCertificateEntry) {
        final TrustedCertificateEntry certEntry = (TrustedCertificateEntry) entry;
        if (!certEntry.getTrustedCertificate().getPublicKey().getAlgorithm().equals(keyAlgorithm_)) {
            return null;
        }
        return JceMasterKey.getInstance(certEntry.getTrustedCertificate().getPublicKey(), null, provider, keyId,
                wrappingAlgorithm_);
    } else {
        throw new NoSuchMasterKeyException();
    }
}
 
Example #11
Source File: TrustManager.java    From LoboBrowser with MIT License 5 votes vote down vote up
public static SSLSocketFactory makeSSLSocketFactory(final InputStream extraCertsStream) {
  final String sep = File.separator;
  final String hardDefaultPath = System.getProperty("java.home") + sep + "lib" + sep + "security" + sep + "cacerts";
  final String defaultStorePath = System.getProperty("javax.net.ssl.trustStore", hardDefaultPath);
  try (
      final FileInputStream defaultIS = new FileInputStream(defaultStorePath)) {

    final KeyStore defKeyStore = KeyStore.getInstance(KeyStore.getDefaultType());
    defKeyStore.load(defaultIS, "changeit".toCharArray());

    final KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
    keyStore.load(extraCertsStream, null);

    // final KeyStore keyStore =  KeyStore.Builder.newInstance(defKeyStore, null).getKeyStore();
    final Enumeration<String> aliases = defKeyStore.aliases();
    while (aliases.hasMoreElements()) {
      final String alias = aliases.nextElement();
      if (defKeyStore.isCertificateEntry(alias)) {
        final Entry entry = defKeyStore.getEntry(alias, null);
        keyStore.setEntry(alias, entry, null);
      }
    }

    final TrustManagerFactory tmf =
        TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
    tmf.init(keyStore);
    final SSLContext sc = SSLContext.getInstance("TLS");
    sc.init(null, tmf.getTrustManagers(), null);
    return sc.getSocketFactory();
  } catch (KeyManagementException | KeyStoreException | NoSuchAlgorithmException | IOException | CertificateException
      | UnrecoverableEntryException e) {
    throw new RuntimeException(e);
  }

}
 
Example #12
Source File: ECKeyStore.java    From balzac with Apache License 2.0 5 votes vote down vote up
public void changePassword(char[] password) throws KeyStoreException {
    try {
        for (String alias : Collections.list(ks.aliases())) {
            Entry entry = ks.getEntry(alias, new PasswordProtection(this.password)); // read
            ks.setEntry(alias, entry, new PasswordProtection(password)); // override
        }

        // update the password
        Arrays.fill(this.password, '0');
        this.password = Arrays.copyOf(password, password.length);

    } catch (NoSuchAlgorithmException | UnrecoverableEntryException e) {
        throw new KeyStoreException(e);
    }
}
 
Example #13
Source File: KeyStoreMaterialsProvider.java    From aws-dynamodb-encryption-java with Apache License 2.0 5 votes vote down vote up
private void loadKeys() throws NoSuchAlgorithmException, UnrecoverableEntryException,
        KeyStoreException {
    Entry encryptionEntry = keyStore.getEntry(encryptionAlias, encryptionProtection);
    Entry signingEntry = keyStore.getEntry(signingAlias, signingProtection);
    CurrentMaterials newMaterials = new CurrentMaterials(encryptionEntry, signingEntry);
    currMaterials.set(newMaterials);
}
 
Example #14
Source File: KeyStoreMaterialsProvider.java    From aws-dynamodb-encryption-java with Apache License 2.0 5 votes vote down vote up
private void loadKeys() throws NoSuchAlgorithmException, UnrecoverableEntryException,
        KeyStoreException {
    Entry encryptionEntry = keyStore.getEntry(encryptionAlias, encryptionProtection);
    Entry signingEntry = keyStore.getEntry(signingAlias, signingProtection);
    CurrentMaterials newMaterials = new CurrentMaterials(encryptionEntry, signingEntry);
    currMaterials.set(newMaterials);
}
 
Example #15
Source File: MapDemo.java    From JavaCommon with Apache License 2.0 5 votes vote down vote up
public static void statis(String str, int top) {
    HashMap<Character, Integer> hashMap = new HashMap<Character, Integer>();
    char[] cs = str.toCharArray();
    for (char c : cs) {
        if (null == hashMap.get(c)) {
            hashMap.put(c, 1);
        } else {
            hashMap.put(c, hashMap.get(c) + 1);
        }
    }
    // 把entry取出来进行排序
    List<Map.Entry<Character, Integer>> list = new ArrayList<Map.Entry<Character, Integer>>(hashMap.entrySet());
    Collections.sort(list, new Comparator<Map.Entry<Character, Integer>>() {
        public int compare(Map.Entry<Character, Integer> o1, Map.Entry<Character, Integer> o2) {
            return (o2.getValue() - o1.getValue());
        }
    });
    for (int i = 0; i < top; i++) {
        if (i < list.size()) {
            System.out.println(list.get(i).getKey() + "--" + list.get(i).getValue());
        }
    }
    // 只把value取出来
    List<Integer> valueList = new ArrayList<>(hashMap.values());
    Collections.sort(valueList, new Comparator<Integer>() {
        @Override
        public int compare(Integer a, Integer b) {
            return b.compareTo(a);
        }
    });
    for (int i = 0; i < top; i++) {
        if (i < valueList.size()) {
            System.out.println(valueList.get(i));
        }
    }
}
 
Example #16
Source File: X509KeyManagerImpl.java    From openjsse with GNU General Public License v2.0 4 votes vote down vote up
@Override protected boolean removeEldestEntry(Map.Entry<K,V> eldest) {
    return size() > 10;
}
 
Example #17
Source File: X509KeyManagerImpl.java    From Bytecoder with Apache License 2.0 4 votes vote down vote up
@Override protected boolean removeEldestEntry(Map.Entry<K,V> eldest) {
    return size() > 10;
}