/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/**
* Implementation of key store for Windows using the Microsoft Crypto API.
*
* @since 1.6
*/
public MY() {
super("MY");
}
}
public ROOT() {
super("ROOT");
}
}
class KeyEntry
{
}
this.privateKey = key;
/*
* The default alias for both entry types is derived from a
* hash value intrinsic to the first certificate in the chain.
*/
} else {
}
}
/**
* Gets the alias for the keystore entry.
*/
{
return alias;
}
/**
* Sets the alias for the keystore entry.
*/
{
// TODO - set friendly name prop in cert store
}
/**
* Gets the private key for the keystore entry.
*/
{
return privateKey;
}
/**
* Sets the private key for the keystore entry.
*/
throws InvalidKeyException, KeyStoreException
{
// Adjust key length due to sign bit
byte[] keyBlob = generatePrivateKeyBlob(
}
/**
* Gets the certificate chain for the keystore entry.
*/
{
return certChain;
}
/**
* Sets the certificate chain for the keystore entry.
*/
{
} else {
}
}
}
};
/*
* An X.509 certificate factory.
* Used to create an X.509 certificate from its DER-encoding.
*/
/*
* Compatibility mode: for applications that assume keystores are
* stream-based this mode tolerates (but ignores) a non-null stream
* or password parameter when passed to the load or store methods.
* The mode is enabled by default.
*/
"sun.security.mscapi.keyStoreCompatibilityMode";
private final boolean keyStoreCompatibilityMode;
/*
* The keystore entries.
*/
/*
* The keystore name.
* Case is not significant.
*/
// Get the compatibility mode
keyStoreCompatibilityMode = false;
} else {
keyStoreCompatibilityMode = true;
}
}
/**
* Returns the key associated with the given alias.
* <p>
* A compatibility mode is supported for applications that assume
* a password must be supplied. It permits (but ignores) a non-null
* <code>password</code>. The mode is enabled by default.
* Set the
* <code>sun.security.mscapi.keyStoreCompatibilityMode</code>
* system property to <code>false</code> to disable compatibility mode
* and reject a non-null <code>password</code>.
*
* @param alias the alias name
* @param password the password, which should be <code>null</code>
*
* @return the requested key, or null if the given alias does not exist
* or does not identify a <i>key entry</i>.
*
* @exception NoSuchAlgorithmException if the algorithm for recovering the
* key cannot be found,
* or if compatibility mode is disabled and <code>password</code> is
* non-null.
* @exception UnrecoverableKeyException if the key cannot be recovered.
*/
{
return null;
}
throw new UnrecoverableKeyException("Password must be null");
}
if (engineIsKeyEntry(alias) == false)
return null;
return entry.getPrivateKey();
}
}
return null;
}
/**
* Returns the certificate chain associated with the given alias.
*
* @param alias the alias name
*
* @return the certificate chain (ordered with the user's certificate first
* and the root certificate authority last), or null if the given alias
* does not exist or does not contain a certificate chain (i.e., the given
* alias identifies either a <i>trusted certificate entry</i> or a
* <i>key entry</i> without a certificate chain).
*/
{
return null;
}
}
}
return null;
}
/**
* Returns the certificate associated with the given alias.
*
* <p>If the given alias name identifies a
* <i>trusted certificate entry</i>, the certificate associated with that
* entry is returned. If the given alias name identifies a
* <i>key entry</i>, the first element of the certificate chain of that
* entry is returned, or null if that entry does not have a certificate
* chain.
*
* @param alias the alias name
*
* @return the certificate, or null if the given alias does not exist or
* does not contain a certificate.
*/
{
return null;
}
{
return certChain[0];
}
}
return null;
}
/**
* Returns the creation date of the entry identified by the given alias.
*
* @param alias the alias name
*
* @return the creation date of this entry, or null if the given alias does
* not exist
*/
return null;
}
return new Date();
}
/**
* Stores the given private key and associated certificate chain in the
* keystore.
*
* <p>The given java.security.PrivateKey <code>key</code> must
* be accompanied by a certificate chain certifying the
* corresponding public key.
*
* <p>If the given alias already exists, the keystore information
* associated with it is overridden by the given key and certificate
* chain. Otherwise, a new entry is created.
*
* <p>
* A compatibility mode is supported for applications that assume
* a password must be supplied. It permits (but ignores) a non-null
* <code>password</code>. The mode is enabled by default.
* Set the
* <code>sun.security.mscapi.keyStoreCompatibilityMode</code>
* system property to <code>false</code> to disable compatibility mode
* and reject a non-null <code>password</code>.
*
* @param alias the alias name
* @param key the private key to be associated with the alias
* @param password the password, which should be <code>null</code>
* @param chain the certificate chain for the corresponding public
* key (only required if the given key is of type
* <code>java.security.PrivateKey</code>).
*
* @exception KeyStoreException if the given key is not a private key,
* cannot be protected, or if compatibility mode is disabled and
* <code>password</code> is non-null, or if this operation fails for
* some other reason.
*/
{
throw new KeyStoreException("alias must not be null");
}
throw new KeyStoreException("Password must be null");
}
if (key instanceof RSAPrivateCrtKey) {
boolean found = false;
found = true;
entry = e;
break;
}
}
if (! found) {
entry =
//TODO new KeyEntry(alias, key, (X509Certificate[]) chain);
}
try {
} catch (CertificateException ce) {
throw new KeyStoreException(ce);
} catch (InvalidKeyException ike) {
throw new KeyStoreException(ike);
}
} else {
throw new UnsupportedOperationException(
"Cannot assign the key to the given alias.");
}
}
/**
* Assigns the given key (that has already been protected) to the given
* alias.
*
* <p>If the protected key is of type
* <code>java.security.PrivateKey</code>, it must be accompanied by a
* certificate chain certifying the corresponding public key. If the
* underlying keystore implementation is of type <code>jks</code>,
* <code>key</code> must be encoded as an
* <code>EncryptedPrivateKeyInfo</code> as defined in the PKCS #8 standard.
*
* <p>If the given alias already exists, the keystore information
* associated with it is overridden by the given key (and possibly
* certificate chain).
*
* @param alias the alias name
* @param key the key (in protected format) to be associated with the alias
* @param chain the certificate chain for the corresponding public
* key (only useful if the protected key is of type
* <code>java.security.PrivateKey</code>).
*
* @exception KeyStoreException if this operation fails.
*/
Certificate[] chain)
throws KeyStoreException
{
throw new UnsupportedOperationException(
"Cannot assign the encoded key to the given alias.");
}
/**
* Assigns the given certificate to the given alias.
*
* <p>If the given alias already exists in this keystore and identifies a
* <i>trusted certificate entry</i>, the certificate associated with it is
* overridden by the given certificate.
*
* @param alias the alias name
* @param cert the certificate
*
* @exception KeyStoreException if the given alias already exists and does
* not identify a <i>trusted certificate entry</i>, or this operation
* fails for some other reason.
*/
throws KeyStoreException
{
throw new KeyStoreException("alias must not be null");
}
if (cert instanceof X509Certificate) {
// TODO - build CryptoAPI chain?
X509Certificate[] chain =
boolean found = false;
found = true;
entry = e;
break;
}
}
if (! found) {
entry =
}
try {
} catch (CertificateException ce) {
throw new KeyStoreException(ce);
}
}
} else {
throw new UnsupportedOperationException(
"Cannot assign the certificate to the given alias.");
}
}
/**
* Deletes the entry identified by the given alias from this keystore.
*
* @param alias the alias name
*
* @exception KeyStoreException if the entry cannot be removed.
*/
throws KeyStoreException
{
throw new KeyStoreException("alias must not be null");
}
// Get end-entity certificate and remove from system cert store
try {
} catch (CertificateException e) {
throw new KeyStoreException("Cannot remove entry: " +
e);
}
}
if (privateKey != null) {
}
break;
}
}
}
/**
* Lists all the alias names of this keystore.
*
* @return enumeration of the alias names
*/
return new Enumeration()
{
public boolean hasMoreElements()
{
}
public Object nextElement()
{
}
};
}
/**
* Checks if the given alias exists in this keystore.
*
* @param alias the alias name
*
* @return true if the alias exists, false otherwise
*/
{
return true;
}
return false;
}
/**
* Retrieves the number of entries in this keystore.
*
* @return the number of entries in this keystore
*/
public int engineSize() {
}
/**
* Returns true if the entry identified by the given alias is a
* <i>key entry</i>, and false otherwise.
*
* @return true if the entry identified by the given alias is a
* <i>key entry</i>, false otherwise.
*/
return false;
}
}
}
return false;
}
/**
* Returns true if the entry identified by the given alias is a
* <i>trusted certificate entry</i>, and false otherwise.
*
* @return true if the entry identified by the given alias is a
* <i>trusted certificate entry</i>, false otherwise.
*/
{
}
}
return false;
}
/**
* Returns the (alias) name of the first keystore entry whose certificate
* matches the given certificate.
*
* <p>This method attempts to match the given certificate with each
* keystore entry. If the entry being considered
* is a <i>trusted certificate entry</i>, the given certificate is
* compared to that entry's certificate. If the entry being considered is
* a <i>key entry</i>, the given certificate is compared to the first
* element of that entry's certificate chain (if a chain exists).
*
* @param cert the certificate to match with.
*
* @return the (alias) name of the first entry with matching certificate,
* or null if no such entry exists in this keystore.
*/
{
}
}
return null;
}
/**
* engineStore is currently a no-op.
* Entries are stored during engineSetEntry.
*
* A compatibility mode is supported for applications that assume
* keystores are stream-based. It permits (but ignores) a non-null
* <code>stream</code> or <code>password</code>.
* The mode is enabled by default.
* Set the
* <code>sun.security.mscapi.keyStoreCompatibilityMode</code>
* system property to <code>false</code> to disable compatibility mode
* and reject a non-null <code>stream</code> or <code>password</code>.
*
* @param stream the output stream, which should be <code>null</code>
* @param password the password, which should be <code>null</code>
*
* @exception IOException if compatibility mode is disabled and either
* parameter is non-null.
*/
{
throw new IOException("Keystore output stream must be null");
}
throw new IOException("Keystore password must be null");
}
}
/**
* Loads the keystore.
*
* A compatibility mode is supported for applications that assume
* keystores are stream-based. It permits (but ignores) a non-null
* <code>stream</code> or <code>password</code>.
* The mode is enabled by default.
* Set the
* <code>sun.security.mscapi.keyStoreCompatibilityMode</code>
* system property to <code>false</code> to disable compatibility mode
* and reject a non-null <code>stream</code> or <code>password</code>.
*
* @param stream the input stream, which should be <code>null</code>.
* @param password the password, which should be <code>null</code>.
*
* @exception IOException if there is an I/O or format problem with the
* keystore data. Or if compatibility mode is disabled and either
* parameter is non-null.
* @exception NoSuchAlgorithmException if the algorithm used to check
* the integrity of the keystore cannot be found
* @exception CertificateException if any of the certificates in the
* keystore could not be loaded
* @exception SecurityException if the security check for
* <code>SecurityPermission("authProvider.<i>name</i>")</code> does not
* pass, where <i>name</i> is the value returned by
* this provider's <code>getName</code> method.
*/
{
throw new IOException("Keystore input stream must be null");
}
throw new IOException("Keystore password must be null");
}
/*
* Use the same security check as AuthProvider.login
*/
"authProvider.SunMSCAPI"));
}
// Clear all key entries
try {
} catch (KeyStoreException e) {
throw new IOException(e);
}
}
/**
* Generates a certificate chain from the collection of
* certificates and stores the result into a key entry.
*/
{
try
{
int i = 0;
{
}
// Add cert chain
}
catch (Throwable e)
{
// Ignore the exception and skip this entry
// TODO - throw CertificateException?
}
}
/**
* Generates RSA key and certificate chain from the private key handle,
* collection of certificates and stores the result into key entries.
*/
{
try
{
int i = 0;
{
}
// Add cert chain
}
catch (Throwable e)
{
// Ignore the exception and skip this entry
// TODO - throw CertificateException?
}
}
/**
* Generates certificates from byte data and stores into cert collection.
*
* @param data Byte data.
* @param certCollection Collection of certificates.
*/
{
try
{
// Obtain certificate factory
if (certificateFactory == null) {
}
// Generate certificate
certCollection.addAll(c);
}
catch (CertificateException e)
{
// Ignore the exception and skip this certificate
// TODO - throw CertificateException?
}
{
// Ignore the exception and skip this certificate
// TODO - throw CertificateException?
}
}
/**
* Returns the name of the keystore.
*/
{
return storeName;
}
/**
*
* @param name Name of keystore.
* @param entries Collection of key/certificate.
*/
/**
* Stores a DER-encoded certificate into the certificate store
*
* @param name Name of the keystore.
* @param alias Name of the certificate.
* @param encoding DER-encoded certificate.
*/
/**
* Removes the certificate from the certificate store
*
* @param name Name of the keystore.
* @param alias Name of the certificate.
* @param encoding DER-encoded certificate.
*/
byte[] encoding, int encodingLength)
throws CertificateException, KeyStoreException;
/**
* Destroys the key container.
*
* @param keyContainerName The name of the key container.
*/
throws KeyStoreException;
/**
* Generates a private-key BLOB from a key's components.
*/
private native byte[] generatePrivateKeyBlob(
int keyBitLength,
byte[] modulus,
byte[] publicExponent,
byte[] privateExponent,
byte[] primeP,
byte[] primeQ,
byte[] exponentP,
byte[] exponentQ,
byte[] crtCoefficient) throws InvalidKeyException;
}