Java Code Examples for java.security.KeyStore#ProtectionParameter
The following examples show how to use
java.security.KeyStore#ProtectionParameter .
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: JavaKeyStoreUnitTest.java From tutorials with MIT License | 6 votes |
@Test public void givenLoadedKeyStoreWithOneEntry_whenDeleteEntry_thenKeyStoreSizeIsZero() throws Exception { keyStore.createEmptyKeyStore(); keyStore.loadKeyStore(); KeyGenerator keygen = KeyGenerator.getInstance("HmacSHA256"); SecretKey secretKey = keygen.generateKey(); //ideally, password should be different for every key KeyStore.ProtectionParameter protParam = new KeyStore.PasswordProtection(KEYSTORE_PWD.toCharArray()); KeyStore.SecretKeyEntry secretKeyEntry = new KeyStore.SecretKeyEntry(secretKey); keyStore.setEntry(MY_SECRET_ENTRY, secretKeyEntry, protParam); keyStore.deleteEntry(MY_SECRET_ENTRY); KeyStore result = this.keyStore.getKeyStore(); Assert.assertTrue(result.size() == 0); }
Example 2
Source File: P12SecretKey.java From openjdk-jdk8u-backup with GNU General Public License v2.0 | 5 votes |
private void run(String keystoreType) throws Exception { char[] pw = "password".toCharArray(); KeyStore ks = KeyStore.getInstance(keystoreType); ks.load(null, pw); KeyGenerator kg = KeyGenerator.getInstance("AES"); kg.init(128); SecretKey key = kg.generateKey(); KeyStore.SecretKeyEntry ske = new KeyStore.SecretKeyEntry(key); KeyStore.ProtectionParameter kspp = new KeyStore.PasswordProtection(pw); ks.setEntry(ALIAS, ske, kspp); File ksFile = File.createTempFile("test", ".test"); try (FileOutputStream fos = new FileOutputStream(ksFile)) { ks.store(fos, pw); fos.flush(); } // now see if we can get it back try (FileInputStream fis = new FileInputStream(ksFile)) { KeyStore ks2 = KeyStore.getInstance(keystoreType); ks2.load(fis, pw); KeyStore.Entry entry = ks2.getEntry(ALIAS, kspp); SecretKey keyIn = ((KeyStore.SecretKeyEntry)entry).getSecretKey(); if (Arrays.equals(key.getEncoded(), keyIn.getEncoded())) { System.err.println("OK: worked just fine with " + keystoreType + " keystore"); } else { System.err.println("ERROR: keys are NOT equal after storing in " + keystoreType + " keystore"); } } }
Example 3
Source File: P12SecretKey.java From dragonwell8_jdk with GNU General Public License v2.0 | 5 votes |
private void run(String keystoreType) throws Exception { char[] pw = "password".toCharArray(); KeyStore ks = KeyStore.getInstance(keystoreType); ks.load(null, pw); KeyGenerator kg = KeyGenerator.getInstance("AES"); kg.init(128); SecretKey key = kg.generateKey(); KeyStore.SecretKeyEntry ske = new KeyStore.SecretKeyEntry(key); KeyStore.ProtectionParameter kspp = new KeyStore.PasswordProtection(pw); ks.setEntry(ALIAS, ske, kspp); File ksFile = File.createTempFile("test", ".test"); try (FileOutputStream fos = new FileOutputStream(ksFile)) { ks.store(fos, pw); fos.flush(); } // now see if we can get it back try (FileInputStream fis = new FileInputStream(ksFile)) { KeyStore ks2 = KeyStore.getInstance(keystoreType); ks2.load(fis, pw); KeyStore.Entry entry = ks2.getEntry(ALIAS, kspp); SecretKey keyIn = ((KeyStore.SecretKeyEntry)entry).getSecretKey(); if (Arrays.equals(key.getEncoded(), keyIn.getEncoded())) { System.err.println("OK: worked just fine with " + keystoreType + " keystore"); } else { System.err.println("ERROR: keys are NOT equal after storing in " + keystoreType + " keystore"); } } }
Example 4
Source File: P12SecretKey.java From jdk8u-jdk with GNU General Public License v2.0 | 5 votes |
private void run(String keystoreType) throws Exception { char[] pw = "password".toCharArray(); KeyStore ks = KeyStore.getInstance(keystoreType); ks.load(null, pw); KeyGenerator kg = KeyGenerator.getInstance("AES"); kg.init(128); SecretKey key = kg.generateKey(); KeyStore.SecretKeyEntry ske = new KeyStore.SecretKeyEntry(key); KeyStore.ProtectionParameter kspp = new KeyStore.PasswordProtection(pw); ks.setEntry(ALIAS, ske, kspp); File ksFile = File.createTempFile("test", ".test"); try (FileOutputStream fos = new FileOutputStream(ksFile)) { ks.store(fos, pw); fos.flush(); } // now see if we can get it back try (FileInputStream fis = new FileInputStream(ksFile)) { KeyStore ks2 = KeyStore.getInstance(keystoreType); ks2.load(fis, pw); KeyStore.Entry entry = ks2.getEntry(ALIAS, kspp); SecretKey keyIn = ((KeyStore.SecretKeyEntry)entry).getSecretKey(); if (Arrays.equals(key.getEncoded(), keyIn.getEncoded())) { System.err.println("OK: worked just fine with " + keystoreType + " keystore"); } else { System.err.println("ERROR: keys are NOT equal after storing in " + keystoreType + " keystore"); } } }
Example 5
Source File: P12SecretKey.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
private void run(String keystoreType) throws Exception { char[] pw = "password".toCharArray(); KeyStore ks = KeyStore.getInstance(keystoreType); ks.load(null, pw); KeyGenerator kg = KeyGenerator.getInstance("AES"); kg.init(128); SecretKey key = kg.generateKey(); KeyStore.SecretKeyEntry ske = new KeyStore.SecretKeyEntry(key); KeyStore.ProtectionParameter kspp = new KeyStore.PasswordProtection(pw); ks.setEntry(ALIAS, ske, kspp); File ksFile = File.createTempFile("test", ".test"); try (FileOutputStream fos = new FileOutputStream(ksFile)) { ks.store(fos, pw); fos.flush(); } // now see if we can get it back try (FileInputStream fis = new FileInputStream(ksFile)) { KeyStore ks2 = KeyStore.getInstance(keystoreType); ks2.load(fis, pw); KeyStore.Entry entry = ks2.getEntry(ALIAS, kspp); SecretKey keyIn = ((KeyStore.SecretKeyEntry)entry).getSecretKey(); if (Arrays.equals(key.getEncoded(), keyIn.getEncoded())) { System.err.println("OK: worked just fine with " + keystoreType + " keystore"); } else { System.err.println("ERROR: keys are NOT equal after storing in " + keystoreType + " keystore"); } } }
Example 6
Source File: CryptoUtil.java From floodlight_with_topoguard with Apache License 2.0 | 5 votes |
public static byte[] getSharedSecret(String keyStorePath, String keyStorePassword) throws Exception { if (keyStorePath == null) return null; char[] password = keyStorePassword.toCharArray(); KeyStore.ProtectionParameter protParam = new KeyStore.PasswordProtection(password); KeyStore ks = readKeyStore(keyStorePath, password); KeyStore.SecretKeyEntry entry = (KeyStore.SecretKeyEntry) ks.getEntry(CHALLENGE_RESPONSE_SECRET, protParam); SecretKey secretKey = entry.getSecretKey(); return secretKey.getEncoded(); }
Example 7
Source File: PKCS12KeyStore.java From jdk8u-dev-jdk with GNU General Public License v2.0 | 4 votes |
/** * Gets a <code>KeyStore.Entry</code> for the specified alias * with the specified protection parameter. * * @param alias get the <code>KeyStore.Entry</code> for this alias * @param protParam the <code>ProtectionParameter</code> * used to protect the <code>Entry</code>, * which may be <code>null</code> * * @return the <code>KeyStore.Entry</code> for the specified alias, * or <code>null</code> if there is no such entry * * @exception KeyStoreException if the operation failed * @exception NoSuchAlgorithmException if the algorithm for recovering the * entry cannot be found * @exception UnrecoverableEntryException if the specified * <code>protParam</code> were insufficient or invalid * @exception UnrecoverableKeyException if the entry is a * <code>PrivateKeyEntry</code> or <code>SecretKeyEntry</code> * and the specified <code>protParam</code> does not contain * the information needed to recover the key (e.g. wrong password) * * @since 1.5 */ @Override public KeyStore.Entry engineGetEntry(String alias, KeyStore.ProtectionParameter protParam) throws KeyStoreException, NoSuchAlgorithmException, UnrecoverableEntryException { if (!engineContainsAlias(alias)) { return null; } Entry entry = entries.get(alias.toLowerCase(Locale.ENGLISH)); if (protParam == null) { if (engineIsCertificateEntry(alias)) { if (entry instanceof CertEntry && ((CertEntry) entry).trustedKeyUsage != null) { if (debug != null) { debug.println("Retrieved a trusted certificate at " + "alias '" + alias + "'"); } return new KeyStore.TrustedCertificateEntry( ((CertEntry)entry).cert, getAttributes(entry)); } } else { throw new UnrecoverableKeyException ("requested entry requires a password"); } } if (protParam instanceof KeyStore.PasswordProtection) { if (engineIsCertificateEntry(alias)) { throw new UnsupportedOperationException ("trusted certificate entries are not password-protected"); } else if (engineIsKeyEntry(alias)) { KeyStore.PasswordProtection pp = (KeyStore.PasswordProtection)protParam; char[] password = pp.getPassword(); Key key = engineGetKey(alias, password); if (key instanceof PrivateKey) { Certificate[] chain = engineGetCertificateChain(alias); return new KeyStore.PrivateKeyEntry((PrivateKey)key, chain, getAttributes(entry)); } else if (key instanceof SecretKey) { return new KeyStore.SecretKeyEntry((SecretKey)key, getAttributes(entry)); } } else if (!engineIsKeyEntry(alias)) { throw new UnsupportedOperationException ("untrusted certificate entries are not " + "password-protected"); } } throw new UnsupportedOperationException(); }
Example 8
Source File: PKCS12KeyStore.java From jdk8u-jdk with GNU General Public License v2.0 | 4 votes |
/** * Saves a <code>KeyStore.Entry</code> under the specified alias. * The specified protection parameter is used to protect the * <code>Entry</code>. * * <p> If an entry already exists for the specified alias, * it is overridden. * * @param alias save the <code>KeyStore.Entry</code> under this alias * @param entry the <code>Entry</code> to save * @param protParam the <code>ProtectionParameter</code> * used to protect the <code>Entry</code>, * which may be <code>null</code> * * @exception KeyStoreException if this operation fails * * @since 1.5 */ @Override public synchronized void engineSetEntry(String alias, KeyStore.Entry entry, KeyStore.ProtectionParameter protParam) throws KeyStoreException { // get password if (protParam != null && !(protParam instanceof KeyStore.PasswordProtection)) { throw new KeyStoreException("unsupported protection parameter"); } KeyStore.PasswordProtection pProtect = null; if (protParam != null) { pProtect = (KeyStore.PasswordProtection)protParam; } // set entry if (entry instanceof KeyStore.TrustedCertificateEntry) { if (protParam != null && pProtect.getPassword() != null) { // pre-1.5 style setCertificateEntry did not allow password throw new KeyStoreException ("trusted certificate entries are not password-protected"); } else { KeyStore.TrustedCertificateEntry tce = (KeyStore.TrustedCertificateEntry)entry; setCertEntry(alias, tce.getTrustedCertificate(), tce.getAttributes()); return; } } else if (entry instanceof KeyStore.PrivateKeyEntry) { if (pProtect == null || pProtect.getPassword() == null) { // pre-1.5 style setKeyEntry required password throw new KeyStoreException ("non-null password required to create PrivateKeyEntry"); } else { KeyStore.PrivateKeyEntry pke = (KeyStore.PrivateKeyEntry)entry; setKeyEntry(alias, pke.getPrivateKey(), pProtect, pke.getCertificateChain(), pke.getAttributes()); return; } } else if (entry instanceof KeyStore.SecretKeyEntry) { if (pProtect == null || pProtect.getPassword() == null) { // pre-1.5 style setKeyEntry required password throw new KeyStoreException ("non-null password required to create SecretKeyEntry"); } else { KeyStore.SecretKeyEntry ske = (KeyStore.SecretKeyEntry)entry; setKeyEntry(alias, ske.getSecretKey(), pProtect, (Certificate[])null, ske.getAttributes()); return; } } throw new KeyStoreException ("unsupported entry type: " + entry.getClass().getName()); }
Example 9
Source File: PKCS12KeyStore.java From openjdk-jdk8u-backup with GNU General Public License v2.0 | 4 votes |
/** * Saves a <code>KeyStore.Entry</code> under the specified alias. * The specified protection parameter is used to protect the * <code>Entry</code>. * * <p> If an entry already exists for the specified alias, * it is overridden. * * @param alias save the <code>KeyStore.Entry</code> under this alias * @param entry the <code>Entry</code> to save * @param protParam the <code>ProtectionParameter</code> * used to protect the <code>Entry</code>, * which may be <code>null</code> * * @exception KeyStoreException if this operation fails * * @since 1.5 */ @Override public synchronized void engineSetEntry(String alias, KeyStore.Entry entry, KeyStore.ProtectionParameter protParam) throws KeyStoreException { // get password if (protParam != null && !(protParam instanceof KeyStore.PasswordProtection)) { throw new KeyStoreException("unsupported protection parameter"); } KeyStore.PasswordProtection pProtect = null; if (protParam != null) { pProtect = (KeyStore.PasswordProtection)protParam; } // set entry if (entry instanceof KeyStore.TrustedCertificateEntry) { if (protParam != null && pProtect.getPassword() != null) { // pre-1.5 style setCertificateEntry did not allow password throw new KeyStoreException ("trusted certificate entries are not password-protected"); } else { KeyStore.TrustedCertificateEntry tce = (KeyStore.TrustedCertificateEntry)entry; setCertEntry(alias, tce.getTrustedCertificate(), tce.getAttributes()); return; } } else if (entry instanceof KeyStore.PrivateKeyEntry) { if (pProtect == null || pProtect.getPassword() == null) { // pre-1.5 style setKeyEntry required password throw new KeyStoreException ("non-null password required to create PrivateKeyEntry"); } else { KeyStore.PrivateKeyEntry pke = (KeyStore.PrivateKeyEntry)entry; setKeyEntry(alias, pke.getPrivateKey(), pProtect, pke.getCertificateChain(), pke.getAttributes()); return; } } else if (entry instanceof KeyStore.SecretKeyEntry) { if (pProtect == null || pProtect.getPassword() == null) { // pre-1.5 style setKeyEntry required password throw new KeyStoreException ("non-null password required to create SecretKeyEntry"); } else { KeyStore.SecretKeyEntry ske = (KeyStore.SecretKeyEntry)entry; setKeyEntry(alias, ske.getSecretKey(), pProtect, (Certificate[])null, ske.getAttributes()); return; } } throw new KeyStoreException ("unsupported entry type: " + entry.getClass().getName()); }
Example 10
Source File: KeyStoreSpi.java From j2objc with Apache License 2.0 | 4 votes |
/** * Gets a {@code KeyStore.Entry} for the specified alias * with the specified protection parameter. * * @param alias get the {@code KeyStore.Entry} for this alias * @param protParam the {@code ProtectionParameter} * used to protect the {@code Entry}, * which may be {@code null} * * @return the {@code KeyStore.Entry} for the specified alias, * or {@code null} if there is no such entry * * @exception KeyStoreException if the operation failed * @exception NoSuchAlgorithmException if the algorithm for recovering the * entry cannot be found * @exception UnrecoverableEntryException if the specified * {@code protParam} were insufficient or invalid * @exception UnrecoverableKeyException if the entry is a * {@code PrivateKeyEntry} or {@code SecretKeyEntry} * and the specified {@code protParam} does not contain * the information needed to recover the key (e.g. wrong password) * * @since 1.5 */ public KeyStore.Entry engineGetEntry(String alias, KeyStore.ProtectionParameter protParam) throws KeyStoreException, NoSuchAlgorithmException, UnrecoverableEntryException { if (!engineContainsAlias(alias)) { return null; } if (protParam == null) { if (engineIsCertificateEntry(alias)) { return new KeyStore.TrustedCertificateEntry (engineGetCertificate(alias)); // Android-removed: Allow access to entries with no password. // } else { // throw new UnrecoverableKeyException // ("requested entry requires a password"); } } // Android-changed: Add protParam == null to allow access to entries with no password. if ((protParam == null) || protParam instanceof KeyStore.PasswordProtection) { if (engineIsCertificateEntry(alias)) { throw new UnsupportedOperationException ("trusted certificate entries are not password-protected"); } else if (engineIsKeyEntry(alias)) { // Android-changed: Allow access to entries with no password. // KeyStore.PasswordProtection pp = // (KeyStore.PasswordProtection)protParam; // char[] password = pp.getPassword(); char[] password = null; if (protParam != null) { KeyStore.PasswordProtection pp = (KeyStore.PasswordProtection)protParam; password = pp.getPassword(); } Key key = engineGetKey(alias, password); if (key instanceof PrivateKey) { Certificate[] chain = engineGetCertificateChain(alias); return new KeyStore.PrivateKeyEntry((PrivateKey)key, chain); } else if (key instanceof SecretKey) { return new KeyStore.SecretKeyEntry((SecretKey)key); } } } throw new UnsupportedOperationException(); }
Example 11
Source File: PKCS12KeyStore.java From openjdk-jdk8u with GNU General Public License v2.0 | 4 votes |
/** * Saves a <code>KeyStore.Entry</code> under the specified alias. * The specified protection parameter is used to protect the * <code>Entry</code>. * * <p> If an entry already exists for the specified alias, * it is overridden. * * @param alias save the <code>KeyStore.Entry</code> under this alias * @param entry the <code>Entry</code> to save * @param protParam the <code>ProtectionParameter</code> * used to protect the <code>Entry</code>, * which may be <code>null</code> * * @exception KeyStoreException if this operation fails * * @since 1.5 */ @Override public synchronized void engineSetEntry(String alias, KeyStore.Entry entry, KeyStore.ProtectionParameter protParam) throws KeyStoreException { // get password if (protParam != null && !(protParam instanceof KeyStore.PasswordProtection)) { throw new KeyStoreException("unsupported protection parameter"); } KeyStore.PasswordProtection pProtect = null; if (protParam != null) { pProtect = (KeyStore.PasswordProtection)protParam; } // set entry if (entry instanceof KeyStore.TrustedCertificateEntry) { if (protParam != null && pProtect.getPassword() != null) { // pre-1.5 style setCertificateEntry did not allow password throw new KeyStoreException ("trusted certificate entries are not password-protected"); } else { KeyStore.TrustedCertificateEntry tce = (KeyStore.TrustedCertificateEntry)entry; setCertEntry(alias, tce.getTrustedCertificate(), tce.getAttributes()); return; } } else if (entry instanceof KeyStore.PrivateKeyEntry) { if (pProtect == null || pProtect.getPassword() == null) { // pre-1.5 style setKeyEntry required password throw new KeyStoreException ("non-null password required to create PrivateKeyEntry"); } else { KeyStore.PrivateKeyEntry pke = (KeyStore.PrivateKeyEntry)entry; setKeyEntry(alias, pke.getPrivateKey(), pProtect, pke.getCertificateChain(), pke.getAttributes()); return; } } else if (entry instanceof KeyStore.SecretKeyEntry) { if (pProtect == null || pProtect.getPassword() == null) { // pre-1.5 style setKeyEntry required password throw new KeyStoreException ("non-null password required to create SecretKeyEntry"); } else { KeyStore.SecretKeyEntry ske = (KeyStore.SecretKeyEntry)entry; setKeyEntry(alias, ske.getSecretKey(), pProtect, (Certificate[])null, ske.getAttributes()); return; } } throw new KeyStoreException ("unsupported entry type: " + entry.getClass().getName()); }
Example 12
Source File: AuthToken.java From opensoc-streaming with Apache License 2.0 | 4 votes |
public static void main( String[] args ) throws Exception { Options options = new Options(); options.addOption( "keystoreFile", true, "Keystore File" ); options.addOption( "keystorePassword", true, "Keystore Password" ); options.addOption( "authTokenAlias", true, ""); CommandLineParser parser = new GnuParser(); CommandLine cmd = parser.parse( options, args); try { KeyStore ks = KeyStore.getInstance("JCEKS"); String keystorePassword = cmd.getOptionValue("keystorePassword"); String keystoreFile = cmd.getOptionValue("keystoreFile"); String authTokenAlias = cmd.getOptionValue("authTokenAlias"); ks.load(null, keystorePassword.toCharArray()); // generate a key and store it in the keystore... KeyGenerator keyGen = KeyGenerator.getInstance("AES"); SecretKey key = keyGen.generateKey(); KeyStore.ProtectionParameter protParam = new KeyStore.PasswordProtection(keystorePassword.toCharArray()); KeyStore.SecretKeyEntry skEntry = new KeyStore.SecretKeyEntry(key); ks.setEntry(authTokenAlias, skEntry, protParam); java.io.FileOutputStream fos = null; try { fos = new java.io.FileOutputStream(keystoreFile); ks.store(fos, keystorePassword.toCharArray()); } finally { if (fos != null) { fos.close(); } } System.out.println( "done" ); } catch( Exception e ) { e.printStackTrace(); } }
Example 13
Source File: JavaKeyStore.java From tutorials with MIT License | 4 votes |
KeyStore.Entry getEntry(String alias) throws UnrecoverableEntryException, NoSuchAlgorithmException, KeyStoreException { KeyStore.ProtectionParameter protParam = new KeyStore.PasswordProtection(keyStorePassword.toCharArray()); return keyStore.getEntry(alias, protParam); }
Example 14
Source File: MyLoadStoreParams.java From j2objc with Apache License 2.0 | 4 votes |
public MyLoadStoreParams(KeyStore.ProtectionParameter p) { this.protPar = p; }
Example 15
Source File: PKCS12KeyStore.java From jdk8u60 with GNU General Public License v2.0 | 4 votes |
/** * Gets a <code>KeyStore.Entry</code> for the specified alias * with the specified protection parameter. * * @param alias get the <code>KeyStore.Entry</code> for this alias * @param protParam the <code>ProtectionParameter</code> * used to protect the <code>Entry</code>, * which may be <code>null</code> * * @return the <code>KeyStore.Entry</code> for the specified alias, * or <code>null</code> if there is no such entry * * @exception KeyStoreException if the operation failed * @exception NoSuchAlgorithmException if the algorithm for recovering the * entry cannot be found * @exception UnrecoverableEntryException if the specified * <code>protParam</code> were insufficient or invalid * @exception UnrecoverableKeyException if the entry is a * <code>PrivateKeyEntry</code> or <code>SecretKeyEntry</code> * and the specified <code>protParam</code> does not contain * the information needed to recover the key (e.g. wrong password) * * @since 1.5 */ @Override public KeyStore.Entry engineGetEntry(String alias, KeyStore.ProtectionParameter protParam) throws KeyStoreException, NoSuchAlgorithmException, UnrecoverableEntryException { if (!engineContainsAlias(alias)) { return null; } Entry entry = entries.get(alias.toLowerCase(Locale.ENGLISH)); if (protParam == null) { if (engineIsCertificateEntry(alias)) { if (entry instanceof CertEntry && ((CertEntry) entry).trustedKeyUsage != null) { if (debug != null) { debug.println("Retrieved a trusted certificate at " + "alias '" + alias + "'"); } return new KeyStore.TrustedCertificateEntry( ((CertEntry)entry).cert, getAttributes(entry)); } } else { throw new UnrecoverableKeyException ("requested entry requires a password"); } } if (protParam instanceof KeyStore.PasswordProtection) { if (engineIsCertificateEntry(alias)) { throw new UnsupportedOperationException ("trusted certificate entries are not password-protected"); } else if (engineIsKeyEntry(alias)) { KeyStore.PasswordProtection pp = (KeyStore.PasswordProtection)protParam; char[] password = pp.getPassword(); Key key = engineGetKey(alias, password); if (key instanceof PrivateKey) { Certificate[] chain = engineGetCertificateChain(alias); return new KeyStore.PrivateKeyEntry((PrivateKey)key, chain, getAttributes(entry)); } else if (key instanceof SecretKey) { return new KeyStore.SecretKeyEntry((SecretKey)key, getAttributes(entry)); } } else if (!engineIsKeyEntry(alias)) { throw new UnsupportedOperationException ("untrusted certificate entries are not " + "password-protected"); } } throw new UnsupportedOperationException(); }
Example 16
Source File: PKCS12KeyStore.java From jdk8u_jdk with GNU General Public License v2.0 | 4 votes |
/** * Gets a <code>KeyStore.Entry</code> for the specified alias * with the specified protection parameter. * * @param alias get the <code>KeyStore.Entry</code> for this alias * @param protParam the <code>ProtectionParameter</code> * used to protect the <code>Entry</code>, * which may be <code>null</code> * * @return the <code>KeyStore.Entry</code> for the specified alias, * or <code>null</code> if there is no such entry * * @exception KeyStoreException if the operation failed * @exception NoSuchAlgorithmException if the algorithm for recovering the * entry cannot be found * @exception UnrecoverableEntryException if the specified * <code>protParam</code> were insufficient or invalid * @exception UnrecoverableKeyException if the entry is a * <code>PrivateKeyEntry</code> or <code>SecretKeyEntry</code> * and the specified <code>protParam</code> does not contain * the information needed to recover the key (e.g. wrong password) * * @since 1.5 */ @Override public KeyStore.Entry engineGetEntry(String alias, KeyStore.ProtectionParameter protParam) throws KeyStoreException, NoSuchAlgorithmException, UnrecoverableEntryException { if (!engineContainsAlias(alias)) { return null; } Entry entry = entries.get(alias.toLowerCase(Locale.ENGLISH)); if (protParam == null) { if (engineIsCertificateEntry(alias)) { if (entry instanceof CertEntry && ((CertEntry) entry).trustedKeyUsage != null) { if (debug != null) { debug.println("Retrieved a trusted certificate at " + "alias '" + alias + "'"); } return new KeyStore.TrustedCertificateEntry( ((CertEntry)entry).cert, getAttributes(entry)); } } else { throw new UnrecoverableKeyException ("requested entry requires a password"); } } if (protParam instanceof KeyStore.PasswordProtection) { if (engineIsCertificateEntry(alias)) { throw new UnsupportedOperationException ("trusted certificate entries are not password-protected"); } else if (engineIsKeyEntry(alias)) { KeyStore.PasswordProtection pp = (KeyStore.PasswordProtection)protParam; char[] password = pp.getPassword(); Key key = engineGetKey(alias, password); if (key instanceof PrivateKey) { Certificate[] chain = engineGetCertificateChain(alias); return new KeyStore.PrivateKeyEntry((PrivateKey)key, chain, getAttributes(entry)); } else if (key instanceof SecretKey) { return new KeyStore.SecretKeyEntry((SecretKey)key, getAttributes(entry)); } } else if (!engineIsKeyEntry(alias)) { throw new UnsupportedOperationException ("untrusted certificate entries are not " + "password-protected"); } } throw new UnsupportedOperationException(); }
Example 17
Source File: PKCS12KeyStore.java From TencentKona-8 with GNU General Public License v2.0 | 4 votes |
/** * Saves a <code>KeyStore.Entry</code> under the specified alias. * The specified protection parameter is used to protect the * <code>Entry</code>. * * <p> If an entry already exists for the specified alias, * it is overridden. * * @param alias save the <code>KeyStore.Entry</code> under this alias * @param entry the <code>Entry</code> to save * @param protParam the <code>ProtectionParameter</code> * used to protect the <code>Entry</code>, * which may be <code>null</code> * * @exception KeyStoreException if this operation fails * * @since 1.5 */ @Override public synchronized void engineSetEntry(String alias, KeyStore.Entry entry, KeyStore.ProtectionParameter protParam) throws KeyStoreException { // get password if (protParam != null && !(protParam instanceof KeyStore.PasswordProtection)) { throw new KeyStoreException("unsupported protection parameter"); } KeyStore.PasswordProtection pProtect = null; if (protParam != null) { pProtect = (KeyStore.PasswordProtection)protParam; } // set entry if (entry instanceof KeyStore.TrustedCertificateEntry) { if (protParam != null && pProtect.getPassword() != null) { // pre-1.5 style setCertificateEntry did not allow password throw new KeyStoreException ("trusted certificate entries are not password-protected"); } else { KeyStore.TrustedCertificateEntry tce = (KeyStore.TrustedCertificateEntry)entry; setCertEntry(alias, tce.getTrustedCertificate(), tce.getAttributes()); return; } } else if (entry instanceof KeyStore.PrivateKeyEntry) { if (pProtect == null || pProtect.getPassword() == null) { // pre-1.5 style setKeyEntry required password throw new KeyStoreException ("non-null password required to create PrivateKeyEntry"); } else { KeyStore.PrivateKeyEntry pke = (KeyStore.PrivateKeyEntry)entry; setKeyEntry(alias, pke.getPrivateKey(), pProtect, pke.getCertificateChain(), pke.getAttributes()); return; } } else if (entry instanceof KeyStore.SecretKeyEntry) { if (pProtect == null || pProtect.getPassword() == null) { // pre-1.5 style setKeyEntry required password throw new KeyStoreException ("non-null password required to create SecretKeyEntry"); } else { KeyStore.SecretKeyEntry ske = (KeyStore.SecretKeyEntry)entry; setKeyEntry(alias, ske.getSecretKey(), pProtect, (Certificate[])null, ske.getAttributes()); return; } } throw new KeyStoreException ("unsupported entry type: " + entry.getClass().getName()); }
Example 18
Source File: PKCS12KeyStore.java From dragonwell8_jdk with GNU General Public License v2.0 | 4 votes |
/** * Saves a <code>KeyStore.Entry</code> under the specified alias. * The specified protection parameter is used to protect the * <code>Entry</code>. * * <p> If an entry already exists for the specified alias, * it is overridden. * * @param alias save the <code>KeyStore.Entry</code> under this alias * @param entry the <code>Entry</code> to save * @param protParam the <code>ProtectionParameter</code> * used to protect the <code>Entry</code>, * which may be <code>null</code> * * @exception KeyStoreException if this operation fails * * @since 1.5 */ @Override public synchronized void engineSetEntry(String alias, KeyStore.Entry entry, KeyStore.ProtectionParameter protParam) throws KeyStoreException { // get password if (protParam != null && !(protParam instanceof KeyStore.PasswordProtection)) { throw new KeyStoreException("unsupported protection parameter"); } KeyStore.PasswordProtection pProtect = null; if (protParam != null) { pProtect = (KeyStore.PasswordProtection)protParam; } // set entry if (entry instanceof KeyStore.TrustedCertificateEntry) { if (protParam != null && pProtect.getPassword() != null) { // pre-1.5 style setCertificateEntry did not allow password throw new KeyStoreException ("trusted certificate entries are not password-protected"); } else { KeyStore.TrustedCertificateEntry tce = (KeyStore.TrustedCertificateEntry)entry; setCertEntry(alias, tce.getTrustedCertificate(), tce.getAttributes()); return; } } else if (entry instanceof KeyStore.PrivateKeyEntry) { if (pProtect == null || pProtect.getPassword() == null) { // pre-1.5 style setKeyEntry required password throw new KeyStoreException ("non-null password required to create PrivateKeyEntry"); } else { KeyStore.PrivateKeyEntry pke = (KeyStore.PrivateKeyEntry)entry; setKeyEntry(alias, pke.getPrivateKey(), pProtect, pke.getCertificateChain(), pke.getAttributes()); return; } } else if (entry instanceof KeyStore.SecretKeyEntry) { if (pProtect == null || pProtect.getPassword() == null) { // pre-1.5 style setKeyEntry required password throw new KeyStoreException ("non-null password required to create SecretKeyEntry"); } else { KeyStore.SecretKeyEntry ske = (KeyStore.SecretKeyEntry)entry; setKeyEntry(alias, ske.getSecretKey(), pProtect, (Certificate[])null, ske.getAttributes()); return; } } throw new KeyStoreException ("unsupported entry type: " + entry.getClass().getName()); }
Example 19
Source File: PKCS12KeyStore.java From jdk8u-jdk with GNU General Public License v2.0 | 4 votes |
/** * Gets a <code>KeyStore.Entry</code> for the specified alias * with the specified protection parameter. * * @param alias get the <code>KeyStore.Entry</code> for this alias * @param protParam the <code>ProtectionParameter</code> * used to protect the <code>Entry</code>, * which may be <code>null</code> * * @return the <code>KeyStore.Entry</code> for the specified alias, * or <code>null</code> if there is no such entry * * @exception KeyStoreException if the operation failed * @exception NoSuchAlgorithmException if the algorithm for recovering the * entry cannot be found * @exception UnrecoverableEntryException if the specified * <code>protParam</code> were insufficient or invalid * @exception UnrecoverableKeyException if the entry is a * <code>PrivateKeyEntry</code> or <code>SecretKeyEntry</code> * and the specified <code>protParam</code> does not contain * the information needed to recover the key (e.g. wrong password) * * @since 1.5 */ @Override public KeyStore.Entry engineGetEntry(String alias, KeyStore.ProtectionParameter protParam) throws KeyStoreException, NoSuchAlgorithmException, UnrecoverableEntryException { if (!engineContainsAlias(alias)) { return null; } Entry entry = entries.get(alias.toLowerCase(Locale.ENGLISH)); if (protParam == null) { if (engineIsCertificateEntry(alias)) { if (entry instanceof CertEntry && ((CertEntry) entry).trustedKeyUsage != null) { if (debug != null) { debug.println("Retrieved a trusted certificate at " + "alias '" + alias + "'"); } return new KeyStore.TrustedCertificateEntry( ((CertEntry)entry).cert, getAttributes(entry)); } } else { throw new UnrecoverableKeyException ("requested entry requires a password"); } } if (protParam instanceof KeyStore.PasswordProtection) { if (engineIsCertificateEntry(alias)) { throw new UnsupportedOperationException ("trusted certificate entries are not password-protected"); } else if (engineIsKeyEntry(alias)) { KeyStore.PasswordProtection pp = (KeyStore.PasswordProtection)protParam; char[] password = pp.getPassword(); Key key = engineGetKey(alias, password); if (key instanceof PrivateKey) { Certificate[] chain = engineGetCertificateChain(alias); return new KeyStore.PrivateKeyEntry((PrivateKey)key, chain, getAttributes(entry)); } else if (key instanceof SecretKey) { return new KeyStore.SecretKeyEntry((SecretKey)key, getAttributes(entry)); } } else if (!engineIsKeyEntry(alias)) { throw new UnsupportedOperationException ("untrusted certificate entries are not " + "password-protected"); } } throw new UnsupportedOperationException(); }
Example 20
Source File: KeyStoreProxyImpl.java From android_9.0.0_r45 with Apache License 2.0 | 4 votes |
@Override public void setEntry(String alias, KeyStore.Entry entry, KeyStore.ProtectionParameter protParam) throws KeyStoreException { mKeyStore.setEntry(alias, entry, protParam); }