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
  
177
  
178
  
179
  
180
  
181
  
182
  
183
  
184
  
185
  
186
  
187
  
188
  
189
  
190
  
191
  
192
  
193
  
194
  
195
  
196
  
197
  
198
  
199
  
200
  
201
  
202
  
203
  
204
  
205
  
206
  
207
  
208
  
209
  
210
  
211
  
212
  
213
  
214
  
215
  
216
  
217
  
218
  
219
  
220
  
221
  
222
  
223
  
224
  
225
  
226
  
227
  
228
  
229
  
230
  
231
  
232
  
233
  
234
  
235
  
236
  
237
  
238
  
239
  
240
  
241
  
242
  
243
  
244
  
245
  
246
  
247
  
248
  
249
  
250
  
251
  
252
  
253
  
254
  
255
  
256
  
257
  
258
  
259
  
260
  
261
  
262
  
263
  
264
  
265
  
266
  
267
  
268
  
269
  
270
  
271
  
272
  
273
  
274
  
275
  
276
  
277
  
278
  
279
  
280
  
281
  
282
  
283
  
284
  
285
  
286
  
287
  
288
  
289
  
290
  
291
  
292
  
293
  
294
  
295
  
296
  
297
  
298
  
299
  
300
  
301
  
302
  
303
  
304
  
305
  
306
  
307
  
308
  
309
  
310
  
311
  
312
  
313
  
314
  
315
  
316
  
317
  
318
  
 
//! Handle PKCS-6 and PKCS-10 certificates and certificate requests. 
 
#pike __REAL_VERSION__ 
 
/* ASN.1 structures: 
 
CertificationRequestInfo ::= SEQUENCE { 
  version Version, 
  subject Name, 
  subjectPublicKeyInfo SubjectPublicKeyInfo, 
  attributes [0] IMPLICIT Attributes } 
 
Version ::= INTEGER 
 
Attributes ::= SET OF Attribute 
 
-- From the last section of PKCS-9. 
Attribute ::= SEQUENCE { 
  attribyteType ::= OBJECT IDENTIFIER, 
  attributeValue ::= SET OF ANY } 
 
CertificationRequest ::= SEQUENCE { 
  certificationRequestInfo CertificationRequestInfo, 
  signatureAlgorithm SignatureAlgorithmIdentifier, 
  signature Signature } 
 
SignatureAlgorithmIdentifier ::= AlgorithmIdentifier 
 
Signature ::= BIT STRING 
 
CertificationRequest ::= SEQUENCE { 
  certificationRequestInfo CertificationRequestInfo, 
  signatureAlgorithm SignatureAlgorithmIdentifier, 
  signature Signature } 
 
SignatureAlgorithmIdentifier ::= AlgorithmIdentifier 
 
Signature ::= BIT STRING 
 
ExtendedCertificateOrCertificate ::= CHOICE { 
  certificate Certificate, -- X.509 
  extendedCertificate [0] IMPLICIT ExtendedCertificate 
 
ExtendedCertificateInfo ::= SEQUENCE { 
  version Version, 
  certificate Certificate, 
  attributes Attributes } 
 
Version ::= INTEGER 
 
ExtendedCertificate ::= SEQUENCE { 
  extendedCertificateInfo ExtendedCertificateInfo, 
  signatureAlgorithm SignatureAlgorithmIdentifier, 
  signature Signature } 
 
SignatureAlgorithmIdentifier ::= AlgorithmIdentifier 
 
Signature ::= BIT STRING 
 
CertificateInfo ::= SEQUENCE { 
  version [0] Version DEFAULT v1988, 
  serialNumber CertificateSerialNumber, 
  signature AlgorithmIdentifier, 
  issuer Name, 
  validity Validity, 
  subject Name, 
  subjectPublicKeyInfo SubjectPublicKeyInfo } 
 
Version ::= INTEGER { v1988(0) } 
 
CertificateSerialNumber ::= INTEGER 
 
Validity ::= SEQUENCE { 
  notBefore UTCTime, 
  notAfter UTCTime } 
 
SubjectPublicKeyInfo ::= SEQUENCE { 
  algorithm AlgorithmIdentifier, 
  subjectPublicKey BIT STRING } 
 
AlgorithmIdentifier ::= SEQUENCE { 
  algorithm OBJECT IDENTIFIER, 
  parameters ANY DEFINED BY ALGORITHM OPTIONAL } 
 
Certificate ::= SEQUENCE { 
  certificateInfo CertificateInfo, 
  signatureAlgorithm AlgorithmIdentifier, 
  signature BIT STRING } 
 
Name ::= CHOICE { 
  RDNSequence } 
 
RDNSequence ::= SEQUENCE OF RelativeDistinguishedName 
 
RelativeDistinguishedName ::= 
  SET OF AttributeValueAssertion 
 
AttributeValueAssertion ::= SEQUENCE { 
   AttributeType, 
   AttributeValue } 
 
AttributeType ::= OBJECT IDENTIFIER 
 
AttributeValue ::= ANY 
 
RSAPublicKey ::= SEQUENCE { 
  modulus INTEGER, -- n 
  publicExponent INTEGER -- e } 
 
RSAPrivateKey ::= SEQUENCE { 
  version Version, 
  modulus INTEGER, -- n 
  publicExponent INTEGER, -- e 
  privateExponent INTEGER, -- d 
  prime1 INTEGER, -- p 
  prime2 INTEGER, -- q 
  exponent1 INTEGER, -- d mod (p-1) 
  exponent2 INTEGER, -- d mod (q-1) 
  coefficient INTEGER -- (inverse of q) mod p } 
 
Version ::= INTEGER 
 
*/ 
 
import Standards.ASN1.Types; 
import .Identifiers; 
 
protected object X509 = master()->resolv("Standards.X509"); 
 
class AttributeValueAssertion 
{ 
  inherit Sequence; 
  void create(mapping(string:object) types, 
              string type, 
              object value) 
    { 
      if (!objectp(types[type])) 
        error("Unknown attribute type '%s':%O\n", type, types[type]); 
      ::create( ({ types[type], value }) ); 
    } 
} 
 
/* RelativeDistinguishedName */ 
class attribute_set 
{ 
  inherit Set; 
 
  void create(mapping(string:object) types, mapping(string:object) pairs) 
  { 
    ::create(map(indices(pairs), 
                 lambda(string s, mapping m, mapping t) { 
                   return AttributeValueAssertion(t, s, m[s]); 
                 }, 
                 pairs, types)); 
  } 
} 
 
//! 
variant Sequence build_distinguished_name(mapping args) 
{ 
  // Turn mapping into array of pairs 
  array a = ({}); 
  foreach(sort(indices(args)), string name) 
    a += ({ ([name : args[name]]) }); 
 
  return build_distinguished_name(a); 
} 
 
//! Creates an ASN.1 @[Sequence] with the distinguished name of the 
//! list of pairs given in @[args]. Supported identifiers are 
//! 
//! @dl 
//!   @item commonName 
//!   @item surname 
//!   @item countryName 
//!   @item localityName 
//!   @item stateOrProvinceName 
//!   @item organizationName 
//!   @item organizationUnitName 
//!   @item title 
//!   @item name 
//!   @item givenName 
//!   @item initials 
//!   @item generationQualifier 
//!   @item dnQualifier 
//!   @item emailAddress 
//! @enddl 
//! 
//! @param args 
//!   Either a mapping that lists from id string to string or ASN.1 
//!   object, or an array with mappings, each containing one pair. No 
//!   type validation is performed. 
variant Sequence build_distinguished_name(array args) 
{ 
  args += ({}); 
  // DWIM support. Turn string values into PrintableString. 
  foreach(args; int i; mapping m) { 
    foreach(m; string name; mixed value) 
      if( stringp(value) ) 
      { 
        args[i] = ([ name : PrintableString(value) ]); 
        break; 
      } 
  } 
 
  return Sequence(map(args, lambda(mapping rdn) { 
                              return attribute_set( 
                                .Identifiers.at_ids, rdn); 
                            } )); 
} 
 
//! Perform the reverse operation of @[build_distinguished_name()]. 
//! 
//! @seealso 
//!   @[build_distinguished_name()] 
array(mapping(string(7bit):string)) decode_distinguished_name(Sequence dn) 
{ 
  array(mapping(string(7bit):string)) ret = 
    allocate(sizeof(dn->elements), aggregate_mapping)(); 
  foreach(dn->elements; int i; Set attr) { 
    foreach(attr->elements, Sequence val) { 
      string(7bit) name = reverse_at_ids[val[0]]; 
      if (!name) { 
        // Unknown identifier. 
        name = ((array(string))val[0]->id) * "."; 
      } 
      ret[i][name] = val[1]->value; 
    } 
  } 
  return ret; 
} 
 
//! Converts an RDN (relative distinguished name) Seqeunce object to a 
//! human readable string in X500 format. 
//! 
//! @returns 
//!  A string containing the certificate issuer 
//!  Distinguished Name (DN) in human readable X500 format. 
//! 
//! @note 
//!  We don't currently handle attributes with multiple values, not 
//!  all attribute types are understood. 
string get_dn_string(Sequence dnsequence) 
{ 
  string dn=""; 
  array rdns=({}); 
 
  foreach(reverse(dnsequence->elements), Set att) 
  { 
    foreach(att->elements, Sequence val) 
    { 
      array value = ({}); 
      string t = short_name_ids[val[0]]; 
      string v = val[1]->value; 
 
      // we must escape characters now. 
      v = replace(v, 
          ({",", "+", "\"", "\\", "<", ">", ";"}), 
          ({"\\,", "\\+", "\\\"", "\\\\", "\\<", "\\>", "\\;"}) ); 
 
      if(v[0..0] == " ") 
         v="\\" + v; 
      else if(v[0..0] == "#") 
         v="\\" + v; 
 
      if(v[(sizeof(v)-1)..(sizeof(v)-1)] == " ") 
         v=v[0..(sizeof(v)-2)] + "\\ "; 
 
      rdns += ({ (t + "=" + v) }); 
    } 
  } 
 
  dn = rdns * ","; 
  return dn; 
} 
 
class Attribute 
{ 
  inherit Sequence; 
 
  protected void create(mapping(string:object) types, string type, 
                        array(object) v) 
  { 
    if (!types[type]) 
      error( "Unknown attribute type '%s'\n", type); 
    ::create( ({ types[type], Set(v) }) ); 
  } 
  protected variant void create(array(Object) elements) 
  { 
    if (sizeof(elements) != 2) 
      error("Invalid attribute encoding.\n"); 
    ::create(elements); 
  } 
} 
 
class Attributes 
{ 
  inherit Set; 
 
  protected void create(mapping(string:object) types, 
                        mapping(string:array(object)) m) 
  { 
    ::create(map(indices(m), 
                 lambda(string field, mapping m, mapping t) { 
                   return Attribute(t, field, m[field]); 
                 }, m, types)); 
  } 
  protected variant void create(array(Object) elements) 
  { 
    ::create(map(elements, 
                 lambda(object e) { 
                   return Attribute(e->elements); 
                 })); 
  } 
}