Org.BouncyCastle.X509.X509V2AttributeCertificate.GetAttributes C# (CSharp) Method

GetAttributes() public method

public GetAttributes ( ) : Org.BouncyCastle.X509.X509Attribute[]
return Org.BouncyCastle.X509.X509Attribute[]
		public virtual X509Attribute[] GetAttributes()
		{
			Asn1Sequence seq = cert.ACInfo.Attributes;
			X509Attribute[] attrs = new X509Attribute[seq.Count];

			for (int i = 0; i != seq.Count; i++)
			{
				attrs[i] = new X509Attribute((Asn1Encodable)seq[i]);
			}

			return attrs;
		}

Same methods

X509V2AttributeCertificate::GetAttributes ( string oid ) : Org.BouncyCastle.X509.X509Attribute[]

Usage Example

Example #1
0
		public override void PerformTest()
		{
			IX509AttributeCertificate aCert = new X509V2AttributeCertificate(attrCert);
			X509CertificateParser fact = new X509CertificateParser();
			X509Certificate sCert = fact.ReadCertificate(signCert);

			aCert.Verify(sCert.GetPublicKey());

			//
			// search test
			//
			IList list = new ArrayList();

			list.Add(sCert);

//			CollectionCertStoreParameters ccsp = new CollectionCertStoreParameters(list);
//			CertStore store = CertStore.getInstance("Collection", ccsp);
			IX509Store store = X509StoreFactory.Create(
				"Certificate/Collection",
				new X509CollectionStoreParameters(list));

			ArrayList certs = new ArrayList(
//				store.getCertificates(aCert.getIssuer()));
				store.GetMatches(aCert.Issuer));

			if (certs.Count != 1 || !certs.Contains(sCert))
			{
				Fail("sCert not found by issuer");
			}

			X509Attribute[] attrs = aCert.GetAttributes("1.3.6.1.4.1.6760.8.1.1");
			if (attrs == null || attrs.Length != 1)
			{
				Fail("attribute not found");
			}

			//
			// reencode test
			//
			aCert = new X509V2AttributeCertificate(aCert.GetEncoded());

			aCert.Verify(sCert.GetPublicKey());

			IX509AttributeCertificate saCert = new X509V2AttributeCertificate(aCert.GetEncoded());

			if (!aCert.NotAfter.Equals(saCert.NotAfter))
			{
				Fail("failed date comparison");
			}

			// base generator test

			//
			// a sample key pair.
			//
			RsaKeyParameters pubKey = new RsaKeyParameters(
				false,
				new BigInteger("b4a7e46170574f16a97082b22be58b6a2a629798419be12872a4bdba626cfae9900f76abfb12139dce5de56564fab2b6543165a040c606887420e33d91ed7ed7", 16),
				new BigInteger("11", 16));

			AsymmetricKeyParameter privKey = RSA_PRIVATE_KEY_SPEC;

			//
			// set up the keys
			//
//			PrivateKey          privKey;
//			PublicKey           pubKey;
//
//			KeyFactory  kFact = KeyFactory.getInstance("RSA");
//
//			privKey = kFact.generatePrivate(privKeySpec);
//			pubKey = kFact.generatePublic(pubKeySpec);

			X509V2AttributeCertificateGenerator gen = new X509V2AttributeCertificateGenerator();

			gen.AddAttribute(attrs[0]);
			gen.SetHolder(aCert.Holder);
			gen.SetIssuer(aCert.Issuer);
			gen.SetNotBefore(DateTime.UtcNow.AddSeconds(-50));
			gen.SetNotAfter(DateTime.UtcNow.AddSeconds(50));
			gen.SetSerialNumber(aCert.SerialNumber);
			gen.SetSignatureAlgorithm("SHA1WithRSAEncryption");

			aCert = gen.Generate(privKey);

			aCert.CheckValidity();

			aCert.Verify(pubKey);

			// as the issuer is the same this should still work (even though it is not
			// technically correct

			certs = new ArrayList(
//				store.getCertificates(aCert.Issuer));
				store.GetMatches(aCert.Issuer));

			if (certs.Count != 1 || !certs.Contains(sCert))
			{
				Fail("sCert not found by issuer");
			}

			attrs = aCert.GetAttributes("1.3.6.1.4.1.6760.8.1.1");
			if (attrs == null || attrs.Length != 1)
			{
				Fail("attribute not found");
			}

			//
			// reencode test
			//
			aCert = new X509V2AttributeCertificate(aCert.GetEncoded());

			aCert.Verify(pubKey);

			AttributeCertificateIssuer issuer = aCert.Issuer;

			X509Name[] principals = issuer.GetPrincipals();

			//
			// test holder
			//
			AttributeCertificateHolder holder = aCert.Holder;

			if (holder.GetEntityNames() == null)
			{
				Fail("entity names not set");
			}

			if (holder.SerialNumber != null)
			{
				Fail("holder serial number set when none expected");
			}

			if (holder.GetIssuer() != null)
			{
				Fail("holder issuer set when none expected");
			}

			principals = holder.GetEntityNames();

			string ps = principals[0].ToString();

			// TODO Check that this is a good enough test
//			if (!ps.Equals("C=US, O=vt, OU=Class 2, OU=Virginia Tech User, CN=Markus Lorch (mlorch), [email protected]"))
			if (!principals[0].Equivalent(new X509Name("C=US, O=vt, OU=Class 2, OU=Virginia Tech User, CN=Markus Lorch (mlorch), [email protected]")))
			{
				Fail("principal[0] for entity names don't Match");
			}

			//
			// extension test
			//

			gen.AddExtension("1.1", true, new DerOctetString(new byte[10]));

			gen.AddExtension("2.2", false, new DerOctetString(new byte[20]));

			aCert = gen.Generate(privKey);

			ISet exts = aCert.GetCriticalExtensionOids();

			if (exts.Count != 1 || !exts.Contains("1.1"))
			{
				Fail("critical extension test failed");
			}

			exts = aCert.GetNonCriticalExtensionOids();

			if (exts.Count != 1 || !exts.Contains("2.2"))
			{
				Fail("non-critical extension test failed");
			}

			Asn1OctetString extString = aCert.GetExtensionValue(new DerObjectIdentifier("1.1"));
			Asn1Encodable extValue = X509ExtensionUtilities.FromExtensionValue(extString);

			if (!extValue.Equals(new DerOctetString(new byte[10])))
			{
				Fail("wrong extension value found for 1.1");
			}

			doTestCertWithBaseCertificateID();
			doTestGenerateWithCert();
			doTestGenerateWithPrincipal();
		}