Update copyright year
[openssl.git] / crypto / x509 / v3_alt.c
1 /*
2  * Copyright 1999-2020 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 #include <stdio.h>
11 #include "internal/cryptlib.h"
12 #include <openssl/conf.h>
13 #include <openssl/x509v3.h>
14 #include <openssl/bio.h>
15 #include "ext_dat.h"
16
17 DEFINE_STACK_OF(CONF_VALUE)
18 DEFINE_STACK_OF(GENERAL_NAME)
19
20 static GENERAL_NAMES *v2i_subject_alt(X509V3_EXT_METHOD *method,
21                                       X509V3_CTX *ctx,
22                                       STACK_OF(CONF_VALUE) *nval);
23 static GENERAL_NAMES *v2i_issuer_alt(X509V3_EXT_METHOD *method,
24                                      X509V3_CTX *ctx,
25                                      STACK_OF(CONF_VALUE) *nval);
26 static int copy_email(X509V3_CTX *ctx, GENERAL_NAMES *gens, int move_p);
27 static int copy_issuer(X509V3_CTX *ctx, GENERAL_NAMES *gens);
28 static int do_othername(GENERAL_NAME *gen, const char *value, X509V3_CTX *ctx);
29 static int do_dirname(GENERAL_NAME *gen, const char *value, X509V3_CTX *ctx);
30
31 const X509V3_EXT_METHOD v3_alt[3] = {
32     {NID_subject_alt_name, 0, ASN1_ITEM_ref(GENERAL_NAMES),
33      0, 0, 0, 0,
34      0, 0,
35      (X509V3_EXT_I2V) i2v_GENERAL_NAMES,
36      (X509V3_EXT_V2I)v2i_subject_alt,
37      NULL, NULL, NULL},
38
39     {NID_issuer_alt_name, 0, ASN1_ITEM_ref(GENERAL_NAMES),
40      0, 0, 0, 0,
41      0, 0,
42      (X509V3_EXT_I2V) i2v_GENERAL_NAMES,
43      (X509V3_EXT_V2I)v2i_issuer_alt,
44      NULL, NULL, NULL},
45
46     {NID_certificate_issuer, 0, ASN1_ITEM_ref(GENERAL_NAMES),
47      0, 0, 0, 0,
48      0, 0,
49      (X509V3_EXT_I2V) i2v_GENERAL_NAMES,
50      NULL, NULL, NULL, NULL},
51 };
52
53 STACK_OF(CONF_VALUE) *i2v_GENERAL_NAMES(X509V3_EXT_METHOD *method,
54                                         GENERAL_NAMES *gens,
55                                         STACK_OF(CONF_VALUE) *ret)
56 {
57     int i;
58     GENERAL_NAME *gen;
59     STACK_OF(CONF_VALUE) *tmpret = NULL, *origret = ret;
60
61     for (i = 0; i < sk_GENERAL_NAME_num(gens); i++) {
62         gen = sk_GENERAL_NAME_value(gens, i);
63         /*
64          * i2v_GENERAL_NAME allocates ret if it is NULL. If something goes
65          * wrong we need to free the stack - but only if it was empty when we
66          * originally entered this function.
67          */
68         tmpret = i2v_GENERAL_NAME(method, gen, ret);
69         if (tmpret == NULL) {
70             if (origret == NULL)
71                 sk_CONF_VALUE_pop_free(ret, X509V3_conf_free);
72             return NULL;
73         }
74         ret = tmpret;
75     }
76     if (ret == NULL)
77         return sk_CONF_VALUE_new_null();
78     return ret;
79 }
80
81 STACK_OF(CONF_VALUE) *i2v_GENERAL_NAME(X509V3_EXT_METHOD *method,
82                                        GENERAL_NAME *gen,
83                                        STACK_OF(CONF_VALUE) *ret)
84 {
85     char othername[300];
86     char oline[256], *tmp;
87
88     switch (gen->type) {
89     case GEN_OTHERNAME:
90         switch (OBJ_obj2nid(gen->d.otherName->type_id)) {
91         case NID_id_on_SmtpUTF8Mailbox:
92             if (gen->d.otherName->value->type != V_ASN1_UTF8STRING
93                     || !X509V3_add_value_uchar("othername: SmtpUTF8Mailbox:",
94                             gen->d.otherName->value->value.utf8string->data,
95                             &ret))
96                 return NULL;
97             break;
98         case NID_XmppAddr:
99             if (gen->d.otherName->value->type != V_ASN1_UTF8STRING
100                     || !X509V3_add_value_uchar("othername: XmppAddr:",
101                             gen->d.otherName->value->value.utf8string->data,
102                             &ret))
103                 return NULL;
104             break;
105         case NID_SRVName:
106             if (gen->d.otherName->value->type != V_ASN1_IA5STRING
107                     || !X509V3_add_value_uchar("othername: SRVName:",
108                             gen->d.otherName->value->value.ia5string->data,
109                             &ret))
110                 return NULL;
111             break;
112         case NID_ms_upn:
113             if (gen->d.otherName->value->type != V_ASN1_UTF8STRING
114                     || !X509V3_add_value_uchar("othername: UPN:",
115                             gen->d.otherName->value->value.utf8string->data,
116                             &ret))
117                 return NULL;
118             break;
119         case NID_NAIRealm:
120             if (gen->d.otherName->value->type != V_ASN1_UTF8STRING
121                     || !X509V3_add_value_uchar("othername: NAIRealm:",
122                             gen->d.otherName->value->value.utf8string->data,
123                             &ret))
124                 return NULL;
125             break;
126         default:
127             if (OBJ_obj2txt(oline, sizeof(oline), gen->d.otherName->type_id, 0) > 0) 
128                 BIO_snprintf(othername, sizeof(othername), "othername: %s:",
129                              oline);
130             else
131                 strncpy(othername, "othername:", sizeof(othername));
132
133             /* check if the value is something printable */
134             if (gen->d.otherName->value->type == V_ASN1_IA5STRING) {
135                 if (X509V3_add_value_uchar(othername,
136                              gen->d.otherName->value->value.ia5string->data,
137                              &ret)) 
138                     return ret;
139             }
140             if (gen->d.otherName->value->type == V_ASN1_UTF8STRING) {
141                 if (X509V3_add_value_uchar(othername,
142                              gen->d.otherName->value->value.utf8string->data,
143                              &ret)) 
144                     return ret;
145             }
146             if (!X509V3_add_value(othername, "<unsupported>", &ret))
147                 return NULL;
148             break;
149         }
150         break;
151
152     case GEN_X400:
153         if (!X509V3_add_value("X400Name", "<unsupported>", &ret))
154             return NULL;
155         break;
156
157     case GEN_EDIPARTY:
158         if (!X509V3_add_value("EdiPartyName", "<unsupported>", &ret))
159             return NULL;
160         break;
161
162     case GEN_EMAIL:
163         if (!X509V3_add_value_uchar("email", gen->d.ia5->data, &ret))
164             return NULL;
165         break;
166
167     case GEN_DNS:
168         if (!X509V3_add_value_uchar("DNS", gen->d.ia5->data, &ret))
169             return NULL;
170         break;
171
172     case GEN_URI:
173         if (!X509V3_add_value_uchar("URI", gen->d.ia5->data, &ret))
174             return NULL;
175         break;
176
177     case GEN_DIRNAME:
178         if (X509_NAME_oneline(gen->d.dirn, oline, sizeof(oline)) == NULL
179                 || !X509V3_add_value("DirName", oline, &ret))
180             return NULL;
181         break;
182
183     case GEN_IPADD:
184         tmp = ipaddr_to_asc(gen->d.ip->data, gen->d.ip->length);
185         if (tmp == NULL || !X509V3_add_value("IP Address", tmp, &ret))
186             ret = NULL;
187         OPENSSL_free(tmp);
188         break;
189
190     case GEN_RID:
191         i2t_ASN1_OBJECT(oline, 256, gen->d.rid);
192         if (!X509V3_add_value("Registered ID", oline, &ret))
193             return NULL;
194         break;
195     }
196     return ret;
197 }
198
199 int GENERAL_NAME_print(BIO *out, GENERAL_NAME *gen)
200 {
201     char *tmp;
202     int nid;
203
204     switch (gen->type) {
205     case GEN_OTHERNAME:
206         nid = OBJ_obj2nid(gen->d.otherName->type_id);
207         /* Validate the types are as we expect before we use them */
208         if ((nid == NID_SRVName
209              && gen->d.otherName->value->type != V_ASN1_IA5STRING)
210                 || (nid != NID_SRVName
211                     && gen->d.otherName->value->type != V_ASN1_UTF8STRING)) {
212             BIO_printf(out, "othername:<unsupported>");
213             break;
214         }
215
216         switch (nid) {
217         case NID_id_on_SmtpUTF8Mailbox:
218             BIO_printf(out, "othername:SmtpUTF8Mailbox:%s",
219                        gen->d.otherName->value->value.utf8string->data);
220             break;
221         case NID_XmppAddr:
222             BIO_printf(out, "othername:XmppAddr:%s",
223                        gen->d.otherName->value->value.utf8string->data);
224             break;
225         case NID_SRVName:
226             BIO_printf(out, "othername:SRVName:%s",
227                        gen->d.otherName->value->value.ia5string->data);
228             break;
229         case NID_ms_upn:
230             BIO_printf(out, "othername:UPN:%s",
231                        gen->d.otherName->value->value.utf8string->data);
232             break;
233         case NID_NAIRealm:
234             BIO_printf(out, "othername:NAIRealm:%s",
235                        gen->d.otherName->value->value.utf8string->data);
236             break;
237         default:
238             BIO_printf(out, "othername:<unsupported>");
239             break;
240         }
241         break;
242
243     case GEN_X400:
244         BIO_printf(out, "X400Name:<unsupported>");
245         break;
246
247     case GEN_EDIPARTY:
248         /* Maybe fix this: it is supported now */
249         BIO_printf(out, "EdiPartyName:<unsupported>");
250         break;
251
252     case GEN_EMAIL:
253         BIO_printf(out, "email:");
254         ASN1_STRING_print(out, gen->d.ia5);
255         break;
256
257     case GEN_DNS:
258         BIO_printf(out, "DNS:");
259         ASN1_STRING_print(out, gen->d.ia5);
260         break;
261
262     case GEN_URI:
263         BIO_printf(out, "URI:");
264         ASN1_STRING_print(out, gen->d.ia5);
265         break;
266
267     case GEN_DIRNAME:
268         BIO_printf(out, "DirName:");
269         X509_NAME_print_ex(out, gen->d.dirn, 0, XN_FLAG_ONELINE);
270         break;
271
272     case GEN_IPADD:
273         tmp = ipaddr_to_asc(gen->d.ip->data, gen->d.ip->length);
274         if (tmp == NULL)
275             return 0;
276         BIO_printf(out, "IP Address:%s", tmp);
277         OPENSSL_free(tmp);
278         break;
279
280     case GEN_RID:
281         BIO_printf(out, "Registered ID:");
282         i2a_ASN1_OBJECT(out, gen->d.rid);
283         break;
284     }
285     return 1;
286 }
287
288 static GENERAL_NAMES *v2i_issuer_alt(X509V3_EXT_METHOD *method,
289                                      X509V3_CTX *ctx,
290                                      STACK_OF(CONF_VALUE) *nval)
291 {
292     const int num = sk_CONF_VALUE_num(nval);
293     GENERAL_NAMES *gens = sk_GENERAL_NAME_new_reserve(NULL, num);
294     int i;
295
296     if (gens == NULL) {
297         X509V3err(X509V3_F_V2I_ISSUER_ALT, ERR_R_MALLOC_FAILURE);
298         sk_GENERAL_NAME_free(gens);
299         return NULL;
300     }
301     for (i = 0; i < num; i++) {
302         CONF_VALUE *cnf = sk_CONF_VALUE_value(nval, i);
303
304         if (!v3_name_cmp(cnf->name, "issuer")
305             && cnf->value && strcmp(cnf->value, "copy") == 0) {
306             if (!copy_issuer(ctx, gens))
307                 goto err;
308         } else {
309             GENERAL_NAME *gen = v2i_GENERAL_NAME(method, ctx, cnf);
310
311             if (gen == NULL)
312                 goto err;
313             sk_GENERAL_NAME_push(gens, gen); /* no failure as it was reserved */
314         }
315     }
316     return gens;
317  err:
318     sk_GENERAL_NAME_pop_free(gens, GENERAL_NAME_free);
319     return NULL;
320 }
321
322 /* Append subject altname of issuer to issuer alt name of subject */
323
324 static int copy_issuer(X509V3_CTX *ctx, GENERAL_NAMES *gens)
325 {
326     GENERAL_NAMES *ialt;
327     GENERAL_NAME *gen;
328     X509_EXTENSION *ext;
329     int i, num;
330
331     if (ctx && (ctx->flags == CTX_TEST))
332         return 1;
333     if (!ctx || !ctx->issuer_cert) {
334         X509V3err(X509V3_F_COPY_ISSUER, X509V3_R_NO_ISSUER_DETAILS);
335         goto err;
336     }
337     i = X509_get_ext_by_NID(ctx->issuer_cert, NID_subject_alt_name, -1);
338     if (i < 0)
339         return 1;
340     if ((ext = X509_get_ext(ctx->issuer_cert, i)) == NULL
341         || (ialt = X509V3_EXT_d2i(ext)) == NULL) {
342         X509V3err(X509V3_F_COPY_ISSUER, X509V3_R_ISSUER_DECODE_ERROR);
343         goto err;
344     }
345
346     num = sk_GENERAL_NAME_num(ialt);
347     if (!sk_GENERAL_NAME_reserve(gens, num)) {
348         X509V3err(X509V3_F_COPY_ISSUER, ERR_R_MALLOC_FAILURE);
349         goto err;
350     }
351
352     for (i = 0; i < num; i++) {
353         gen = sk_GENERAL_NAME_value(ialt, i);
354         sk_GENERAL_NAME_push(gens, gen);     /* no failure as it was reserved */
355     }
356     sk_GENERAL_NAME_free(ialt);
357
358     return 1;
359
360  err:
361     return 0;
362
363 }
364
365 static GENERAL_NAMES *v2i_subject_alt(X509V3_EXT_METHOD *method,
366                                       X509V3_CTX *ctx,
367                                       STACK_OF(CONF_VALUE) *nval)
368 {
369     GENERAL_NAMES *gens;
370     CONF_VALUE *cnf;
371     const int num = sk_CONF_VALUE_num(nval);
372     int i;
373
374     gens = sk_GENERAL_NAME_new_reserve(NULL, num);
375     if (gens == NULL) {
376         X509V3err(X509V3_F_V2I_SUBJECT_ALT, ERR_R_MALLOC_FAILURE);
377         sk_GENERAL_NAME_free(gens);
378         return NULL;
379     }
380
381     for (i = 0; i < num; i++) {
382         cnf = sk_CONF_VALUE_value(nval, i);
383         if (!v3_name_cmp(cnf->name, "email")
384             && cnf->value && strcmp(cnf->value, "copy") == 0) {
385             if (!copy_email(ctx, gens, 0))
386                 goto err;
387         } else if (!v3_name_cmp(cnf->name, "email")
388                    && cnf->value && strcmp(cnf->value, "move") == 0) {
389             if (!copy_email(ctx, gens, 1))
390                 goto err;
391         } else {
392             GENERAL_NAME *gen;
393             if ((gen = v2i_GENERAL_NAME(method, ctx, cnf)) == NULL)
394                 goto err;
395             sk_GENERAL_NAME_push(gens, gen); /* no failure as it was reserved */
396         }
397     }
398     return gens;
399  err:
400     sk_GENERAL_NAME_pop_free(gens, GENERAL_NAME_free);
401     return NULL;
402 }
403
404 /*
405  * Copy any email addresses in a certificate or request to GENERAL_NAMES
406  */
407
408 static int copy_email(X509V3_CTX *ctx, GENERAL_NAMES *gens, int move_p)
409 {
410     X509_NAME *nm;
411     ASN1_IA5STRING *email = NULL;
412     X509_NAME_ENTRY *ne;
413     GENERAL_NAME *gen = NULL;
414     int i = -1;
415
416     if (ctx != NULL && ctx->flags == CTX_TEST)
417         return 1;
418     if (ctx == NULL
419         || (ctx->subject_cert == NULL && ctx->subject_req == NULL)) {
420         X509V3err(X509V3_F_COPY_EMAIL, X509V3_R_NO_SUBJECT_DETAILS);
421         goto err;
422     }
423     /* Find the subject name */
424     if (ctx->subject_cert)
425         nm = X509_get_subject_name(ctx->subject_cert);
426     else
427         nm = X509_REQ_get_subject_name(ctx->subject_req);
428
429     /* Now add any email address(es) to STACK */
430     while ((i = X509_NAME_get_index_by_NID(nm,
431                                            NID_pkcs9_emailAddress, i)) >= 0) {
432         ne = X509_NAME_get_entry(nm, i);
433         email = ASN1_STRING_dup(X509_NAME_ENTRY_get_data(ne));
434         if (move_p) {
435             X509_NAME_delete_entry(nm, i);
436             X509_NAME_ENTRY_free(ne);
437             i--;
438         }
439         if (email == NULL || (gen = GENERAL_NAME_new()) == NULL) {
440             X509V3err(X509V3_F_COPY_EMAIL, ERR_R_MALLOC_FAILURE);
441             goto err;
442         }
443         gen->d.ia5 = email;
444         email = NULL;
445         gen->type = GEN_EMAIL;
446         if (!sk_GENERAL_NAME_push(gens, gen)) {
447             X509V3err(X509V3_F_COPY_EMAIL, ERR_R_MALLOC_FAILURE);
448             goto err;
449         }
450         gen = NULL;
451     }
452
453     return 1;
454
455  err:
456     GENERAL_NAME_free(gen);
457     ASN1_IA5STRING_free(email);
458     return 0;
459
460 }
461
462 GENERAL_NAMES *v2i_GENERAL_NAMES(const X509V3_EXT_METHOD *method,
463                                  X509V3_CTX *ctx, STACK_OF(CONF_VALUE) *nval)
464 {
465     GENERAL_NAME *gen;
466     GENERAL_NAMES *gens;
467     CONF_VALUE *cnf;
468     const int num = sk_CONF_VALUE_num(nval);
469     int i;
470
471     gens = sk_GENERAL_NAME_new_reserve(NULL, num);
472     if (gens == NULL) {
473         X509V3err(X509V3_F_V2I_GENERAL_NAMES, ERR_R_MALLOC_FAILURE);
474         sk_GENERAL_NAME_free(gens);
475         return NULL;
476     }
477
478     for (i = 0; i < num; i++) {
479         cnf = sk_CONF_VALUE_value(nval, i);
480         if ((gen = v2i_GENERAL_NAME(method, ctx, cnf)) == NULL)
481             goto err;
482         sk_GENERAL_NAME_push(gens, gen);    /* no failure as it was reserved */
483     }
484     return gens;
485  err:
486     sk_GENERAL_NAME_pop_free(gens, GENERAL_NAME_free);
487     return NULL;
488 }
489
490 GENERAL_NAME *v2i_GENERAL_NAME(const X509V3_EXT_METHOD *method,
491                                X509V3_CTX *ctx, CONF_VALUE *cnf)
492 {
493     return v2i_GENERAL_NAME_ex(NULL, method, ctx, cnf, 0);
494 }
495
496 GENERAL_NAME *a2i_GENERAL_NAME(GENERAL_NAME *out,
497                                const X509V3_EXT_METHOD *method,
498                                X509V3_CTX *ctx, int gen_type, const char *value,
499                                int is_nc)
500 {
501     char is_string = 0;
502     GENERAL_NAME *gen = NULL;
503
504     if (!value) {
505         X509V3err(X509V3_F_A2I_GENERAL_NAME, X509V3_R_MISSING_VALUE);
506         return NULL;
507     }
508
509     if (out)
510         gen = out;
511     else {
512         gen = GENERAL_NAME_new();
513         if (gen == NULL) {
514             X509V3err(X509V3_F_A2I_GENERAL_NAME, ERR_R_MALLOC_FAILURE);
515             return NULL;
516         }
517     }
518
519     switch (gen_type) {
520     case GEN_URI:
521     case GEN_EMAIL:
522     case GEN_DNS:
523         is_string = 1;
524         break;
525
526     case GEN_RID:
527         {
528             ASN1_OBJECT *obj;
529             if ((obj = OBJ_txt2obj(value, 0)) == NULL) {
530                 X509V3err(X509V3_F_A2I_GENERAL_NAME, X509V3_R_BAD_OBJECT);
531                 ERR_add_error_data(2, "value=", value);
532                 goto err;
533             }
534             gen->d.rid = obj;
535         }
536         break;
537
538     case GEN_IPADD:
539         if (is_nc)
540             gen->d.ip = a2i_IPADDRESS_NC(value);
541         else
542             gen->d.ip = a2i_IPADDRESS(value);
543         if (gen->d.ip == NULL) {
544             X509V3err(X509V3_F_A2I_GENERAL_NAME, X509V3_R_BAD_IP_ADDRESS);
545             ERR_add_error_data(2, "value=", value);
546             goto err;
547         }
548         break;
549
550     case GEN_DIRNAME:
551         if (!do_dirname(gen, value, ctx)) {
552             X509V3err(X509V3_F_A2I_GENERAL_NAME, X509V3_R_DIRNAME_ERROR);
553             goto err;
554         }
555         break;
556
557     case GEN_OTHERNAME:
558         if (!do_othername(gen, value, ctx)) {
559             X509V3err(X509V3_F_A2I_GENERAL_NAME, X509V3_R_OTHERNAME_ERROR);
560             goto err;
561         }
562         break;
563     default:
564         X509V3err(X509V3_F_A2I_GENERAL_NAME, X509V3_R_UNSUPPORTED_TYPE);
565         goto err;
566     }
567
568     if (is_string) {
569         if ((gen->d.ia5 = ASN1_IA5STRING_new()) == NULL ||
570             !ASN1_STRING_set(gen->d.ia5, (unsigned char *)value,
571                              strlen(value))) {
572             X509V3err(X509V3_F_A2I_GENERAL_NAME, ERR_R_MALLOC_FAILURE);
573             goto err;
574         }
575     }
576
577     gen->type = gen_type;
578
579     return gen;
580
581  err:
582     if (!out)
583         GENERAL_NAME_free(gen);
584     return NULL;
585 }
586
587 GENERAL_NAME *v2i_GENERAL_NAME_ex(GENERAL_NAME *out,
588                                   const X509V3_EXT_METHOD *method,
589                                   X509V3_CTX *ctx, CONF_VALUE *cnf, int is_nc)
590 {
591     int type;
592
593     char *name, *value;
594
595     name = cnf->name;
596     value = cnf->value;
597
598     if (!value) {
599         X509V3err(X509V3_F_V2I_GENERAL_NAME_EX, X509V3_R_MISSING_VALUE);
600         return NULL;
601     }
602
603     if (!v3_name_cmp(name, "email"))
604         type = GEN_EMAIL;
605     else if (!v3_name_cmp(name, "URI"))
606         type = GEN_URI;
607     else if (!v3_name_cmp(name, "DNS"))
608         type = GEN_DNS;
609     else if (!v3_name_cmp(name, "RID"))
610         type = GEN_RID;
611     else if (!v3_name_cmp(name, "IP"))
612         type = GEN_IPADD;
613     else if (!v3_name_cmp(name, "dirName"))
614         type = GEN_DIRNAME;
615     else if (!v3_name_cmp(name, "otherName"))
616         type = GEN_OTHERNAME;
617     else {
618         X509V3err(X509V3_F_V2I_GENERAL_NAME_EX, X509V3_R_UNSUPPORTED_OPTION);
619         ERR_add_error_data(2, "name=", name);
620         return NULL;
621     }
622
623     return a2i_GENERAL_NAME(out, method, ctx, type, value, is_nc);
624
625 }
626
627 static int do_othername(GENERAL_NAME *gen, const char *value, X509V3_CTX *ctx)
628 {
629     char *objtmp = NULL, *p;
630     int objlen;
631
632     if ((p = strchr(value, ';')) == NULL)
633         return 0;
634     if ((gen->d.otherName = OTHERNAME_new()) == NULL)
635         return 0;
636     /*
637      * Free this up because we will overwrite it. no need to free type_id
638      * because it is static
639      */
640     ASN1_TYPE_free(gen->d.otherName->value);
641     if ((gen->d.otherName->value = ASN1_generate_v3(p + 1, ctx)) == NULL)
642         return 0;
643     objlen = p - value;
644     objtmp = OPENSSL_strndup(value, objlen);
645     if (objtmp == NULL)
646         return 0;
647     gen->d.otherName->type_id = OBJ_txt2obj(objtmp, 0);
648     OPENSSL_free(objtmp);
649     if (!gen->d.otherName->type_id)
650         return 0;
651     return 1;
652 }
653
654 static int do_dirname(GENERAL_NAME *gen, const char *value, X509V3_CTX *ctx)
655 {
656     int ret = 0;
657     STACK_OF(CONF_VALUE) *sk = NULL;
658     X509_NAME *nm;
659
660     if ((nm = X509_NAME_new()) == NULL)
661         goto err;
662     sk = X509V3_get_section(ctx, value);
663     if (!sk) {
664         X509V3err(X509V3_F_DO_DIRNAME, X509V3_R_SECTION_NOT_FOUND);
665         ERR_add_error_data(2, "section=", value);
666         goto err;
667     }
668     /* FIXME: should allow other character types... */
669     ret = X509V3_NAME_from_section(nm, sk, MBSTRING_ASC);
670     if (!ret)
671         goto err;
672     gen->d.dirn = nm;
673
674 err:
675     if (ret == 0)
676         X509_NAME_free(nm);
677     X509V3_section_free(ctx, sk);
678     return ret;
679 }