// Methods

		public int Add (X509Certificate value)
		{
			if (value == null)
				throw new ArgumentNullException ("value");
			
			return InnerList.Add (value);
		}
		public void AddRange (X509Certificate [] value) 
		{
			if (value == null)
				throw new ArgumentNullException ("value");

			for (int i = 0; i < value.Length; i++) 
				InnerList.Add (value [i]);
		}
		public void CopyTo (X509Certificate[] array, int index)
		{
			InnerList.CopyTo (array, index);
		}
		public bool Contains (X509Certificate value) 
		{
			return (IndexOf (value) != -1);
		}
		public void Remove (X509Certificate value)
		{
			InnerList.Remove (value);
		}
		public X509CertificateCollection (X509Certificate [] value) 
		{
			AddRange (value);
		}
		public int IndexOf (X509Certificate value)
		{
			if (value == null)
				throw new ArgumentNullException ("value");

			byte[] hash = value.Hash;
			for (int i=0; i < InnerList.Count; i++) {
				X509Certificate x509 = (X509Certificate) InnerList [i];
				if (Compare (x509.Hash, hash))
					return i;
			}
			return -1;
		}
		public void Insert (int index, X509Certificate value)
		{
			InnerList.Insert (index, value);
		}
Beispiel #9
0
		public IDictionary GetAttributes (X509Certificate cert)
		{
			IDictionary result = new Hashtable ();

			foreach (SafeBag sb in _safeBags) {
				if (sb.BagOID.Equals (certBag)) {
					ASN1 safeBag = sb.ASN1;
					ASN1 bagValue = safeBag [1];
					PKCS7.ContentInfo crt = new PKCS7.ContentInfo (bagValue.Value);
					X509Certificate xc = new X509Certificate (crt.Content [0].Value);

					if (Compare (cert.RawData, xc.RawData)) {
						if (safeBag.Count == 3) {
							ASN1 bagAttributes = safeBag [2];

							for (int i = 0; i < bagAttributes.Count; i++) {
								ASN1 pkcs12Attribute = bagAttributes [i];
								ASN1 attrId = pkcs12Attribute [0];

								string aOid = ASN1Convert.ToOid (attrId);
								ArrayList aValues = new ArrayList ();

								ASN1 attrValues = pkcs12Attribute [1];
									
								for (int j = 0; j < attrValues.Count; j++) {
									ASN1 attrValue = attrValues [j];
									aValues.Add (attrValue.Value);
								}
								result.Add (aOid, aValues);
							}
						}
					}
				}
			}

			return result;
		}
Beispiel #10
0
		private void ReadSafeBag (ASN1 safeBag) 
		{
			if (safeBag.Tag != 0x30)
				throw new ArgumentException ("invalid safeBag");

			ASN1 bagId = safeBag [0];
			if (bagId.Tag != 0x06)
				throw new ArgumentException ("invalid safeBag id");

			ASN1 bagValue = safeBag [1];
			string oid = ASN1Convert.ToOid (bagId);
			switch (oid) {
				case keyBag:
					// NEED UNIT TEST
					AddPrivateKey (new PKCS8.PrivateKeyInfo (bagValue.Value));
					break;
				case pkcs8ShroudedKeyBag:
					PKCS8.EncryptedPrivateKeyInfo epki = new PKCS8.EncryptedPrivateKeyInfo (bagValue.Value);
					byte[] decrypted = Decrypt (epki.Algorithm, epki.Salt, epki.IterationCount, epki.EncryptedData);
					AddPrivateKey (new PKCS8.PrivateKeyInfo (decrypted));
					Array.Clear (decrypted, 0, decrypted.Length);
					break;
				case certBag:
					PKCS7.ContentInfo cert = new PKCS7.ContentInfo (bagValue.Value);
					if (cert.ContentType != x509Certificate)
						throw new NotSupportedException ("unsupport certificate type");
					X509Certificate x509 = new X509Certificate (cert.Content [0].Value);
					_certs.Add (x509);
					break;
				case crlBag:
					// TODO
					break;
				case secretBag: 
					byte[] secret = bagValue.Value;
					_secretBags.Add(secret);
					break;
				case safeContentsBag:
					// TODO - ? recurse ?
					break;
				default:
					throw new ArgumentException ("unknown safeBag oid");
			}

			if (safeBag.Count > 2) {
				ASN1 bagAttributes = safeBag [2];
				if (bagAttributes.Tag != 0x31)
					throw new ArgumentException ("invalid safeBag attributes id");

				for (int i = 0; i < bagAttributes.Count; i++) {
					ASN1 pkcs12Attribute = bagAttributes[i];
						
					if (pkcs12Attribute.Tag != 0x30)
						throw new ArgumentException ("invalid PKCS12 attributes id");

					ASN1 attrId = pkcs12Attribute [0];
					if (attrId.Tag != 0x06)
						throw new ArgumentException ("invalid attribute id");
						
					string attrOid = ASN1Convert.ToOid (attrId);

					ASN1 attrValues = pkcs12Attribute[1];
					for (int j = 0; j < attrValues.Count; j++) {
						ASN1 attrValue = attrValues[j];

						switch (attrOid) {
						case PKCS9.friendlyName:
							if (attrValue.Tag != 0x1e)
								throw new ArgumentException ("invalid attribute value id");
							break;
						case PKCS9.localKeyId:
							if (attrValue.Tag != 0x04)
								throw new ArgumentException ("invalid attribute value id");
							break;
						default:
							// Unknown OID -- don't check Tag
							break;
						}
					}
				}
			}

			_safeBags.Add (new SafeBag(oid, safeBag));
		}
