LCOV - code coverage report
Current view: top level - source4/heimdal/lib/hx509 - name.c (source / functions) Hit Total Coverage
Test: coverage report for abartlet/fix-coverage dd10fb34 Lines: 144 490 29.4 %
Date: 2021-09-23 10:06:22 Functions: 12 23 52.2 %

          Line data    Source code
       1             : /*
       2             :  * Copyright (c) 2004 - 2009 Kungliga Tekniska Högskolan
       3             :  * (Royal Institute of Technology, Stockholm, Sweden).
       4             :  * All rights reserved.
       5             :  *
       6             :  * Redistribution and use in source and binary forms, with or without
       7             :  * modification, are permitted provided that the following conditions
       8             :  * are met:
       9             :  *
      10             :  * 1. Redistributions of source code must retain the above copyright
      11             :  *    notice, this list of conditions and the following disclaimer.
      12             :  *
      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.
      16             :  *
      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.
      20             :  *
      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.
      32             :  */
      33             : 
      34             : #include "hx_locl.h"
      35             : #include <wind.h>
      36             : #include "char_map.h"
      37             : 
      38             : /**
      39             :  * @page page_name PKIX/X.509 Names
      40             :  *
      41             :  * There are several names in PKIX/X.509, GeneralName and Name.
      42             :  *
      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.
      50             :  *
      51             :  * GeneralName is a broader nametype that can contains al kind of
      52             :  * stuff like Name, IP addresses, partial Name, etc.
      53             :  *
      54             :  * Name is mapped into a hx509_name object.
      55             :  *
      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().
      58             :  *
      59             :  * Name string are defined rfc2253, rfc1779 and X.501.
      60             :  *
      61             :  * See the library functions here: @ref hx509_name
      62             :  */
      63             : 
      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 }
      80             : };
      81             : 
      82             : static char *
      83         600 : quote_string(const char *f, size_t len, int flags, size_t *rlen)
      84             : {
      85             :     size_t i, j, tolen;
      86         600 :     const unsigned char *from = (const unsigned char *)f;
      87             :     unsigned char *to;
      88             : 
      89         600 :     tolen = len * 3 + 1;
      90         600 :     to = malloc(tolen);
      91         600 :     if (to == NULL)
      92           0 :         return NULL;
      93             : 
      94        9967 :     for (i = 0, j = 0; i < len; i++) {
      95        9367 :         unsigned char map = char_map[from[i]] & flags;
      96        9367 :         if (i == 0 && (map & Q_RFC2253_QUOTE_FIRST)) {
      97           0 :             to[j++] = '\\';
      98           0 :             to[j++] = from[i];
      99        9367 :         } else if ((i + 1) == len && (map & Q_RFC2253_QUOTE_LAST)) {
     100             : 
     101           0 :             to[j++] = '\\';
     102           0 :             to[j++] = from[i];
     103        9367 :         } else if (map & Q_RFC2253_QUOTE) {
     104           0 :             to[j++] = '\\';
     105           0 :             to[j++] = from[i];
     106        9367 :         } else if (map & Q_RFC2253_HEX) {
     107           0 :             int l = snprintf((char *)&to[j], tolen - j - 1,
     108           0 :                              "#%02x", (unsigned char)from[i]);
     109           0 :             j += l;
     110             :         } else {
     111        9367 :             to[j++] = from[i];
     112             :         }
     113             :     }
     114         600 :     to[j] = '\0';
     115         600 :     assert(j < tolen);
     116         600 :     *rlen = j;
     117         600 :     return (char *)to;
     118             : }
     119             : 
     120             : 
     121             : static int
     122        2300 : append_string(char **str, size_t *total_len, const char *ss,
     123             :               size_t len, int quote)
     124             : {
     125             :     char *s, *qs;
     126             : 
     127        2300 :     if (quote)
     128         600 :         qs = quote_string(ss, len, Q_RFC2253, &len);
     129             :     else
     130        1700 :         qs = rk_UNCONST(ss);
     131             : 
     132        2300 :     s = realloc(*str, len + *total_len + 1);
     133        2300 :     if (s == NULL)
     134           0 :         _hx509_abort("allocation failure"); /* XXX */
     135        2300 :     memcpy(s + *total_len, qs, len);
     136        2300 :     if (qs != ss)
     137         600 :         free(qs);
     138        2300 :     s[*total_len + len] = '\0';
     139        2300 :     *str = s;
     140        2300 :     *total_len += len;
     141        2300 :     return 0;
     142             : }
     143             : 
     144             : static char *
     145         600 : oidtostring(const heim_oid *type)
     146             : {
     147             :     char *s;
     148             :     size_t i;
     149             : 
     150        3100 :     for (i = 0; i < sizeof(no)/sizeof(no[0]); i++) {
     151        3100 :         if (der_heim_oid_cmp(no[i].o, type) == 0)
     152         600 :             return strdup(no[i].n);
     153             :     }
     154           0 :     if (der_print_heim_oid(type, '.', &s) != 0)
     155           0 :         return NULL;
     156           0 :     return s;
     157             : }
     158             : 
     159             : static int
     160           0 : stringtooid(const char *name, size_t len, heim_oid *oid)
     161             : {
     162             :     int ret;
     163             :     size_t i;
     164             :     char *s;
     165             : 
     166           0 :     memset(oid, 0, sizeof(*oid));
     167             : 
     168           0 :     for (i = 0; i < sizeof(no)/sizeof(no[0]); i++) {
     169           0 :         if (strncasecmp(no[i].n, name, len) == 0)
     170           0 :             return der_copy_oid(no[i].o, oid);
     171             :     }
     172           0 :     s = malloc(len + 1);
     173           0 :     if (s == NULL)
     174           0 :         return ENOMEM;
     175           0 :     memcpy(s, name, len);
     176           0 :     s[len] = '\0';
     177           0 :     ret = der_parse_heim_oid(s, ".", oid);
     178           0 :     free(s);
     179           0 :     return ret;
     180             : }
     181             : 
     182             : /**
     183             :  * Convert the hx509 name object into a printable string.
     184             :  * The resulting string should be freed with free().
     185             :  *
     186             :  * @param name name to print
     187             :  * @param str the string to return
     188             :  *
     189             :  * @return An hx509 error code, see hx509_get_error_string().
     190             :  *
     191             :  * @ingroup hx509_name
     192             :  */
     193             : 
     194             : int
     195         100 : hx509_name_to_string(const hx509_name name, char **str)
     196             : {
     197         100 :     return _hx509_Name_to_string(&name->der_name, str);
     198             : }
     199             : 
     200             : int
     201         100 : _hx509_Name_to_string(const Name *n, char **str)
     202             : {
     203         100 :     size_t total_len = 0;
     204             :     size_t i, j, m;
     205             :     int ret;
     206             : 
     207         100 :     *str = strdup("");
     208         100 :     if (*str == NULL)
     209           0 :         return ENOMEM;
     210             : 
     211         700 :     for (m = n->u.rdnSequence.len; m > 0; m--) {
     212             :         size_t len;
     213         600 :         i = m - 1;
     214             : 
     215        1200 :         for (j = 0; j < n->u.rdnSequence.val[i].len; j++) {
     216         600 :             DirectoryString *ds = &n->u.rdnSequence.val[i].val[j].value;
     217             :             char *oidname;
     218             :             char *ss;
     219             : 
     220         600 :             oidname = oidtostring(&n->u.rdnSequence.val[i].val[j].type);
     221             : 
     222         600 :             switch(ds->element) {
     223         100 :             case choice_DirectoryString_ia5String:
     224         100 :                 ss = ds->u.ia5String.data;
     225         100 :                 len = ds->u.ia5String.length;
     226         100 :                 break;
     227         100 :             case choice_DirectoryString_printableString:
     228         100 :                 ss = ds->u.printableString.data;
     229         100 :                 len = ds->u.printableString.length;
     230         100 :                 break;
     231         400 :             case choice_DirectoryString_utf8String:
     232         400 :                 ss = ds->u.utf8String;
     233         400 :                 len = strlen(ss);
     234         400 :                 break;
     235           0 :             case choice_DirectoryString_bmpString: {
     236           0 :                 const uint16_t *bmp = ds->u.bmpString.data;
     237           0 :                 size_t bmplen = ds->u.bmpString.length;
     238             :                 size_t k;
     239             : 
     240           0 :                 ret = wind_ucs2utf8_length(bmp, bmplen, &k);
     241           0 :                 if (ret)
     242           0 :                     return ret;
     243             : 
     244           0 :                 ss = malloc(k + 1);
     245           0 :                 if (ss == NULL)
     246           0 :                     _hx509_abort("allocation failure"); /* XXX */
     247           0 :                 ret = wind_ucs2utf8(bmp, bmplen, ss, NULL);
     248           0 :                 if (ret) {
     249           0 :                     free(ss);
     250           0 :                     return ret;
     251             :                 }
     252           0 :                 ss[k] = '\0';
     253           0 :                 len = k;
     254           0 :                 break;
     255             :             }
     256           0 :             case choice_DirectoryString_teletexString:
     257           0 :                 ss = ds->u.teletexString;
     258           0 :                 len = strlen(ss);
     259           0 :                 break;
     260           0 :             case choice_DirectoryString_universalString: {
     261           0 :                 const uint32_t *uni = ds->u.universalString.data;
     262           0 :                 size_t unilen = ds->u.universalString.length;
     263             :                 size_t k;
     264             : 
     265           0 :                 ret = wind_ucs4utf8_length(uni, unilen, &k);
     266           0 :                 if (ret)
     267           0 :                     return ret;
     268             : 
     269           0 :                 ss = malloc(k + 1);
     270           0 :                 if (ss == NULL)
     271           0 :                     _hx509_abort("allocation failure"); /* XXX */
     272           0 :                 ret = wind_ucs4utf8(uni, unilen, ss, NULL);
     273           0 :                 if (ret) {
     274           0 :                     free(ss);
     275           0 :                     return ret;
     276             :                 }
     277           0 :                 ss[k] = '\0';
     278           0 :                 len = k;
     279           0 :                 break;
     280             :             }
     281           0 :             default:
     282           0 :                 _hx509_abort("unknown directory type: %d", ds->element);
     283             :                 exit(1);
     284             :             }
     285         600 :             append_string(str, &total_len, oidname, strlen(oidname), 0);
     286         600 :             free(oidname);
     287         600 :             append_string(str, &total_len, "=", 1, 0);
     288         600 :             append_string(str, &total_len, ss, len, 1);
     289        1200 :             if (ds->element == choice_DirectoryString_bmpString ||
     290         600 :                 ds->element == choice_DirectoryString_universalString)
     291             :             {
     292           0 :                 free(ss);
     293             :             }
     294         600 :             if (j + 1 < n->u.rdnSequence.val[i].len)
     295           0 :                 append_string(str, &total_len, "+", 1, 0);
     296             :         }
     297             : 
     298         600 :         if (i > 0)
     299         500 :             append_string(str, &total_len, ",", 1, 0);
     300             :     }
     301         100 :     return 0;
     302             : }
     303             : 
     304             : #define COPYCHARARRAY(_ds,_el,_l,_n)            \
     305             :         (_l) = strlen(_ds->u._el);           \
     306             :         (_n) = malloc((_l) * sizeof((_n)[0]));  \
     307             :         if ((_n) == NULL)                       \
     308             :             return ENOMEM;                      \
     309             :         for (i = 0; i < (_l); i++)           \
     310             :             (_n)[i] = _ds->u._el[i]
     311             : 
     312             : 
     313             : #define COPYVALARRAY(_ds,_el,_l,_n)             \
     314             :         (_l) = _ds->u._el.length;            \
     315             :         (_n) = malloc((_l) * sizeof((_n)[0]));  \
     316             :         if ((_n) == NULL)                       \
     317             :             return ENOMEM;                      \
     318             :         for (i = 0; i < (_l); i++)           \
     319             :             (_n)[i] = _ds->u._el.data[i]
     320             : 
     321             : #define COPYVOIDARRAY(_ds,_el,_l,_n)            \
     322             :         (_l) = _ds->u._el.length;            \
     323             :         (_n) = malloc((_l) * sizeof((_n)[0]));  \
     324             :         if ((_n) == NULL)                       \
     325             :             return ENOMEM;                      \
     326             :         for (i = 0; i < (_l); i++)           \
     327             :             (_n)[i] = ((unsigned char *)_ds->u._el.data)[i]
     328             : 
     329             : 
     330             : 
     331             : static int
     332        2520 : dsstringprep(const DirectoryString *ds, uint32_t **rname, size_t *rlen)
     333             : {
     334             :     wind_profile_flags flags;
     335             :     size_t i, len;
     336             :     int ret;
     337             :     uint32_t *name;
     338             : 
     339        2520 :     *rname = NULL;
     340        2520 :     *rlen = 0;
     341             : 
     342        2520 :     switch(ds->element) {
     343         360 :     case choice_DirectoryString_ia5String:
     344         360 :         flags = WIND_PROFILE_LDAP;
     345         360 :         COPYVOIDARRAY(ds, ia5String, len, name);
     346         360 :         break;
     347         360 :     case choice_DirectoryString_printableString:
     348         360 :         flags = WIND_PROFILE_LDAP;
     349         360 :         flags |= WIND_PROFILE_LDAP_CASE_EXACT_ATTRIBUTE;
     350         360 :         COPYVOIDARRAY(ds, printableString, len, name);
     351         360 :         break;
     352           0 :     case choice_DirectoryString_teletexString:
     353           0 :         flags = WIND_PROFILE_LDAP_CASE;
     354           0 :         COPYCHARARRAY(ds, teletexString, len, name);
     355           0 :         break;
     356           0 :     case choice_DirectoryString_bmpString:
     357           0 :         flags = WIND_PROFILE_LDAP;
     358           0 :         COPYVALARRAY(ds, bmpString, len, name);
     359           0 :         break;
     360           0 :     case choice_DirectoryString_universalString:
     361           0 :         flags = WIND_PROFILE_LDAP;
     362           0 :         COPYVALARRAY(ds, universalString, len, name);
     363           0 :         break;
     364        1800 :     case choice_DirectoryString_utf8String:
     365        1800 :         flags = WIND_PROFILE_LDAP;
     366        1800 :         ret = wind_utf8ucs4_length(ds->u.utf8String, &len);
     367        1800 :         if (ret)
     368           0 :             return ret;
     369        1800 :         name = malloc(len * sizeof(name[0]));
     370        1800 :         if (name == NULL)
     371           0 :             return ENOMEM;
     372        1800 :         ret = wind_utf8ucs4(ds->u.utf8String, name, &len);
     373        1800 :         if (ret) {
     374           0 :             free(name);
     375           0 :             return ret;
     376             :         }
     377        1800 :         break;
     378           0 :     default:
     379           0 :         _hx509_abort("unknown directory type: %d", ds->element);
     380             :     }
     381             : 
     382        2520 :     *rlen = len;
     383             :     /* try a couple of times to get the length right, XXX gross */
     384        5040 :     for (i = 0; i < 4; i++) {
     385        2520 :         *rlen = *rlen * 2;
     386        2520 :         *rname = malloc(*rlen * sizeof((*rname)[0]));
     387             : 
     388        2520 :         ret = wind_stringprep(name, len, *rname, rlen, flags);
     389        2520 :         if (ret == WIND_ERR_OVERRUN) {
     390           0 :             free(*rname);
     391           0 :             *rname = NULL;
     392           0 :             continue;
     393             :         } else
     394        2520 :             break;
     395             :     }
     396        2520 :     free(name);
     397        2520 :     if (ret) {
     398           0 :         if (*rname)
     399           0 :             free(*rname);
     400           0 :         *rname = NULL;
     401           0 :         *rlen = 0;
     402           0 :         return ret;
     403             :     }
     404             : 
     405        2520 :     return 0;
     406             : }
     407             : 
     408             : int
     409        1260 : _hx509_name_ds_cmp(const DirectoryString *ds1,
     410             :                    const DirectoryString *ds2,
     411             :                    int *diff)
     412             : {
     413             :     uint32_t *ds1lp, *ds2lp;
     414             :     size_t ds1len, ds2len, i;
     415             :     int ret;
     416             : 
     417        1260 :     ret = dsstringprep(ds1, &ds1lp, &ds1len);
     418        1260 :     if (ret)
     419           0 :         return ret;
     420        1260 :     ret = dsstringprep(ds2, &ds2lp, &ds2len);
     421        1260 :     if (ret) {
     422           0 :         free(ds1lp);
     423           0 :         return ret;
     424             :     }
     425             : 
     426        1260 :     if (ds1len != ds2len)
     427           0 :         *diff = ds1len - ds2len;
     428             :     else {
     429       22320 :         for (i = 0; i < ds1len; i++) {
     430       21060 :             *diff = ds1lp[i] - ds2lp[i];
     431       21060 :             if (*diff)
     432           0 :                 break;
     433             :         }
     434             :     }
     435        1260 :     free(ds1lp);
     436        1260 :     free(ds2lp);
     437             : 
     438        1260 :     return 0;
     439             : }
     440             : 
     441             : int
     442         340 : _hx509_name_cmp(const Name *n1, const Name *n2, int *c)
     443             : {
     444             :     int ret;
     445             :     size_t i, j;
     446             : 
     447         340 :     *c = n1->u.rdnSequence.len - n2->u.rdnSequence.len;
     448         340 :     if (*c)
     449         160 :         return 0;
     450             : 
     451        1440 :     for (i = 0 ; i < n1->u.rdnSequence.len; i++) {
     452        1260 :         *c = n1->u.rdnSequence.val[i].len - n2->u.rdnSequence.val[i].len;
     453        1260 :         if (*c)
     454           0 :             return 0;
     455             : 
     456        2520 :         for (j = 0; j < n1->u.rdnSequence.val[i].len; j++) {
     457        1260 :             *c = der_heim_oid_cmp(&n1->u.rdnSequence.val[i].val[j].type,
     458        1260 :                                   &n1->u.rdnSequence.val[i].val[j].type);
     459        1260 :             if (*c)
     460           0 :                 return 0;
     461             : 
     462        1260 :             ret = _hx509_name_ds_cmp(&n1->u.rdnSequence.val[i].val[j].value,
     463        1260 :                                      &n2->u.rdnSequence.val[i].val[j].value,
     464             :                                      c);
     465        1260 :             if (ret)
     466           0 :                 return ret;
     467        1260 :             if (*c)
     468           0 :                 return 0;
     469             :         }
     470             :     }
     471         180 :     *c = 0;
     472         180 :     return 0;
     473             : }
     474             : 
     475             : /**
     476             :  * Compare to hx509 name object, useful for sorting.
     477             :  *
     478             :  * @param n1 a hx509 name object.
     479             :  * @param n2 a hx509 name object.
     480             :  *
     481             :  * @return 0 the objects are the same, returns > 0 is n2 is "larger"
     482             :  * then n2, < 0 if n1 is "smaller" then n2.
     483             :  *
     484             :  * @ingroup hx509_name
     485             :  */
     486             : 
     487             : int
     488           0 : hx509_name_cmp(hx509_name n1, hx509_name n2)
     489             : {
     490             :     int ret, diff;
     491           0 :     ret = _hx509_name_cmp(&n1->der_name, &n2->der_name, &diff);
     492           0 :     if (ret)
     493           0 :         return ret;
     494           0 :     return diff;
     495             : }
     496             : 
     497             : 
     498             : int
     499         180 : _hx509_name_from_Name(const Name *n, hx509_name *name)
     500             : {
     501             :     int ret;
     502         180 :     *name = calloc(1, sizeof(**name));
     503         180 :     if (*name == NULL)
     504           0 :         return ENOMEM;
     505         180 :     ret = copy_Name(n, &(*name)->der_name);
     506         180 :     if (ret) {
     507           0 :         free(*name);
     508           0 :         *name = NULL;
     509             :     }
     510         180 :     return ret;
     511             : }
     512             : 
     513             : int
     514           0 : _hx509_name_modify(hx509_context context,
     515             :                    Name *name,
     516             :                    int append,
     517             :                    const heim_oid *oid,
     518             :                    const char *str)
     519             : {
     520             :     RelativeDistinguishedName *rdn;
     521             :     int ret;
     522             :     void *ptr;
     523             : 
     524           0 :     ptr = realloc(name->u.rdnSequence.val,
     525             :                   sizeof(name->u.rdnSequence.val[0]) *
     526           0 :                   (name->u.rdnSequence.len + 1));
     527           0 :     if (ptr == NULL) {
     528           0 :         hx509_set_error_string(context, 0, ENOMEM, "Out of memory");
     529           0 :         return ENOMEM;
     530             :     }
     531           0 :     name->u.rdnSequence.val = ptr;
     532             : 
     533           0 :     if (append) {
     534           0 :         rdn = &name->u.rdnSequence.val[name->u.rdnSequence.len];
     535             :     } else {
     536           0 :         memmove(&name->u.rdnSequence.val[1],
     537           0 :                 &name->u.rdnSequence.val[0],
     538           0 :                 name->u.rdnSequence.len *
     539             :                 sizeof(name->u.rdnSequence.val[0]));
     540             : 
     541           0 :         rdn = &name->u.rdnSequence.val[0];
     542             :     }
     543           0 :     rdn->val = malloc(sizeof(rdn->val[0]));
     544           0 :     if (rdn->val == NULL)
     545           0 :         return ENOMEM;
     546           0 :     rdn->len = 1;
     547           0 :     ret = der_copy_oid(oid, &rdn->val[0].type);
     548           0 :     if (ret)
     549           0 :         return ret;
     550           0 :     rdn->val[0].value.element = choice_DirectoryString_utf8String;
     551           0 :     rdn->val[0].value.u.utf8String = strdup(str);
     552           0 :     if (rdn->val[0].value.u.utf8String == NULL)
     553           0 :         return ENOMEM;
     554           0 :     name->u.rdnSequence.len += 1;
     555             : 
     556           0 :     return 0;
     557             : }
     558             : 
     559             : /**
     560             :  * Parse a string into a hx509 name object.
     561             :  *
     562             :  * @param context A hx509 context.
     563             :  * @param str a string to parse.
     564             :  * @param name the resulting object, NULL in case of error.
     565             :  *
     566             :  * @return An hx509 error code, see hx509_get_error_string().
     567             :  *
     568             :  * @ingroup hx509_name
     569             :  */
     570             : 
     571             : int
     572           0 : hx509_parse_name(hx509_context context, const char *str, hx509_name *name)
     573             : {
     574             :     const char *p, *q;
     575             :     size_t len;
     576             :     hx509_name n;
     577             :     int ret;
     578             : 
     579           0 :     *name = NULL;
     580             : 
     581           0 :     n = calloc(1, sizeof(*n));
     582           0 :     if (n == NULL) {
     583           0 :         hx509_set_error_string(context, 0, ENOMEM, "out of memory");
     584           0 :         return ENOMEM;
     585             :     }
     586             : 
     587           0 :     n->der_name.element = choice_Name_rdnSequence;
     588             : 
     589           0 :     p = str;
     590             : 
     591           0 :     while (p != NULL && *p != '\0') {
     592             :         heim_oid oid;
     593             :         int last;
     594             : 
     595           0 :         q = strchr(p, ',');
     596           0 :         if (q) {
     597           0 :             len = (q - p);
     598           0 :             last = 1;
     599             :         } else {
     600           0 :             len = strlen(p);
     601           0 :             last = 0;
     602             :         }
     603             : 
     604           0 :         q = strchr(p, '=');
     605           0 :         if (q == NULL) {
     606           0 :             ret = HX509_PARSING_NAME_FAILED;
     607           0 :             hx509_set_error_string(context, 0, ret, "missing = in %s", p);
     608           0 :             goto out;
     609             :         }
     610           0 :         if (q == p) {
     611           0 :             ret = HX509_PARSING_NAME_FAILED;
     612           0 :             hx509_set_error_string(context, 0, ret,
     613             :                                    "missing name before = in %s", p);
     614           0 :             goto out;
     615             :         }
     616             : 
     617           0 :         if ((size_t)(q - p) > len) {
     618           0 :             ret = HX509_PARSING_NAME_FAILED;
     619           0 :             hx509_set_error_string(context, 0, ret, " = after , in %s", p);
     620           0 :             goto out;
     621             :         }
     622             : 
     623           0 :         ret = stringtooid(p, q - p, &oid);
     624           0 :         if (ret) {
     625           0 :             ret = HX509_PARSING_NAME_FAILED;
     626           0 :             hx509_set_error_string(context, 0, ret,
     627           0 :                                    "unknown type: %.*s", (int)(q - p), p);
     628           0 :             goto out;
     629             :         }
     630             : 
     631             :         {
     632           0 :             size_t pstr_len = len - (q - p) - 1;
     633           0 :             const char *pstr = p + (q - p) + 1;
     634             :             char *r;
     635             : 
     636           0 :             r = malloc(pstr_len + 1);
     637           0 :             if (r == NULL) {
     638           0 :                 der_free_oid(&oid);
     639           0 :                 ret = ENOMEM;
     640           0 :                 hx509_set_error_string(context, 0, ret, "out of memory");
     641           0 :                 goto out;
     642             :             }
     643           0 :             memcpy(r, pstr, pstr_len);
     644           0 :             r[pstr_len] = '\0';
     645             : 
     646           0 :             ret = _hx509_name_modify(context, &n->der_name, 0, &oid, r);
     647           0 :             free(r);
     648           0 :             der_free_oid(&oid);
     649           0 :             if(ret)
     650           0 :                 goto out;
     651             :         }
     652           0 :         p += len + last;
     653             :     }
     654             : 
     655           0 :     *name = n;
     656             : 
     657           0 :     return 0;
     658           0 : out:
     659           0 :     hx509_name_free(&n);
     660           0 :     return HX509_NAME_MALFORMED;
     661             : }
     662             : 
     663             : /**
     664             :  * Copy a hx509 name object.
     665             :  *
     666             :  * @param context A hx509 cotext.
     667             :  * @param from the name to copy from
     668             :  * @param to the name to copy to
     669             :  *
     670             :  * @return An hx509 error code, see hx509_get_error_string().
     671             :  *
     672             :  * @ingroup hx509_name
     673             :  */
     674             : 
     675             : int
     676           0 : hx509_name_copy(hx509_context context, const hx509_name from, hx509_name *to)
     677             : {
     678             :     int ret;
     679             : 
     680           0 :     *to = calloc(1, sizeof(**to));
     681           0 :     if (*to == NULL)
     682           0 :         return ENOMEM;
     683           0 :     ret = copy_Name(&from->der_name, &(*to)->der_name);
     684           0 :     if (ret) {
     685           0 :         free(*to);
     686           0 :         *to = NULL;
     687           0 :         return ENOMEM;
     688             :     }
     689           0 :     return 0;
     690             : }
     691             : 
     692             : /**
     693             :  * Convert a hx509_name into a Name.
     694             :  *
     695             :  * @param from the name to copy from
     696             :  * @param to the name to copy to
     697             :  *
     698             :  * @return An hx509 error code, see hx509_get_error_string().
     699             :  *
     700             :  * @ingroup hx509_name
     701             :  */
     702             : 
     703             : int
     704          40 : hx509_name_to_Name(const hx509_name from, Name *to)
     705             : {
     706          40 :     return copy_Name(&from->der_name, to);
     707             : }
     708             : 
     709             : int
     710           0 : hx509_name_normalize(hx509_context context, hx509_name name)
     711             : {
     712           0 :     return 0;
     713             : }
     714             : 
     715             : /**
     716             :  * Expands variables in the name using env. Variables are on the form
     717             :  * ${name}. Useful when dealing with certificate templates.
     718             :  *
     719             :  * @param context A hx509 cotext.
     720             :  * @param name the name to expand.
     721             :  * @param env environment variable to expand.
     722             :  *
     723             :  * @return An hx509 error code, see hx509_get_error_string().
     724             :  *
     725             :  * @ingroup hx509_name
     726             :  */
     727             : 
     728             : int
     729           0 : hx509_name_expand(hx509_context context,
     730             :                   hx509_name name,
     731             :                   hx509_env env)
     732             : {
     733           0 :     Name *n = &name->der_name;
     734             :     size_t i, j;
     735             : 
     736           0 :     if (env == NULL)
     737           0 :         return 0;
     738             : 
     739           0 :     if (n->element != choice_Name_rdnSequence) {
     740           0 :         hx509_set_error_string(context, 0, EINVAL, "RDN not of supported type");
     741           0 :         return EINVAL;
     742             :     }
     743             : 
     744           0 :     for (i = 0 ; i < n->u.rdnSequence.len; i++) {
     745           0 :         for (j = 0; j < n->u.rdnSequence.val[i].len; j++) {
     746             :             /** Only UTF8String rdnSequence names are allowed */
     747             :             /*
     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
     752             :                 expand variables
     753             :                 convert back to orignal format, store in COMP
     754             :               free normalized utf8 string
     755             :             */
     756           0 :             DirectoryString *ds = &n->u.rdnSequence.val[i].val[j].value;
     757             :             char *p, *p2;
     758           0 :             struct rk_strpool *strpool = NULL;
     759             : 
     760           0 :             if (ds->element != choice_DirectoryString_utf8String) {
     761           0 :                 hx509_set_error_string(context, 0, EINVAL, "unsupported type");
     762           0 :                 return EINVAL;
     763             :             }
     764           0 :             p = strstr(ds->u.utf8String, "${");
     765           0 :             if (p) {
     766           0 :                 strpool = rk_strpoolprintf(strpool, "%.*s",
     767           0 :                                            (int)(p - ds->u.utf8String),
     768             :                                            ds->u.utf8String);
     769           0 :                 if (strpool == NULL) {
     770           0 :                     hx509_set_error_string(context, 0, ENOMEM, "out of memory");
     771           0 :                     return ENOMEM;
     772             :                 }
     773             :             }
     774           0 :             while (p != NULL) {
     775             :                 /* expand variables */
     776             :                 const char *value;
     777           0 :                 p2 = strchr(p, '}');
     778           0 :                 if (p2 == NULL) {
     779           0 :                     hx509_set_error_string(context, 0, EINVAL, "missing }");
     780           0 :                     rk_strpoolfree(strpool);
     781           0 :                     return EINVAL;
     782             :                 }
     783           0 :                 p += 2;
     784           0 :                 value = hx509_env_lfind(context, env, p, p2 - p);
     785           0 :                 if (value == NULL) {
     786           0 :                     hx509_set_error_string(context, 0, EINVAL,
     787             :                                            "variable %.*s missing",
     788           0 :                                            (int)(p2 - p), p);
     789           0 :                     rk_strpoolfree(strpool);
     790           0 :                     return EINVAL;
     791             :                 }
     792           0 :                 strpool = rk_strpoolprintf(strpool, "%s", value);
     793           0 :                 if (strpool == NULL) {
     794           0 :                     hx509_set_error_string(context, 0, ENOMEM, "out of memory");
     795           0 :                     return ENOMEM;
     796             :                 }
     797           0 :                 p2++;
     798             : 
     799           0 :                 p = strstr(p2, "${");
     800           0 :                 if (p)
     801           0 :                     strpool = rk_strpoolprintf(strpool, "%.*s",
     802           0 :                                                (int)(p - p2), p2);
     803             :                 else
     804           0 :                     strpool = rk_strpoolprintf(strpool, "%s", p2);
     805           0 :                 if (strpool == NULL) {
     806           0 :                     hx509_set_error_string(context, 0, ENOMEM, "out of memory");
     807           0 :                     return ENOMEM;
     808             :                 }
     809             :             }
     810           0 :             if (strpool) {
     811           0 :                 free(ds->u.utf8String);
     812           0 :                 ds->u.utf8String = rk_strpoolcollect(strpool);
     813           0 :                 if (ds->u.utf8String == NULL) {
     814           0 :                     hx509_set_error_string(context, 0, ENOMEM, "out of memory");
     815           0 :                     return ENOMEM;
     816             :                 }
     817             :             }
     818             :         }
     819             :     }
     820           0 :     return 0;
     821             : }
     822             : 
     823             : /**
     824             :  * Free a hx509 name object, upond return *name will be NULL.
     825             :  *
     826             :  * @param name a hx509 name object to be freed.
     827             :  *
     828             :  * @ingroup hx509_name
     829             :  */
     830             : 
     831             : void
     832         180 : hx509_name_free(hx509_name *name)
     833             : {
     834         180 :     free_Name(&(*name)->der_name);
     835         180 :     memset(*name, 0, sizeof(**name));
     836         180 :     free(*name);
     837         180 :     *name = NULL;
     838         180 : }
     839             : 
     840             : /**
     841             :  * Convert a DER encoded name info a string.
     842             :  *
     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.
     846             :  *
     847             :  * @return An hx509 error code, see hx509_get_error_string().
     848             :  *
     849             :  * @ingroup hx509_name
     850             :  */
     851             : 
     852             : int
     853           0 : hx509_unparse_der_name(const void *data, size_t length, char **str)
     854             : {
     855             :     Name name;
     856             :     int ret;
     857             : 
     858           0 :     *str = NULL;
     859             : 
     860           0 :     ret = decode_Name(data, length, &name, NULL);
     861           0 :     if (ret)
     862           0 :         return ret;
     863           0 :     ret = _hx509_Name_to_string(&name, str);
     864           0 :     free_Name(&name);
     865           0 :     return ret;
     866             : }
     867             : 
     868             : /**
     869             :  * Convert a hx509_name object to DER encoded name.
     870             :  *
     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).
     874             :  *
     875             :  * @return An hx509 error code, see hx509_get_error_string().
     876             :  *
     877             :  * @ingroup hx509_name
     878             :  */
     879             : 
     880             : int
     881           0 : hx509_name_binary(const hx509_name name, heim_octet_string *os)
     882             : {
     883             :     size_t size;
     884             :     int ret;
     885             : 
     886           0 :     ASN1_MALLOC_ENCODE(Name, os->data, os->length, &name->der_name, &size, ret);
     887           0 :     if (ret)
     888           0 :         return ret;
     889           0 :     if (os->length != size)
     890           0 :         _hx509_abort("internal ASN.1 encoder error");
     891             : 
     892           0 :     return 0;
     893             : }
     894             : 
     895             : int
     896           0 : _hx509_unparse_Name(const Name *aname, char **str)
     897             : {
     898             :     hx509_name name;
     899             :     int ret;
     900             : 
     901           0 :     ret = _hx509_name_from_Name(aname, &name);
     902           0 :     if (ret)
     903           0 :         return ret;
     904             : 
     905           0 :     ret = hx509_name_to_string(name, str);
     906           0 :     hx509_name_free(&name);
     907           0 :     return ret;
     908             : }
     909             : 
     910             : /**
     911             :  * Unparse the hx509 name in name into a string.
     912             :  *
     913             :  * @param name the name to check if its empty/null.
     914             :  *
     915             :  * @return non zero if the name is empty/null.
     916             :  *
     917             :  * @ingroup hx509_name
     918             :  */
     919             : 
     920             : int
     921          40 : hx509_name_is_null_p(const hx509_name name)
     922             : {
     923          40 :     return name->der_name.u.rdnSequence.len == 0;
     924             : }
     925             : 
     926             : /**
     927             :  * Unparse the hx509 name in name into a string.
     928             :  *
     929             :  * @param name the name to print
     930             :  * @param str an allocated string returns the name in string form
     931             :  *
     932             :  * @return An hx509 error code, see hx509_get_error_string().
     933             :  *
     934             :  * @ingroup hx509_name
     935             :  */
     936             : 
     937             : int
     938           0 : hx509_general_name_unparse(GeneralName *name, char **str)
     939             : {
     940           0 :     struct rk_strpool *strpool = NULL;
     941             : 
     942           0 :     *str = NULL;
     943             : 
     944           0 :     switch (name->element) {
     945           0 :     case choice_GeneralName_otherName: {
     946             :         char *oid;
     947           0 :         hx509_oid_sprint(&name->u.otherName.type_id, &oid);
     948           0 :         if (oid == NULL)
     949           0 :             return ENOMEM;
     950           0 :         strpool = rk_strpoolprintf(strpool, "otherName: %s", oid);
     951           0 :         free(oid);
     952           0 :         break;
     953             :     }
     954           0 :     case choice_GeneralName_rfc822Name:
     955           0 :         strpool = rk_strpoolprintf(strpool, "rfc822Name: %.*s\n",
     956           0 :                                    (int)name->u.rfc822Name.length,
     957           0 :                                    (char *)name->u.rfc822Name.data);
     958           0 :         break;
     959           0 :     case choice_GeneralName_dNSName:
     960           0 :         strpool = rk_strpoolprintf(strpool, "dNSName: %.*s\n",
     961           0 :                                    (int)name->u.dNSName.length,
     962           0 :                                    (char *)name->u.dNSName.data);
     963           0 :         break;
     964           0 :     case choice_GeneralName_directoryName: {
     965             :         Name dir;
     966             :         char *s;
     967             :         int ret;
     968           0 :         memset(&dir, 0, sizeof(dir));
     969           0 :         dir.element = name->u.directoryName.element;
     970           0 :         dir.u.rdnSequence = name->u.directoryName.u.rdnSequence;
     971           0 :         ret = _hx509_unparse_Name(&dir, &s);
     972           0 :         if (ret)
     973           0 :             return ret;
     974           0 :         strpool = rk_strpoolprintf(strpool, "directoryName: %s", s);
     975           0 :         free(s);
     976           0 :         break;
     977             :     }
     978           0 :     case choice_GeneralName_uniformResourceIdentifier:
     979           0 :         strpool = rk_strpoolprintf(strpool, "URI: %.*s",
     980           0 :                                    (int)name->u.uniformResourceIdentifier.length,
     981           0 :                                    (char *)name->u.uniformResourceIdentifier.data);
     982           0 :         break;
     983           0 :     case choice_GeneralName_iPAddress: {
     984           0 :         unsigned char *a = name->u.iPAddress.data;
     985             : 
     986           0 :         strpool = rk_strpoolprintf(strpool, "IPAddress: ");
     987           0 :         if (strpool == NULL)
     988           0 :             break;
     989           0 :         if (name->u.iPAddress.length == 4)
     990           0 :             strpool = rk_strpoolprintf(strpool, "%d.%d.%d.%d",
     991           0 :                                        a[0], a[1], a[2], a[3]);
     992           0 :         else if (name->u.iPAddress.length == 16)
     993           0 :             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           0 :                                        a[0], a[1], a[2], a[3],
     999           0 :                                        a[4], a[5], a[6], a[7],
    1000           0 :                                        a[8], a[9], a[10], a[11],
    1001           0 :                                        a[12], a[13], a[14], a[15]);
    1002             :         else
    1003           0 :             strpool = rk_strpoolprintf(strpool,
    1004             :                                        "unknown IP address of length %lu",
    1005           0 :                                        (unsigned long)name->u.iPAddress.length);
    1006           0 :         break;
    1007             :     }
    1008           0 :     case choice_GeneralName_registeredID: {
    1009             :         char *oid;
    1010           0 :         hx509_oid_sprint(&name->u.registeredID, &oid);
    1011           0 :         if (oid == NULL)
    1012           0 :             return ENOMEM;
    1013           0 :         strpool = rk_strpoolprintf(strpool, "registeredID: %s", oid);
    1014           0 :         free(oid);
    1015           0 :         break;
    1016             :     }
    1017           0 :     default:
    1018           0 :         return EINVAL;
    1019             :     }
    1020           0 :     if (strpool == NULL)
    1021           0 :         return ENOMEM;
    1022             : 
    1023           0 :     *str = rk_strpoolcollect(strpool);
    1024             : 
    1025           0 :     return 0;
    1026             : }

Generated by: LCOV version 1.13