heimdal - fix various warnings
[sfrench/samba-autobuild/.git] / source4 / heimdal / lib / hx509 / name.c
index ccc33a3e55338c984f3468340ce13748adaef023..546b7490157d36c20ca14cd17e40c8afd2d4703a 100644 (file)
@@ -1,39 +1,38 @@
 /*
- * Copyright (c) 2004 - 2007 Kungliga Tekniska Högskolan
- * (Royal Institute of Technology, Stockholm, Sweden). 
- * All rights reserved. 
- *
- * Redistribution and use in source and binary forms, with or without 
- * modification, are permitted provided that the following conditions 
- * are met: 
- *
- * 1. Redistributions of source code must retain the above copyright 
- *    notice, this list of conditions and the following disclaimer. 
- *
- * 2. Redistributions in binary form must reproduce the above copyright 
- *    notice, this list of conditions and the following disclaimer in the 
- *    documentation and/or other materials provided with the distribution. 
- *
- * 3. Neither the name of the Institute nor the names of its contributors 
- *    may be used to endorse or promote products derived from this software 
- *    without specific prior written permission. 
- *
- * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND 
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
- * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE 
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 
- * SUCH DAMAGE. 
+ * Copyright (c) 2004 - 2009 Kungliga Tekniska Högskolan
+ * (Royal Institute of Technology, Stockholm, Sweden).
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * 3. Neither the name of the Institute nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
  */
 
 #include "hx_locl.h"
 #include <wind.h>
-RCSID("$Id: name.c 22677 2008-03-13 17:35:49Z lha $");
 
 /**
  * @page page_name PKIX/X.509 Names
@@ -63,20 +62,20 @@ RCSID("$Id: name.c 22677 2008-03-13 17:35:49Z lha $");
 
 static const struct {
     const char *n;
-    const heim_oid *(*o)(void);
+    const heim_oid *o;
     wind_profile_flags flags;
 } no[] = {
-    { "C", oid_id_at_countryName },
-    { "CN", oid_id_at_commonName },
-    { "DC", oid_id_domainComponent },
-    { "L", oid_id_at_localityName },
-    { "O", oid_id_at_organizationName },
-    { "OU", oid_id_at_organizationalUnitName },
-    { "S", oid_id_at_stateOrProvinceName },
-    { "STREET", oid_id_at_streetAddress },
-    { "UID", oid_id_Userid },
-    { "emailAddress", oid_id_pkcs9_emailAddress },
-    { "serialNumber", oid_id_at_serialNumber }
+    { "C", &asn1_oid_id_at_countryName },
+    { "CN", &asn1_oid_id_at_commonName },
+    { "DC", &asn1_oid_id_domainComponent },
+    { "L", &asn1_oid_id_at_localityName },
+    { "O", &asn1_oid_id_at_organizationName },
+    { "OU", &asn1_oid_id_at_organizationalUnitName },
+    { "S", &asn1_oid_id_at_stateOrProvinceName },
+    { "STREET", &asn1_oid_id_at_streetAddress },
+    { "UID", &asn1_oid_id_Userid },
+    { "emailAddress", &asn1_oid_id_pkcs9_emailAddress },
+    { "serialNumber", &asn1_oid_id_at_serialNumber }
 };
 
 static char *
@@ -116,7 +115,7 @@ quote_string(const char *f, size_t len, size_t *rlen)
 
 
 static int
-append_string(char **str, size_t *total_len, const char *ss, 
+append_string(char **str, size_t *total_len, const char *ss,
              size_t len, int quote)
 {
     char *s, *qs;
@@ -143,9 +142,9 @@ oidtostring(const heim_oid *type)
 {
     char *s;
     size_t i;
-    
+
     for (i = 0; i < sizeof(no)/sizeof(no[0]); i++) {
-       if (der_heim_oid_cmp((*no[i].o)(), type) == 0)
+       if (der_heim_oid_cmp(no[i].o, type) == 0)
            return strdup(no[i].n);
     }
     if (der_print_heim_oid(type, '.', &s) != 0)
@@ -158,12 +157,12 @@ stringtooid(const char *name, size_t len, heim_oid *oid)
 {
     int i, ret;
     char *s;
-    
+
     memset(oid, 0, sizeof(*oid));
 
     for (i = 0; i < sizeof(no)/sizeof(no[0]); i++) {
        if (strncasecmp(no[i].n, name, len) == 0)
-           return der_copy_oid((*no[i].o)(), oid);
+           return der_copy_oid(no[i].o, oid);
     }
     s = malloc(len + 1);
     if (s == NULL)
@@ -197,7 +196,7 @@ int
 _hx509_Name_to_string(const Name *n, char **str)
 {
     size_t total_len = 0;
-    int i, j;
+    int i, j, ret;
 
     *str = strdup("");
     if (*str == NULL)
@@ -210,7 +209,7 @@ _hx509_Name_to_string(const Name *n, char **str)
            DirectoryString *ds = &n->u.rdnSequence.val[i].val[j].value;
            char *oidname;
            char *ss;
-           
+       
            oidname = oidtostring(&n->u.rdnSequence.val[i].val[j].type);
 
            switch(ds->element) {
@@ -224,15 +223,22 @@ _hx509_Name_to_string(const Name *n, char **str)
                ss = ds->u.utf8String;
                break;
            case choice_DirectoryString_bmpString: {
-               uint16_t *bmp = ds->u.bmpString.data;
+               const uint16_t *bmp = ds->u.bmpString.data;
                size_t bmplen = ds->u.bmpString.length;
                size_t k;
 
-               ss = malloc(bmplen + 1);
+               ret = wind_ucs2utf8_length(bmp, bmplen, &k);
+               if (ret)
+                   return ret;
+               
+               ss = malloc(k + 1);
                if (ss == NULL)
                    _hx509_abort("allocation failure"); /* XXX */
