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 // (C) 2004 Novell (http://www.novell.com)
12 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35 using System.Globalization;
40 namespace Mono.Security.X509 {
42 * Extension ::= SEQUENCE {
43 * extnID OBJECT IDENTIFIER,
44 * critical BOOLEAN DEFAULT FALSE,
45 * extnValue OCTET STRING
55 protected string extnOid;
56 protected bool extnCritical;
57 protected ASN1 extnValue;
59 internal X509Extension ()
64 public X509Extension (ASN1 asn1)
66 if ((asn1.Tag != 0x30) || (asn1.Count < 2))
67 throw new ArgumentException ("Invalid X.509 extension");
68 if (asn1[0].Tag != 0x06)
69 throw new ArgumentException ("Invalid X.509 extension");
70 extnOid = ASN1Convert.ToOid (asn1 [0]);
71 extnCritical = ((asn1[1].Tag == 0x01) && (asn1[1].Value[0] == 0xFF));
72 extnValue = asn1 [asn1.Count - 1]; // last element
76 public X509Extension (X509Extension extension) : this ()
78 if (extension == null)
79 throw new ArgumentNullException ("extension");
80 if ((extension.Value.Tag != 0x04) || (extension.Value.Count != 0))
81 throw new ArgumentException ("Invalid extension");
82 extnOid = extension.Oid;
83 extnCritical = extension.Critical;
84 extnValue = extension.Value;
88 protected virtual void Decode ()
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] { 0x01 }));
102 ASN1 os = extension.Add (new ASN1 (0x04));
110 get { return extnOid; }
113 public bool Critical {
114 get { return extnCritical; }
117 // this gets overrided with more meaningful names
118 public virtual string Name {
119 get { return extnOid; }
123 get { return extnValue; }
126 public override bool Equals (object obj)
131 X509Extension ex = (obj as X509Extension);
135 if (extnCritical != ex.extnCritical)
137 if (extnOid != ex.extnOid)
139 if (extnValue.Length != ex.extnValue.Length)
142 for (int i=0; i < extnValue.Length; i++) {
143 if (extnValue [i] != ex.extnValue [i])
149 public byte[] GetBytes ()
151 return ASN1.GetBytes ();
154 public override int GetHashCode ()
156 // OID should be unique in a collection of extensions
157 return extnOid.GetHashCode ();
160 private void WriteLine (StringBuilder sb, int n, int pos)
162 byte[] value = extnValue.Value;
164 StringBuilder preview = new StringBuilder ();
165 for (int j=0; j < 8; j++) {
167 sb.Append (value [p++].ToString ("X2", CultureInfo.InvariantCulture));
175 for (int j=0; j < n; j++) {
176 byte b = value [p++];
180 sb.Append (Convert.ToChar (b));
182 sb.Append (Environment.NewLine);
185 public override string ToString ()
187 StringBuilder sb = new StringBuilder ();
188 int div = (extnValue.Length >> 3);
189 int rem = (extnValue.Length - (div << 3));
191 for (int i=0; i < div; i++) {
192 WriteLine (sb, 8, x);
195 WriteLine (sb, rem, x);
196 return sb.ToString ();