blob: 6f833ad04a6c41249825cd1448b072de20792de6 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
|
//
// X509Extension.cs: Base class for all X.509 extensions.
//
// Author:
// Sebastien Pouliot <sebastien@ximian.com>
//
// (C) 2003 Motus Technologies Inc. (http://www.motus.com)
// (C) 2004 Novell (http://www.novell.com)
//
using System;
using System.Globalization;
using System.Text;
using Mono.Security;
namespace Mono.Security.X509 {
/*
* Extension ::= SEQUENCE {
* extnID OBJECT IDENTIFIER,
* critical BOOLEAN DEFAULT FALSE,
* extnValue OCTET STRING
* }
*/
#if INSIDE_CORLIB
internal
#else
public
#endif
class X509Extension {
protected string extnOid;
protected bool extnCritical;
protected ASN1 extnValue;
internal X509Extension ()
{
extnCritical = false;
}
public X509Extension (ASN1 asn1)
{
if ((asn1.Tag != 0x30) || (asn1.Count < 2))
throw new ArgumentException ("Invalid X.509 extension");
if (asn1[0].Tag != 0x06)
throw new ArgumentException ("Invalid X.509 extension");
extnOid = ASN1Convert.ToOid (asn1 [0]);
extnCritical = ((asn1[1].Tag == 0x01) && (asn1[1].Value[0] == 0xFF));
extnValue = asn1 [asn1.Count - 1]; // last element
Decode ();
}
public X509Extension (X509Extension extension) : this ()
{
if (extension == null)
throw new ArgumentNullException ("extension");
if ((extension.Value.Tag != 0x04) || (extension.Value.Count != 0))
throw new ArgumentException ("Invalid extension");
extnOid = extension.Oid;
extnCritical = extension.Critical;
extnValue = extension.Value;
Decode ();
}
protected virtual void Decode ()
{
}
protected virtual void Encode ()
{
}
public ASN1 ASN1 {
get {
ASN1 extension = new ASN1 (0x30);
extension.Add (ASN1Convert.FromOid (extnOid));
if (extnCritical)
extension.Add (new ASN1 (0x01, new byte [1] { 0x01 }));
ASN1 os = extension.Add (new ASN1 (0x04));
Encode ();
os.Add (extnValue);
return extension;
}
}
public string Oid {
get { return extnOid; }
}
public bool Critical {
get { return extnCritical; }
}
// this gets overrided with more meaningful names
public virtual string Name {
get { return extnOid; }
}
public ASN1 Value {
get { return extnValue; }
}
public override bool Equals (object obj)
{
if (obj == null)
return false;
X509Extension ex = (obj as X509Extension);
if (ex == null)
return false;
if (extnCritical != ex.extnCritical)
return false;
if (extnOid != ex.extnOid)
return false;
if (extnValue.Length != ex.extnValue.Length)
return false;
for (int i=0; i < extnValue.Length; i++) {
if (extnValue [i] != ex.extnValue [i])
return false;
}
return true;
}
public byte[] GetBytes ()
{
return ASN1.GetBytes ();
}
public override int GetHashCode ()
{
// OID should be unique in a collection of extensions
return extnOid.GetHashCode ();
}
private void WriteLine (StringBuilder sb, int n, int pos)
{
byte[] value = extnValue.Value;
int p = pos;
StringBuilder preview = new StringBuilder ();
for (int j=0; j < 8; j++) {
if (j < n) {
sb.Append (value [p++].ToString ("X2", CultureInfo.InvariantCulture));
sb.Append (" ");
}
else
sb.Append (" ");
}
sb.Append (" ");
p = pos;
for (int j=0; j < n; j++) {
byte b = value [p++];
if (b < 0x20)
sb.Append (".");
else
sb.Append (Convert.ToChar (b));
}
sb.Append (Environment.NewLine);
}
public override string ToString ()
{
StringBuilder sb = new StringBuilder ();
int div = (extnValue.Length >> 3);
int rem = (extnValue.Length - (div << 3));
int x = 0;
for (int i=0; i < div; i++) {
WriteLine (sb, 8, x);
x += 8;
}
WriteLine (sb, rem, x);
return sb.ToString ();
}
}
}
|