-               for (k = 0; k < bmplen; k++)
-                   ss[k] = bmp[k] & 0xff; /* XXX */
+               ret = wind_ucs2utf8(bmp, bmplen, ss, NULL);
+               if (ret) {
+                   free(ss);
+                   return ret;
+               }
                ss[k] = '\0';
                break;
            }
@@ -244,15 +250,22 @@ _hx509_Name_to_string(const Name *n, char **str)
                ss[ds->u.teletexString.length] = '\0';
                break;
            case choice_DirectoryString_universalString: {
-               uint32_t *uni = ds->u.universalString.data;
+               const uint32_t *uni = ds->u.universalString.data;
                size_t unilen = ds->u.universalString.length;
                size_t k;
 
-               ss = malloc(unilen + 1);
+               ret = wind_ucs4utf8_length(uni, unilen, &k);
+               if (ret)
+                   return ret;
+
+               ss = malloc(k + 1);
                if (ss == NULL)
                    _hx509_abort("allocation failure"); /* XXX */
-               for (k = 0; k < unilen; k++)
-                   ss[k] = uni[k] & 0xff; /* XXX */
+               ret = wind_ucs4utf8(uni, unilen, ss, NULL);
+               if (ret) {
+                   free(ss);
+                   return ret;
+               }
                ss[k] = '\0';
                break;
            }
@@ -344,8 +357,10 @@ dsstringprep(const DirectoryString *ds, uint32_t **rname, size_t *rlen)
        if (name == NULL)
            return ENOMEM;
        ret = wind_utf8ucs4(ds->u.utf8String, name, &len);
-       if (ret)
+       if (ret) {
+           free(name);
            return ret;
+       }
        break;
     default:
        _hx509_abort("unknown directory type: %d", ds->element);
@@ -426,7 +441,7 @@ _hx509_name_cmp(const Name *n1, const Name *n2, int *c)
                                  &n1->u.rdnSequence.val[i].val[j].type);
            if (*c)
                return 0;
-                            
+                       
            ret = _hx509_name_ds_cmp(&n1->u.rdnSequence.val[i].val[j].value,
                                     &n2->u.rdnSequence.val[i].val[j].value,
                                     c);
