2 * Copyright (c) 2004 - 2009 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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
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
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
67 wind_profile_flags flags;
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 }
83 quote_string(const char *f, size_t len, int flags, size_t *rlen)
86 const unsigned char *from = (const unsigned char *)f;
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)) {
99 } else if ((i + 1) == len && (map & Q_RFC2253_QUOTE_LAST)) {
103 } else if (map & Q_RFC2253_QUOTE) {
106 } else if (map & Q_RFC2253_HEX) {
107 int l = snprintf((char *)&to[j], tolen - j - 1,
108 "#%02x", (unsigned char)from[i]);
122 append_string(char **str, size_t *total_len, const char *ss,
123 size_t len, int quote)
128 qs = quote_string(ss, len, Q_RFC2253, &len);
132 s = realloc(*str, len + *total_len + 1);
134 _hx509_abort("allocation failure"); /* XXX */
135 memcpy(s + *total_len, qs, len);
138 s[*total_len + len] = '\0';
145 oidtostring(const heim_oid *type)
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)
160 stringtooid(const char *name, size_t len, heim_oid *oid)
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);
175 memcpy(s, name, len);
177 ret = der_parse_heim_oid(s, ".", oid);
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;
211 for (m = n->u.rdnSequence.len; m > 0; m--) {
215 for (j = 0; j < n->u.rdnSequence.val[i].len; j++) {
216 DirectoryString *ds = &n->u.rdnSequence.val[i].val[j].value;
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;
227 case choice_DirectoryString_printableString:
228 ss = ds->u.printableString.data;
229 len = ds->u.printableString.length;
231 case choice_DirectoryString_utf8String:
232 ss = ds->u.utf8String;
235 case choice_DirectoryString_bmpString: {
236 const uint16_t *bmp = ds->u.bmpString.data;
237 size_t bmplen = ds->u.bmpString.length;
240 ret = wind_ucs2utf8_length(bmp, bmplen, &k);
246 _hx509_abort("allocation failure"); /* XXX */
247 ret = wind_ucs2utf8(bmp, bmplen, ss, NULL);
256 case choice_DirectoryString_teletexString:
257 ss = ds->u.teletexString;
260 case choice_DirectoryString_universalString: {
261 const uint32_t *uni = ds->u.universalString.data;
262 size_t unilen = ds->u.universalString.length;
265 ret = wind_ucs4utf8_length(uni, unilen, &k);
271 _hx509_abort("allocation failure"); /* XXX */
272 ret = wind_ucs4utf8(uni, unilen, ss, NULL);
282 _hx509_abort("unknown directory type: %d", ds->element);
285 append_string(str, &total_len, oidname, strlen(oidname), 0);
287 append_string(str, &total_len, "=", 1, 0);
288 append_string(str, &total_len, ss, len, 1);
289 if (ds->element == choice_DirectoryString_bmpString ||
290 ds->element == choice_DirectoryString_universalString)
294 if (j + 1 < n->u.rdnSequence.val[i].len)
295 append_string(str, &total_len, "+", 1, 0);
299 append_string(str, &total_len, ",", 1, 0);
304 #define COPYCHARARRAY(_ds,_el,_l,_n) \
305 (_l) = strlen(_ds->u._el); \
306 (_n) = malloc((_l) * sizeof((_n)[0])); \
309 for (i = 0; i < (_l); i++) \
310 (_n)[i] = _ds->u._el[i]
313 #define COPYVALARRAY(_ds,_el,_l,_n) \
314 (_l) = _ds->u._el.length; \
315 (_n) = malloc((_l) * sizeof((_n)[0])); \
318 for (i = 0; i < (_l); i++) \
319 (_n)[i] = _ds->u._el.data[i]
321 #define COPYVOIDARRAY(_ds,_el,_l,_n) \
322 (_l) = _ds->u._el.length; \
323 (_n) = malloc((_l) * sizeof((_n)[0])); \
326 for (i = 0; i < (_l); i++) \
327 (_n)[i] = ((unsigned char *)_ds->u._el.data)[i]
332 dsstringprep(const DirectoryString *ds, uint32_t **rname, size_t *rlen)
334 wind_profile_flags flags;
342 switch(ds->element) {
343 case choice_DirectoryString_ia5String:
344 flags = WIND_PROFILE_LDAP;
345 COPYVOIDARRAY(ds, ia5String, len, name);
347 case choice_DirectoryString_printableString:
348 flags = WIND_PROFILE_LDAP;
349 flags |= WIND_PROFILE_LDAP_CASE_EXACT_ATTRIBUTE;
350 COPYVOIDARRAY(ds, printableString, len, name);
352 case choice_DirectoryString_teletexString:
353 flags = WIND_PROFILE_LDAP_CASE;
354 COPYCHARARRAY(ds, teletexString, len, name);
356 case choice_DirectoryString_bmpString:
357 flags = WIND_PROFILE_LDAP;
358 COPYVALARRAY(ds, bmpString, len, name);
360 case choice_DirectoryString_universalString:
361 flags = WIND_PROFILE_LDAP;
362 COPYVALARRAY(ds, universalString, len, name);
364 case choice_DirectoryString_utf8String:
365 flags = WIND_PROFILE_LDAP;
366 ret = wind_utf8ucs4_length(ds->u.utf8String, &len);
369 name = malloc(len * sizeof(name[0]));
372 ret = wind_utf8ucs4(ds->u.utf8String, name, &len);
379 _hx509_abort("unknown directory type: %d", ds->element);
383 /* try a couple of times to get the length right, XXX gross */
384 for (i = 0; i < 4; i++) {
386 *rname = malloc(*rlen * sizeof((*rname)[0]));
388 ret = wind_stringprep(name, len, *rname, rlen, flags);
389 if (ret == WIND_ERR_OVERRUN) {
409 _hx509_name_ds_cmp(const DirectoryString *ds1,
410 const DirectoryString *ds2,
413 uint32_t *ds1lp, *ds2lp;
414 size_t ds1len, ds2len, i;
417 ret = dsstringprep(ds1, &ds1lp, &ds1len);
420 ret = dsstringprep(ds2, &ds2lp, &ds2len);
426 if (ds1len != ds2len)
427 *diff = ds1len - ds2len;
429 for (i = 0; i < ds1len; i++) {
430 *diff = ds1lp[i] - ds2lp[i];
442 _hx509_name_cmp(const Name *n1, const Name *n2, int *c)
447 *c = n1->u.rdnSequence.len - n2->u.rdnSequence.len;
451 for (i = 0 ; i < n1->u.rdnSequence.len; i++) {
452 *c = n1->u.rdnSequence.val[i].len - n2->u.rdnSequence.val[i].len;
456 for (j = 0; j < n1->u.rdnSequence.val[i].len; j++) {
457 *c = der_heim_oid_cmp(&n1->u.rdnSequence.val[i].val[j].type,
458 &n1->u.rdnSequence.val[i].val[j].type);
462 ret = _hx509_name_ds_cmp(&n1->u.rdnSequence.val[i].val[j].value,
463 &n2->u.rdnSequence.val[i].val[j].value,
476 * Compare to hx509 name object, useful for sorting.
478 * @param n1 a hx509 name object.
479 * @param n2 a hx509 name object.
481 * @return 0 the objects are the same, returns > 0 is n2 is "larger"
482 * then n2, < 0 if n1 is "smaller" then n2.
484 * @ingroup hx509_name
488 hx509_name_cmp(hx509_name n1, hx509_name n2)
491 ret = _hx509_name_cmp(&n1->der_name, &n2->der_name, &diff);
499 _hx509_name_from_Name(const Name *n, hx509_name *name)
502 *name = calloc(1, sizeof(**name));
505 ret = copy_Name(n, &(*name)->der_name);
514 _hx509_name_modify(hx509_context context,
520 RelativeDistinguishedName *rdn;
524 ptr = realloc(name->u.rdnSequence.val,
525 sizeof(name->u.rdnSequence.val[0]) *
526 (name->u.rdnSequence.len + 1));
528 hx509_set_error_string(context, 0, ENOMEM, "Out of memory");
531 name->u.rdnSequence.val = ptr;
534 rdn = &name->u.rdnSequence.val[name->u.rdnSequence.len];
536 memmove(&name->u.rdnSequence.val[1],
537 &name->u.rdnSequence.val[0],
538 name->u.rdnSequence.len *
539 sizeof(name->u.rdnSequence.val[0]));
541 rdn = &name->u.rdnSequence.val[0];
543 rdn->val = malloc(sizeof(rdn->val[0]));
544 if (rdn->val == NULL)
547 ret = der_copy_oid(oid, &rdn->val[0].type);
550 rdn->val[0].value.element = choice_DirectoryString_utf8String;
551 rdn->val[0].value.u.utf8String = strdup(str);
552 if (rdn->val[0].value.u.utf8String == NULL)
554 name->u.rdnSequence.len += 1;
560 * Parse a string into a hx509 name object.
562 * @param context A hx509 context.
563 * @param str a string to parse.
564 * @param name the resulting object, NULL in case of error.
566 * @return An hx509 error code, see hx509_get_error_string().
568 * @ingroup hx509_name
572 hx509_parse_name(hx509_context context, const char *str, hx509_name *name)
581 n = calloc(1, sizeof(*n));
583 hx509_set_error_string(context, 0, ENOMEM, "out of memory");
587 n->der_name.element = choice_Name_rdnSequence;
591 while (p != NULL && *p != '\0') {
606 ret = HX509_PARSING_NAME_FAILED;
607 hx509_set_error_string(context, 0, ret, "missing = in %s", p);
611 ret = HX509_PARSING_NAME_FAILED;
612 hx509_set_error_string(context, 0, ret,
613 "missing name before = in %s", p);
617 if ((size_t)(q - p) > len) {
618 ret = HX509_PARSING_NAME_FAILED;
619 hx509_set_error_string(context, 0, ret, " = after , in %s", p);
623 ret = stringtooid(p, q - p, &oid);
625 ret = HX509_PARSING_NAME_FAILED;
626 hx509_set_error_string(context, 0, ret,
627 "unknown type: %.*s", (int)(q - p), p);
632 size_t pstr_len = len - (q - p) - 1;
633 const char *pstr = p + (q - p) + 1;
636 r = malloc(pstr_len + 1);
640 hx509_set_error_string(context, 0, ret, "out of memory");
643 memcpy(r, pstr, pstr_len);
646 ret = _hx509_name_modify(context, &n->der_name, 0, &oid, r);
660 return HX509_NAME_MALFORMED;
664 * Copy a hx509 name object.
666 * @param context A hx509 cotext.
667 * @param from the name to copy from
668 * @param to the name to copy to
670 * @return An hx509 error code, see hx509_get_error_string().
672 * @ingroup hx509_name
676 hx509_name_copy(hx509_context context, const hx509_name from, hx509_name *to)
680 *to = calloc(1, sizeof(**to));
683 ret = copy_Name(&from->der_name, &(*to)->der_name);
693 * Convert a hx509_name into a Name.
695 * @param from the name to copy from
696 * @param to the name to copy to
698 * @return An hx509 error code, see hx509_get_error_string().
700 * @ingroup hx509_name
704 hx509_name_to_Name(const hx509_name from, Name *to)
706 return copy_Name(&from->der_name, to);
710 hx509_name_normalize(hx509_context context, hx509_name name)
716 * Expands variables in the name using env. Variables are on the form
717 * ${name}. Useful when dealing with certificate templates.
719 * @param context A hx509 cotext.
720 * @param name the name to expand.
721 * @param env environment variable to expand.
723 * @return An hx509 error code, see hx509_get_error_string().
725 * @ingroup hx509_name
729 hx509_name_expand(hx509_context context,
733 Name *n = &name->der_name;
739 if (n->element != choice_Name_rdnSequence) {
740 hx509_set_error_string(context, 0, EINVAL, "RDN not of supported type");
744 for (i = 0 ; i < n->u.rdnSequence.len; i++) {
745 for (j = 0; j < n->u.rdnSequence.val[i].len; j++) {
746 /** Only UTF8String rdnSequence names are allowed */
748 THIS SHOULD REALLY BE:
749 COMP = n->u.rdnSequence.val[i].val[j];
750 normalize COMP to utf8
751 check if there are variables
753 convert back to orignal format, store in COMP
754 free normalized utf8 string
756 DirectoryString *ds = &n->u.rdnSequence.val[i].val[j].value;
758 struct rk_strpool *strpool = NULL;
760 if (ds->element != choice_DirectoryString_utf8String) {
761 hx509_set_error_string(context, 0, EINVAL, "unsupported type");
764 p = strstr(ds->u.utf8String, "${");
766 strpool = rk_strpoolprintf(strpool, "%.*s",
767 (int)(p - ds->u.utf8String),
769 if (strpool == NULL) {
770 hx509_set_error_string(context, 0, ENOMEM, "out of memory");
775 /* expand variables */
779 hx509_set_error_string(context, 0, EINVAL, "missing }");
780 rk_strpoolfree(strpool);
784 value = hx509_env_lfind(context, env, p, p2 - p);
786 hx509_set_error_string(context, 0, EINVAL,
787 "variable %.*s missing",
789 rk_strpoolfree(strpool);
792 strpool = rk_strpoolprintf(strpool, "%s", value);
793 if (strpool == NULL) {
794 hx509_set_error_string(context, 0, ENOMEM, "out of memory");
799 p = strstr(p2, "${");
801 strpool = rk_strpoolprintf(strpool, "%.*s",
804 strpool = rk_strpoolprintf(strpool, "%s", p2);
805 if (strpool == NULL) {
806 hx509_set_error_string(context, 0, ENOMEM, "out of memory");
811 free(ds->u.utf8String);
812 ds->u.utf8String = rk_strpoolcollect(strpool);
813 if (ds->u.utf8String == NULL) {
814 hx509_set_error_string(context, 0, ENOMEM, "out of memory");
824 * Free a hx509 name object, upond return *name will be NULL.
826 * @param name a hx509 name object to be freed.
828 * @ingroup hx509_name
832 hx509_name_free(hx509_name *name)
834 free_Name(&(*name)->der_name);
835 memset(*name, 0, sizeof(**name));
841 * Convert a DER encoded name info a string.
843 * @param data data to a DER/BER encoded name
844 * @param length length of data
845 * @param str the resulting string, is NULL on failure.
847 * @return An hx509 error code, see hx509_get_error_string().
849 * @ingroup hx509_name
853 hx509_unparse_der_name(const void *data, size_t length, char **str)
860 ret = decode_Name(data, length, &name, NULL);
863 ret = _hx509_Name_to_string(&name, str);
869 * Convert a hx509_name object to DER encoded name.
871 * @param name name to concert
872 * @param os data to a DER encoded name, free the resulting octet
873 * string with hx509_xfree(os->data).
875 * @return An hx509 error code, see hx509_get_error_string().
877 * @ingroup hx509_name
881 hx509_name_binary(const hx509_name name, heim_octet_string *os)
886 ASN1_MALLOC_ENCODE(Name, os->data, os->length, &name->der_name, &size, ret);
889 if (os->length != size)
890 _hx509_abort("internal ASN.1 encoder error");
896 _hx509_unparse_Name(const Name *aname, char **str)
901 ret = _hx509_name_from_Name(aname, &name);
905 ret = hx509_name_to_string(name, str);
906 hx509_name_free(&name);
911 * Unparse the hx509 name in name into a string.
913 * @param name the name to check if its empty/null.
915 * @return non zero if the name is empty/null.
917 * @ingroup hx509_name
921 hx509_name_is_null_p(const hx509_name name)
923 return name->der_name.u.rdnSequence.len == 0;
927 * Unparse the hx509 name in name into a string.
929 * @param name the name to print
930 * @param str an allocated string returns the name in string form
932 * @return An hx509 error code, see hx509_get_error_string().
934 * @ingroup hx509_name
938 hx509_general_name_unparse(GeneralName *name, char **str)
940 struct rk_strpool *strpool = NULL;
944 switch (name->element) {
945 case choice_GeneralName_otherName: {
947 hx509_oid_sprint(&name->u.otherName.type_id, &oid);
950 strpool = rk_strpoolprintf(strpool, "otherName: %s", oid);
954 case choice_GeneralName_rfc822Name:
955 strpool = rk_strpoolprintf(strpool, "rfc822Name: %.*s\n",
956 (int)name->u.rfc822Name.length,
957 (char *)name->u.rfc822Name.data);
959 case choice_GeneralName_dNSName:
960 strpool = rk_strpoolprintf(strpool, "dNSName: %.*s\n",
961 (int)name->u.dNSName.length,
962 (char *)name->u.dNSName.data);
964 case choice_GeneralName_directoryName: {
968 memset(&dir, 0, sizeof(dir));
969 dir.element = name->u.directoryName.element;
970 dir.u.rdnSequence = name->u.directoryName.u.rdnSequence;
971 ret = _hx509_unparse_Name(&dir, &s);
974 strpool = rk_strpoolprintf(strpool, "directoryName: %s", s);
978 case choice_GeneralName_uniformResourceIdentifier:
979 strpool = rk_strpoolprintf(strpool, "URI: %.*s",
980 (int)name->u.uniformResourceIdentifier.length,
981 (char *)name->u.uniformResourceIdentifier.data);
983 case choice_GeneralName_iPAddress: {
984 unsigned char *a = name->u.iPAddress.data;
986 strpool = rk_strpoolprintf(strpool, "IPAddress: ");
989 if (name->u.iPAddress.length == 4)
990 strpool = rk_strpoolprintf(strpool, "%d.%d.%d.%d",
991 a[0], a[1], a[2], a[3]);
992 else if (name->u.iPAddress.length == 16)
993 strpool = rk_strpoolprintf(strpool,
994 "%02X:%02X:%02X:%02X:"
995 "%02X:%02X:%02X:%02X:"
996 "%02X:%02X:%02X:%02X:"
997 "%02X:%02X:%02X:%02X",
998 a[0], a[1], a[2], a[3],
999 a[4], a[5], a[6], a[7],
1000 a[8], a[9], a[10], a[11],
1001 a[12], a[13], a[14], a[15]);
1003 strpool = rk_strpoolprintf(strpool,
1004 "unknown IP address of length %lu",
1005 (unsigned long)name->u.iPAddress.length);
1008 case choice_GeneralName_registeredID: {
1010 hx509_oid_sprint(&name->u.registeredID, &oid);
1013 strpool = rk_strpoolprintf(strpool, "registeredID: %s", oid);
1020 if (strpool == NULL)
1023 *str = rk_strpoolcollect(strpool);