Пример #1
0
  /**
   * 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
   */
  public 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;
        engineSetCertificateEntry(alias, tce.getTrustedCertificate());
        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 {
        engineSetKeyEntry(
            alias,
            ((KeyStore.PrivateKeyEntry) entry).getPrivateKey(),
            pProtect.getPassword(),
            ((KeyStore.PrivateKeyEntry) entry).getCertificateChain());
        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 {
        engineSetKeyEntry(
            alias,
            ((KeyStore.SecretKeyEntry) entry).getSecretKey(),
            pProtect.getPassword(),
            (Certificate[]) null);
        return;
      }
    }

    throw new KeyStoreException("unsupported entry type: " + entry.getClass().getName());
  }
Пример #2
0
 /**
  * Assign the certificate to the alias in the keystore. It will overwrite an existing entry.
  *
  * @param alias the alias name
  * @param cert the certificate to add
  * @throws KeyStoreException if it fails
  */
 public final void setCertificateEntry(String alias, java.security.cert.Certificate cert)
     throws KeyStoreException {
   keyStoreSpi.engineSetCertificateEntry(alias, cert);
 }