Author: spouliot
Date: 2006-11-17 15:42:40 -0500 (Fri, 17 Nov 2006)
New Revision: 68096
Modified:
trunk/mcs/class/System/System.Security.Cryptography.X509Certificates/ChangeLog
trunk/mcs/class/System/System.Security.Cryptography.X509Certificates/X509Certificate2.cs
trunk/mcs/class/System/System.Security.Cryptography.X509Certificates/X509Certificate2Collection.cs
trunk/mcs/class/System/System.Security.Cryptography.X509Certificates/X509ExtensionCollection.cs
trunk/mcs/class/System/System.Security.Cryptography.X509Certificates/X509ExtensionEnumerator.cs
trunk/mcs/class/System/System.Security.Cryptography.X509Certificates/X509SubjectKeyIdentifierExtension.cs
Log:
2006-11-17 Sebastien Pouliot <[EMAIL PROTECTED]>
* X509Certificate2Collection.cs: Implement Export (to DER encoded
format), Find (for most X509FindType), Import (for single
certificates), RemoveRange (but it's not transactional).
* X509Certificate2.cs: Fix Reset method to reset every field.
* X509ExtensionCollection.cs: Implement missing CopyTo. Fix exception
handling to match new unit tests.
* X509ExtensionEnumerator.cs: Fix recursion.
* X509SubjectKeyIdentifierExtension.cs: Add support for ctors
accepting a PublicKey instance.
Modified:
trunk/mcs/class/System/System.Security.Cryptography.X509Certificates/ChangeLog
===================================================================
---
trunk/mcs/class/System/System.Security.Cryptography.X509Certificates/ChangeLog
2006-11-17 20:35:16 UTC (rev 68095)
+++
trunk/mcs/class/System/System.Security.Cryptography.X509Certificates/ChangeLog
2006-11-17 20:42:40 UTC (rev 68096)
@@ -1,3 +1,15 @@
+2006-11-17 Sebastien Pouliot <[EMAIL PROTECTED]>
+
+ * X509Certificate2Collection.cs: Implement Export (to DER encoded
+ format), Find (for most X509FindType), Import (for single
+ certificates), RemoveRange (but it's not transactional).
+ * X509Certificate2.cs: Fix Reset method to reset every field.
+ * X509ExtensionCollection.cs: Implement missing CopyTo. Fix exception
+ handling to match new unit tests.
+ * X509ExtensionEnumerator.cs: Fix recursion.
+ * X509SubjectKeyIdentifierExtension.cs: Add support for ctors
+ accepting a PublicKey instance.
+
2006-11-13 Sebastien Pouliot <[EMAIL PROTECTED]>
* X509Certificate2.cs: Throw a CryptographicException for (most)
Modified:
trunk/mcs/class/System/System.Security.Cryptography.X509Certificates/X509Certificate2.cs
===================================================================
---
trunk/mcs/class/System/System.Security.Cryptography.X509Certificates/X509Certificate2.cs
2006-11-17 20:35:16 UTC (rev 68095)
+++
trunk/mcs/class/System/System.Security.Cryptography.X509Certificates/X509Certificate2.cs
2006-11-17 20:42:40 UTC (rev 68096)
@@ -314,6 +314,8 @@
{
switch (nameType) {
case X509NameType.SimpleName:
+ if (_cert == null)
+ throw new CryptographicException
(empty_error);
// return CN= or, if missing, the first part of
the DN
ASN1 sn = forIssuer ? _cert.GetIssuerName () :
_cert.GetSubjectName ();
ASN1 dn = Find (commonName, sn);
@@ -487,8 +489,15 @@
public override void Reset ()
{
+ _cert = null;
+ _archived = false;
+ _extensions = null;
+ _name = String.Empty;
_serial = null;
_publicKey = null;
+ issuer_name = null;
+ subject_name = null;
+ signature_algorithm = null;
base.Reset ();
}
Modified:
trunk/mcs/class/System/System.Security.Cryptography.X509Certificates/X509Certificate2Collection.cs
===================================================================
---
trunk/mcs/class/System/System.Security.Cryptography.X509Certificates/X509Certificate2Collection.cs
2006-11-17 20:35:16 UTC (rev 68095)
+++
trunk/mcs/class/System/System.Security.Cryptography.X509Certificates/X509Certificate2Collection.cs
2006-11-17 20:42:40 UTC (rev 68096)
@@ -7,7 +7,7 @@
//
// (C) 2003 Motus Technologies Inc. (http://www.motus.com)
// Copyright (C) Tim Coleman, 2004
-// Copyright (C) 2005 Novell Inc. (http://www.novell.com)
+// Copyright (C) 2005, 2006 Novell Inc. (http://www.novell.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
@@ -32,6 +32,7 @@
#if NET_2_0 && SECURITY_DEP
using System.Collections;
+using System.Globalization;
namespace System.Security.Cryptography.X509Certificates {
@@ -81,7 +82,7 @@
return InnerList.Add (certificate);
}
- // note: transactional
+ [MonoTODO ("Method isn't transactional (like documented)")]
public void AddRange (X509Certificate2[] certificates)
{
if (certificates == null)
@@ -91,7 +92,7 @@
InnerList.Add (certificates [i]);
}
- // note: transactional
+ [MonoTODO ("Method isn't transactional (like documented)")]
public void AddRange (X509Certificate2Collection certificates)
{
if (certificates == null)
@@ -106,46 +107,244 @@
throw new ArgumentNullException ("certificate");
foreach (X509Certificate2 c in InnerList) {
- if (certificate.Equals (c))
+ if (c.Equals (certificate))
return true;
}
return false;
}
+ [MonoTODO ("only support X509ContentType.Cert")]
public byte[] Export (X509ContentType contentType)
{
- return null;
+ return Export (contentType, null);
}
+ [MonoTODO ("only support X509ContentType.Cert")]
public byte[] Export (X509ContentType contentType, string
password)
{
+ switch (contentType) {
+ case X509ContentType.Cert:
+ case X509ContentType.Pfx: // this includes Pkcs12
+ case X509ContentType.SerializedCert:
+ // if multiple certificates are present we only
export the last one
+ if (Count > 0)
+ return this [Count - 1].Export
(contentType, password);
+ break;
+ case X509ContentType.Pkcs7:
+ // TODO
+ break;
+ case X509ContentType.SerializedStore:
+ // TODO
+ break;
+ default:
+ // this includes Authenticode, Unknown and bad
values
+ string msg = Locale.GetText ("Cannot export
certificate(s) to the '{0}' format", contentType);
+ throw new CryptographicException (msg);
+ }
return null;
}
+ [MonoTODO ("Does not support X509FindType.FindByTemplateName,
FindByApplicationPolicy and FindByCertificatePolicy")]
public X509Certificate2Collection Find (X509FindType findType,
object findValue, bool validOnly)
{
- return null;
+ if (findValue == null)
+ throw new ArgumentNullException ("findValue");
+
+ string str = String.Empty;
+ string oid = String.Empty;
+ X509KeyUsageFlags ku = X509KeyUsageFlags.None;
+ DateTime dt = DateTime.MinValue;
+
+ switch (findType) {
+ case X509FindType.FindByThumbprint:
+ case X509FindType.FindBySubjectName:
+ case X509FindType.FindBySubjectDistinguishedName:
+ case X509FindType.FindByIssuerName:
+ case X509FindType.FindByIssuerDistinguishedName:
+ case X509FindType.FindBySerialNumber:
+ case X509FindType.FindByTemplateName:
+ case X509FindType.FindBySubjectKeyIdentifier:
+ try {
+ str = (string) findValue;
+ }
+ catch (Exception e) {
+ string msg = Locale.GetText ("Invalid
find value type '{0}', expected '{1}'.",
+ findValue.GetType (), "string");
+ throw new CryptographicException (msg,
e);
+ }
+ break;
+ case X509FindType.FindByApplicationPolicy:
+ case X509FindType.FindByCertificatePolicy:
+ case X509FindType.FindByExtension:
+ try {
+ oid = (string) findValue;
+ }
+ catch (Exception e) {
+ string msg = Locale.GetText ("Invalid
find value type '{0}', expected '{1}'.",
+ findValue.GetType (),
"X509KeyUsageFlags");
+ throw new CryptographicException (msg,
e);
+ }
+ // OID validation
+ try {
+ CryptoConfig.EncodeOID (oid);
+ }
+ catch
(CryptographicUnexpectedOperationException) {
+ string msg = Locale.GetText ("Invalid
OID value '{0}'.", oid);
+ throw new ArgumentException
("findValue", msg);
+ }
+ break;
+ case X509FindType.FindByKeyUsage:
+ try {
+ ku = (X509KeyUsageFlags) findValue;
+ }
+ catch (Exception e) {
+ string msg = Locale.GetText ("Invalid
find value type '{0}', expected '{1}'.",
+ findValue.GetType (),
"X509KeyUsageFlags");
+ throw new CryptographicException (msg,
e);
+ }
+ break;
+ case X509FindType.FindByTimeValid:
+ case X509FindType.FindByTimeNotYetValid:
+ case X509FindType.FindByTimeExpired:
+ try {
+ dt = (DateTime) findValue;
+ }
+ catch (Exception e) {
+ string msg = Locale.GetText ("Invalid
find value type '{0}', expected '{1}'.",
+ findValue.GetType (),
"X509DateTime");
+ throw new CryptographicException (msg,e
);
+ }
+ break;
+ default:
+ string msg = Locale.GetText ("Invalid find type
'{0}'.", findType);
+ throw new CryptographicException (msg);
+ }
+
+ CultureInfo cinv = CultureInfo.InvariantCulture;
+ X509Certificate2Collection results = new
X509Certificate2Collection ();
+ foreach (X509Certificate2 x in InnerList) {
+ bool value_match = false;
+
+ switch (findType) {
+ case X509FindType.FindByThumbprint:
+ // works with Thumbprint,
GetCertHashString in both normal (upper) and lower case
+ value_match = ((String.Compare (str,
x.Thumbprint, true, cinv) == 0) ||
+ (String.Compare (str,
x.GetCertHashString (), true, cinv) == 0));
+ break;
+ case X509FindType.FindBySubjectName:
+ string sname = x.GetNameInfo
(X509NameType.SimpleName, false);
+ value_match = (sname.IndexOf (str,
StringComparison.InvariantCultureIgnoreCase) >= 0);
+ break;
+ case
X509FindType.FindBySubjectDistinguishedName:
+ value_match = (String.Compare (str,
x.Subject, true, cinv) == 0);
+ break;
+ case X509FindType.FindByIssuerName:
+ string iname = x.GetNameInfo
(X509NameType.SimpleName, true);
+ value_match = (iname.IndexOf (str,
StringComparison.InvariantCultureIgnoreCase) >= 0);
+ break;
+ case X509FindType.FindByIssuerDistinguishedName:
+ value_match = (String.Compare (str,
x.Issuer, true, cinv) == 0);
+ break;
+ case X509FindType.FindBySerialNumber:
+ value_match = (String.Compare (str,
x.SerialNumber, true, cinv) == 0);
+ break;
+ case X509FindType.FindByTemplateName:
+ // TODO - find a valid test case
+ break;
+ case X509FindType.FindBySubjectKeyIdentifier:
+ X509SubjectKeyIdentifierExtension ski =
(x.Extensions ["2.5.29.14"] as X509SubjectKeyIdentifierExtension);
+ if (ski != null) {
+ value_match = (String.Compare
(str, ski.SubjectKeyIdentifier, true, cinv) == 0);
+ }
+ break;
+ case X509FindType.FindByApplicationPolicy:
+ // note: include when no extensions are
present (even if v3)
+ value_match = (x.Extensions.Count == 0);
+ // TODO - find test case with extension
+ break;
+ case X509FindType.FindByCertificatePolicy:
+ // TODO - find test case with extension
+ break;
+ case X509FindType.FindByExtension:
+ value_match = (x.Extensions [oid] !=
null);
+ break;
+ case X509FindType.FindByKeyUsage:
+ X509KeyUsageExtension kue =
(x.Extensions ["2.5.29.15"] as X509KeyUsageExtension);
+ if (kue == null) {
+ // key doesn't have any hard
coded limitations
+ // note: MS doesn't check for
ExtendedKeyUsage
+ value_match = true;
+ } else {
+ value_match = ((kue.KeyUsages &
ku) == ku);
+ }
+ break;
+ case X509FindType.FindByTimeValid:
+ value_match = ((dt >= x.NotBefore) &&
(dt <= x.NotAfter));
+ break;
+ case X509FindType.FindByTimeNotYetValid:
+ value_match = (dt < x.NotBefore);
+ break;
+ case X509FindType.FindByTimeExpired:
+ value_match = (dt > x.NotAfter);
+ break;
+ }
+
+ if (!value_match)
+ continue;
+
+ if (validOnly) {
+ try {
+ if (x.Verify ())
+ results.Add (x);
+ }
+ catch {
+ }
+ } else {
+ results.Add (x);
+ }
+ }
+ return results;
}
public new X509Certificate2Enumerator GetEnumerator ()
{
- return null;
+ return new X509Certificate2Enumerator (this);
}
+ [MonoTODO ("same limitations as X509Certificate2.Import")]
public void Import (byte[] rawData)
{
+ // FIXME: can it import multiple certificates, e.g. a
pkcs7 file ?
+ X509Certificate2 cert = new X509Certificate2 ();
+ cert.Import (rawData);
+ Add (cert);
}
+ [MonoTODO ("same limitations as X509Certificate2.Import")]
public void Import (byte[] rawData, string password,
X509KeyStorageFlags keyStorageFlags)
{
+ // FIXME: can it import multiple certificates, e.g. a
pkcs7 file ?
+ X509Certificate2 cert = new X509Certificate2 ();
+ cert.Import (rawData, password, keyStorageFlags);
+ Add (cert);
}
+ [MonoTODO ("same limitations as X509Certificate2.Import")]
public void Import (string fileName)
{
+ // FIXME: can it import multiple certificates, e.g. a
pkcs7 file ?
+ X509Certificate2 cert = new X509Certificate2 ();
+ cert.Import (fileName);
+ Add (cert);
}
+ [MonoTODO ("same limitations as X509Certificate2.Import")]
public void Import (string fileName, string password,
X509KeyStorageFlags keyStorageFlags)
{
+ // FIXME: can it import multiple certificates, e.g. a
pkcs7 file ?
+ X509Certificate2 cert = new X509Certificate2 ();
+ cert.Import (fileName, password, keyStorageFlags);
+ Add (cert);
}
public void Insert (int index, X509Certificate2 certificate)
@@ -166,8 +365,8 @@
throw new ArgumentNullException ("certificate");
for (int i=0; i < InnerList.Count; i++) {
- X509Certificate2 c = (X509Certificate2)
InnerList [i];
- if (certificate.Equals (c)) {
+ X509Certificate c = (X509Certificate) InnerList
[i];
+ if (c.Equals (certificate)) {
InnerList.RemoveAt (i);
// only first instance is removed
return;
@@ -175,18 +374,25 @@
}
}
- // note: transactional
+ [MonoTODO ("Method isn't transactional (like documented)")]
public void RemoveRange (X509Certificate2[] certificates)
{
if (certificates == null)
throw new ArgumentNullException ("certificate");
+
+ foreach (X509Certificate2 x in certificates)
+ Remove (x);
}
- // note: transactional
+ [MonoTODO ("Method isn't transactional (like documented)")]
public void RemoveRange (X509Certificate2Collection
certificates)
{
if (certificates == null)
throw new ArgumentNullException ("certificate");
+
+ // note: X509Certificate2Collection can contain both
X509Certificate2 and X509Certificate instances
+ foreach (X509Certificate2 x in certificates)
+ Remove (x);
}
}
}
Modified:
trunk/mcs/class/System/System.Security.Cryptography.X509Certificates/X509ExtensionCollection.cs
===================================================================
---
trunk/mcs/class/System/System.Security.Cryptography.X509Certificates/X509ExtensionCollection.cs
2006-11-17 20:35:16 UTC (rev 68095)
+++
trunk/mcs/class/System/System.Security.Cryptography.X509Certificates/X509ExtensionCollection.cs
2006-11-17 20:42:40 UTC (rev 68096)
@@ -7,7 +7,7 @@
//
// (C) 2003 Motus Technologies Inc. (http://www.motus.com)
// Copyright (C) Tim Coleman, 2004
-// Copyright (C) 2004-2005 Novell Inc. (http://www.novell.com)
+// Copyright (C) 2004-2006 Novell Inc. (http://www.novell.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
@@ -86,16 +86,25 @@
}
public object SyncRoot {
- get { return _list.SyncRoot; }
+ get { return this; }
}
public X509Extension this [int index] {
- get { return (X509Extension) _list [index]; }
+ get {
+ if (index < 0)
+ throw new InvalidOperationException
("index");
+ return (X509Extension) _list [index];
+ }
}
public X509Extension this [string oid] {
- get {
- foreach (X509Extension extension in this) {
+ get {
+ if (oid == null)
+ throw new ArgumentNullException ("oid");
+ if ((_list.Count == 0) || (oid.Length == 0))
+ return null;
+
+ foreach (X509Extension extension in _list) {
if (extension.Oid.Value.Equals (oid))
return extension;
}
@@ -107,33 +116,44 @@
public int Add (X509Extension extension)
{
+ if (extension == null)
+ throw new ArgumentNullException ("extension");
+
return _list.Add (extension);
}
- [MonoTODO]
public void CopyTo (X509Extension[] array, int index)
{
if (array == null)
throw new ArgumentNullException ("array");
if (index < 0)
- throw new ArgumentException ("negative index");
- if (index > array.Length)
- throw new ArgumentOutOfRangeException ("index >
array.Length");
+ throw new ArgumentOutOfRangeException
("negative index");
+ if (index >= array.Length)
+ throw new ArgumentOutOfRangeException ("index
>= array.Length");
+
+ _list.CopyTo (array, index);
}
void ICollection.CopyTo (Array array, int index)
{
+ if (array == null)
+ throw new ArgumentNullException ("array");
+ if (index < 0)
+ throw new ArgumentOutOfRangeException
("negative index");
+ if (index >= array.Length)
+ throw new ArgumentOutOfRangeException ("index
>= array.Length");
+
_list.CopyTo (array, index);
}
public X509ExtensionEnumerator GetEnumerator ()
{
- return new X509ExtensionEnumerator (this);
+ return new X509ExtensionEnumerator (_list);
}
IEnumerator IEnumerable.GetEnumerator ()
{
- return new X509ExtensionEnumerator (this);
+ return new X509ExtensionEnumerator (_list);
}
}
}
Modified:
trunk/mcs/class/System/System.Security.Cryptography.X509Certificates/X509ExtensionEnumerator.cs
===================================================================
---
trunk/mcs/class/System/System.Security.Cryptography.X509Certificates/X509ExtensionEnumerator.cs
2006-11-17 20:35:16 UTC (rev 68095)
+++
trunk/mcs/class/System/System.Security.Cryptography.X509Certificates/X509ExtensionEnumerator.cs
2006-11-17 20:42:40 UTC (rev 68096)
@@ -2,12 +2,11 @@
// X509ExtensionEnumerator.cs -
System.Security.Cryptography.X509ExtensionEnumerator
//
// Author:
-// Sebastien Pouliot ([EMAIL PROTECTED])
+// Sebastien Pouliot <[EMAIL PROTECTED]>
//
// (C) 2003 Motus Technologies Inc. (http://www.motus.com)
+// Copyright (C) 2006 Novell Inc. (http://www.novell.com)
//
-
-//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
@@ -34,15 +33,13 @@
namespace System.Security.Cryptography.X509Certificates {
- // Note: Match the definition of framework version 1.2.3400.0 on
http://longhorn.msdn.microsoft.com
-
public sealed class X509ExtensionEnumerator : IEnumerator {
private IEnumerator enumerator;
- internal X509ExtensionEnumerator (X509ExtensionCollection
collection)
+ internal X509ExtensionEnumerator (ArrayList list)
{
- enumerator = ((IEnumerable) collection).GetEnumerator
();
+ enumerator = list.GetEnumerator ();
}
// properties
Modified:
trunk/mcs/class/System/System.Security.Cryptography.X509Certificates/X509SubjectKeyIdentifierExtension.cs
===================================================================
---
trunk/mcs/class/System/System.Security.Cryptography.X509Certificates/X509SubjectKeyIdentifierExtension.cs
2006-11-17 20:35:16 UTC (rev 68095)
+++
trunk/mcs/class/System/System.Security.Cryptography.X509Certificates/X509SubjectKeyIdentifierExtension.cs
2006-11-17 20:42:40 UTC (rev 68096)
@@ -88,17 +88,51 @@
RawData = Encode ();
}
- [MonoTODO]
public X509SubjectKeyIdentifierExtension (PublicKey key, bool
critical)
: this (key,
X509SubjectKeyIdentifierHashAlgorithm.Sha1, critical)
{
}
- [MonoTODO]
public X509SubjectKeyIdentifierExtension (PublicKey key,
X509SubjectKeyIdentifierHashAlgorithm algorithm, bool critical)
{
if (key == null)
throw new ArgumentNullException ("key");
+
+ byte[] pkraw = key.EncodedKeyValue.RawData;
+ // compute SKI
+ switch (algorithm) {
+ // hash of the public key, excluding Tag, Length and
unused bits values
+ case X509SubjectKeyIdentifierHashAlgorithm.Sha1:
+ _subjectKeyIdentifier = SHA1.Create
().ComputeHash (pkraw);
+ break;
+ // 0100 bit pattern followed by the 60 last bit of the
hash
+ case X509SubjectKeyIdentifierHashAlgorithm.ShortSha1:
+ byte[] hash = SHA1.Create ().ComputeHash
(pkraw);
+ _subjectKeyIdentifier = new byte [8];
+ Buffer.BlockCopy (hash, 12,
_subjectKeyIdentifier, 0, 8);
+ _subjectKeyIdentifier [0] = (byte) (0x40 |
(_subjectKeyIdentifier [0] & 0x0F));
+ break;
+ // hash of the public key, including Tag, Length and
unused bits values
+ case X509SubjectKeyIdentifierHashAlgorithm.CapiSha1:
+ // CryptoAPI does that hash on the complete
subjectPublicKeyInfo (unlike PKIX)
+ //
http://groups.google.ca/groups?selm=e7RqM%24plCHA.1488%40tkmsftngp02&oe=UTF-8&output=gplain
+ ASN1 subjectPublicKeyInfo = new ASN1 (0x30);
+ ASN1 algo = subjectPublicKeyInfo.Add (new ASN1
(0x30));
+ algo.Add (new ASN1 (CryptoConfig.EncodeOID
(key.Oid.Value)));
+ algo.Add (new ASN1
(key.EncodedParameters.RawData));
+ // add an extra byte for the unused bits (none)
+ byte[] full = new byte [pkraw.Length + 1];
+ Buffer.BlockCopy (pkraw, 0, full, 1,
pkraw.Length);
+ subjectPublicKeyInfo.Add (new ASN1 (0x03,
full));
+ _subjectKeyIdentifier = SHA1.Create
().ComputeHash (subjectPublicKeyInfo.GetBytes ());
+ break;
+ default:
+ throw new ArgumentException ("algorithm");
+ }
+
+ _oid = new Oid (oid, friendlyName);
+ base.Critical = critical;
+ RawData = Encode ();
}
// properties
_______________________________________________
Mono-patches maillist - [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches