No empty .Rs/.Re
[netbsd-mini2440.git] / crypto / dist / heimdal / lib / hx509 / name.c
blob91209e5afcde2f4baa228c86cdc2bdf7cbc05231
1 /*
2 * Copyright (c) 2004 - 2007 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
34 #include "hx_locl.h"
35 __RCSID("$Heimdal: name.c 22432 2008-01-13 14:08:03Z lha $"
36 "$NetBSD$");
38 /**
39 * @page page_name PKIX/X.509 Names
41 * There are several names in PKIX/X.509, GeneralName and Name.
43 * A Name consists of an ordered list of Relative Distinguished Names
44 * (RDN). Each RDN consists of an unordered list of typed strings. The
45 * types are defined by OID and have long and short description. For
46 * example id-at-commonName (2.5.4.3) have the long name CommonName
47 * and short name CN. The string itself can be of serveral encoding,
48 * UTF8, UTF16, Teltex string, etc. The type limit what encoding
49 * should be used.
51 * GeneralName is a broader nametype that can contains al kind of
52 * stuff like Name, IP addresses, partial Name, etc.
54 * Name is mapped into a hx509_name object.
56 * Parse and string name into a hx509_name object with hx509_parse_name(),
57 * make it back into string representation with hx509_name_to_string().
59 * Name string are defined rfc2253, rfc1779 and X.501.
61 * See the library functions here: @ref hx509_name
64 static const struct {
65 const char *n;
66 const heim_oid *(*o)(void);
67 } no[] = {
68 { "C", oid_id_at_countryName },
69 { "CN", oid_id_at_commonName },
70 { "DC", oid_id_domainComponent },
71 { "L", oid_id_at_localityName },
72 { "O", oid_id_at_organizationName },
73 { "OU", oid_id_at_organizationalUnitName },
74 { "S", oid_id_at_stateOrProvinceName },
75 { "STREET", oid_id_at_streetAddress },
76 { "UID", oid_id_Userid },
77 { "emailAddress", oid_id_pkcs9_emailAddress },
78 { "serialNumber", oid_id_at_serialNumber }
81 static char *
82 quote_string(const char *f, size_t len, size_t *rlen)
84 size_t i, j, tolen;
85 const char *from = f;
86 char *to;
88 tolen = len * 3 + 1;
89 to = malloc(tolen);
90 if (to == NULL)
91 return NULL;
93 for (i = 0, j = 0; i < len; i++) {
94 if (from[i] == ' ' && i + 1 < len)
95 to[j++] = from[i];
96 else if (from[i] == ',' || from[i] == '=' || from[i] == '+' ||
97 from[i] == '<' || from[i] == '>' || from[i] == '#' ||
98 from[i] == ';' || from[i] == ' ')
100 to[j++] = '\\';
101 to[j++] = from[i];
102 } else if (((unsigned char)from[i]) >= 32 && ((unsigned char)from[i]) <= 127) {
103 to[j++] = from[i];
104 } else {
105 int l = snprintf(&to[j], tolen - j - 1,
106 "#%02x", (unsigned char)from[i]);
107 j += l;
110 to[j] = '\0';
111 assert(j < tolen);
112 *rlen = j;
113 return to;
117 static int
118 append_string(char **str, size_t *total_len, const char *ss,
119 size_t len, int quote)
121 char *s, *qs;
123 if (quote)
124 qs = quote_string(ss, len, &len);
125 else
126 qs = rk_UNCONST(ss);
128 s = realloc(*str, len + *total_len + 1);
129 if (s == NULL)
130 _hx509_abort("allocation failure"); /* XXX */
131 memcpy(s + *total_len, qs, len);
132 if (qs != ss)
133 free(qs);
134 s[*total_len + len] = '\0';
135 *str = s;
136 *total_len += len;
137 return 0;
140 static char *
141 oidtostring(const heim_oid *type)
143 char *s;
144 size_t i;
146 for (i = 0; i < sizeof(no)/sizeof(no[0]); i++) {
147 if (der_heim_oid_cmp((*no[i].o)(), type) == 0)
148 return strdup(no[i].n);
150 if (der_print_heim_oid(type, '.', &s) != 0)
151 return NULL;
152 return s;
155 static int
156 stringtooid(const char *name, size_t len, heim_oid *oid)
158 int i, ret;
159 char *s;
161 memset(oid, 0, sizeof(*oid));
163 for (i = 0; i < sizeof(no)/sizeof(no[0]); i++) {
164 if (strncasecmp(no[i].n, name, len) == 0)
165 return der_copy_oid((*no[i].o)(), oid);
167 s = malloc(len + 1);
168 if (s == NULL)
169 return ENOMEM;
170 memcpy(s, name, len);
171 s[len] = '\0';
172 ret = der_parse_heim_oid(s, ".", oid);
173 free(s);
174 return ret;
178 * Convert the hx509 name object into a printable string.
179 * The resulting string should be freed with free().
181 * @param name name to print
182 * @param str the string to return
184 * @return An hx509 error code, see hx509_get_error_string().
186 * @ingroup hx509_name
190 hx509_name_to_string(const hx509_name name, char **str)
192 return _hx509_Name_to_string(&name->der_name, str);
196 _hx509_Name_to_string(const Name *n, char **str)
198 size_t total_len = 0;
199 int i, j;
201 *str = strdup("");
202 if (*str == NULL)
203 return ENOMEM;
205 for (i = n->u.rdnSequence.len - 1 ; i >= 0 ; i--) {
206 int len;
208 for (j = 0; j < n->u.rdnSequence.val[i].len; j++) {
209 DirectoryString *ds = &n->u.rdnSequence.val[i].val[j].value;
210 char *oidname;
211 char *ss;
213 oidname = oidtostring(&n->u.rdnSequence.val[i].val[j].type);
215 switch(ds->element) {
216 case choice_DirectoryString_ia5String:
217 ss = ds->u.ia5String;
218 break;
219 case choice_DirectoryString_printableString:
220 ss = ds->u.printableString;
221 break;
222 case choice_DirectoryString_utf8String:
223 ss = ds->u.utf8String;
224 break;
225 case choice_DirectoryString_bmpString: {
226 uint16_t *bmp = ds->u.bmpString.data;
227 size_t bmplen = ds->u.bmpString.length;
228 size_t k;
230 ss = malloc(bmplen + 1);
231 if (ss == NULL)
232 _hx509_abort("allocation failure"); /* XXX */
233 for (k = 0; k < bmplen; k++)
234 ss[k] = bmp[k] & 0xff; /* XXX */
235 ss[k] = '\0';
236 break;
238 case choice_DirectoryString_teletexString:
239 ss = malloc(ds->u.teletexString.length + 1);
240 if (ss == NULL)
241 _hx509_abort("allocation failure"); /* XXX */
242 memcpy(ss, ds->u.teletexString.data, ds->u.teletexString.length);
243 ss[ds->u.teletexString.length] = '\0';
244 break;
245 case choice_DirectoryString_universalString: {
246 uint32_t *uni = ds->u.universalString.data;
247 size_t unilen = ds->u.universalString.length;
248 size_t k;
250 ss = malloc(unilen + 1);
251 if (ss == NULL)
252 _hx509_abort("allocation failure"); /* XXX */
253 for (k = 0; k < unilen; k++)
254 ss[k] = uni[k] & 0xff; /* XXX */
255 ss[k] = '\0';
256 break;
258 default:
259 _hx509_abort("unknown directory type: %d", ds->element);
260 exit(1);
262 append_string(str, &total_len, oidname, strlen(oidname), 0);
263 free(oidname);
264 append_string(str, &total_len, "=", 1, 0);
265 len = strlen(ss);
266 append_string(str, &total_len, ss, len, 1);
267 if (ds->element == choice_DirectoryString_universalString ||
268 ds->element == choice_DirectoryString_bmpString ||
269 ds->element == choice_DirectoryString_teletexString)
271 free(ss);
273 if (j + 1 < n->u.rdnSequence.val[i].len)
274 append_string(str, &total_len, "+", 1, 0);
277 if (i > 0)
278 append_string(str, &total_len, ",", 1, 0);
280 return 0;
284 * XXX this function is broken, it needs to compare code points, not
285 * bytes.
288 static void
289 prune_space(const unsigned char **s)
291 while (**s == ' ')
292 (*s)++;
296 _hx509_name_ds_cmp(const DirectoryString *ds1, const DirectoryString *ds2)
298 int c;
300 c = ds1->element - ds2->element;
301 if (c)
302 return c;
304 switch(ds1->element) {
305 case choice_DirectoryString_ia5String:
306 c = strcmp(ds1->u.ia5String, ds2->u.ia5String);
307 break;
308 case choice_DirectoryString_teletexString:
309 c = der_heim_octet_string_cmp(&ds1->u.teletexString,
310 &ds2->u.teletexString);
311 break;
312 case choice_DirectoryString_printableString: {
313 const unsigned char *s1 = (unsigned char*)ds1->u.printableString;
314 const unsigned char *s2 = (unsigned char*)ds2->u.printableString;
315 prune_space(&s1); prune_space(&s2);
316 while (*s1 && *s2) {
317 if (toupper(*s1) != toupper(*s2)) {
318 c = toupper(*s1) - toupper(*s2);
319 break;
321 if (*s1 == ' ') { prune_space(&s1); prune_space(&s2); }
322 else { s1++; s2++; }
324 prune_space(&s1); prune_space(&s2);
325 c = *s1 - *s2;
326 break;
328 case choice_DirectoryString_utf8String:
329 c = strcmp(ds1->u.utf8String, ds2->u.utf8String);
330 break;
331 case choice_DirectoryString_universalString:
332 c = der_heim_universal_string_cmp(&ds1->u.universalString,
333 &ds2->u.universalString);
334 break;
335 case choice_DirectoryString_bmpString:
336 c = der_heim_bmp_string_cmp(&ds1->u.bmpString,
337 &ds2->u.bmpString);
338 break;
339 default:
340 c = 1;
341 break;
343 return c;
347 _hx509_name_cmp(const Name *n1, const Name *n2)
349 int i, j, c;
351 c = n1->u.rdnSequence.len - n2->u.rdnSequence.len;
352 if (c)
353 return c;
355 for (i = 0 ; i < n1->u.rdnSequence.len; i++) {
356 c = n1->u.rdnSequence.val[i].len - n2->u.rdnSequence.val[i].len;
357 if (c)
358 return c;
360 for (j = 0; j < n1->u.rdnSequence.val[i].len; j++) {
361 c = der_heim_oid_cmp(&n1->u.rdnSequence.val[i].val[j].type,
362 &n1->u.rdnSequence.val[i].val[j].type);
363 if (c)
364 return c;
366 c = _hx509_name_ds_cmp(&n1->u.rdnSequence.val[i].val[j].value,
367 &n2->u.rdnSequence.val[i].val[j].value);
368 if (c)
369 return c;
372 return 0;
376 * Compare to hx509 name object, useful for sorting.
378 * @param n1 a hx509 name object.
379 * @param n2 a hx509 name object.
381 * @return 0 the objects are the same, returns > 0 is n2 is "larger"
382 * then n2, < 0 if n1 is "smaller" then n2.
384 * @ingroup hx509_name
388 hx509_name_cmp(hx509_name n1, hx509_name n2)
390 return _hx509_name_cmp(&n1->der_name, &n2->der_name);
395 _hx509_name_from_Name(const Name *n, hx509_name *name)
397 int ret;
398 *name = calloc(1, sizeof(**name));
399 if (*name == NULL)
400 return ENOMEM;
401 ret = copy_Name(n, &(*name)->der_name);
402 if (ret) {
403 free(*name);
404 *name = NULL;
406 return ret;
410 _hx509_name_modify(hx509_context context,
411 Name *name,
412 int append,
413 const heim_oid *oid,
414 const char *str)
416 RelativeDistinguishedName *rdn;
417 int ret;
418 void *ptr;
420 ptr = realloc(name->u.rdnSequence.val,
421 sizeof(name->u.rdnSequence.val[0]) *
422 (name->u.rdnSequence.len + 1));
423 if (ptr == NULL) {
424 hx509_set_error_string(context, 0, ENOMEM, "Out of memory");
425 return ENOMEM;
427 name->u.rdnSequence.val = ptr;
429 if (append) {
430 rdn = &name->u.rdnSequence.val[name->u.rdnSequence.len];
431 } else {
432 memmove(&name->u.rdnSequence.val[1],
433 &name->u.rdnSequence.val[0],
434 name->u.rdnSequence.len *
435 sizeof(name->u.rdnSequence.val[0]));
437 rdn = &name->u.rdnSequence.val[0];
439 rdn->val = malloc(sizeof(rdn->val[0]));
440 if (rdn->val == NULL)
441 return ENOMEM;
442 rdn->len = 1;
443 ret = der_copy_oid(oid, &rdn->val[0].type);
444 if (ret)
445 return ret;
446 rdn->val[0].value.element = choice_DirectoryString_utf8String;
447 rdn->val[0].value.u.utf8String = strdup(str);
448 if (rdn->val[0].value.u.utf8String == NULL)
449 return ENOMEM;
450 name->u.rdnSequence.len += 1;
452 return 0;
456 * Parse a string into a hx509 name object.
458 * @param context A hx509 context.
459 * @param str a string to parse.
460 * @param name the resulting object, NULL in case of error.
462 * @return An hx509 error code, see hx509_get_error_string().
464 * @ingroup hx509_name
468 hx509_parse_name(hx509_context context, const char *str, hx509_name *name)
470 const char *p, *q;
471 size_t len;
472 hx509_name n;
473 int ret;
475 *name = NULL;
477 n = calloc(1, sizeof(*n));
478 if (n == NULL) {
479 hx509_set_error_string(context, 0, ENOMEM, "out of memory");
480 return ENOMEM;
483 n->der_name.element = choice_Name_rdnSequence;
485 p = str;
487 while (p != NULL && *p != '\0') {
488 heim_oid oid;
489 int last;
491 q = strchr(p, ',');
492 if (q) {
493 len = (q - p);
494 last = 1;
495 } else {
496 len = strlen(p);
497 last = 0;
500 q = strchr(p, '=');
501 if (q == NULL) {
502 ret = HX509_PARSING_NAME_FAILED;
503 hx509_set_error_string(context, 0, ret, "missing = in %s", p);
504 goto out;
506 if (q == p) {
507 ret = HX509_PARSING_NAME_FAILED;
508 hx509_set_error_string(context, 0, ret,
509 "missing name before = in %s", p);
510 goto out;
513 if ((q - p) > len) {
514 ret = HX509_PARSING_NAME_FAILED;
515 hx509_set_error_string(context, 0, ret, " = after , in %s", p);
516 goto out;
519 ret = stringtooid(p, q - p, &oid);
520 if (ret) {
521 ret = HX509_PARSING_NAME_FAILED;
522 hx509_set_error_string(context, 0, ret,
523 "unknown type: %.*s", (int)(q - p), p);
524 goto out;
528 size_t pstr_len = len - (q - p) - 1;
529 const char *pstr = p + (q - p) + 1;
530 char *r;
532 r = malloc(pstr_len + 1);
533 if (r == NULL) {
534 der_free_oid(&oid);
535 ret = ENOMEM;
536 hx509_set_error_string(context, 0, ret, "out of memory");
537 goto out;
539 memcpy(r, pstr, pstr_len);
540 r[pstr_len] = '\0';
542 ret = _hx509_name_modify(context, &n->der_name, 0, &oid, r);
543 free(r);
544 der_free_oid(&oid);
545 if(ret)
546 goto out;
548 p += len + last;
551 *name = n;
553 return 0;
554 out:
555 hx509_name_free(&n);
556 return HX509_NAME_MALFORMED;
560 * Copy a hx509 name object.
562 * @param context A hx509 cotext.
563 * @param from the name to copy from
564 * @param to the name to copy to
566 * @return An hx509 error code, see hx509_get_error_string().
568 * @ingroup hx509_name
572 hx509_name_copy(hx509_context context, const hx509_name from, hx509_name *to)
574 int ret;
576 *to = calloc(1, sizeof(**to));
577 if (*to == NULL)
578 return ENOMEM;
579 ret = copy_Name(&from->der_name, &(*to)->der_name);
580 if (ret) {
581 free(*to);
582 *to = NULL;
583 return ENOMEM;
585 return 0;
589 * Convert a hx509_name into a Name.
591 * @param from the name to copy from
592 * @param to the name to copy to
594 * @return An hx509 error code, see hx509_get_error_string().
596 * @ingroup hx509_name
600 hx509_name_to_Name(const hx509_name from, Name *to)
602 return copy_Name(&from->der_name, to);
606 hx509_name_normalize(hx509_context context, hx509_name name)
608 return 0;
612 * Expands variables in the name using env. Variables are on the form
613 * ${name}. Useful when dealing with certificate templates.
615 * @param context A hx509 cotext.
616 * @param name the name to expand.
617 * @param env environment variable to expand.
619 * @return An hx509 error code, see hx509_get_error_string().
621 * @ingroup hx509_name
625 hx509_name_expand(hx509_context context,
626 hx509_name name,
627 hx509_env env)
629 Name *n = &name->der_name;
630 int i, j;
632 if (env == NULL)
633 return 0;
635 if (n->element != choice_Name_rdnSequence) {
636 hx509_set_error_string(context, 0, EINVAL, "RDN not of supported type");
637 return EINVAL;
640 for (i = 0 ; i < n->u.rdnSequence.len; i++) {
641 for (j = 0; j < n->u.rdnSequence.val[i].len; j++) {
642 /** Only UTF8String rdnSequence names are allowed */
644 THIS SHOULD REALLY BE:
645 COMP = n->u.rdnSequence.val[i].val[j];
646 normalize COMP to utf8
647 check if there are variables
648 expand variables
649 convert back to orignal format, store in COMP
650 free normalized utf8 string
652 DirectoryString *ds = &n->u.rdnSequence.val[i].val[j].value;
653 char *p, *p2;
654 struct rk_strpool *strpool = NULL;
656 if (ds->element != choice_DirectoryString_utf8String) {
657 hx509_set_error_string(context, 0, EINVAL, "unsupported type");
658 return EINVAL;
660 p = strstr(ds->u.utf8String, "${");
661 if (p) {
662 strpool = rk_strpoolprintf(strpool, "%.*s",
663 (int)(p - ds->u.utf8String),
664 ds->u.utf8String);
665 if (strpool == NULL) {
666 hx509_set_error_string(context, 0, ENOMEM, "out of memory");
667 return ENOMEM;
670 while (p != NULL) {
671 /* expand variables */
672 const char *value;
673 p2 = strchr(p, '}');
674 if (p2 == NULL) {
675 hx509_set_error_string(context, 0, EINVAL, "missing }");
676 rk_strpoolfree(strpool);
677 return EINVAL;
679 p += 2;
680 value = hx509_env_lfind(context, env, p, p2 - p);
681 if (value == NULL) {
682 hx509_set_error_string(context, 0, EINVAL,
683 "variable %.*s missing",
684 (int)(p2 - p), p);
685 rk_strpoolfree(strpool);
686 return EINVAL;
688 strpool = rk_strpoolprintf(strpool, "%s", value);
689 if (strpool == NULL) {
690 hx509_set_error_string(context, 0, ENOMEM, "out of memory");
691 return ENOMEM;
693 p2++;
695 p = strstr(p2, "${");
696 if (p)
697 strpool = rk_strpoolprintf(strpool, "%.*s",
698 (int)(p - p2), p2);
699 else
700 strpool = rk_strpoolprintf(strpool, "%s", p2);
701 if (strpool == NULL) {
702 hx509_set_error_string(context, 0, ENOMEM, "out of memory");
703 return ENOMEM;
706 if (strpool) {
707 free(ds->u.utf8String);
708 ds->u.utf8String = rk_strpoolcollect(strpool);
709 if (ds->u.utf8String == NULL) {
710 hx509_set_error_string(context, 0, ENOMEM, "out of memory");
711 return ENOMEM;
716 return 0;
720 * Free a hx509 name object, upond return *name will be NULL.
722 * @param name a hx509 name object to be freed.
724 * @ingroup hx509_name
727 void
728 hx509_name_free(hx509_name *name)
730 free_Name(&(*name)->der_name);
731 memset(*name, 0, sizeof(**name));
732 free(*name);
733 *name = NULL;
737 * Convert a DER encoded name info a string.
739 * @param data data to a DER/BER encoded name
740 * @param length length of data
741 * @param str the resulting string, is NULL on failure.
743 * @return An hx509 error code, see hx509_get_error_string().
745 * @ingroup hx509_name
749 hx509_unparse_der_name(const void *data, size_t length, char **str)
751 Name name;
752 int ret;
754 *str = NULL;
756 ret = decode_Name(data, length, &name, NULL);
757 if (ret)
758 return ret;
759 ret = _hx509_Name_to_string(&name, str);
760 free_Name(&name);
761 return ret;
765 * Convert a hx509_name object to DER encoded name.
767 * @param name name to concert
768 * @param os data to a DER encoded name, free the resulting octet
769 * string with hx509_xfree(os->data).
771 * @return An hx509 error code, see hx509_get_error_string().
773 * @ingroup hx509_name
777 hx509_name_binary(const hx509_name name, heim_octet_string *os)
779 size_t size;
780 int ret;
782 ASN1_MALLOC_ENCODE(Name, os->data, os->length, &name->der_name, &size, ret);
783 if (ret)
784 return ret;
785 if (os->length != size)
786 _hx509_abort("internal ASN.1 encoder error");
788 return 0;
792 _hx509_unparse_Name(const Name *aname, char **str)
794 hx509_name name;
795 int ret;
797 ret = _hx509_name_from_Name(aname, &name);
798 if (ret)
799 return ret;
801 ret = hx509_name_to_string(name, str);
802 hx509_name_free(&name);
803 return ret;
807 * Unparse the hx509 name in name into a string.
809 * @param name the name to check if its empty/null.
811 * @return non zero if the name is empty/null.
813 * @ingroup hx509_name
817 hx509_name_is_null_p(const hx509_name name)
819 return name->der_name.u.rdnSequence.len == 0;
823 * Unparse the hx509 name in name into a string.
825 * @param name the name to print
826 * @param str an allocated string returns the name in string form
828 * @return An hx509 error code, see krb5_get_error_string().
830 * @ingroup hx509_name
834 hx509_general_name_unparse(GeneralName *name, char **str)
836 struct rk_strpool *strpool = NULL;
838 *str = NULL;
840 switch (name->element) {
841 case choice_GeneralName_otherName: {
842 char *str;
843 hx509_oid_sprint(&name->u.otherName.type_id, &str);
844 if (str == NULL)
845 return ENOMEM;
846 strpool = rk_strpoolprintf(strpool, "otherName: %s", str);
847 free(str);
848 break;
850 case choice_GeneralName_rfc822Name:
851 strpool = rk_strpoolprintf(strpool, "rfc822Name: %s\n",
852 name->u.rfc822Name);
853 break;
854 case choice_GeneralName_dNSName:
855 strpool = rk_strpoolprintf(strpool, "dNSName: %s\n",
856 name->u.dNSName);
857 break;
858 case choice_GeneralName_directoryName: {
859 Name dir;
860 char *s;
861 int ret;
862 memset(&dir, 0, sizeof(dir));
863 dir.element = name->u.directoryName.element;
864 dir.u.rdnSequence = name->u.directoryName.u.rdnSequence;
865 ret = _hx509_unparse_Name(&dir, &s);
866 if (ret)
867 return ret;
868 strpool = rk_strpoolprintf(strpool, "directoryName: %s", s);
869 free(s);
870 break;
872 case choice_GeneralName_uniformResourceIdentifier:
873 strpool = rk_strpoolprintf(strpool, "URI: %s",
874 name->u.uniformResourceIdentifier);
875 break;
876 case choice_GeneralName_iPAddress: {
877 unsigned char *a = name->u.iPAddress.data;
879 strpool = rk_strpoolprintf(strpool, "IPAddress: ");
880 if (strpool == NULL)
881 break;
882 if (name->u.iPAddress.length == 4)
883 strpool = rk_strpoolprintf(strpool, "%d.%d.%d.%d",
884 a[0], a[1], a[2], a[3]);
885 else if (name->u.iPAddress.length == 16)
886 strpool = rk_strpoolprintf(strpool,
887 "%02X:%02X:%02X:%02X:"
888 "%02X:%02X:%02X:%02X:"
889 "%02X:%02X:%02X:%02X:"
890 "%02X:%02X:%02X:%02X",
891 a[0], a[1], a[2], a[3],
892 a[4], a[5], a[6], a[7],
893 a[8], a[9], a[10], a[11],
894 a[12], a[13], a[14], a[15]);
895 else
896 strpool = rk_strpoolprintf(strpool,
897 "unknown IP address of length %lu",
898 (unsigned long)name->u.iPAddress.length);
899 break;
901 case choice_GeneralName_registeredID: {
902 char *str;
903 hx509_oid_sprint(&name->u.registeredID, &str);
904 if (str == NULL)
905 return ENOMEM;
906 strpool = rk_strpoolprintf(strpool, "registeredID: %s", str);
907 free(str);
908 break;
910 default:
911 return EINVAL;
913 if (strpool == NULL)
914 return ENOMEM;
916 *str = rk_strpoolcollect(strpool);
918 return 0;