/*
* 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.
*
* 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.
*/
/**
* A <code>KeySelector</code> that returns {@link PublicKey}s. If the
* selector is created as trusted, it only returns public keys of trusted
* {@link X509Certificate}s stored in a {@link KeyStore}. Otherwise, it
* returns trusted or untrusted public keys (it doesn't care as long
* as it finds one).
*
* <p>This <code>KeySelector</code> uses the specified <code>KeyStore</code>
* to find a trusted <code>X509Certificate</code> that matches information
* specified in the {@link KeyInfo} passed to the {@link #select} method.
* The public key from the first match is returned. If no match,
* <code>null</code> is returned. See the <code>select</code> method for more
* information.
*
* @author Sean Mullan
*/
private boolean trusted = true;
/**
* Creates a trusted <code>X509KeySelector</code>.
*
* @param keyStore the keystore
* @throws KeyStoreException if the keystore has not been initialized
* @throws NullPointerException if <code>keyStore</code> is
* <code>null</code>
*/
this(keyStore, true);
}
throws KeyStoreException {
throw new NullPointerException("keyStore is null");
}
// test to see if KeyStore has been initialized
}
/**
* Finds a key from the keystore satisfying the specified constraints.
*
* <p>This method compares data contained in {@link KeyInfo} entries
* with information stored in the <code>KeyStore</code>. The implementation
* iterates over the KeyInfo types and returns the first {@link PublicKey}
* of an X509Certificate in the keystore that is compatible with the
* specified AlgorithmMethod according to the following rules for each
* keyinfo type:
*
* X509Data X509Certificate: if it contains a <code>KeyUsage</code>
* extension that asserts the <code>digitalSignature</code> bit and
* matches an <code>X509Certificate</code> in the <code>KeyStore</code>.
* X509Data X509IssuerSerial: if the serial number and issuer DN match an
* <code>X509Certificate</code> in the <code>KeyStore</code>.
* X509Data X509SubjectName: if the subject DN matches an
* <code>X509Certificate</code> in the <code>KeyStore</code>.
* X509Data X509SKI: if the subject key identifier matches an
* <code>X509Certificate</code> in the <code>KeyStore</code>.
* KeyName: if the keyname matches an alias in the <code>KeyStore</code>.
* RetrievalMethod: supports rawX509Certificate and X509Data types. If
* rawX509Certificate type, it must match an <code>X509Certificate</code>
* in the <code>KeyStore</code>.
*
* @param keyInfo a <code>KeyInfo</code> (may be <code>null</code>)
* @param purpose the key's purpose
* @param method the algorithm method that this key is to be used for.
* Only keys that are compatible with the algorithm and meet the
* constraints of the specified algorithm should be returned.
* @param an <code>XMLCryptoContext</code> that may contain additional
* useful information for finding an appropriate key
* @return a key selector result
* @throws KeySelectorException if an exceptional condition occurs while
* attempting to find a key. Note that an inability to find a key is not
* considered an exception (<code>null</code> should be
* returned in that case). However, an error condition (ex: network
* communications failure) that prevented the <code>KeySelector</code>
* from finding a potential key should be considered an exception.
* @throws ClassCastException if the data type of <code>method</code>
* is not supported by this key selector
*/
try {
// return null if keyinfo is null or keystore is empty
return new SimpleKeySelectorResult(null);
}
// Iterate through KeyInfo types
while (i.hasNext()) {
// check X509Data
return ksr;
}
// check KeyName
}
// check RetrievalMethod
} else if (kiType instanceof RetrievalMethod) {
try {
} else {
// skip; keyinfo type is not supported
continue;
}
return ksr;
}
} catch (Exception e) {
throw new KeySelectorException(e);
}
}
}
} catch (KeyStoreException kse) {
// throw exception if keystore is uninitialized
throw new KeySelectorException(kse);
}
// return null since no match could be found
return new SimpleKeySelectorResult(null);
}
/**
* Searches the specified keystore for a certificate that matches the
* criteria specified in the CertSelector.
*
* @return a KeySelectorResult containing the cert's public key if there
* is a match; otherwise null
*/
throws KeyStoreException {
while (aliases.hasMoreElements()) {
}
}
return null;
}
/**
* Searches the specified keystore for a certificate that matches the
* specified X509Certificate and contains a public key that is compatible
* with the specified SignatureMethod.
*
* @return a KeySelectorResult containing the cert's public key if there
* is a match; otherwise null
*/
// skip non-signer certs
return null;
}
// make sure algorithm is compatible with method
return new SimpleKeySelectorResult(pk);
}
}
return null;
}
/**
* Returns an OID of a public-key algorithm compatible with the specified
* signature algorithm URI.
*/
return "1.2.840.10040.4.1";
return "1.2.840.113549.1.1";
} else {
return null;
}
}
/**
* A simple KeySelectorResult containing a public key.
*/
}
/**
* the specified signature algorithm URI.
*/
return true;
return true;
} else {
return false;
}
}
/**
* Searches the specified keystore for a certificate that matches an
* entry of the specified X509Data and contains a public key that is
* compatible with the specified SignatureMethod.
*
* @return a KeySelectorResult containing the cert's public key if there
* is a match; otherwise null
*/
throws KeyStoreException, KeySelectorException {
// convert signature algorithm to compatible public-key alg OID
try {
} catch (IOException ioe) {
throw new KeySelectorException(ioe);
}
// check X509IssuerSerial
if (o instanceof X509IssuerSerial) {
try {
// strip off newline
}
} catch (IOException ioe) {
throw new KeySelectorException(ioe);
}
// check X509SubjectName
} else if (o instanceof String) {
try {
// strip off newline
}
} catch (IOException ioe) {
throw new KeySelectorException(ioe);
}
// check X509SKI
} else if (o instanceof byte[]) {
byte[] ski = (byte[]) o;
// DER-encode ski - required by X509CertSelector
} else if (o instanceof X509Certificate) {
// check X509CRL
// not supported: should use CertPath API
} else {
// skip all other entries
continue;
}
}
return ksr;
}
// try to find public key in certs in X509Data
}
}
}
return null;
}
}