using System;
using System.Collections;
using System.IO;
using Org.BouncyCastle.Asn1;
using Org.BouncyCastle.Asn1.X509;
using Org.BouncyCastle.Math;
using Org.BouncyCastle.Utilities.Collections;
using Org.BouncyCastle.Utilities.Date;
using Org.BouncyCastle.X509.Extension;
namespace Org.BouncyCastle.X509.Store
{
/**
* This class is an Selector
like implementation to select
* attribute certificates from a given set of criteria.
*
* @see org.bouncycastle.x509.X509AttributeCertificate
* @see org.bouncycastle.x509.X509Store
*/
public class X509AttrCertStoreSelector
: IX509Selector
{
// TODO: name constraints???
private IX509AttributeCertificate attributeCert;
private DateTimeObject attributeCertificateValid;
private AttributeCertificateHolder holder;
private AttributeCertificateIssuer issuer;
private BigInteger serialNumber;
private ISet targetNames = new HashSet();
private ISet targetGroups = new HashSet();
public X509AttrCertStoreSelector()
{
}
private X509AttrCertStoreSelector(
X509AttrCertStoreSelector o)
{
this.attributeCert = o.attributeCert;
this.attributeCertificateValid = o.attributeCertificateValid;
this.holder = o.holder;
this.issuer = o.issuer;
this.serialNumber = o.serialNumber;
this.targetGroups = new HashSet(o.targetGroups);
this.targetNames = new HashSet(o.targetNames);
}
/// true
if the object matches this selector.X509AttributeCertificate
* must contain at least one of the specified target names.
*
* Each attribute certificate may contain a target information extension * limiting the servers where this attribute certificate can be used. If * this extension is not present, the attribute certificate is not targeted * and may be accepted by any server. *
* * @param name The name as a GeneralName (notnull
)
*/
public void AddTargetName(
GeneralName name)
{
targetNames.Add(name);
}
/**
* Adds a target name criterion for the attribute certificate to the target
* information extension criteria. The X509AttributeCertificate
* must contain at least one of the specified target names.
* * Each attribute certificate may contain a target information extension * limiting the servers where this attribute certificate can be used. If * this extension is not present, the attribute certificate is not targeted * and may be accepted by any server. *
* * @param name a byte array containing the name in ASN.1 DER encoded form of a GeneralName * @throws IOException if a parsing error occurs. */ public void AddTargetName( byte[] name) { AddTargetName(GeneralName.GetInstance(Asn1Object.FromByteArray(name))); } /** * Adds a collection with target names criteria. Ifnull
is
* given any will do.
* * The collection consists of either GeneralName objects or byte[] arrays representing * DER encoded GeneralName structures. *
* * @param names A collection of target names. * @throws IOException if a parsing error occurs. * @see #AddTargetName(byte[]) * @see #AddTargetName(GeneralName) */ public void SetTargetNames( IEnumerable names) { targetNames = ExtractGeneralNames(names); } /** * Gets the target names. The collection consists ofList
s
* made up of an Integer
in the first entry and a DER encoded
* byte array or a String
in the second entry.
* The returned collection is immutable.
* * @return The collection of target names * @see #setTargetNames(Collection) */ public IEnumerable GetTargetNames() { return new EnumerableProxy(targetNames); } /** * Adds a target group criterion for the attribute certificate to the target * information extension criteria. TheX509AttributeCertificate
* must contain at least one of the specified target groups.
* * Each attribute certificate may contain a target information extension * limiting the servers where this attribute certificate can be used. If * this extension is not present, the attribute certificate is not targeted * and may be accepted by any server. *
* * @param group The group as GeneralName form (notnull
)
*/
public void AddTargetGroup(
GeneralName group)
{
targetGroups.Add(group);
}
/**
* Adds a target group criterion for the attribute certificate to the target
* information extension criteria. The X509AttributeCertificate
* must contain at least one of the specified target groups.
* * Each attribute certificate may contain a target information extension * limiting the servers where this attribute certificate can be used. If * this extension is not present, the attribute certificate is not targeted * and may be accepted by any server. *
* * @param name a byte array containing the group in ASN.1 DER encoded form of a GeneralName * @throws IOException if a parsing error occurs. */ public void AddTargetGroup( byte[] name) { AddTargetGroup(GeneralName.GetInstance(Asn1Object.FromByteArray(name))); } /** * Adds a collection with target groups criteria. Ifnull
is
* given any will do.
*
* The collection consists of GeneralName
objects or byte[]
* representing DER encoded GeneralNames.
*
List
s
* made up of an Integer
in the first entry and a DER encoded
* byte array or a String
in the second entry.
* The returned collection is immutable.
* * @return The collection of target groups. * @see #setTargetGroups(Collection) */ public IEnumerable GetTargetGroups() { return new EnumerableProxy(targetGroups); } private ISet ExtractGeneralNames( IEnumerable names) { ISet result = new HashSet(); if (names != null) { foreach (object o in names) { if (o is GeneralName) { result.Add(o); } else { result.Add(GeneralName.GetInstance(Asn1Object.FromByteArray((byte[]) o))); } } } return result; } } }