Beispiel #11
0
		public void RemoveCertificate (X509Certificate cert, IDictionary attrs)
		{
			int certIndex = -1;

			for (int i = 0; certIndex == -1 && i < _safeBags.Count; i++) {
				SafeBag sb = (SafeBag)_safeBags [i];

				if (sb.BagOID.Equals (certBag)) {
					ASN1 safeBag = sb.ASN1;
					ASN1 bagValue = safeBag [1];
					PKCS7.ContentInfo crt = new PKCS7.ContentInfo (bagValue.Value);
					X509Certificate c = new X509Certificate (crt.Content [0].Value);
					if (Compare (cert.RawData, c.RawData)) {
						if (attrs != null) {
							if (safeBag.Count == 3) {
								ASN1 bagAttributes = safeBag [2];
								int bagAttributesFound = 0;
								for (int j = 0; j < bagAttributes.Count; j++) {
									ASN1 pkcs12Attribute = bagAttributes [j];
									ASN1 attrId = pkcs12Attribute [0];
									string ao = ASN1Convert.ToOid (attrId);
									ArrayList dattrValues = (ArrayList)attrs [ao];

									if (dattrValues != null) {
										ASN1 attrValues = pkcs12Attribute [1];

										if (dattrValues.Count == attrValues.Count) {
											int attrValuesFound = 0;
											for (int k = 0; k < attrValues.Count; k++) {
												ASN1 attrValue = attrValues [k];
												byte[] value = (byte[])dattrValues [k];
									
												if (Compare (value, attrValue.Value)) {
													attrValuesFound += 1;
												}
											}
											if (attrValuesFound == attrValues.Count) {
												bagAttributesFound += 1;
											}
										}
									}
								}
								if (bagAttributesFound == bagAttributes.Count) {
									certIndex = i;
								}
							}
						} else {
							certIndex = i;
						}
					}
				}
			}

			if (certIndex != -1) {
				_safeBags.RemoveAt (certIndex);
				_certsChanged = true;
			}
		}
Beispiel #12
0
		public void RemoveCertificate (X509Certificate cert)
		{
			RemoveCertificate (cert, null);
		}
Beispiel #13
0
		public void AddCertificate (X509Certificate cert, IDictionary attributes)
		{
			bool found = false;

			for (int i = 0; !found && i < _safeBags.Count; i++) {
				SafeBag sb = (SafeBag)_safeBags [i];

				if (sb.BagOID.Equals (certBag)) {
					ASN1 safeBag = sb.ASN1;
					ASN1 bagValue = safeBag [1];
					PKCS7.ContentInfo crt = new PKCS7.ContentInfo (bagValue.Value);
					X509Certificate c = new X509Certificate (crt.Content [0].Value);
					if (Compare (cert.RawData, c.RawData)) {
						found = true;
					}
				}
			}

			if (!found) {
				_safeBags.Add (new SafeBag (certBag, CertificateSafeBag (cert, attributes)));
				_certsChanged = true;
			}
		}
Beispiel #14
0
		public void AddCertificate (X509Certificate cert)
		{
			AddCertificate (cert, null);
		}
Beispiel #15
0
		private ASN1 CertificateSafeBag (X509Certificate x509, IDictionary attributes) 
		{
			ASN1 encapsulatedCertificate = new ASN1 (0x04, x509.RawData);

			PKCS7.ContentInfo ci = new PKCS7.ContentInfo ();
			ci.ContentType = x509Certificate;
			ci.Content.Add (encapsulatedCertificate);

			ASN1 bagValue = new ASN1 (0xA0);
			bagValue.Add (ci.ASN1);

			ASN1 safeBag = new ASN1 (0x30);
			safeBag.Add (ASN1Convert.FromOid (certBag));
			safeBag.Add (bagValue);

			if (attributes != null) {
				ASN1 bagAttributes = new ASN1 (0x31);
				IDictionaryEnumerator de = attributes.GetEnumerator ();

				while (de.MoveNext ()) {
					string oid = (string)de.Key;
					switch (oid) {
					case PKCS9.friendlyName:
						ArrayList names = (ArrayList)de.Value;
						if (names.Count > 0) {
							ASN1 pkcs12Attribute = new ASN1 (0x30);
							pkcs12Attribute.Add (ASN1Convert.FromOid (PKCS9.friendlyName));
							ASN1 attrValues = new ASN1 (0x31);
							foreach (byte[] name in names) {
								ASN1 attrValue = new ASN1 (0x1e);
								attrValue.Value = name;
								attrValues.Add (attrValue);
							}
							pkcs12Attribute.Add (attrValues);
							bagAttributes.Add (pkcs12Attribute);
						}
						break;
					case PKCS9.localKeyId:
						ArrayList keys = (ArrayList)de.Value;
						if (keys.Count > 0) {
							ASN1 pkcs12Attribute = new ASN1 (0x30);
							pkcs12Attribute.Add (ASN1Convert.FromOid (PKCS9.localKeyId));
							ASN1 attrValues = new ASN1 (0x31);
							foreach (byte[] key in keys) {
								ASN1 attrValue = new ASN1 (0x04);
								attrValue.Value = key;
								attrValues.Add (attrValue);
							}
							pkcs12Attribute.Add (attrValues);
							bagAttributes.Add (pkcs12Attribute);
						}
						break;
					default:
						break;
					}
				}

				if (bagAttributes.Count > 0) {
					safeBag.Add (bagAttributes);
				}
			}

			return safeBag;
		}