Author: spouliot
Date: 2006-11-08 09:48:16 -0500 (Wed, 08 Nov 2006)
New Revision: 67531
Modified:
trunk/mcs/class/Mono.Security/Mono.Security.X509/ChangeLog
trunk/mcs/class/Mono.Security/Mono.Security.X509/X501Name.cs
trunk/mcs/class/Mono.Security/Mono.Security.X509/X509Certificate.cs
Log:
2006-11-08 Sebastien Pouliot <[EMAIL PROTECTED]>
* X501Name.cs: Refactor ToString method to allow most options available
when using fx 2.0.
* X509Certificate.cs: Add methods to retrieve the Issuer and Subject
Distinguished Names in binary (ASN.1) form. Reverse (actually correct)
the text representation of Issuer and Subject for 2.0.
Modified: trunk/mcs/class/Mono.Security/Mono.Security.X509/ChangeLog
===================================================================
--- trunk/mcs/class/Mono.Security/Mono.Security.X509/ChangeLog 2006-11-08
14:45:24 UTC (rev 67530)
+++ trunk/mcs/class/Mono.Security/Mono.Security.X509/ChangeLog 2006-11-08
14:48:16 UTC (rev 67531)
@@ -1,3 +1,11 @@
+2006-11-08 Sebastien Pouliot <[EMAIL PROTECTED]>
+
+ * X501Name.cs: Refactor ToString method to allow most options available
+ when using fx 2.0.
+ * X509Certificate.cs: Add methods to retrieve the Issuer and Subject
+ Distinguished Names in binary (ASN.1) form. Reverse (actually correct)
+ the text representation of Issuer and Subject for 2.0.
+
2006-10-08 Sebastien Pouliot <[EMAIL PROTECTED]>
* PKCS12.cs: A String.Empty is different from a null password. Fix bug
Modified: trunk/mcs/class/Mono.Security/Mono.Security.X509/X501Name.cs
===================================================================
--- trunk/mcs/class/Mono.Security/Mono.Security.X509/X501Name.cs
2006-11-08 14:45:24 UTC (rev 67530)
+++ trunk/mcs/class/Mono.Security/Mono.Security.X509/X501Name.cs
2006-11-08 14:48:16 UTC (rev 67531)
@@ -5,7 +5,7 @@
// Sebastien Pouliot <[EMAIL PROTECTED]>
//
// (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
-// 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
@@ -77,77 +77,109 @@
StringBuilder sb = new StringBuilder ();
for (int i = 0; i < seq.Count; i++) {
ASN1 entry = seq [i];
- // multiple entries are valid
- for (int k = 0; k < entry.Count; k++) {
- ASN1 pair = entry [k];
- ASN1 s = pair [1];
- if (s == null)
- continue;
+ AppendEntry (sb, entry, true);
- ASN1 poid = pair [0];
- if (poid == null)
- continue;
+ // separator (not on last iteration)
+ if (i < seq.Count - 1)
+ sb.Append (", ");
+ }
+ return sb.ToString ();
+ }
- if (poid.CompareValue (countryName))
- sb.Append ("C=");
- else if (poid.CompareValue
(organizationName))
- sb.Append ("O=");
- else if (poid.CompareValue
(organizationalUnitName))
- sb.Append ("OU=");
- else if (poid.CompareValue (commonName))
- sb.Append ("CN=");
- else if (poid.CompareValue
(localityName))
- sb.Append ("L=");
- else if (poid.CompareValue
(stateOrProvinceName))
- sb.Append ("S="); //
NOTE: RFC2253 uses ST=
- else if (poid.CompareValue
(streetAddress))
- sb.Append ("STREET=");
- else if (poid.CompareValue
(domainComponent))
- sb.Append ("DC=");
- else if (poid.CompareValue (userid))
- sb.Append ("UID=");
- else if (poid.CompareValue (email))
- sb.Append ("E="); //
NOTE: Not part of RFC2253
- else {
- // unknown OID
- sb.Append ("OID."); //
NOTE: Not present as RFC2253
- sb.Append (ASN1Convert.ToOid
(poid));
- sb.Append ("=");
- }
+#if INSIDE_CORLIB || NET_2_0
+ static public string ToString (ASN1 seq, bool reversed, string
separator, bool quotes)
+ {
+ StringBuilder sb = new StringBuilder ();
- string sValue = null;
- // 16bits or 8bits string ? TODO not
complete (+special chars!)
- if (s.Tag == 0x1E) {
- // BMPSTRING
- StringBuilder sb2 = new
StringBuilder ();
- for (int j = 1; j <
s.Value.Length; j += 2)
- sb2.Append
((char)s.Value[j]);
- sValue = sb2.ToString ();
- } else {
- sValue =
Encoding.UTF8.GetString (s.Value);
- // in some cases we must quote
(") the value
- // Note: this doesn't seems to
conform to RFC2253
- char[] specials = { ',', '+',
'"', '\\', '<', '>', ';' };
- if (sValue.IndexOfAny
(specials, 0, sValue.Length) > 0)
- sValue = "\"" + sValue
+ "\"";
- else if (sValue.StartsWith ("
"))
- sValue = "\"" + sValue
+ "\"";
- else if (sValue.EndsWith (" "))
- sValue = "\"" + sValue
+ "\"";
- }
+ if (reversed) {
+ for (int i = seq.Count - 1; i >= 0; i--) {
+ ASN1 entry = seq [i];
+ AppendEntry (sb, entry, quotes);
- sb.Append (sValue);
+ // separator (not on last iteration)
+ if (i > 0)
+ sb.Append (separator);
+ }
+ } else {
+ for (int i = 0; i < seq.Count; i++) {
+ ASN1 entry = seq [i];
+ AppendEntry (sb, entry, quotes);
// separator (not on last iteration)
- if (k < entry.Count - 1)
- sb.Append (", ");
+ if (i < seq.Count - 1)
+ sb.Append (separator);
}
+ }
+ return sb.ToString ();
+ }
+#endif
+ static private void AppendEntry (StringBuilder sb, ASN1 entry,
bool quotes)
+ {
+ // multiple entries are valid
+ for (int k = 0; k < entry.Count; k++) {
+ ASN1 pair = entry [k];
+ ASN1 s = pair [1];
+ if (s == null)
+ continue;
+
+ ASN1 poid = pair [0];
+ if (poid == null)
+ continue;
+
+ if (poid.CompareValue (countryName))
+ sb.Append ("C=");
+ else if (poid.CompareValue (organizationName))
+ sb.Append ("O=");
+ else if (poid.CompareValue
(organizationalUnitName))
+ sb.Append ("OU=");
+ else if (poid.CompareValue (commonName))
+ sb.Append ("CN=");
+ else if (poid.CompareValue (localityName))
+ sb.Append ("L=");
+ else if (poid.CompareValue
(stateOrProvinceName))
+ sb.Append ("S="); // NOTE:
RFC2253 uses ST=
+ else if (poid.CompareValue (streetAddress))
+ sb.Append ("STREET=");
+ else if (poid.CompareValue (domainComponent))
+ sb.Append ("DC=");
+ else if (poid.CompareValue (userid))
+ sb.Append ("UID=");
+ else if (poid.CompareValue (email))
+ sb.Append ("E="); // NOTE: Not
part of RFC2253
+ else {
+ // unknown OID
+ sb.Append ("OID."); // NOTE: Not
present as RFC2253
+ sb.Append (ASN1Convert.ToOid (poid));
+ sb.Append ("=");
+ }
+
+ string sValue = null;
+ // 16bits or 8bits string ? TODO not complete
(+special chars!)
+ if (s.Tag == 0x1E) {
+ // BMPSTRING
+ StringBuilder sb2 = new StringBuilder
();
+ for (int j = 1; j < s.Value.Length; j
+= 2)
+ sb2.Append ((char)s.Value[j]);
+ sValue = sb2.ToString ();
+ } else {
+ sValue = Encoding.UTF8.GetString
(s.Value);
+ // in some cases we must quote (") the
value
+ // Note: this doesn't seems to conform
to RFC2253
+ char[] specials = { ',', '+', '"',
'\\', '<', '>', ';' };
+ if (quotes) {
+ if ((sValue.IndexOfAny
(specials, 0, sValue.Length) > 0) ||
+ sValue.StartsWith (" ") ||
(sValue.EndsWith (" ")))
+ sValue = "\"" + sValue
+ "\"";
+ }
+ }
+
+ sb.Append (sValue);
+
// separator (not on last iteration)
- if (i < seq.Count - 1)
+ if (k < entry.Count - 1)
sb.Append (", ");
}
- return sb.ToString ();
}
static private X520.AttributeTypeAndValue GetAttributeFromOid
(string attributeType)
Modified: trunk/mcs/class/Mono.Security/Mono.Security.X509/X509Certificate.cs
===================================================================
--- trunk/mcs/class/Mono.Security/Mono.Security.X509/X509Certificate.cs
2006-11-08 14:45:24 UTC (rev 67530)
+++ trunk/mcs/class/Mono.Security/Mono.Security.X509/X509Certificate.cs
2006-11-08 14:48:16 UTC (rev 67531)
@@ -55,9 +55,11 @@
private byte[] m_encodedcert;
private DateTime m_from;
private DateTime m_until;
+ private ASN1 issuer;
private string m_issuername;
private string m_keyalgo;
private byte[] m_keyalgoparams;
+ private ASN1 subject;
private string m_subject;
private byte[] m_publickey;
private byte[] signature;
@@ -95,6 +97,12 @@
// private byte[] subjectUniqueID;
private X509ExtensionCollection extensions;
+#if NET_2_0
+ private const bool reversed = true;
+#else
+ private const bool reversed = false;
+#endif
+
// that's were the real job is!
private void Parse (byte[] data)
{
@@ -131,7 +139,7 @@
tbs++;
// ASN1 signatureAlgo = tbsCertificate.Element
(tbs++, 0x30);
- ASN1 issuer = tbsCertificate.Element (tbs++,
0x30);
+ issuer = tbsCertificate.Element (tbs++, 0x30);
m_issuername = X501.ToString (issuer);
ASN1 validity = tbsCertificate.Element (tbs++,
0x30);
@@ -140,7 +148,7 @@
ASN1 notAfter = validity [1];
m_until = ASN1Convert.ToDateTime (notAfter);
- ASN1 subject = tbsCertificate.Element (tbs++,
0x30);
+ subject = tbsCertificate.Element (tbs++, 0x30);
m_subject = X501.ToString (subject);
ASN1 subjectPublicKeyInfo =
tbsCertificate.Element (tbs++, 0x30);
@@ -505,6 +513,16 @@
}
#if INSIDE_CORLIB || NET_2_0
+ public ASN1 GetIssuerName ()
+ {
+ return issuer;
+ }
+
+ public ASN1 GetSubjectName ()
+ {
+ return subject;
+ }
+
protected X509Certificate (SerializationInfo info,
StreamingContext context)
{
Parse ((byte[]) info.GetValue ("raw", typeof (byte[])));
_______________________________________________
Mono-patches maillist - [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches