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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
|
/* $OpenBSD: v3_genn.c,v 1.12 2015/09/26 17:38:41 jsing Exp $ */
/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
* project 1999.
*/
/* ====================================================================
* Copyright (c) 1999-2008 The OpenSSL Project. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. All advertising materials mentioning features or use of this
* software must display the following acknowledgment:
* "This product includes software developed by the OpenSSL Project
* for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
*
* 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
* endorse or promote products derived from this software without
* prior written permission. For written permission, please contact
* licensing@OpenSSL.org.
*
* 5. Products derived from this software may not be called "OpenSSL"
* nor may "OpenSSL" appear in their names without prior written
* permission of the OpenSSL Project.
*
* 6. Redistributions of any form whatsoever must retain the following
* acknowledgment:
* "This product includes software developed by the OpenSSL Project
* for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
*
* THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
* ====================================================================
*
* This product includes cryptographic software written by Eric Young
* (eay@cryptsoft.com). This product includes software written by Tim
* Hudson (tjh@cryptsoft.com).
*
*/
#include <stdio.h>
#include <openssl/asn1t.h>
#include <openssl/conf.h>
#include <openssl/x509v3.h>
static const ASN1_TEMPLATE OTHERNAME_seq_tt[] = {
{
.flags = 0,
.tag = 0,
.offset = offsetof(OTHERNAME, type_id),
.field_name = "type_id",
.item = &ASN1_OBJECT_it,
},
/* Maybe have a true ANY DEFINED BY later */
{
.flags = ASN1_TFLG_EXPLICIT,
.tag = 0,
.offset = offsetof(OTHERNAME, value),
.field_name = "value",
.item = &ASN1_ANY_it,
},
};
const ASN1_ITEM OTHERNAME_it = {
.itype = ASN1_ITYPE_SEQUENCE,
.utype = V_ASN1_SEQUENCE,
.templates = OTHERNAME_seq_tt,
.tcount = sizeof(OTHERNAME_seq_tt) / sizeof(ASN1_TEMPLATE),
.funcs = NULL,
.size = sizeof(OTHERNAME),
.sname = "OTHERNAME",
};
OTHERNAME *
d2i_OTHERNAME(OTHERNAME **a, const unsigned char **in, long len)
{
return (OTHERNAME *)ASN1_item_d2i((ASN1_VALUE **)a, in, len,
&OTHERNAME_it);
}
int
i2d_OTHERNAME(OTHERNAME *a, unsigned char **out)
{
return ASN1_item_i2d((ASN1_VALUE *)a, out, &OTHERNAME_it);
}
OTHERNAME *
OTHERNAME_new(void)
{
return (OTHERNAME *)ASN1_item_new(&OTHERNAME_it);
}
void
OTHERNAME_free(OTHERNAME *a)
{
ASN1_item_free((ASN1_VALUE *)a, &OTHERNAME_it);
}
static const ASN1_TEMPLATE EDIPARTYNAME_seq_tt[] = {
{
.flags = ASN1_TFLG_IMPLICIT | ASN1_TFLG_OPTIONAL,
.tag = 0,
.offset = offsetof(EDIPARTYNAME, nameAssigner),
.field_name = "nameAssigner",
.item = &DIRECTORYSTRING_it,
},
{
.flags = ASN1_TFLG_IMPLICIT | ASN1_TFLG_OPTIONAL,
.tag = 1,
.offset = offsetof(EDIPARTYNAME, partyName),
.field_name = "partyName",
.item = &DIRECTORYSTRING_it,
},
};
const ASN1_ITEM EDIPARTYNAME_it = {
.itype = ASN1_ITYPE_SEQUENCE,
.utype = V_ASN1_SEQUENCE,
.templates = EDIPARTYNAME_seq_tt,
.tcount = sizeof(EDIPARTYNAME_seq_tt) / sizeof(ASN1_TEMPLATE),
.funcs = NULL,
.size = sizeof(EDIPARTYNAME),
.sname = "EDIPARTYNAME",
};
EDIPARTYNAME *
d2i_EDIPARTYNAME(EDIPARTYNAME **a, const unsigned char **in, long len)
{
return (EDIPARTYNAME *)ASN1_item_d2i((ASN1_VALUE **)a, in, len,
&EDIPARTYNAME_it);
}
int
i2d_EDIPARTYNAME(EDIPARTYNAME *a, unsigned char **out)
{
return ASN1_item_i2d((ASN1_VALUE *)a, out, &EDIPARTYNAME_it);
}
EDIPARTYNAME *
EDIPARTYNAME_new(void)
{
return (EDIPARTYNAME *)ASN1_item_new(&EDIPARTYNAME_it);
}
void
EDIPARTYNAME_free(EDIPARTYNAME *a)
{
ASN1_item_free((ASN1_VALUE *)a, &EDIPARTYNAME_it);
}
static const ASN1_TEMPLATE GENERAL_NAME_ch_tt[] = {
{
.flags = ASN1_TFLG_IMPLICIT,
.tag = GEN_OTHERNAME,
.offset = offsetof(GENERAL_NAME, d.otherName),
.field_name = "d.otherName",
.item = &OTHERNAME_it,
},
{
.flags = ASN1_TFLG_IMPLICIT,
.tag = GEN_EMAIL,
.offset = offsetof(GENERAL_NAME, d.rfc822Name),
.field_name = "d.rfc822Name",
.item = &ASN1_IA5STRING_it,
},
{
.flags = ASN1_TFLG_IMPLICIT,
.tag = GEN_DNS,
.offset = offsetof(GENERAL_NAME, d.dNSName),
.field_name = "d.dNSName",
.item = &ASN1_IA5STRING_it,
},
/* Don't decode this */
{
.flags = ASN1_TFLG_IMPLICIT,
.tag = GEN_X400,
.offset = offsetof(GENERAL_NAME, d.x400Address),
.field_name = "d.x400Address",
.item = &ASN1_SEQUENCE_it,
},
/* X509_NAME is a CHOICE type so use EXPLICIT */
{
.flags = ASN1_TFLG_EXPLICIT,
.tag = GEN_DIRNAME,
.offset = offsetof(GENERAL_NAME, d.directoryName),
.field_name = "d.directoryName",
.item = &X509_NAME_it,
},
{
.flags = ASN1_TFLG_IMPLICIT,
.tag = GEN_EDIPARTY,
.offset = offsetof(GENERAL_NAME, d.ediPartyName),
.field_name = "d.ediPartyName",
.item = &EDIPARTYNAME_it,
},
{
.flags = ASN1_TFLG_IMPLICIT,
.tag = GEN_URI,
.offset = offsetof(GENERAL_NAME, d.uniformResourceIdentifier),
.field_name = "d.uniformResourceIdentifier",
.item = &ASN1_IA5STRING_it,
},
{
.flags = ASN1_TFLG_IMPLICIT,
.tag = GEN_IPADD,
.offset = offsetof(GENERAL_NAME, d.iPAddress),
.field_name = "d.iPAddress",
.item = &ASN1_OCTET_STRING_it,
},
{
.flags = ASN1_TFLG_IMPLICIT,
.tag = GEN_RID,
.offset = offsetof(GENERAL_NAME, d.registeredID),
.field_name = "d.registeredID",
.item = &ASN1_OBJECT_it,
},
};
const ASN1_ITEM GENERAL_NAME_it = {
.itype = ASN1_ITYPE_CHOICE,
.utype = offsetof(GENERAL_NAME, type),
.templates = GENERAL_NAME_ch_tt,
.tcount = sizeof(GENERAL_NAME_ch_tt) / sizeof(ASN1_TEMPLATE),
.funcs = NULL,
.size = sizeof(GENERAL_NAME),
.sname = "GENERAL_NAME",
};
GENERAL_NAME *
d2i_GENERAL_NAME(GENERAL_NAME **a, const unsigned char **in, long len)
{
return (GENERAL_NAME *)ASN1_item_d2i((ASN1_VALUE **)a, in, len,
&GENERAL_NAME_it);
}
int
i2d_GENERAL_NAME(GENERAL_NAME *a, unsigned char **out)
{
return ASN1_item_i2d((ASN1_VALUE *)a, out, &GENERAL_NAME_it);
}
GENERAL_NAME *
GENERAL_NAME_new(void)
{
return (GENERAL_NAME *)ASN1_item_new(&GENERAL_NAME_it);
}
void
GENERAL_NAME_free(GENERAL_NAME *a)
{
ASN1_item_free((ASN1_VALUE *)a, &GENERAL_NAME_it);
}
static const ASN1_TEMPLATE GENERAL_NAMES_item_tt = {
.flags = ASN1_TFLG_SEQUENCE_OF,
.tag = 0,
.offset = 0,
.field_name = "GeneralNames",
.item = &GENERAL_NAME_it,
};
const ASN1_ITEM GENERAL_NAMES_it = {
.itype = ASN1_ITYPE_PRIMITIVE,
.utype = -1,
.templates = &GENERAL_NAMES_item_tt,
.tcount = 0,
.funcs = NULL,
.size = 0,
.sname = "GENERAL_NAMES",
};
GENERAL_NAMES *
d2i_GENERAL_NAMES(GENERAL_NAMES **a, const unsigned char **in, long len)
{
return (GENERAL_NAMES *)ASN1_item_d2i((ASN1_VALUE **)a, in, len,
&GENERAL_NAMES_it);
}
int
i2d_GENERAL_NAMES(GENERAL_NAMES *a, unsigned char **out)
{
return ASN1_item_i2d((ASN1_VALUE *)a, out, &GENERAL_NAMES_it);
}
GENERAL_NAMES *
GENERAL_NAMES_new(void)
{
return (GENERAL_NAMES *)ASN1_item_new(&GENERAL_NAMES_it);
}
void
GENERAL_NAMES_free(GENERAL_NAMES *a)
{
ASN1_item_free((ASN1_VALUE *)a, &GENERAL_NAMES_it);
}
GENERAL_NAME *
GENERAL_NAME_dup(GENERAL_NAME *a)
{
return ASN1_item_dup(&GENERAL_NAME_it, a);
}
/* Returns 0 if they are equal, != 0 otherwise. */
int
GENERAL_NAME_cmp(GENERAL_NAME *a, GENERAL_NAME *b)
{
int result = -1;
if (!a || !b || a->type != b->type)
return -1;
switch (a->type) {
case GEN_X400:
case GEN_EDIPARTY:
result = ASN1_TYPE_cmp(a->d.other, b->d.other);
break;
case GEN_OTHERNAME:
result = OTHERNAME_cmp(a->d.otherName, b->d.otherName);
break;
case GEN_EMAIL:
case GEN_DNS:
case GEN_URI:
result = ASN1_STRING_cmp(a->d.ia5, b->d.ia5);
break;
case GEN_DIRNAME:
result = X509_NAME_cmp(a->d.dirn, b->d.dirn);
break;
case GEN_IPADD:
result = ASN1_OCTET_STRING_cmp(a->d.ip, b->d.ip);
break;
case GEN_RID:
result = OBJ_cmp(a->d.rid, b->d.rid);
break;
}
return result;
}
/* Returns 0 if they are equal, != 0 otherwise. */
int
OTHERNAME_cmp(OTHERNAME *a, OTHERNAME *b)
{
int result = -1;
if (!a || !b)
return -1;
/* Check their type first. */
if ((result = OBJ_cmp(a->type_id, b->type_id)) != 0)
return result;
/* Check the value. */
result = ASN1_TYPE_cmp(a->value, b->value);
return result;
}
void
GENERAL_NAME_set0_value(GENERAL_NAME *a, int type, void *value)
{
switch (type) {
case GEN_X400:
case GEN_EDIPARTY:
a->d.other = value;
break;
case GEN_OTHERNAME:
a->d.otherName = value;
break;
case GEN_EMAIL:
case GEN_DNS:
case GEN_URI:
a->d.ia5 = value;
break;
case GEN_DIRNAME:
a->d.dirn = value;
break;
case GEN_IPADD:
a->d.ip = value;
break;
case GEN_RID:
a->d.rid = value;
break;
}
a->type = type;
}
void *
GENERAL_NAME_get0_value(GENERAL_NAME *a, int *ptype)
{
if (ptype)
*ptype = a->type;
switch (a->type) {
case GEN_X400:
case GEN_EDIPARTY:
return a->d.other;
case GEN_OTHERNAME:
return a->d.otherName;
case GEN_EMAIL:
case GEN_DNS:
case GEN_URI:
return a->d.ia5;
case GEN_DIRNAME:
return a->d.dirn;
case GEN_IPADD:
return a->d.ip;
case GEN_RID:
return a->d.rid;
default:
return NULL;
}
}
int
GENERAL_NAME_set0_othername(GENERAL_NAME *gen, ASN1_OBJECT *oid,
ASN1_TYPE *value)
{
OTHERNAME *oth;
oth = OTHERNAME_new();
if (!oth)
return 0;
oth->type_id = oid;
oth->value = value;
GENERAL_NAME_set0_value(gen, GEN_OTHERNAME, oth);
return 1;
}
int
GENERAL_NAME_get0_otherName(GENERAL_NAME *gen, ASN1_OBJECT **poid,
ASN1_TYPE **pvalue)
{
if (gen->type != GEN_OTHERNAME)
return 0;
if (poid)
*poid = gen->d.otherName->type_id;
if (pvalue)
*pvalue = gen->d.otherName->value;
return 1;
}
|