@@ -480,17 +495,17 @@ _hx509_name_from_Name(const Name *n, hx509_name *name)
 
 int
 _hx509_name_modify(hx509_context context,
-                  Name *name, 
+                  Name *name,
                   int append,
-                  const heim_oid *oid, 
+                  const heim_oid *oid,
                   const char *str)
 {
     RelativeDistinguishedName *rdn;
     int ret;
     void *ptr;
 
-    ptr = realloc(name->u.rdnSequence.val, 
-                 sizeof(name->u.rdnSequence.val[0]) * 
+    ptr = realloc(name->u.rdnSequence.val,
+                 sizeof(name->u.rdnSequence.val[0]) *
                  (name->u.rdnSequence.len + 1));
     if (ptr == NULL) {
        hx509_set_error_string(context, 0, ENOMEM, "Out of memory");
@@ -503,7 +518,7 @@ _hx509_name_modify(hx509_context context,
     } else {
        memmove(&name->u.rdnSequence.val[1],
                &name->u.rdnSequence.val[0],
-               name->u.rdnSequence.len * 
+               name->u.rdnSequence.len *
                sizeof(name->u.rdnSequence.val[0]));
        
        rdn = &name->u.rdnSequence.val[0];
@@ -577,7 +592,7 @@ hx509_parse_name(hx509_context context, const char *str, hx509_name *name)
        }
        if (q == p) {
            ret = HX509_PARSING_NAME_FAILED;
-           hx509_set_error_string(context, 0, ret, 
+           hx509_set_error_string(context, 0, ret,
                                   "missing name before = in %s", p);
            goto out;
        }
@@ -591,7 +606,7 @@ hx509_parse_name(hx509_context context, const char *str, hx509_name *name)
        ret = stringtooid(p, q - p, &oid);
        if (ret) {
            ret = HX509_PARSING_NAME_FAILED;
-           hx509_set_error_string(context, 0, ret, 
+           hx509_set_error_string(context, 0, ret,
                                   "unknown type: %.*s", (int)(q - p), p);
            goto out;
        }
@@ -600,7 +615,7 @@ hx509_parse_name(hx509_context context, const char *str, hx509_name *name)
            size_t pstr_len = len - (q - p) - 1;
            const char *pstr = p + (q - p) + 1;
            char *r;
-           
+       
            r = malloc(pstr_len + 1);
            if (r == NULL) {
                der_free_oid(&oid);
@@ -731,8 +746,8 @@ hx509_name_expand(hx509_context context,
            }
            p = strstr(ds->u.utf8String, "${");
            if (p) {
-               strpool = rk_strpoolprintf(strpool, "%.*s", 
-                                          (int)(p - ds->u.utf8String), 
+               strpool = rk_strpoolprintf(strpool, "%.*s",
+                                          (int)(p - ds->u.utf8String),
                                           ds->u.utf8String);
                if (strpool == NULL) {
                    hx509_set_error_string(context, 0, ENOMEM, "out of memory");
@@ -751,7 +766,7 @@ hx509_name_expand(hx509_context context,
                p += 2;
                value = hx509_env_lfind(context, env, p, p2 - p);
                if (value == NULL) {
-                   hx509_set_error_string(context, 0, EINVAL, 
+                   hx509_set_error_string(context, 0, EINVAL,
                                           "variable %.*s missing",
                                           (int)(p2 - p), p);
                    rk_strpoolfree(strpool);
@@ -766,7 +781,7 @@ hx509_name_expand(hx509_context context,
 
                p = strstr(p2, "${");
                if (p)
-                   strpool = rk_strpoolprintf(strpool, "%.*s", 
+                   strpool = rk_strpoolprintf(strpool, "%.*s",
                                               (int)(p - p2), p2);
                else
                    strpool = rk_strpoolprintf(strpool, "%s", p2);
@@ -911,12 +926,12 @@ hx509_general_name_unparse(GeneralName *name, char **str)
 
     switch (name->element) {
     case choice_GeneralName_otherName: {
-       char *str;
-       hx509_oid_sprint(&name->u.otherName.type_id, &str);
-       if (str == NULL)
+       char *str2;
+       hx509_oid_sprint(&name->u.otherName.type_id, &str2);
+       if (str2 == NULL)
            return ENOMEM;
-       strpool = rk_strpoolprintf(strpool, "otherName: %s", str);
-       free(str);
+       strpool = rk_strpoolprintf(strpool, "otherName: %s", str2);
+       free(str2);
        break;
     }
     case choice_GeneralName_rfc822Name:
@@ -942,7 +957,7 @@ hx509_general_name_unparse(GeneralName *name, char **str)
        break;
     }
     case choice_GeneralName_uniformResourceIdentifier:
-       strpool = rk_strpoolprintf(strpool, "URI: %s", 
+       strpool = rk_strpoolprintf(strpool, "URI: %s",
                                   name->u.uniformResourceIdentifier);
        break;
     case choice_GeneralName_iPAddress: {
@@ -952,31 +967,31 @@ hx509_general_name_unparse(GeneralName *name, char **str)
        if (strpool == NULL)
            break;
        if (name->u.iPAddress.length == 4)
-           strpool = rk_strpoolprintf(strpool, "%d.%d.%d.%d", 
+           strpool = rk_strpoolprintf(strpool, "%d.%d.%d.%d",
                                       a[0], a[1], a[2], a[3]);
        else if (name->u.iPAddress.length == 16)
-           strpool = rk_strpoolprintf(strpool, 
+           strpool = rk_strpoolprintf(strpool,
                                       "%02X:%02X:%02X:%02X:"
                                       "%02X:%02X:%02X:%02X:"
                                       "%02X:%02X:%02X:%02X:"
-                                      "%02X:%02X:%02X:%02X", 
+                                      "%02X:%02X:%02X:%02X",
                                       a[0], a[1], a[2], a[3],
                                       a[4], a[5], a[6], a[7],
                                       a[8], a[9], a[10], a[11],
                                       a[12], a[13], a[14], a[15]);
        else
-           strpool = rk_strpoolprintf(strpool, 
+           strpool = rk_strpoolprintf(strpool,
                                       "unknown IP address of length %lu",
                                       (unsigned long)name->u.iPAddress.length);
        break;
     }
     case choice_GeneralName_registeredID: {
-       char *str;
-       hx509_oid_sprint(&name->u.registeredID, &str);
-       if (str == NULL)
+       char *str2;
+       hx509_oid_sprint(&name->u.registeredID, &str2);
+       if (str2 == NULL)
            return ENOMEM;
-       strpool = rk_strpoolprintf(strpool, "registeredID: %s", str);
-       free(str);
+       strpool = rk_strpoolprintf(strpool, "registeredID: %s", str2);
+       free(str2);
        break;
     }
     default: