2005-01-31 Zoltan Varga <vargaz@freemail.hu>
[mono.git] / mcs / class / Mono.Security / Mono.Security.X509.Extensions / SubjectAltNameExtension.cs
index 9b8ef7003a94dcc094748c7d16d44729a80b1454..a0af1bf1c917fa78dfcf3148fc20c374d74e0ccf 100644 (file)
@@ -2,12 +2,35 @@
 // SubjectAltNameExtension.cs: Handles X.509 SubjectAltName extensions.
 //
 // Author:
-//     Sebastien Pouliot (spouliot@motus.com)
+//     Sebastien Pouliot <sebastien@ximian.com>
 //
 // (C) 2003 Motus Technologies Inc. (http://www.motus.com)
+// (C) 2004 Novell (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
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+// 
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+// 
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 //
 
 using System;
+using System.Net;
 using System.Collections;
 using System.Text;
 
@@ -46,10 +69,12 @@ namespace Mono.Security.X509.Extensions {
         * }
         */
 
-       // TODO - incomplete (only rfc822Name is supported)
+       // TODO - incomplete (only rfc822Name, dNSName are supported)
        public class SubjectAltNameExtension : X509Extension {
                
                private ArrayList rfc822Name;
+               private ArrayList dnsName;
+               private ArrayList ipAddr;
 
                public SubjectAltNameExtension () : base () 
                {
@@ -72,6 +97,16 @@ namespace Mono.Security.X509.Extensions {
                                                        rfc822Name = new ArrayList ();
                                                rfc822Name.Add (Encoding.ASCII.GetString (sequence [i].Value));
                                                break;
+                                       case 0x82: // dNSName           [2]     IA5String
+                                               if (dnsName == null)
+                                                       dnsName = new ArrayList ();
+                                               dnsName.Add (Encoding.ASCII.GetString (sequence [i].Value));
+                                               break;
+                                       case 0x87: // iPAddress         [7]     OCTET STRING
+                                               if (ipAddr == null)
+                                                       ipAddr = new ArrayList ();
+                                               // TODO - Must find sample certificates
+                                               break;
                                        default:
                                                break;
                                }
@@ -84,10 +119,26 @@ namespace Mono.Security.X509.Extensions {
 
                public string[] RFC822 {
                        get {
-                               string[] names = new string [rfc822Name.Count];
-                               for (int i=0; i < rfc822Name.Count; i++)
-                                       names [i] = (string) rfc822Name [i];
-                               return names;
+                               if (rfc822Name == null)
+                                       return new string [0];
+                               return (string[]) rfc822Name.ToArray (typeof(string));
+                       }
+               }
+
+               public string[] DNSNames {
+                       get {
+                               if (dnsName == null)
+                                       return new string [0];
+                               return (string[]) dnsName.ToArray (typeof(string));
+                       }
+               }
+
+               // Incomplete support
+               public string[] IPAddresses {
+                       get {
+                               if (ipAddr == null)
+                                       return new string [0];
+                               return (string[]) ipAddr.ToArray (typeof(string));
                        }
                }
 
@@ -101,6 +152,20 @@ namespace Mono.Security.X509.Extensions {
                                        sb.Append (Environment.NewLine);
                                }
                        }
+                       if (dnsName != null) {
+                               foreach (string s in dnsName) {
+                                       sb.Append ("DNS Name=");
+                                       sb.Append (s);
+                                       sb.Append (Environment.NewLine);
+                               }
+                       }
+                       if (ipAddr != null) {
+                               foreach (string s in ipAddr) {
+                                       sb.Append ("IP Address=");
+                                       sb.Append (s);
+                                       sb.Append (Environment.NewLine);
+                               }
+                       }
                        return sb.ToString ();
                }
        }