private OidEnumerator GetEnumerator () { OidCollection oc = new OidCollection (); oc.Add (new Oid ("1.0")); oc.Add (new Oid ("1.1")); oc.Add (new Oid ("1.2")); return oc.GetEnumerator (); }
//BUG [ExpectedException (typeof (ArgumentNullException))] public void AddNull () { OidCollection oc = new OidCollection (); oc.Add (null); Assert.AreEqual (1, oc.Count, "Count"); // Assert.IsNull (oc, "[0]"); throw NullReferenceException }
public void CopyToOidNull () { OidCollection oc = new OidCollection (); oc.Add (new Oid ("1.0")); Oid[] array = null; oc.CopyTo (array, 0); }
//BUG [ExpectedException (typeof (ArgumentNullException))] public void AddNull () { OidCollection oc = new OidCollection (); oc.Add (null); AssertEquals ("Count", 1, oc.Count); // AssertNull ("[0]", oc); throw NullReferenceException }
public void Add () { OidCollection oc = new OidCollection (); oc.Add (new Oid ("1.0")); Assert.AreEqual (1, oc.Count, "Count"); Assert.AreEqual ("1.0", oc [0].Value, "[0]"); Assert.AreEqual ("1.0", oc ["1.0"].Value, "['1.0']"); }
public void CopyToOid () { OidCollection oc = new OidCollection (); oc.Add (new Oid ("1.0")); Oid[] array = new Oid [1]; oc.CopyTo (array, 0); Assert.AreEqual ("1.0", array [0].Value, "CopyTo(Oid)"); }
public void Add () { OidCollection oc = new OidCollection (); oc.Add (new Oid ("1.0")); AssertEquals ("Count", 1, oc.Count); AssertEquals ("[0]", "1.0", oc [0].Value); AssertEquals ("['1.0']", "1.0", oc ["1.0"].Value); }
internal OidCollection ReadOnlyCopy() { OidCollection copy = new OidCollection(); foreach (Oid oid in _list) { copy.Add(oid); } copy._readOnly = true; return(copy); }
internal OidCollection ReadOnlyCopy() { OidCollection oidCollection = new OidCollection(); foreach (object obj in this._list) { Oid oid = (Oid)obj; oidCollection.Add(oid); } oidCollection._readOnly = true; return(oidCollection); }
/// <summary> /// Creates a new self-signed X509 certificate /// </summary> /// <param name="issuer">The certificate issuer</param> /// <param name="friendlyName">Human readable name</param> /// <param name="password">The certificate's password</param> /// <param name="startTime">Certificate creation date & time</param> /// <param name="endTime">Certificate expiry date & time</param> /// <returns>An X509Certificate2</returns> public static X509Certificate2 CreateSelfSignedCert(string issuer, string friendlyName, string password, DateTime startTime, DateTime endTime) { string distinguishedNameString = issuer; var key = Create2048RsaKey(); var creationParams = new X509CertificateCreationParameters(new X500DistinguishedName(distinguishedNameString)) { TakeOwnershipOfKey = true, StartTime = startTime, EndTime = endTime }; // adding client authentication, -eku = 1.3.6.1.5.5.7.3.2, // This is mandatory for the upload to be successful OidCollection oidCollection = new OidCollection(); oidCollection.Add(new Oid(OIDClientAuthValue, OIDClientAuthFriendlyName)); creationParams.Extensions.Add(new X509EnhancedKeyUsageExtension(oidCollection, false)); // Documentation of CreateSelfSignedCertificate states: // If creationParameters have TakeOwnershipOfKey set to true, the certificate // generated will own the key and the input CngKey will be disposed to ensure // that the caller doesn't accidentally use it beyond its lifetime (which is // now controlled by the certificate object). // We don't dispose it ourselves in this case. var cert = key.CreateSelfSignedCertificate(creationParams); key = null; cert.FriendlyName = friendlyName; // X509 certificate needs PersistKeySet flag set. // Reload a new X509Certificate2 instance from exported bytes in order to set the PersistKeySet flag. var bytes = cert.Export(X509ContentType.Pfx, password); // NOTE: PfxValidation is not done here because these are newly created certs and assumed valid. ICommonEventSource evtSource = null; return X509Certificate2Helper.NewX509Certificate2(bytes, password, X509KeyStorageFlags.PersistKeySet | X509KeyStorageFlags.Exportable, evtSource, doPfxValidation: false); }
public unsafe void DecodeX509EnhancedKeyUsageExtension(byte[] encoded, out OidCollection usages) { OidCollection oids = new OidCollection(); using (SafeEkuExtensionHandle eku = Interop.libcrypto.OpenSslD2I(Interop.libcrypto.d2i_EXTENDED_KEY_USAGE, encoded)) { Interop.libcrypto.CheckValidOpenSslHandle(eku); int count = Interop.Crypto.GetX509EkuFieldCount(eku); for (int i = 0; i < count; i++) { IntPtr oidPtr = Interop.Crypto.GetX509EkuField(eku, i); if (oidPtr == IntPtr.Zero) { throw Interop.libcrypto.CreateOpenSslCryptographicException(); } string oidValue = Interop.libcrypto.OBJ_obj2txt_helper(oidPtr); oids.Add(new Oid(oidValue)); } } usages = oids; }
public void ConstructorOidCollection () { OidCollection oc = new OidCollection (); X509EnhancedKeyUsageExtension eku = new X509EnhancedKeyUsageExtension (oc, true); Assert.AreEqual ("30-00", BitConverter.ToString (eku.RawData), "RawData"); Assert.AreEqual (0, eku.EnhancedKeyUsages.Count, "Count 0"); // FIXME: Don't expect that FriendlyName is English. This test fails under non-English Windows. //Assert.AreEqual ("Information Not Available", eku.Format (true), "Format(true)"); //Assert.AreEqual ("Information Not Available", eku.Format (false), "Format(false)"); oc.Add (new Oid ("1.2.3.4")); Assert.AreEqual (0, eku.EnhancedKeyUsages.Count, "Count still 0"); int n = eku.EnhancedKeyUsages.Add (new Oid ("1.2.3")); Assert.AreEqual (0, n, "Add"); Assert.AreEqual (0, eku.EnhancedKeyUsages.Count, "Count again 0"); // readonly! Assert.AreEqual (1, oc.Count, "Count 1 - oc"); Assert.AreEqual ("1.2.3.4", oc [0].Value, "Value - oc"); oc.Add (new Oid ("1.3.6.1.5.5.7.3.1")); eku = new X509EnhancedKeyUsageExtension (oc, true); Assert.AreEqual (2, eku.EnhancedKeyUsages.Count, "Count 2"); Assert.AreEqual ("1.2.3.4", eku.EnhancedKeyUsages[0].Value, "Value - 1"); Assert.AreEqual ("1.3.6.1.5.5.7.3.1", eku.EnhancedKeyUsages[1].Value, "Value - 2"); // FIXME: Don't expect that FriendlyName is English. This test fails under non-English Windows. //Assert.AreEqual ("Unknown Key Usage (1.2.3.4)" + Environment.NewLine + "Server Authentication (1.3.6.1.5.5.7.3.1)" + Environment.NewLine, // eku.Format (true), "Format(true)"); //Assert.AreEqual ("Unknown Key Usage (1.2.3.4), Server Authentication (1.3.6.1.5.5.7.3.1)", eku.Format (false), "Format(false)"); }
public void WrongExtension_X509Extension () { X509Extension ex = new X509Extension ("1.2.3", new byte[0], true); OidCollection oc = new OidCollection (); oc.Add (new Oid ("1.2.3.4")); X509EnhancedKeyUsageExtension eku = new X509EnhancedKeyUsageExtension (oc, false); Assert.AreEqual (1, eku.EnhancedKeyUsages.Count, "EnhancedKeyUsages"); Assert.IsFalse (eku.Critical, "Critical"); eku.CopyFrom (ex); Assert.IsTrue (eku.Critical, "Critical"); Assert.AreEqual (String.Empty, BitConverter.ToString (eku.RawData), "RawData"); Assert.AreEqual ("1.2.3", eku.Oid.Value, "Oid.Value"); Assert.IsNull (eku.Oid.FriendlyName, "Oid.FriendlyName"); }
public void CopyFrom_Self () { OidCollection oc = new OidCollection (); oc.Add (new Oid ("1.2.3.4")); X509EnhancedKeyUsageExtension eku = new X509EnhancedKeyUsageExtension (oc, true); Assert.IsTrue (eku.Critical, "Critical"); byte[] raw = eku.RawData; Assert.AreEqual ("30-05-06-03-2A-03-04", BitConverter.ToString (raw), "RawData"); AsnEncodedData aed = new AsnEncodedData (raw); X509EnhancedKeyUsageExtension copy = new X509EnhancedKeyUsageExtension (aed, false); Assert.IsFalse (copy.Critical, "Critical"); Assert.AreEqual (7, copy.RawData.Length, "RawData"); // original Oid ignored Assert.AreEqual (oid, copy.Oid.Value, "Oid.Value"); // FIXME: Don't expect that FriendlyName is English. This test fails under non-English Windows. //Assert.AreEqual (fname, copy.Oid.FriendlyName, "Oid.FriendlyName"); Assert.AreEqual (1, copy.EnhancedKeyUsages.Count, "EnhancedKeyUsages"); Assert.AreEqual ("1.2.3.4", copy.EnhancedKeyUsages[0].Value, "EnhancedKeyUsages Oid"); }
internal OidCollection ReadOnlyCopy () { OidCollection copy = new OidCollection (); foreach (Oid oid in _list) { copy.Add (oid); } copy._readOnly = true; return copy; }
public void DecodeX509EnhancedKeyUsageExtension(byte[] encoded, out OidCollection usages) { OidCollection localUsages = new OidCollection(); unsafe { encoded.DecodeObject( CryptDecodeObjectStructType.X509_ENHANCED_KEY_USAGE, delegate (void* pvDecoded) { CERT_ENHKEY_USAGE* pEnhKeyUsage = (CERT_ENHKEY_USAGE*)pvDecoded; int count = pEnhKeyUsage->cUsageIdentifier; for (int i = 0; i < count; i++) { IntPtr oidValuePointer = pEnhKeyUsage->rgpszUsageIdentifier[i]; string oidValue = Marshal.PtrToStringAnsi(oidValuePointer); Oid oid = new Oid(oidValue); localUsages.Add(oid); } } ); } usages = localUsages; }
private void DecodeExtension () { uint cbDecoded = 0; SafeLocalAllocHandle decoded = null; bool result = CAPI.DecodeObject(new IntPtr(CAPI.X509_ENHANCED_KEY_USAGE), m_rawData, out decoded, out cbDecoded); if (result == false) throw new CryptographicException(Marshal.GetLastWin32Error()); CAPI.CERT_ENHKEY_USAGE pEnhKeyUsage = (CAPI.CERT_ENHKEY_USAGE) Marshal.PtrToStructure(decoded.DangerousGetHandle(), typeof(CAPI.CERT_ENHKEY_USAGE)); m_enhancedKeyUsages = new OidCollection(); for (int index = 0; index < pEnhKeyUsage.cUsageIdentifier; index++) { IntPtr pszOid = Marshal.ReadIntPtr(new IntPtr((long) pEnhKeyUsage.rgpszUsageIdentifier + index * Marshal.SizeOf(typeof(IntPtr)))); string oidValue = Marshal.PtrToStringAnsi(pszOid); Oid oid = new Oid(oidValue, OidGroup.ExtensionOrAttribute, false); m_enhancedKeyUsages.Add(oid); } m_decoded = true; decoded.Dispose(); }
public void DecodeX509EnhancedKeyUsageExtension(byte[] encoded, out OidCollection usages) { OidCollection oids = new OidCollection(); using (SafeEkuExtensionHandle eku = Interop.Crypto.DecodeExtendedKeyUsage(encoded, encoded.Length)) { Interop.Crypto.CheckValidOpenSslHandle(eku); int count = Interop.Crypto.GetX509EkuFieldCount(eku); for (int i = 0; i < count; i++) { IntPtr oidPtr = Interop.Crypto.GetX509EkuField(eku, i); if (oidPtr == IntPtr.Zero) { throw Interop.Crypto.CreateOpenSslCryptographicException(); } string oidValue = Interop.Crypto.GetOidValue(oidPtr); oids.Add(new Oid(oidValue)); } } usages = oids; }