Exemplo n.º 1
0
        /// <summary>
        /// Saves a {@code KeyStore.Entry} under the specified alias.
        /// The specified protection parameter is used to protect the
        /// {@code Entry}.
        ///
        /// <para> If an entry already exists for the specified alias,
        /// it is overridden.
        ///
        /// </para>
        /// </summary>
        /// <param name="alias"> save the {@code KeyStore.Entry} under this alias </param>
        /// <param name="entry"> the {@code Entry} to save </param>
        /// <param name="protParam"> the {@code ProtectionParameter}
        ///          used to protect the {@code Entry},
        ///          which may be {@code null}
        /// </param>
        /// <exception cref="KeyStoreException"> if this operation fails
        ///
        /// @since 1.5 </exception>
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: public void engineSetEntry(String alias, KeyStore.Entry entry, KeyStore.ProtectionParameter protParam) throws KeyStoreException
        public virtual void EngineSetEntry(String alias, KeyStore.Entry entry, KeyStore.ProtectionParameter protParam)
        {
            // get password
            if (protParam != null && !(protParam is KeyStore.PasswordProtection))
            {
                throw new KeyStoreException("unsupported protection parameter");
            }
            KeyStore.PasswordProtection pProtect = null;
            if (protParam != null)
            {
                pProtect = (KeyStore.PasswordProtection)protParam;
            }

            // set entry
            if (entry is KeyStore.TrustedCertificateEntry)
            {
                if (protParam != null && pProtect.Password != 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.TrustedCertificate);
                    return;
                }
            }
            else if (entry is KeyStore.PrivateKeyEntry)
            {
                if (pProtect == null || pProtect.Password == null)
                {
                    // pre-1.5 style setKeyEntry required password
                    throw new KeyStoreException("non-null password required to create PrivateKeyEntry");
                }
                else
                {
                    EngineSetKeyEntry(alias, ((KeyStore.PrivateKeyEntry)entry).PrivateKey, pProtect.Password, ((KeyStore.PrivateKeyEntry)entry).CertificateChain);
                    return;
                }
            }
            else if (entry is KeyStore.SecretKeyEntry)
            {
                if (pProtect == null || pProtect.Password == null)
                {
                    // pre-1.5 style setKeyEntry required password
                    throw new KeyStoreException("non-null password required to create SecretKeyEntry");
                }
                else
                {
                    EngineSetKeyEntry(alias, ((KeyStore.SecretKeyEntry)entry).SecretKey, pProtect.Password, (Certificate[])null);
                    return;
                }
            }

//JAVA TO C# CONVERTER WARNING: The .NET Type.FullName property will not always yield results identical to the Java Class.getName method:
            throw new KeyStoreException("unsupported entry type: " + entry.GetType().FullName);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Gets a {@code KeyStore.Entry} for the specified alias
        /// with the specified protection parameter.
        /// </summary>
        /// <param name="alias"> get the {@code KeyStore.Entry} for this alias </param>
        /// <param name="protParam"> the {@code ProtectionParameter}
        ///          used to protect the {@code Entry},
        ///          which may be {@code null}
        /// </param>
        /// <returns> the {@code KeyStore.Entry} for the specified alias,
        ///          or {@code null} if there is no such entry
        /// </returns>
        /// <exception cref="KeyStoreException"> if the operation failed </exception>
        /// <exception cref="NoSuchAlgorithmException"> if the algorithm for recovering the
        ///          entry cannot be found </exception>
        /// <exception cref="UnrecoverableEntryException"> if the specified
        ///          {@code protParam} were insufficient or invalid </exception>
        /// <exception cref="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 </exception>
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: public KeyStore.Entry engineGetEntry(String alias, KeyStore.ProtectionParameter protParam) throws KeyStoreException, NoSuchAlgorithmException, UnrecoverableEntryException
        public virtual KeyStore.Entry EngineGetEntry(String alias, KeyStore.ProtectionParameter protParam)
        {
            if (!EngineContainsAlias(alias))
            {
                return(null);
            }

            if (protParam == null)
            {
                if (EngineIsCertificateEntry(alias))
                {
                    return(new KeyStore.TrustedCertificateEntry(EngineGetCertificate(alias)));
                }
                else
                {
                    throw new UnrecoverableKeyException("requested entry requires a password");
                }
            }

            if (protParam is 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.Password;

                    Key key = EngineGetKey(alias, password);
                    if (key is PrivateKey)
                    {
                        Certificate[] chain = EngineGetCertificateChain(alias);
                        return(new KeyStore.PrivateKeyEntry((PrivateKey)key, chain));
                    }
                    else if (key is SecretKey)
                    {
                        return(new KeyStore.SecretKeyEntry((SecretKey)key));
                    }
                }
            }

            throw new UnsupportedOperationException();
        }