Round #2 of scan-build warnings cleanup
[heimdal.git] / lib / hx509 / name.c
blobfb6be5cd4e9598eef75d147be167803ef9f311e4
1 /*
2 * Copyright (c) 2004 - 2009 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 #include <wind.h>
36 #include "char_map.h"
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 several 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;
67 wind_profile_flags flags;
68 } no[] = {
69 { "C", &asn1_oid_id_at_countryName, 0 },
70 { "CN", &asn1_oid_id_at_commonName, 0 },
71 { "DC", &asn1_oid_id_domainComponent, 0 },
72 { "L", &asn1_oid_id_at_localityName, 0 },
73 { "O", &asn1_oid_id_at_organizationName, 0 },
74 { "OU", &asn1_oid_id_at_organizationalUnitName, 0 },
75 { "S", &asn1_oid_id_at_stateOrProvinceName, 0 },
76 { "STREET", &asn1_oid_id_at_streetAddress, 0 },
77 { "UID", &asn1_oid_id_Userid, 0 },
78 { "emailAddress", &asn1_oid_id_pkcs9_emailAddress, 0 },
79 { "serialNumber", &asn1_oid_id_at_serialNumber, 0 }
82 static char *
83 quote_string(const char *f, size_t len, int flags, size_t *rlen)
85 size_t i, j, tolen;
86 const unsigned char *from = (const unsigned char *)f;
87 unsigned char *to;
89 tolen = len * 3 + 1;
90 to = malloc(tolen);
91 if (to == NULL)
92 return NULL;
94 for (i = 0, j = 0; i < len; i++) {
95 unsigned char map = char_map[from[i]] & flags;
96 if (i == 0 && (map & Q_RFC2253_QUOTE_FIRST)) {
97 to[j++] = '\\';
98 to[j++] = from[i];
99 } else if ((i + 1) == len && (map & Q_RFC2253_QUOTE_LAST)) {
101 to[j++] = '\\';
102 to[j++] = from[i];
103 } else if (map & Q_RFC2253_QUOTE) {
104 to[j++] = '\\';
105 to[j++] = from[i];
106 } else if (map & Q_RFC2253_HEX) {
107 int l = snprintf((char *)&to[j], tolen - j - 1,
108 "#%02x", (unsigned char)from[i]);
109 j += l;
110 } else {
111 to[j++] = from[i];
114 to[j] = '\0';
115 assert(j < tolen);
116 *rlen = j;
117 return (char *)to;
121 static int
122 append_string(char **str, size_t *total_len, const char *ss,
123 size_t len, int quote)
125 char *s, *qs;
127 if (quote)
128 qs = quote_string(ss, len, Q_RFC2253, &len);
129 else
130 qs = rk_UNCONST(ss);
132 s = realloc(*str, len + *total_len + 1);
133 if (s == NULL)
134 _hx509_abort("allocation failure"); /* XXX */
135 memcpy(s + *total_len, qs, len);
136 if (qs != ss)
137 free(qs);
138 s[*total_len + len] = '\0';
139 *str = s;
140 *total_len += len;
141 return 0;
144 static char *
145 oidtostring(const heim_oid *type)
147 char *s;
148 size_t i;
150 for (i = 0; i < sizeof(no)/sizeof(no[0]); i++) {
151 if (der_heim_oid_cmp(no[i].o, type) == 0)
152 return strdup(no[i].n);
154 if (der_print_heim_oid(type, '.', &s) != 0)
155 return NULL;
156 return s;
159 static int
160 stringtooid(const char *name, size_t len, heim_oid *oid)
162 int ret;
163 size_t i;
164 char *s;
166 memset(oid, 0, sizeof(*oid));
168 for (i = 0; i < sizeof(no)/sizeof(no[0]); i++) {
169 if (strncasecmp(no[i].n, name, len) == 0)
170 return der_copy_oid(no[i].o, oid);
172 s = malloc(len + 1);
173 if (s == NULL)
174 return ENOMEM;
175 memcpy(s, name, len);
176 s[len] = '\0';
177 ret = der_parse_heim_oid(s, ".", oid);
178 free(s);
179 return ret;
183 * Convert the hx509 name object into a printable string.
184 * The resulting string should be freed with free().
186 * @param name name to print
187 * @param str the string to return
189 * @return An hx509 error code, see hx509_get_error_string().
191 * @ingroup hx509_name
195 hx509_name_to_string(const hx509_name name, char **str)
197 return _hx509_Name_to_string(&name->der_name, str);
201 _hx509_Name_to_string(const Name *n, char **str)
203 size_t total_len = 0;
204 size_t i, j, m;
205 int ret;
207 *str = strdup("");
208 if (*str == NULL)
209 return ENOMEM;
211 for (m = n->u.rdnSequence.len; m > 0; m--) {
212 size_t len;
213 i = m - 1;
215 for (j = 0; j < n->u.rdnSequence.val[i].len; j++) {
216 DirectoryString *ds = &n->u.rdnSequence.val[i].val[j].value;
217 char *oidname;
218 char *ss;
220 oidname = oidtostring(&n->u.rdnSequence.val[i].val[j].type);
222 switch(ds->element) {
223 case choice_DirectoryString_ia5String:
224 ss = ds->u.ia5String.data;
225 len = ds->u.ia5String.length;
226 break;
227 case choice_DirectoryString_printableString:
228 ss = ds->u.printableString.data;
229 len = ds->u.printableString.length;
230 break;
231 case choice_DirectoryString_utf8String:
232 ss = ds->u.utf8String;
233 len = strlen(ss);
234 break;
235 case choice_DirectoryString_bmpString: {
236 const uint16_t *bmp = ds->u.bmpString.data;
237 size_t bmplen = ds->u.bmpString.length;
238 size_t k;
240 ret = wind_ucs2utf8_length(bmp, bmplen, &k);
241 if (ret) {
242 free(oidname);
243 return ret;
246 ss = malloc(k + 1);
247 if (ss == NULL)
248 _hx509_abort("allocation failure"); /* XXX */
249 ret = wind_ucs2utf8(bmp, bmplen, ss, NULL);
250 if (ret) {
251 free(oidname);
252 free(ss);
253 return ret;
255 ss[k] = '\0';
256 len = k;
257 break;
259 case choice_DirectoryString_teletexString:
260 ss = ds->u.teletexString;
261 len = strlen(ss);
262 break;
263 case choice_DirectoryString_universalString: {
264 const uint32_t *uni = ds->u.universalString.data;
265 size_t unilen = ds->u.universalString.length;
266 size_t k;
268 ret = wind_ucs4utf8_length(uni, unilen, &k);
269 if (ret) {
270 free(oidname);
271 return ret;
274 ss = malloc(k + 1);
275 if (ss == NULL)
276 _hx509_abort("allocation failure"); /* XXX */
277 ret = wind_ucs4utf8(uni, unilen, ss, NULL);
278 if (ret) {
279 free(ss);
280 free(oidname);
281 return ret;
283 ss[k] = '\0';
284 len = k;
285 break;
287 default:
288 _hx509_abort("unknown directory type: %d", ds->element);
289 exit(1);
291 append_string(str, &total_len, oidname, strlen(oidname), 0);
292 free(oidname);
293 append_string(str, &total_len, "=", 1, 0);
294 append_string(str, &total_len, ss, len, 1);
295 if (ds->element == choice_DirectoryString_bmpString ||
296 ds->element == choice_DirectoryString_universalString)
298 free(ss);
300 if (j + 1 < n->u.rdnSequence.val[i].len)
301 append_string(str, &total_len, "+", 1, 0);
304 if (i > 0)
305 append_string(str, &total_len, ",", 1, 0);
307 return 0;
310 #define COPYCHARARRAY(_ds,_el,_l,_n) \
311 (_l) = strlen(_ds->u._el); \
312 (_n) = malloc((_l) * sizeof((_n)[0])); \
313 if ((_n) == NULL) \
314 return ENOMEM; \
315 for (i = 0; i < (_l); i++) \
316 (_n)[i] = _ds->u._el[i]
319 #define COPYVALARRAY(_ds,_el,_l,_n) \
320 (_l) = _ds->u._el.length; \
321 (_n) = malloc((_l) * sizeof((_n)[0])); \
322 if ((_n) == NULL) \
323 return ENOMEM; \
324 for (i = 0; i < (_l); i++) \
325 (_n)[i] = _ds->u._el.data[i]
327 #define COPYVOIDARRAY(_ds,_el,_l,_n) \
328 (_l) = _ds->u._el.length; \
329 (_n) = malloc((_l) * sizeof((_n)[0])); \
330 if ((_n) == NULL) \
331 return ENOMEM; \
332 for (i = 0; i < (_l); i++) \
333 (_n)[i] = ((unsigned char *)_ds->u._el.data)[i]
337 static int
338 dsstringprep(const DirectoryString *ds, uint32_t **rname, size_t *rlen)
340 wind_profile_flags flags;
341 size_t i, len;
342 int ret;
343 uint32_t *name;
345 *rname = NULL;
346 *rlen = 0;
348 switch(ds->element) {
349 case choice_DirectoryString_ia5String:
350 flags = WIND_PROFILE_LDAP;
351 COPYVOIDARRAY(ds, ia5String, len, name);
352 break;
353 case choice_DirectoryString_printableString:
354 flags = WIND_PROFILE_LDAP;
355 flags |= WIND_PROFILE_LDAP_CASE_EXACT_ATTRIBUTE;
356 COPYVOIDARRAY(ds, printableString, len, name);
357 break;
358 case choice_DirectoryString_teletexString:
359 flags = WIND_PROFILE_LDAP_CASE;
360 COPYCHARARRAY(ds, teletexString, len, name);
361 break;
362 case choice_DirectoryString_bmpString:
363 flags = WIND_PROFILE_LDAP;
364 COPYVALARRAY(ds, bmpString, len, name);
365 break;
366 case choice_DirectoryString_universalString:
367 flags = WIND_PROFILE_LDAP;
368 COPYVALARRAY(ds, universalString, len, name);
369 break;
370 case choice_DirectoryString_utf8String:
371 flags = WIND_PROFILE_LDAP;
372 ret = wind_utf8ucs4_length(ds->u.utf8String, &len);
373 if (ret)
374 return ret;
375 name = malloc(len * sizeof(name[0]));
376 if (name == NULL)
377 return ENOMEM;
378 ret = wind_utf8ucs4(ds->u.utf8String, name, &len);
379 if (ret) {
380 free(name);
381 return ret;
383 break;
384 default:
385 _hx509_abort("unknown directory type: %d", ds->element);
388 *rlen = len;
389 /* try a couple of times to get the length right, XXX gross */
390 for (i = 0; i < 4; i++) {
391 *rlen = *rlen * 2;
392 *rname = malloc(*rlen * sizeof((*rname)[0]));
394 ret = wind_stringprep(name, len, *rname, rlen, flags);
395 if (ret == WIND_ERR_OVERRUN) {
396 free(*rname);
397 *rname = NULL;
398 continue;
399 } else
400 break;
402 free(name);
403 if (ret) {
404 if (*rname)
405 free(*rname);
406 *rname = NULL;
407 *rlen = 0;
408 return ret;
411 return 0;
415 _hx509_name_ds_cmp(const DirectoryString *ds1,
416 const DirectoryString *ds2,
417 int *diff)
419 uint32_t *ds1lp, *ds2lp;
420 size_t ds1len, ds2len, i;
421 int ret;
423 ret = dsstringprep(ds1, &ds1lp, &ds1len);
424 if (ret)
425 return ret;
426 ret = dsstringprep(ds2, &ds2lp, &ds2len);
427 if (ret) {
428 free(ds1lp);
429 return ret;
432 if (ds1len != ds2len)
433 *diff = ds1len - ds2len;
434 else {
435 for (i = 0; i < ds1len; i++) {
436 *diff = ds1lp[i] - ds2lp[i];
437 if (*diff)
438 break;
441 free(ds1lp);
442 free(ds2lp);
444 return 0;
448 _hx509_name_cmp(const Name *n1, const Name *n2, int *c)
450 int ret;
451 size_t i, j;
453 *c = n1->u.rdnSequence.len - n2->u.rdnSequence.len;
454 if (*c)
455 return 0;
457 for (i = 0 ; i < n1->u.rdnSequence.len; i++) {
458 *c = n1->u.rdnSequence.val[i].len - n2->u.rdnSequence.val[i].len;
459 if (*c)
460 return 0;
462 for (j = 0; j < n1->u.rdnSequence.val[i].len; j++) {
463 *c = der_heim_oid_cmp(&n1->u.rdnSequence.val[i].val[j].type,
464 &n1->u.rdnSequence.val[i].val[j].type);
465 if (*c)
466 return 0;
468 ret = _hx509_name_ds_cmp(&n1->u.rdnSequence.val[i].val[j].value,
469 &n2->u.rdnSequence.val[i].val[j].value,
471 if (ret)
472 return ret;
473 if (*c)
474 return 0;
477 *c = 0;
478 return 0;
482 * Compare to hx509 name object, useful for sorting.
484 * @param n1 a hx509 name object.
485 * @param n2 a hx509 name object.
487 * @return 0 the objects are the same, returns > 0 is n2 is "larger"
488 * then n2, < 0 if n1 is "smaller" then n2.
490 * @ingroup hx509_name
494 hx509_name_cmp(hx509_name n1, hx509_name n2)
496 int ret, diff;
497 ret = _hx509_name_cmp(&n1->der_name, &n2->der_name, &diff);
498 if (ret)
499 return ret;
500 return diff;
505 _hx509_name_from_Name(const Name *n, hx509_name *name)
507 int ret;
508 *name = calloc(1, sizeof(**name));
509 if (*name == NULL)
510 return ENOMEM;
511 ret = copy_Name(n, &(*name)->der_name);
512 if (ret) {
513 free(*name);
514 *name = NULL;
516 return ret;
520 _hx509_name_modify(hx509_context context,
521 Name *name,
522 int append,
523 const heim_oid *oid,
524 const char *str)
526 RelativeDistinguishedName *rdn;
527 int ret;
528 void *ptr;
530 ptr = realloc(name->u.rdnSequence.val,
531 sizeof(name->u.rdnSequence.val[0]) *
532 (name->u.rdnSequence.len + 1));
533 if (ptr == NULL) {
534 hx509_set_error_string(context, 0, ENOMEM, "Out of memory");
535 return ENOMEM;
537 name->u.rdnSequence.val = ptr;
539 if (append) {
540 rdn = &name->u.rdnSequence.val[name->u.rdnSequence.len];
541 } else {
542 memmove(&name->u.rdnSequence.val[1],
543 &name->u.rdnSequence.val[0],
544 name->u.rdnSequence.len *
545 sizeof(name->u.rdnSequence.val[0]));
547 rdn = &name->u.rdnSequence.val[0];
549 rdn->val = malloc(sizeof(rdn->val[0]));
550 if (rdn->val == NULL)
551 return ENOMEM;
552 rdn->len = 1;
553 ret = der_copy_oid(oid, &rdn->val[0].type);
554 if (ret)
555 return ret;
556 rdn->val[0].value.element = choice_DirectoryString_utf8String;
557 rdn->val[0].value.u.utf8String = strdup(str);
558 if (rdn->val[0].value.u.utf8String == NULL)
559 return ENOMEM;
560 name->u.rdnSequence.len += 1;
562 return 0;
566 * Parse a string into a hx509 name object.
568 * @param context A hx509 context.
569 * @param str a string to parse.
570 * @param name the resulting object, NULL in case of error.
572 * @return An hx509 error code, see hx509_get_error_string().
574 * @ingroup hx509_name
578 hx509_parse_name(hx509_context context, const char *str, hx509_name *name)
580 const char *p, *q;
581 size_t len;
582 hx509_name n;
583 int ret;
585 *name = NULL;
587 n = calloc(1, sizeof(*n));
588 if (n == NULL) {
589 hx509_set_error_string(context, 0, ENOMEM, "out of memory");
590 return ENOMEM;
593 n->der_name.element = choice_Name_rdnSequence;
595 p = str;
597 while (p != NULL && *p != '\0') {
598 heim_oid oid;
599 int last;
601 q = strchr(p, ',');
602 if (q) {
603 len = (q - p);
604 last = 1;
605 } else {
606 len = strlen(p);
607 last = 0;
610 q = strchr(p, '=');
611 if (q == NULL) {
612 ret = HX509_PARSING_NAME_FAILED;
613 hx509_set_error_string(context, 0, ret, "missing = in %s", p);
614 goto out;
616 if (q == p) {
617 ret = HX509_PARSING_NAME_FAILED;
618 hx509_set_error_string(context, 0, ret,
619 "missing name before = in %s", p);
620 goto out;
623 if ((size_t)(q - p) > len) {
624 ret = HX509_PARSING_NAME_FAILED;
625 hx509_set_error_string(context, 0, ret, " = after , in %s", p);
626 goto out;
629 ret = stringtooid(p, q - p, &oid);
630 if (ret) {
631 ret = HX509_PARSING_NAME_FAILED;
632 hx509_set_error_string(context, 0, ret,
633 "unknown type: %.*s", (int)(q - p), p);
634 goto out;
638 size_t pstr_len = len - (q - p) - 1;
639 const char *pstr = p + (q - p) + 1;
640 char *r;
642 r = malloc(pstr_len + 1);
643 if (r == NULL) {
644 der_free_oid(&oid);
645 ret = ENOMEM;
646 hx509_set_error_string(context, 0, ret, "out of memory");
647 goto out;
649 memcpy(r, pstr, pstr_len);
650 r[pstr_len] = '\0';
652 ret = _hx509_name_modify(context, &n->der_name, 0, &oid, r);
653 free(r);
654 der_free_oid(&oid);
655 if(ret)
656 goto out;
658 p += len + last;
661 *name = n;
663 return 0;
664 out:
665 hx509_name_free(&n);
666 return HX509_NAME_MALFORMED;
670 * Copy a hx509 name object.
672 * @param context A hx509 cotext.
673 * @param from the name to copy from
674 * @param to the name to copy to
676 * @return An hx509 error code, see hx509_get_error_string().
678 * @ingroup hx509_name
682 hx509_name_copy(hx509_context context, const hx509_name from, hx509_name *to)
684 int ret;
686 *to = calloc(1, sizeof(**to));
687 if (*to == NULL)
688 return ENOMEM;
689 ret = copy_Name(&from->der_name, &(*to)->der_name);
690 if (ret) {
691 free(*to);
692 *to = NULL;
693 return ENOMEM;
695 return 0;
699 * Convert a hx509_name into a Name.
701 * @param from the name to copy from
702 * @param to the name to copy to
704 * @return An hx509 error code, see hx509_get_error_string().
706 * @ingroup hx509_name
710 hx509_name_to_Name(const hx509_name from, Name *to)
712 return copy_Name(&from->der_name, to);
716 hx509_name_normalize(hx509_context context, hx509_name name)
718 return 0;
722 * Expands variables in the name using env. Variables are on the form
723 * ${name}. Useful when dealing with certificate templates.
725 * @param context A hx509 cotext.
726 * @param name the name to expand.
727 * @param env environment variable to expand.
729 * @return An hx509 error code, see hx509_get_error_string().
731 * @ingroup hx509_name
735 hx509_name_expand(hx509_context context,
736 hx509_name name,
737 hx509_env env)
739 Name *n = &name->der_name;
740 size_t i, j;
742 if (env == NULL)
743 return 0;
745 if (n->element != choice_Name_rdnSequence) {
746 hx509_set_error_string(context, 0, EINVAL, "RDN not of supported type");
747 return EINVAL;
750 for (i = 0 ; i < n->u.rdnSequence.len; i++) {
751 for (j = 0; j < n->u.rdnSequence.val[i].len; j++) {
752 /** Only UTF8String rdnSequence names are allowed */
754 THIS SHOULD REALLY BE:
755 COMP = n->u.rdnSequence.val[i].val[j];
756 normalize COMP to utf8
757 check if there are variables
758 expand variables
759 convert back to orignal format, store in COMP
760 free normalized utf8 string
762 DirectoryString *ds = &n->u.rdnSequence.val[i].val[j].value;
763 char *p, *p2;
764 struct rk_strpool *strpool = NULL;
766 if (ds->element != choice_DirectoryString_utf8String) {
767 hx509_set_error_string(context, 0, EINVAL, "unsupported type");
768 return EINVAL;
770 p = strstr(ds->u.utf8String, "${");
771 if (p) {
772 strpool = rk_strpoolprintf(strpool, "%.*s",
773 (int)(p - ds->u.utf8String),
774 ds->u.utf8String);
775 if (strpool == NULL) {
776 hx509_set_error_string(context, 0, ENOMEM, "out of memory");
777 return ENOMEM;
780 while (p != NULL) {
781 /* expand variables */
782 const char *value;
783 p2 = strchr(p, '}');
784 if (p2 == NULL) {
785 hx509_set_error_string(context, 0, EINVAL, "missing }");
786 rk_strpoolfree(strpool);
787 return EINVAL;
789 p += 2;
790 value = hx509_env_lfind(context, env, p, p2 - p);
791 if (value == NULL) {
792 hx509_set_error_string(context, 0, EINVAL,
793 "variable %.*s missing",
794 (int)(p2 - p), p);
795 rk_strpoolfree(strpool);
796 return EINVAL;
798 strpool = rk_strpoolprintf(strpool, "%s", value);
799 if (strpool == NULL) {
800 hx509_set_error_string(context, 0, ENOMEM, "out of memory");
801 return ENOMEM;
803 p2++;
805 p = strstr(p2, "${");
806 if (p)
807 strpool = rk_strpoolprintf(strpool, "%.*s",
808 (int)(p - p2), p2);
809 else
810 strpool = rk_strpoolprintf(strpool, "%s", p2);
811 if (strpool == NULL) {
812 hx509_set_error_string(context, 0, ENOMEM, "out of memory");
813 return ENOMEM;
816 if (strpool) {
817 free(ds->u.utf8String);
818 ds->u.utf8String = rk_strpoolcollect(strpool);
819 if (ds->u.utf8String == NULL) {
820 hx509_set_error_string(context, 0, ENOMEM, "out of memory");
821 return ENOMEM;
826 return 0;
830 * Free a hx509 name object, upond return *name will be NULL.
832 * @param name a hx509 name object to be freed.
834 * @ingroup hx509_name
837 void
838 hx509_name_free(hx509_name *name)
840 free_Name(&(*name)->der_name);
841 memset(*name, 0, sizeof(**name));
842 free(*name);
843 *name = NULL;
847 * Convert a DER encoded name info a string.
849 * @param data data to a DER/BER encoded name
850 * @param length length of data
851 * @param str the resulting string, is NULL on failure.
853 * @return An hx509 error code, see hx509_get_error_string().
855 * @ingroup hx509_name
859 hx509_unparse_der_name(const void *data, size_t length, char **str)
861 Name name;
862 int ret;
864 *str = NULL;
866 ret = decode_Name(data, length, &name, NULL);
867 if (ret)
868 return ret;
869 ret = _hx509_Name_to_string(&name, str);
870 free_Name(&name);
871 return ret;
875 * Convert a hx509_name object to DER encoded name.
877 * @param name name to concert
878 * @param os data to a DER encoded name, free the resulting octet
879 * string with hx509_xfree(os->data).
881 * @return An hx509 error code, see hx509_get_error_string().
883 * @ingroup hx509_name
887 hx509_name_binary(const hx509_name name, heim_octet_string *os)
889 size_t size;
890 int ret;
892 ASN1_MALLOC_ENCODE(Name, os->data, os->length, &name->der_name, &size, ret);
893 if (ret)
894 return ret;
895 if (os->length != size)
896 _hx509_abort("internal ASN.1 encoder error");
898 return 0;
902 _hx509_unparse_Name(const Name *aname, char **str)
904 hx509_name name;
905 int ret;
907 ret = _hx509_name_from_Name(aname, &name);
908 if (ret)
909 return ret;
911 ret = hx509_name_to_string(name, str);
912 hx509_name_free(&name);
913 return ret;
917 * Unparse the hx509 name in name into a string.
919 * @param name the name to check if its empty/null.
921 * @return non zero if the name is empty/null.
923 * @ingroup hx509_name
927 hx509_name_is_null_p(const hx509_name name)
929 return name->der_name.u.rdnSequence.len == 0;
933 * Unparse the hx509 name in name into a string.
935 * @param name the name to print
936 * @param str an allocated string returns the name in string form
938 * @return An hx509 error code, see hx509_get_error_string().
940 * @ingroup hx509_name
944 hx509_general_name_unparse(GeneralName *name, char **str)
946 struct rk_strpool *strpool = NULL;
948 *str = NULL;
950 switch (name->element) {
951 case choice_GeneralName_otherName: {
952 char *oid;
953 hx509_oid_sprint(&name->u.otherName.type_id, &oid);
954 if (oid == NULL)
955 return ENOMEM;
956 strpool = rk_strpoolprintf(strpool, "otherName: %s", oid);
957 free(oid);
958 break;
960 case choice_GeneralName_rfc822Name:
961 strpool = rk_strpoolprintf(strpool, "rfc822Name: %.*s\n",
962 (int)name->u.rfc822Name.length,
963 (char *)name->u.rfc822Name.data);
964 break;
965 case choice_GeneralName_dNSName:
966 strpool = rk_strpoolprintf(strpool, "dNSName: %.*s\n",
967 (int)name->u.dNSName.length,
968 (char *)name->u.dNSName.data);
969 break;
970 case choice_GeneralName_directoryName: {
971 Name dir;
972 char *s;
973 int ret;
974 memset(&dir, 0, sizeof(dir));
975 dir.element = (enum Name_enum)name->u.directoryName.element;
976 dir.u.rdnSequence = name->u.directoryName.u.rdnSequence;
977 ret = _hx509_unparse_Name(&dir, &s);
978 if (ret)
979 return ret;
980 strpool = rk_strpoolprintf(strpool, "directoryName: %s", s);
981 free(s);
982 break;
984 case choice_GeneralName_uniformResourceIdentifier:
985 strpool = rk_strpoolprintf(strpool, "URI: %.*s",
986 (int)name->u.uniformResourceIdentifier.length,
987 (char *)name->u.uniformResourceIdentifier.data);
988 break;
989 case choice_GeneralName_iPAddress: {
990 unsigned char *a = name->u.iPAddress.data;
992 strpool = rk_strpoolprintf(strpool, "IPAddress: ");
993 if (strpool == NULL)
994 break;
995 if (name->u.iPAddress.length == 4)
996 strpool = rk_strpoolprintf(strpool, "%d.%d.%d.%d",
997 a[0], a[1], a[2], a[3]);
998 else if (name->u.iPAddress.length == 16)
999 strpool = rk_strpoolprintf(strpool,
1000 "%02X:%02X:%02X:%02X:"
1001 "%02X:%02X:%02X:%02X:"
1002 "%02X:%02X:%02X:%02X:"
1003 "%02X:%02X:%02X:%02X",
1004 a[0], a[1], a[2], a[3],
1005 a[4], a[5], a[6], a[7],
1006 a[8], a[9], a[10], a[11],
1007 a[12], a[13], a[14], a[15]);
1008 else
1009 strpool = rk_strpoolprintf(strpool,
1010 "unknown IP address of length %lu",
1011 (unsigned long)name->u.iPAddress.length);
1012 break;
1014 case choice_GeneralName_registeredID: {
1015 char *oid;
1016 hx509_oid_sprint(&name->u.registeredID, &oid);
1017 if (oid == NULL)
1018 return ENOMEM;
1019 strpool = rk_strpoolprintf(strpool, "registeredID: %s", oid);
1020 free(oid);
1021 break;
1023 default:
1024 return EINVAL;
1026 if (strpool == NULL)
1027 return ENOMEM;
1029 *str = rk_strpoolcollect(strpool);
1031 return 0;