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 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 internal X509Extension ()
60 public X509Extension (ASN1 asn1
)
62 if ((asn1
.Tag
!= 0x30) || (asn1
.Count
< 2))
63 throw new ArgumentException ("Invalid X.509 extension");
64 if (asn1
[0].Tag
!= 0x06)
65 throw new ArgumentException ("Invalid X.509 extension");
66 extnOid
= ASN1Convert
.ToOid (asn1
[0]);
67 extnCritical
= ((asn1
[1].Tag
== 0x01) && (asn1
[1].Value
[0] == 0xFF));
68 extnValue
= asn1
[asn1
.Count
- 1]; // last element
72 public X509Extension (X509Extension extension
) : this ()
74 if (extension
== null)
75 throw new ArgumentNullException ("extension");
76 if ((extension
.Value
.Tag
!= 0x04) || (extension
.Value
.Count
!= 0))
77 throw new ArgumentException ("Invalid extension");
78 extnOid
= extension
.Oid
;
79 extnCritical
= extension
.Critical
;
80 extnValue
= extension
.Value
;
84 protected virtual void Decode ()
88 protected virtual void Encode ()
94 ASN1 extension
= new ASN1 (0x30);
95 extension
.Add (ASN1Convert
.FromOid (extnOid
));
97 extension
.Add (new ASN1 (0x01, new byte [1] { 0x01 }
));
98 ASN1 os
= extension
.Add (new ASN1 (0x04));
106 get { return extnOid; }
109 public bool Critical
{
110 get { return extnCritical; }
113 // this gets overrided with more meaningful names
114 public virtual string Name
{
115 get { return extnOid; }
119 get { return extnValue; }
122 public override bool Equals (object obj
)
127 X509Extension ex
= (obj
as X509Extension
);
131 if (extnCritical
!= ex
.extnCritical
)
133 if (extnOid
!= ex
.extnOid
)
135 if (extnValue
.Length
!= ex
.extnValue
.Length
)
138 for (int i
=0; i
< extnValue
.Length
; i
++) {
139 if (extnValue
[i
] != ex
.extnValue
[i
])
145 public byte[] GetBytes ()
147 return ASN1
.GetBytes ();
150 public override int GetHashCode ()
152 // OID should be unique in a collection of extensions
153 return extnOid
.GetHashCode ();
156 private void WriteLine (StringBuilder sb
, int n
, int pos
)
158 byte[] value = extnValue
.Value
;
160 for (int j
=0; j
< 8; j
++) {
162 sb
.Append (value [p
++].ToString ("X2", CultureInfo
.InvariantCulture
));
170 for (int j
=0; j
< n
; j
++) {
171 byte b
= value [p
++];
175 sb
.Append (Convert
.ToChar (b
));
177 sb
.Append (Environment
.NewLine
);
180 public override string ToString ()
182 StringBuilder sb
= new StringBuilder ();
183 int div
= (extnValue
.Length
>> 3);
184 int rem
= (extnValue
.Length
- (div
<< 3));
186 for (int i
=0; i
< div
; i
++) {
187 WriteLine (sb
, 8, x
);
190 WriteLine (sb
, rem
, x
);
191 return sb
.ToString ();