/* X509CertSelector.java -- selects X.509 certificates by criteria.
Copyright (C) 2004, 2005, 2006 Free Software Foundation, Inc.
This file is part of GNU Classpath.
GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
GNU Classpath 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 for more details.
You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING. If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA.
Linking this library statically or dynamically with other modules is
making a combined work based on this library. Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.
As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module. An independent module is a module which is not derived from
or based on this library. If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so. If you do not wish to do so, delete this
exception statement from your version. */
package java.security.cert;
import gnu.classpath.SystemProperties;
import gnu.java.lang.CPStringBuilder;
import gnu.java.security.OID;
import gnu.java.security.x509.GnuPKIExtension;
import gnu.java.security.x509.ext.CertificatePolicies;
import gnu.java.security.x509.ext.Extension;
import gnu.java.security.x509.ext.GeneralName;
import gnu.java.security.x509.ext.GeneralSubtree;
import gnu.java.security.x509.ext.NameConstraints;
import gnu.java.security.x509.ext.GeneralName.Kind;
import java.io.IOException;
import java.math.BigInteger;
import java.net.InetAddress;
import java.security.KeyFactory;
import java.security.PublicKey;
import java.security.spec.X509EncodedKeySpec;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import javax.security.auth.x500.X500Principal;
/**
* A concrete implementation of {@link CertSelector} for X.509 certificates,
* which allows a number of criteria to be set when accepting certificates,
* from validity dates, to issuer and subject distinguished names, to some
* of the various X.509 extensions.
*
*
Note that this class is not thread-safe. If multiple threads will
* use or modify this class then they need to synchronize on the object.
*
* @author Casey Marshall (csm@gnu.org)
* @since 1.4
*/
public class X509CertSelector implements CertSelector, Cloneable
{
// Constants and fields.
// -------------------------------------------------------------------------
private static final String AUTH_KEY_ID = "2.5.29.35";
private static final String SUBJECT_KEY_ID = "2.5.29.14";
private static final String NAME_CONSTRAINTS_ID = "2.5.29.30";
private static boolean checkOid(int[] oid)
{
return (oid != null && oid.length > 2 &&
(oid[0] >= 0 && oid[0] <= 2) && (oid[1] >= 0 && oid[1] <= 39));
}
private static GeneralName makeName(int id, String name) throws IOException
{
byte[] nameBytes = null;
GeneralName.Kind kind = GeneralName.Kind.forTag(id);
switch (Kind.forTag(id))
{
case dNSName:
case rfc822Name:
case uniformResourceIdentifier:
nameBytes = name.getBytes("ASCII");
break;
case iPAddress:
InetAddress addr = InetAddress.getByName(name);
nameBytes = addr.getAddress();
break;
case registeredId:
OID oid = new OID(name);
nameBytes = oid.getDER();
break;
case directoryName:
X500Principal xname = new X500Principal(name);
nameBytes = xname.getEncoded();
break;
case ediPartyName:
case x400Address:
case otherName:
throw new IOException("cannot decode string representation of "
+ kind);
}
return new GeneralName(kind, nameBytes);
}
private int basicConstraints;
private X509Certificate cert;
private BigInteger serialNo;
private X500Principal issuer;
private X500Principal subject;
private byte[] subjectKeyId;
private byte[] authKeyId;
private boolean[] keyUsage;
private Date certValid;
private OID sigId;
private PublicKey subjectKey;
private X509EncodedKeySpec subjectKeySpec;
private Set keyPurposeSet;
private List altNames;
private boolean matchAllNames;
private byte[] nameConstraints;
private Set policy;
private List pathToNames;
/**
* Creates a new X.509 certificate selector. The new selector will be
* empty, and will accept any certificate (provided that it is an
* {@link X509Certificate}).
*/
public X509CertSelector()
{
basicConstraints = -1;
}
/**
* Add a name to match in the NameConstraints extension. The argument is
* the DER-encoded bytes of a GeneralName structure.
*
* See the method {@link #addSubjectAlternativeName(int, byte[])} for the
* format of the GeneralName structure.
*
* @param id The name identifier. Must be between 0 and 8.
* @param name The DER-encoded bytes of the name to match.
* @throws IOException If the name DER is malformed.
*/
public void addPathToName(int id, byte[] name) throws IOException
{
GeneralName generalName = new GeneralName(GeneralName.Kind.forTag(id), name);
if (pathToNames == null)
pathToNames = new LinkedList();
pathToNames.add(generalName);
}
/**
* Add a name to match in the NameConstraints extension. This method will
* only recognize certain types of name that have convenient string
* encodings. For robustness, you should use the {@link
* #addPathToName(int, byte[])} method whenever possible.
*
* @param id The name identifier. Must be between 0 and 8.
* @param name The name.
* @throws IOException If the name cannot be decoded.
*/
public void addPathToName(int id, String name) throws IOException
{
GeneralName generalName = makeName(id, name);
if (pathToNames == null)
pathToNames = new LinkedList();
pathToNames.add(generalName);
}
/**
* Add a name, as DER-encoded bytes, to the subject alternative names
* criterion.
*
* The name is a GeneralName structure, which has the ASN.1 format:
*
*
*
* @param id The type of name this is.
* @param name The DER-encoded name.
* @throws IOException If the name is not a valid DER sequence.
*/
public void addSubjectAlternativeName(int id, byte[] name)
throws IOException
{
GeneralName generalName = new GeneralName(GeneralName.Kind.forTag(id), name);
if (altNames == null)
altNames = new LinkedList();
altNames.add(generalName);
}
/**
* Add a name to the subject alternative names criterion. This method will
* only recognize certain types of name that have convenient string
* encodings. For robustness, you should use the {@link
* #addSubjectAlternativeName(int, byte[])} method whenever possible.
*
* This method can only decode certain name kinds of names as strings.
*
* @param id The type of name this is. Must be in the range [0,8].
* @param name The name.
* @throws IOException If the id is out of range, or if the name
* is null.
*/
public void addSubjectAlternativeName(int id, String name)
throws IOException
{
GeneralName generalName = makeName(id, name);
if (altNames == null)
altNames = new LinkedList();
altNames.add(generalName);
}
public Object clone()
{
try
{
return super.clone();
}
catch (CloneNotSupportedException shouldNotHappen)
{
throw new Error(shouldNotHappen);
}
}
/**
* Returns the authority key identifier criterion, or null if
* this value was not set. Note that the byte array is cloned to prevent
* modification.
*
* @return The authority key identifier.
*/
public byte[] getAuthorityKeyIdentifier()
{
if (authKeyId != null)
return (byte[]) authKeyId.clone();
else
return null;
}
/**
* Returns the basic constraints criterion, or -1 if this value is not set.
*
* @return The basic constraints.
*/
public int getBasicConstraints()
{
return basicConstraints;
}
/**
* Returns the certificate criterion, or null if this value
* was not set.
*
* @return The certificate.
*/
public X509Certificate getCertificate()
{
return cert;
}
/**
* Returns the date at which certificates must be valid, or null
* if this criterion was not set.
*
* @return The target certificate valitity date.
*/
public Date getCertificateValid()
{
if (certValid != null)
return (Date) certValid.clone();
else
return null;
}
/**
* Returns the set of extended key purpose IDs, as an unmodifiable set
* of OID strings. Returns null if this criterion is not
* set.
*
* @return The set of key purpose OIDs (strings).
*/
public Set getExtendedKeyUsage()
{
if (keyPurposeSet != null)
return Collections.unmodifiableSet(keyPurposeSet);
else
return null;
}
/**
* Returns the issuer criterion as a sequence of DER bytes, or
* null if this value was not set.
*
* @return The issuer.
*/
public byte[] getIssuerAsBytes() throws IOException
{
if (issuer != null)
return issuer.getEncoded();
else
return null;
}
/**
* Returns the issuer criterion as a string, or null if this
* value was not set.
*
* @return The issuer.
*/
public String getIssuerAsString()
{
if (issuer != null)
return issuer.getName();
else
return null;
}
/**
* Returns the public key usage criterion, or null if this
* value is not set. Note that the array is cloned to prevent modification.
*
* @return The public key usage.
*/
public boolean[] getKeyUsage()
{
if (keyUsage != null)
return (boolean[]) keyUsage.clone();
else
return null;
}
/**
* Returns whether or not all specified alternative names must match.
* If false, a certificate is considered a match if one of the
* specified alternative names matches.
*
* @return true if all names must match.
*/
public boolean getMatchAllSubjectAltNames()
{
return matchAllNames;
}
/**
* Returns the name constraints criterion, or null if this
* value is not set. Note that the byte array is cloned to prevent
* modification.
*
* @return The name constraints.
*/
public byte[] getNameConstraints()
{
if (nameConstraints != null)
return (byte[]) nameConstraints.clone();
else
return null;
}
public Collection> getPathToNames()
{
if (pathToNames != null)
{
List> names = new ArrayList>(pathToNames.size());
for (GeneralName name : pathToNames)
{
List