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