2 // X509Extension.cs: Base class for all X.509 extensions.
5 // Sebastien Pouliot <sebastien@ximian.com>
7 // (C) 2003 Motus Technologies Inc. (http://www.motus.com)
8 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 using System.Globalization;
36 namespace Mono.Security.X509 {
38 * Extension ::= SEQUENCE {
39 * extnID OBJECT IDENTIFIER,
40 * critical BOOLEAN DEFAULT FALSE,
41 * extnValue OCTET STRING
51 protected string extnOid;
52 protected bool extnCritical;
53 protected ASN1 extnValue;
55 protected X509Extension ()
60 public X509Extension (ASN1 asn1)
62 if ((asn1.Tag != 0x30) || (asn1.Count < 2))
63 throw new ArgumentException (Locale.GetText ("Invalid X.509 extension."));
64 if (asn1[0].Tag != 0x06)
65 throw new ArgumentException (Locale.GetText ("Invalid X.509 extension."));
67 extnOid = ASN1Convert.ToOid (asn1[0]);
68 extnCritical = ((asn1[1].Tag == 0x01) && (asn1[1].Value[0] == 0xFF));
69 extnValue = asn1 [asn1.Count - 1]; // last element
73 public X509Extension (X509Extension extension)
75 if (extension == null)
76 throw new ArgumentNullException ("extension");
77 if ((extension.Value == null) || (extension.Value.Tag != 0x04) || (extension.Value.Count != 1))
78 throw new ArgumentException (Locale.GetText ("Invalid X.509 extension."));
80 extnOid = extension.Oid;
81 extnCritical = extension.Critical;
82 extnValue = extension.Value;
86 // encode the extension *into* an OCTET STRING
87 protected virtual void Decode ()
91 // decode the extension from *inside* an OCTET STRING
92 protected virtual void Encode ()
98 ASN1 extension = new ASN1 (0x30);
99 extension.Add (ASN1Convert.FromOid (extnOid));
101 extension.Add (new ASN1 (0x01, new byte [1] { 0xFF }));
103 extension.Add (extnValue);
109 get { return extnOid; }
112 public bool Critical {
113 get { return extnCritical; }
114 set { extnCritical = value; }
117 // this gets overrided with more meaningful names
118 public virtual string Name {
119 get { return extnOid; }
124 if (extnValue == null) {
131 public override bool Equals (object obj)
136 X509Extension ex = (obj as X509Extension);
140 if (extnCritical != ex.extnCritical)
142 if (extnOid != ex.extnOid)
144 if (extnValue.Length != ex.extnValue.Length)
147 for (int i=0; i < extnValue.Length; i++) {
148 if (extnValue [i] != ex.extnValue [i])
154 public byte[] GetBytes ()
156 return ASN1.GetBytes ();
159 public override int GetHashCode ()
161 // OID should be unique in a collection of extensions
162 return extnOid.GetHashCode ();
165 private void WriteLine (StringBuilder sb, int n, int pos)
167 byte[] value = extnValue.Value;
169 for (int j=0; j < 8; j++) {
171 sb.Append (value [p++].ToString ("X2", CultureInfo.InvariantCulture));
179 for (int j=0; j < n; j++) {
180 byte b = value [p++];
184 sb.Append (Convert.ToChar (b));
186 sb.Append (Environment.NewLine);
189 public override string ToString ()
191 StringBuilder sb = new StringBuilder ();
192 int div = (extnValue.Length >> 3);
193 int rem = (extnValue.Length - (div << 3));
195 for (int i=0; i < div; i++) {
196 WriteLine (sb, 8, x);
199 WriteLine (sb, rem, x);
200 return sb.ToString ();