| 1 |
/* |
| 2 |
* Copyright (c) 2004 - 2007 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 "crypto-headers.h" |
| 36 |
#include <rtbl.h> |
| 37 |
|
| 38 |
/** |
| 39 |
* @page page_cert The basic certificate |
| 40 |
* |
| 41 |
* The basic hx509 cerificate object in hx509 is hx509_cert. The |
| 42 |
* hx509_cert object is representing one X509/PKIX certificate and |
| 43 |
* associated attributes; like private key, friendly name, etc. |
| 44 |
* |
| 45 |
* A hx509_cert object is usully found via the keyset interfaces (@ref |
| 46 |
* page_keyset), but its also possible to create a certificate |
| 47 |
* directly from a parsed object with hx509_cert_init() and |
| 48 |
* hx509_cert_init_data(). |
| 49 |
* |
| 50 |
* See the library functions here: @ref hx509_cert |
| 51 |
*/ |
| 52 |
|
| 53 |
struct hx509_verify_ctx_data { |
| 54 |
hx509_certs trust_anchors; |
| 55 |
int flags; |
| 56 |
#define HX509_VERIFY_CTX_F_TIME_SET 1 |
| 57 |
#define HX509_VERIFY_CTX_F_ALLOW_PROXY_CERTIFICATE 2 |
| 58 |
#define HX509_VERIFY_CTX_F_REQUIRE_RFC3280 4 |
| 59 |
#define HX509_VERIFY_CTX_F_CHECK_TRUST_ANCHORS 8 |
| 60 |
#define HX509_VERIFY_CTX_F_NO_DEFAULT_ANCHORS 16 |
| 61 |
#define HX509_VERIFY_CTX_F_NO_BEST_BEFORE_CHECK 32 |
| 62 |
time_t time_now; |
| 63 |
unsigned int max_depth; |
| 64 |
#define HX509_VERIFY_MAX_DEPTH 30 |
| 65 |
hx509_revoke_ctx revoke_ctx; |
| 66 |
}; |
| 67 |
|
| 68 |
#define REQUIRE_RFC3280(ctx) ((ctx)->flags & HX509_VERIFY_CTX_F_REQUIRE_RFC3280) |
| 69 |
#define CHECK_TA(ctx) ((ctx)->flags & HX509_VERIFY_CTX_F_CHECK_TRUST_ANCHORS) |
| 70 |
#define ALLOW_DEF_TA(ctx) (((ctx)->flags & HX509_VERIFY_CTX_F_NO_DEFAULT_ANCHORS) == 0) |
| 71 |
|
| 72 |
struct _hx509_cert_attrs { |
| 73 |
size_t len; |
| 74 |
hx509_cert_attribute *val; |
| 75 |
}; |
| 76 |
|
| 77 |
struct hx509_cert_data { |
| 78 |
unsigned int ref; |
| 79 |
char *friendlyname; |
| 80 |
Certificate *data; |
| 81 |
hx509_private_key private_key; |
| 82 |
struct _hx509_cert_attrs attrs; |
| 83 |
hx509_name basename; |
| 84 |
_hx509_cert_release_func release; |
| 85 |
void *ctx; |
| 86 |
}; |
| 87 |
|
| 88 |
typedef struct hx509_name_constraints { |
| 89 |
NameConstraints *val; |
| 90 |
size_t len; |
| 91 |
} hx509_name_constraints; |
| 92 |
|
| 93 |
#define GeneralSubtrees_SET(g,var) \ |
| 94 |
(g)->len = (var)->len, (g)->val = (var)->val; |
| 95 |
|
| 96 |
static void |
| 97 |
init_context_once(void *ignored) |
| 98 |
{ |
| 99 |
|
| 100 |
ENGINE_add_conf_module(); |
| 101 |
OpenSSL_add_all_algorithms(); |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* Creates a hx509 context that most functions in the library |
| 106 |
* uses. The context is only allowed to be used by one thread at each |
| 107 |
* moment. Free the context with hx509_context_free(). |
| 108 |
* |
| 109 |
* @param context Returns a pointer to new hx509 context. |
| 110 |
* |
| 111 |
* @return Returns an hx509 error code. |
| 112 |
* |
| 113 |
* @ingroup hx509 |
| 114 |
*/ |
| 115 |
|
| 116 |
int |
| 117 |
hx509_context_init(hx509_context *context) |
| 118 |
{ |
| 119 |
static heim_base_once_t init_context = HEIM_BASE_ONCE_INIT; |
| 120 |
|
| 121 |
*context = calloc(1, sizeof(**context)); |
| 122 |
if (*context == NULL) |
| 123 |
return ENOMEM; |
| 124 |
|
| 125 |
heim_base_once_f(&init_context, NULL, init_context_once); |
| 126 |
|
| 127 |
_hx509_ks_null_register(*context); |
| 128 |
_hx509_ks_mem_register(*context); |
| 129 |
_hx509_ks_file_register(*context); |
| 130 |
_hx509_ks_pkcs12_register(*context); |
| 131 |
_hx509_ks_pkcs11_register(*context); |
| 132 |
_hx509_ks_dir_register(*context); |
| 133 |
_hx509_ks_keychain_register(*context); |
| 134 |
|
| 135 |
(*context)->ocsp_time_diff = HX509_DEFAULT_OCSP_TIME_DIFF; |
| 136 |
|
| 137 |
initialize_hx_error_table_r(&(*context)->et_list); |
| 138 |
initialize_asn1_error_table_r(&(*context)->et_list); |
| 139 |
|
| 140 |
#ifdef HX509_DEFAULT_ANCHORS |
| 141 |
(void)hx509_certs_init(*context, HX509_DEFAULT_ANCHORS, 0, |
| 142 |
NULL, &(*context)->default_trust_anchors); |
| 143 |
#endif |
| 144 |
|
| 145 |
return 0; |
| 146 |
} |
| 147 |
|
| 148 |
/** |
| 149 |
* Selects if the hx509_revoke_verify() function is going to require |
| 150 |
* the existans of a revokation method (OCSP, CRL) or not. Note that |
| 151 |
* hx509_verify_path(), hx509_cms_verify_signed(), and other function |
| 152 |
* call hx509_revoke_verify(). |
| 153 |
* |
| 154 |
* @param context hx509 context to change the flag for. |
| 155 |
* @param flag zero, revokation method required, non zero missing |
| 156 |
* revokation method ok |
| 157 |
* |
| 158 |
* @ingroup hx509_verify |
| 159 |
*/ |
| 160 |
|
| 161 |
void |
| 162 |
hx509_context_set_missing_revoke(hx509_context context, int flag) |
| 163 |
{ |
| 164 |
if (flag) |
| 165 |
context->flags |= HX509_CTX_VERIFY_MISSING_OK; |
| 166 |
else |
| 167 |
context->flags &= ~HX509_CTX_VERIFY_MISSING_OK; |
| 168 |
} |
| 169 |
|
| 170 |
/** |
| 171 |
* Free the context allocated by hx509_context_init(). |
| 172 |
* |
| 173 |
* @param context context to be freed. |
| 174 |
* |
| 175 |
* @ingroup hx509 |
| 176 |
*/ |
| 177 |
|
| 178 |
void |
| 179 |
hx509_context_free(hx509_context *context) |
| 180 |
{ |
| 181 |
hx509_clear_error_string(*context); |
| 182 |
if ((*context)->ks_ops) { |
| 183 |
free((*context)->ks_ops); |
| 184 |
(*context)->ks_ops = NULL; |
| 185 |
} |
| 186 |
(*context)->ks_num_ops = 0; |
| 187 |
free_error_table ((*context)->et_list); |
| 188 |
if ((*context)->querystat) |
| 189 |
free((*context)->querystat); |
| 190 |
memset(*context, 0, sizeof(**context)); |
| 191 |
free(*context); |
| 192 |
*context = NULL; |
| 193 |
} |
| 194 |
|
| 195 |
/* |
| 196 |
* |
| 197 |
*/ |
| 198 |
|
| 199 |
Certificate * |
| 200 |
_hx509_get_cert(hx509_cert cert) |
| 201 |
{ |
| 202 |
return cert->data; |
| 203 |
} |
| 204 |
|
| 205 |
/* |
| 206 |
* |
| 207 |
*/ |
| 208 |
|
| 209 |
int |
| 210 |
_hx509_cert_get_version(const Certificate *t) |
| 211 |
{ |
| 212 |
return t->tbsCertificate.version ? *t->tbsCertificate.version + 1 : 1; |
| 213 |
} |
| 214 |
|
| 215 |
/** |
| 216 |
* Allocate and init an hx509 certificate object from the decoded |
| 217 |
* certificate `c´. |
| 218 |
* |
| 219 |
* @param context A hx509 context. |
| 220 |
* @param c |
| 221 |
* @param error |
| 222 |
* |
| 223 |
* @return Returns an hx509 certificate |
| 224 |
* |
| 225 |
* @ingroup hx509_cert |
| 226 |
*/ |
| 227 |
|
| 228 |
hx509_cert |
| 229 |
hx509_cert_init(hx509_context context, const Certificate *c, heim_error_t *error) |
| 230 |
{ |
| 231 |
hx509_cert cert; |
| 232 |
int ret; |
| 233 |
|
| 234 |
cert = malloc(sizeof(*cert)); |
| 235 |
if (cert == NULL) { |
| 236 |
if (error) |
| 237 |
*error = heim_error_create_enomem(); |
| 238 |
return NULL; |
| 239 |
} |
| 240 |
cert->ref = 1; |
| 241 |
cert->friendlyname = NULL; |
| 242 |
cert->attrs.len = 0; |
| 243 |
cert->attrs.val = NULL; |
| 244 |
cert->private_key = NULL; |
| 245 |
cert->basename = NULL; |
| 246 |
cert->release = NULL; |
| 247 |
cert->ctx = NULL; |
| 248 |
|
| 249 |
cert->data = calloc(1, sizeof(*(cert->data))); |
| 250 |
if (cert->data == NULL) { |
| 251 |
free(cert); |
| 252 |
if (error) |
| 253 |
*error = heim_error_create_enomem(); |
| 254 |
return NULL; |
| 255 |
} |
| 256 |
ret = copy_Certificate(c, cert->data); |
| 257 |
if (ret) { |
| 258 |
free(cert->data); |
| 259 |
free(cert); |
| 260 |
cert = NULL; |
| 261 |
} |
| 262 |
return cert; |
| 263 |
} |
| 264 |
|
| 265 |
/** |
| 266 |
* Just like hx509_cert_init(), but instead of a decode certificate |
| 267 |
* takes an pointer and length to a memory region that contains a |
| 268 |
* DER/BER encoded certificate. |
| 269 |
* |
| 270 |
* If the memory region doesn't contain just the certificate and |
| 271 |
* nothing more the function will fail with |
| 272 |
* HX509_EXTRA_DATA_AFTER_STRUCTURE. |
| 273 |
* |
| 274 |
* @param context A hx509 context. |
| 275 |
* @param ptr pointer to memory region containing encoded certificate. |
| 276 |
* @param len length of memory region. |
| 277 |
* @param error possibly returns an error |
| 278 |
* |
| 279 |
* @return An hx509 certificate |
| 280 |
* |
| 281 |
* @ingroup hx509_cert |
| 282 |
*/ |
| 283 |
|
| 284 |
hx509_cert |
| 285 |
hx509_cert_init_data(hx509_context context, |
| 286 |
const void *ptr, |
| 287 |
size_t len, |
| 288 |
heim_error_t *error) |
| 289 |
{ |
| 290 |
hx509_cert cert; |
| 291 |
Certificate t; |
| 292 |
size_t size; |
| 293 |
int ret; |
| 294 |
|
| 295 |
ret = decode_Certificate(ptr, len, &t, &size); |
| 296 |
if (ret) { |
| 297 |
if (error) |
| 298 |
*error = heim_error_create(ret, "Failed to decode certificate"); |
| 299 |
return NULL; |
| 300 |
} |
| 301 |
if (size != len) { |
| 302 |
free_Certificate(&t); |
| 303 |
if (error) |
| 304 |
*error = heim_error_create(HX509_EXTRA_DATA_AFTER_STRUCTURE, |
| 305 |
"Extra data after certificate"); |
| 306 |
return NULL; |
| 307 |
} |
| 308 |
|
| 309 |
cert = hx509_cert_init(context, &t, error); |
| 310 |
free_Certificate(&t); |
| 311 |
return cert; |
| 312 |
} |
| 313 |
|
| 314 |
void |
| 315 |
_hx509_cert_set_release(hx509_cert cert, |
| 316 |
_hx509_cert_release_func release, |
| 317 |
void *ctx) |
| 318 |
{ |
| 319 |
cert->release = release; |
| 320 |
cert->ctx = ctx; |
| 321 |
} |
| 322 |
|
| 323 |
|
| 324 |
/* Doesn't make a copy of `private_key'. */ |
| 325 |
|
| 326 |
int |
| 327 |
_hx509_cert_assign_key(hx509_cert cert, hx509_private_key private_key) |
| 328 |
{ |
| 329 |
if (cert->private_key) |
| 330 |
hx509_private_key_free(&cert->private_key); |
| 331 |
cert->private_key = _hx509_private_key_ref(private_key); |
| 332 |
return 0; |
| 333 |
} |
| 334 |
|
| 335 |
/** |
| 336 |
* Free reference to the hx509 certificate object, if the refcounter |
| 337 |
* reaches 0, the object if freed. Its allowed to pass in NULL. |
| 338 |
* |
| 339 |
* @param cert the cert to free. |
| 340 |
* |
| 341 |
* @ingroup hx509_cert |
| 342 |
*/ |
| 343 |
|
| 344 |
void |
| 345 |
hx509_cert_free(hx509_cert cert) |
| 346 |
{ |
| 347 |
size_t i; |
| 348 |
|
| 349 |
if (cert == NULL) |
| 350 |
return; |
| 351 |
|
| 352 |
if (cert->ref <= 0) |
| 353 |
_hx509_abort("cert refcount <= 0 on free"); |
| 354 |
if (--cert->ref > 0) |
| 355 |
return; |
| 356 |
|
| 357 |
if (cert->release) |
| 358 |
(cert->release)(cert, cert->ctx); |
| 359 |
|
| 360 |
if (cert->private_key) |
| 361 |
hx509_private_key_free(&cert->private_key); |
| 362 |
|
| 363 |
free_Certificate(cert->data); |
| 364 |
free(cert->data); |
| 365 |
|
| 366 |
for (i = 0; i < cert->attrs.len; i++) { |
| 367 |
der_free_octet_string(&cert->attrs.val[i]->data); |
| 368 |
der_free_oid(&cert->attrs.val[i]->oid); |
| 369 |
free(cert->attrs.val[i]); |
| 370 |
} |
| 371 |
free(cert->attrs.val); |
| 372 |
free(cert->friendlyname); |
| 373 |
if (cert->basename) |
| 374 |
hx509_name_free(&cert->basename); |
| 375 |
memset(cert, 0, sizeof(*cert)); |
| 376 |
free(cert); |
| 377 |
} |
| 378 |
|
| 379 |
/** |
| 380 |
* Add a reference to a hx509 certificate object. |
| 381 |
* |
| 382 |
* @param cert a pointer to an hx509 certificate object. |
| 383 |
* |
| 384 |
* @return the same object as is passed in. |
| 385 |
* |
| 386 |
* @ingroup hx509_cert |
| 387 |
*/ |
| 388 |
|
| 389 |
hx509_cert |
| 390 |
hx509_cert_ref(hx509_cert cert) |
| 391 |
{ |
| 392 |
if (cert == NULL) |
| 393 |
return NULL; |
| 394 |
if (cert->ref <= 0) |
| 395 |
_hx509_abort("cert refcount <= 0"); |
| 396 |
cert->ref++; |
| 397 |
if (cert->ref == 0) |
| 398 |
_hx509_abort("cert refcount == 0"); |
| 399 |
return cert; |
| 400 |
} |
| 401 |
|
| 402 |
/** |
| 403 |
* Allocate an verification context that is used fo control the |
| 404 |
* verification process. |
| 405 |
* |
| 406 |
* @param context A hx509 context. |
| 407 |
* @param ctx returns a pointer to a hx509_verify_ctx object. |
| 408 |
* |
| 409 |
* @return An hx509 error code, see hx509_get_error_string(). |
| 410 |
* |
| 411 |
* @ingroup hx509_verify |
| 412 |
*/ |
| 413 |
|
| 414 |
int |
| 415 |
hx509_verify_init_ctx(hx509_context context, hx509_verify_ctx *ctx) |
| 416 |
{ |
| 417 |
hx509_verify_ctx c; |
| 418 |
|
| 419 |
c = calloc(1, sizeof(*c)); |
| 420 |
if (c == NULL) |
| 421 |
return ENOMEM; |
| 422 |
|
| 423 |
c->max_depth = HX509_VERIFY_MAX_DEPTH; |
| 424 |
|
| 425 |
*ctx = c; |
| 426 |
|
| 427 |
return 0; |
| 428 |
} |
| 429 |
|
| 430 |
/** |
| 431 |
* Free an hx509 verification context. |
| 432 |
* |
| 433 |
* @param ctx the context to be freed. |
| 434 |
* |
| 435 |
* @ingroup hx509_verify |
| 436 |
*/ |
| 437 |
|
| 438 |
void |
| 439 |
hx509_verify_destroy_ctx(hx509_verify_ctx ctx) |
| 440 |
{ |
| 441 |
if (ctx) { |
| 442 |
hx509_certs_free(&ctx->trust_anchors); |
| 443 |
hx509_revoke_free(&ctx->revoke_ctx); |
| 444 |
memset(ctx, 0, sizeof(*ctx)); |
| 445 |
} |
| 446 |
free(ctx); |
| 447 |
} |
| 448 |
|
| 449 |
/** |
| 450 |
* Set the trust anchors in the verification context, makes an |
| 451 |
* reference to the keyset, so the consumer can free the keyset |
| 452 |
* independent of the destruction of the verification context (ctx). |
| 453 |
* If there already is a keyset attached, it's released. |
| 454 |
* |
| 455 |
* @param ctx a verification context |
| 456 |
* @param set a keyset containing the trust anchors. |
| 457 |
* |
| 458 |
* @ingroup hx509_verify |
| 459 |
*/ |
| 460 |
|
| 461 |
void |
| 462 |
hx509_verify_attach_anchors(hx509_verify_ctx ctx, hx509_certs set) |
| 463 |
{ |
| 464 |
if (ctx->trust_anchors) |
| 465 |
hx509_certs_free(&ctx->trust_anchors); |
| 466 |
ctx->trust_anchors = hx509_certs_ref(set); |
| 467 |
} |
| 468 |
|
| 469 |
/** |
| 470 |
* Attach an revocation context to the verfication context, , makes an |
| 471 |
* reference to the revoke context, so the consumer can free the |
| 472 |
* revoke context independent of the destruction of the verification |
| 473 |
* context. If there is no revoke context, the verification process is |
| 474 |
* NOT going to check any verification status. |
| 475 |
* |
| 476 |
* @param ctx a verification context. |
| 477 |
* @param revoke_ctx a revoke context. |
| 478 |
* |
| 479 |
* @ingroup hx509_verify |
| 480 |
*/ |
| 481 |
|
| 482 |
void |
| 483 |
hx509_verify_attach_revoke(hx509_verify_ctx ctx, hx509_revoke_ctx revoke_ctx) |
| 484 |
{ |
| 485 |
if (ctx->revoke_ctx) |
| 486 |
hx509_revoke_free(&ctx->revoke_ctx); |
| 487 |
ctx->revoke_ctx = _hx509_revoke_ref(revoke_ctx); |
| 488 |
} |
| 489 |
|
| 490 |
/** |
| 491 |
* Set the clock time the the verification process is going to |
| 492 |
* use. Used to check certificate in the past and future time. If not |
| 493 |
* set the current time will be used. |
| 494 |
* |
| 495 |
* @param ctx a verification context. |
| 496 |
* @param t the time the verifiation is using. |
| 497 |
* |
| 498 |
* |
| 499 |
* @ingroup hx509_verify |
| 500 |
*/ |
| 501 |
|
| 502 |
void |
| 503 |
hx509_verify_set_time(hx509_verify_ctx ctx, time_t t) |
| 504 |
{ |
| 505 |
ctx->flags |= HX509_VERIFY_CTX_F_TIME_SET; |
| 506 |
ctx->time_now = t; |
| 507 |
} |
| 508 |
|
| 509 |
time_t |
| 510 |
_hx509_verify_get_time(hx509_verify_ctx ctx) |
| 511 |
{ |
| 512 |
return ctx->time_now; |
| 513 |
} |
| 514 |
|
| 515 |
/** |
| 516 |
* Set the maximum depth of the certificate chain that the path |
| 517 |
* builder is going to try. |
| 518 |
* |
| 519 |
* @param ctx a verification context |
| 520 |
* @param max_depth maxium depth of the certificate chain, include |
| 521 |
* trust anchor. |
| 522 |
* |
| 523 |
* @ingroup hx509_verify |
| 524 |
*/ |
| 525 |
|
| 526 |
void |
| 527 |
hx509_verify_set_max_depth(hx509_verify_ctx ctx, unsigned int max_depth) |
| 528 |
{ |
| 529 |
ctx->max_depth = max_depth; |
| 530 |
} |
| 531 |
|
| 532 |
/** |
| 533 |
* Allow or deny the use of proxy certificates |
| 534 |
* |
| 535 |
* @param ctx a verification context |
| 536 |
* @param boolean if non zero, allow proxy certificates. |
| 537 |
* |
| 538 |
* @ingroup hx509_verify |
| 539 |
*/ |
| 540 |
|
| 541 |
void |
| 542 |
hx509_verify_set_proxy_certificate(hx509_verify_ctx ctx, int boolean) |
| 543 |
{ |
| 544 |
if (boolean) |
| 545 |
ctx->flags |= HX509_VERIFY_CTX_F_ALLOW_PROXY_CERTIFICATE; |
| 546 |
else |
| 547 |
ctx->flags &= ~HX509_VERIFY_CTX_F_ALLOW_PROXY_CERTIFICATE; |
| 548 |
} |
| 549 |
|
| 550 |
/** |
| 551 |
* Select strict RFC3280 verification of certificiates. This means |
| 552 |
* checking key usage on CA certificates, this will make version 1 |
| 553 |
* certificiates unuseable. |
| 554 |
* |
| 555 |
* @param ctx a verification context |
| 556 |
* @param boolean if non zero, use strict verification. |
| 557 |
* |
| 558 |
* @ingroup hx509_verify |
| 559 |
*/ |
| 560 |
|
| 561 |
void |
| 562 |
hx509_verify_set_strict_rfc3280_verification(hx509_verify_ctx ctx, int boolean) |
| 563 |
{ |
| 564 |
if (boolean) |
| 565 |
ctx->flags |= HX509_VERIFY_CTX_F_REQUIRE_RFC3280; |
| 566 |
else |
| 567 |
ctx->flags &= ~HX509_VERIFY_CTX_F_REQUIRE_RFC3280; |
| 568 |
} |
| 569 |
|
| 570 |
/** |
| 571 |
* Allow using the operating system builtin trust anchors if no other |
| 572 |
* trust anchors are configured. |
| 573 |
* |
| 574 |
* @param ctx a verification context |
| 575 |
* @param boolean if non zero, useing the operating systems builtin |
| 576 |
* trust anchors. |
| 577 |
* |
| 578 |
* |
| 579 |
* @return An hx509 error code, see hx509_get_error_string(). |
| 580 |
* |
| 581 |
* @ingroup hx509_cert |
| 582 |
*/ |
| 583 |
|
| 584 |
void |
| 585 |
hx509_verify_ctx_f_allow_default_trustanchors(hx509_verify_ctx ctx, int boolean) |
| 586 |
{ |
| 587 |
if (boolean) |
| 588 |
ctx->flags &= ~HX509_VERIFY_CTX_F_NO_DEFAULT_ANCHORS; |
| 589 |
else |
| 590 |
ctx->flags |= HX509_VERIFY_CTX_F_NO_DEFAULT_ANCHORS; |
| 591 |
} |
| 592 |
|
| 593 |
void |
| 594 |
hx509_verify_ctx_f_allow_best_before_signature_algs(hx509_context ctx, |
| 595 |
int boolean) |
| 596 |
{ |
| 597 |
if (boolean) |
| 598 |
ctx->flags &= ~HX509_VERIFY_CTX_F_NO_BEST_BEFORE_CHECK; |
| 599 |
else |
| 600 |
ctx->flags |= HX509_VERIFY_CTX_F_NO_BEST_BEFORE_CHECK; |
| 601 |
} |
| 602 |
|
| 603 |
static const Extension * |
| 604 |
find_extension(const Certificate *cert, const heim_oid *oid, size_t *idx) |
| 605 |
{ |
| 606 |
const TBSCertificate *c = &cert->tbsCertificate; |
| 607 |
|
| 608 |
if (c->version == NULL || *c->version < 2 || c->extensions == NULL) |
| 609 |
return NULL; |
| 610 |
|
| 611 |
for (;*idx < c->extensions->len; (*idx)++) { |
| 612 |
if (der_heim_oid_cmp(&c->extensions->val[*idx].extnID, oid) == 0) |
| 613 |
return &c->extensions->val[(*idx)++]; |
| 614 |
} |
| 615 |
return NULL; |
| 616 |
} |
| 617 |
|
| 618 |
static int |
| 619 |
find_extension_auth_key_id(const Certificate *subject, |
| 620 |
AuthorityKeyIdentifier *ai) |
| 621 |
{ |
| 622 |
const Extension *e; |
| 623 |
size_t size; |
| 624 |
size_t i = 0; |
| 625 |
|
| 626 |
memset(ai, 0, sizeof(*ai)); |
| 627 |
|
| 628 |
e = find_extension(subject, &asn1_oid_id_x509_ce_authorityKeyIdentifier, &i); |
| 629 |
if (e == NULL) |
| 630 |
return HX509_EXTENSION_NOT_FOUND; |
| 631 |
|
| 632 |
return decode_AuthorityKeyIdentifier(e->extnValue.data, |
| 633 |
e->extnValue.length, |
| 634 |
ai, &size); |
| 635 |
} |
| 636 |
|
| 637 |
int |
| 638 |
_hx509_find_extension_subject_key_id(const Certificate *issuer, |
| 639 |
SubjectKeyIdentifier *si) |
| 640 |
{ |
| 641 |
const Extension *e; |
| 642 |
size_t size; |
| 643 |
size_t i = 0; |
| 644 |
|
| 645 |
memset(si, 0, sizeof(*si)); |
| 646 |
|
| 647 |
e = find_extension(issuer, &asn1_oid_id_x509_ce_subjectKeyIdentifier, &i); |
| 648 |
if (e == NULL) |
| 649 |
return HX509_EXTENSION_NOT_FOUND; |
| 650 |
|
| 651 |
return decode_SubjectKeyIdentifier(e->extnValue.data, |
| 652 |
e->extnValue.length, |
| 653 |
si, &size); |
| 654 |
} |
| 655 |
|
| 656 |
static int |
| 657 |
find_extension_name_constraints(const Certificate *subject, |
| 658 |
NameConstraints *nc) |
| 659 |
{ |
| 660 |
const Extension *e; |
| 661 |
size_t size; |
| 662 |
size_t i = 0; |
| 663 |
|
| 664 |
memset(nc, 0, sizeof(*nc)); |
| 665 |
|
| 666 |
e = find_extension(subject, &asn1_oid_id_x509_ce_nameConstraints, &i); |
| 667 |
if (e == NULL) |
| 668 |
return HX509_EXTENSION_NOT_FOUND; |
| 669 |
|
| 670 |
return decode_NameConstraints(e->extnValue.data, |
| 671 |
e->extnValue.length, |
| 672 |
nc, &size); |
| 673 |
} |
| 674 |
|
| 675 |
static int |
| 676 |
find_extension_subject_alt_name(const Certificate *cert, size_t *i, |
| 677 |
GeneralNames *sa) |
| 678 |
{ |
| 679 |
const Extension *e; |
| 680 |
size_t size; |
| 681 |
|
| 682 |
memset(sa, 0, sizeof(*sa)); |
| 683 |
|
| 684 |
e = find_extension(cert, &asn1_oid_id_x509_ce_subjectAltName, i); |
| 685 |
if (e == NULL) |
| 686 |
return HX509_EXTENSION_NOT_FOUND; |
| 687 |
|
| 688 |
return decode_GeneralNames(e->extnValue.data, |
| 689 |
e->extnValue.length, |
| 690 |
sa, &size); |
| 691 |
} |
| 692 |
|
| 693 |
static int |
| 694 |
find_extension_eku(const Certificate *cert, ExtKeyUsage *eku) |
| 695 |
{ |
| 696 |
const Extension *e; |
| 697 |
size_t size; |
| 698 |
size_t i = 0; |
| 699 |
|
| 700 |
memset(eku, 0, sizeof(*eku)); |
| 701 |
|
| 702 |
e = find_extension(cert, &asn1_oid_id_x509_ce_extKeyUsage, &i); |
| 703 |
if (e == NULL) |
| 704 |
return HX509_EXTENSION_NOT_FOUND; |
| 705 |
|
| 706 |
return decode_ExtKeyUsage(e->extnValue.data, |
| 707 |
e->extnValue.length, |
| 708 |
eku, &size); |
| 709 |
} |
| 710 |
|
| 711 |
static int |
| 712 |
add_to_list(hx509_octet_string_list *list, const heim_octet_string *entry) |
| 713 |
{ |
| 714 |
void *p; |
| 715 |
int ret; |
| 716 |
|
| 717 |
p = realloc(list->val, (list->len + 1) * sizeof(list->val[0])); |
| 718 |
if (p == NULL) |
| 719 |
return ENOMEM; |
| 720 |
list->val = p; |
| 721 |
ret = der_copy_octet_string(entry, &list->val[list->len]); |
| 722 |
if (ret) |
| 723 |
return ret; |
| 724 |
list->len++; |
| 725 |
return 0; |
| 726 |
} |
| 727 |
|
| 728 |
/** |
| 729 |
* Free a list of octet strings returned by another hx509 library |
| 730 |
* function. |
| 731 |
* |
| 732 |
* @param list list to be freed. |
| 733 |
* |
| 734 |
* @ingroup hx509_misc |
| 735 |
*/ |
| 736 |
|
| 737 |
void |
| 738 |
hx509_free_octet_string_list(hx509_octet_string_list *list) |
| 739 |
{ |
| 740 |
size_t i; |
| 741 |
for (i = 0; i < list->len; i++) |
| 742 |
der_free_octet_string(&list->val[i]); |
| 743 |
free(list->val); |
| 744 |
list->val = NULL; |
| 745 |
list->len = 0; |
| 746 |
} |
| 747 |
|
| 748 |
/** |
| 749 |
* Return a list of subjectAltNames specified by oid in the |
| 750 |
* certificate. On error the |
| 751 |
* |
| 752 |
* The returned list of octet string should be freed with |
| 753 |
* hx509_free_octet_string_list(). |
| 754 |
* |
| 755 |
* @param context A hx509 context. |
| 756 |
* @param cert a hx509 certificate object. |
| 757 |
* @param oid an oid to for SubjectAltName. |
| 758 |
* @param list list of matching SubjectAltName. |
| 759 |
* |
| 760 |
* @return An hx509 error code, see hx509_get_error_string(). |
| 761 |
* |
| 762 |
* @ingroup hx509_cert |
| 763 |
*/ |
| 764 |
|
| 765 |
int |
| 766 |
hx509_cert_find_subjectAltName_otherName(hx509_context context, |
| 767 |
hx509_cert cert, |
| 768 |
const heim_oid *oid, |
| 769 |
hx509_octet_string_list *list) |
| 770 |
{ |
| 771 |
GeneralNames sa; |
| 772 |
int ret; |
| 773 |
size_t i, j; |
| 774 |
|
| 775 |
list->val = NULL; |
| 776 |
list->len = 0; |
| 777 |
|
| 778 |
i = 0; |
| 779 |
while (1) { |
| 780 |
ret = find_extension_subject_alt_name(_hx509_get_cert(cert), &i, &sa); |
| 781 |
i++; |
| 782 |
if (ret == HX509_EXTENSION_NOT_FOUND) { |
| 783 |
return 0; |
| 784 |
} else if (ret != 0) { |
| 785 |
hx509_set_error_string(context, 0, ret, "Error searching for SAN"); |
| 786 |
hx509_free_octet_string_list(list); |
| 787 |
return ret; |
| 788 |
} |
| 789 |
|
| 790 |
for (j = 0; j < sa.len; j++) { |
| 791 |
if (sa.val[j].element == choice_GeneralName_otherName && |
| 792 |
der_heim_oid_cmp(&sa.val[j].u.otherName.type_id, oid) == 0) |
| 793 |
{ |
| 794 |
ret = add_to_list(list, &sa.val[j].u.otherName.value); |
| 795 |
if (ret) { |
| 796 |
hx509_set_error_string(context, 0, ret, |
| 797 |
"Error adding an exra SAN to " |
| 798 |
"return list"); |
| 799 |
hx509_free_octet_string_list(list); |
| 800 |
free_GeneralNames(&sa); |
| 801 |
return ret; |
| 802 |
} |
| 803 |
} |
| 804 |
} |
| 805 |
free_GeneralNames(&sa); |
| 806 |
} |
| 807 |
} |
| 808 |
|
| 809 |
|
| 810 |
static int |
| 811 |
check_key_usage(hx509_context context, const Certificate *cert, |
| 812 |
unsigned flags, int req_present) |
| 813 |
{ |
| 814 |
const Extension *e; |
| 815 |
KeyUsage ku; |
| 816 |
size_t size; |
| 817 |
int ret; |
| 818 |
size_t i = 0; |
| 819 |
unsigned ku_flags; |
| 820 |
|
| 821 |
if (_hx509_cert_get_version(cert) < 3) |
| 822 |
return 0; |
| 823 |
|
| 824 |
e = find_extension(cert, &asn1_oid_id_x509_ce_keyUsage, &i); |
| 825 |
if (e == NULL) { |
| 826 |
if (req_present) { |
| 827 |
hx509_set_error_string(context, 0, HX509_KU_CERT_MISSING, |
| 828 |
"Required extension key " |
| 829 |
"usage missing from certifiate"); |
| 830 |
return HX509_KU_CERT_MISSING; |
| 831 |
} |
| 832 |
return 0; |
| 833 |
} |
| 834 |
|
| 835 |
ret = decode_KeyUsage(e->extnValue.data, e->extnValue.length, &ku, &size); |
| 836 |
if (ret) |
| 837 |
return ret; |
| 838 |
ku_flags = KeyUsage2int(ku); |
| 839 |
if ((ku_flags & flags) != flags) { |
| 840 |
unsigned missing = (~ku_flags) & flags; |
| 841 |
char buf[256], *name; |
| 842 |
|
| 843 |
unparse_flags(missing, asn1_KeyUsage_units(), buf, sizeof(buf)); |
| 844 |
_hx509_unparse_Name(&cert->tbsCertificate.subject, &name); |
| 845 |
hx509_set_error_string(context, 0, HX509_KU_CERT_MISSING, |
| 846 |
"Key usage %s required but missing " |
| 847 |
"from certifiate %s", buf, |
| 848 |
name ? name : "<unknown>"); |
| 849 |
free(name); |
| 850 |
return HX509_KU_CERT_MISSING; |
| 851 |
} |
| 852 |
return 0; |
| 853 |
} |
| 854 |
|
| 855 |
/* |
| 856 |
* Return 0 on matching key usage 'flags' for 'cert', otherwise return |
| 857 |
* an error code. If 'req_present' the existance is required of the |
| 858 |
* KeyUsage extension. |
| 859 |
*/ |
| 860 |
|
| 861 |
int |
| 862 |
_hx509_check_key_usage(hx509_context context, hx509_cert cert, |
| 863 |
unsigned flags, int req_present) |
| 864 |
{ |
| 865 |
return check_key_usage(context, _hx509_get_cert(cert), flags, req_present); |
| 866 |
} |
| 867 |
|
| 868 |
enum certtype { PROXY_CERT, EE_CERT, CA_CERT }; |
| 869 |
|
| 870 |
static int |
| 871 |
check_basic_constraints(hx509_context context, const Certificate *cert, |
| 872 |
enum certtype type, size_t depth) |
| 873 |
{ |
| 874 |
BasicConstraints bc; |
| 875 |
const Extension *e; |
| 876 |
size_t size; |
| 877 |
int ret; |
| 878 |
size_t i = 0; |
| 879 |
|
| 880 |
if (_hx509_cert_get_version(cert) < 3) |
| 881 |
return 0; |
| 882 |
|
| 883 |
e = find_extension(cert, &asn1_oid_id_x509_ce_basicConstraints, &i); |
| 884 |
if (e == NULL) { |
| 885 |
switch(type) { |
| 886 |
case PROXY_CERT: |
| 887 |
case EE_CERT: |
| 888 |
return 0; |
| 889 |
case CA_CERT: { |
| 890 |
char *name; |
| 891 |
ret = _hx509_unparse_Name(&cert->tbsCertificate.subject, &name); |
| 892 |
assert(ret == 0); |
| 893 |
hx509_set_error_string(context, 0, HX509_EXTENSION_NOT_FOUND, |
| 894 |
"basicConstraints missing from " |
| 895 |
"CA certifiacte %s", name); |
| 896 |
free(name); |
| 897 |
return HX509_EXTENSION_NOT_FOUND; |
| 898 |
} |
| 899 |
} |
| 900 |
} |
| 901 |
|
| 902 |
ret = decode_BasicConstraints(e->extnValue.data, |
| 903 |
e->extnValue.length, &bc, |
| 904 |
&size); |
| 905 |
if (ret) |
| 906 |
return ret; |
| 907 |
switch(type) { |
| 908 |
case PROXY_CERT: |
| 909 |
if (bc.cA != NULL && *bc.cA) |
| 910 |
ret = HX509_PARENT_IS_CA; |
| 911 |
break; |
| 912 |
case EE_CERT: |
| 913 |
ret = 0; |
| 914 |
break; |
| 915 |
case CA_CERT: |
| 916 |
if (bc.cA == NULL || !*bc.cA) |
| 917 |
ret = HX509_PARENT_NOT_CA; |
| 918 |
else if (bc.pathLenConstraint) |
| 919 |
if (depth - 1 > *bc.pathLenConstraint) |
| 920 |
ret = HX509_CA_PATH_TOO_DEEP; |
| 921 |
break; |
| 922 |
} |
| 923 |
free_BasicConstraints(&bc); |
| 924 |
return ret; |
| 925 |
} |
| 926 |
|
| 927 |
int |
| 928 |
_hx509_cert_is_parent_cmp(const Certificate *subject, |
| 929 |
const Certificate *issuer, |
| 930 |
int allow_self_signed) |
| 931 |
{ |
| 932 |
int diff; |
| 933 |
AuthorityKeyIdentifier ai; |
| 934 |
SubjectKeyIdentifier si; |
| 935 |
int ret_ai, ret_si, ret; |
| 936 |
|
| 937 |
ret = _hx509_name_cmp(&issuer->tbsCertificate.subject, |
| 938 |
&subject->tbsCertificate.issuer, |
| 939 |
&diff); |
| 940 |
if (ret) |
| 941 |
return ret; |
| 942 |
if (diff) |
| 943 |
return diff; |
| 944 |
|
| 945 |
memset(&ai, 0, sizeof(ai)); |
| 946 |
memset(&si, 0, sizeof(si)); |
| 947 |
|
| 948 |
/* |
| 949 |
* Try to find AuthorityKeyIdentifier, if it's not present in the |
| 950 |
* subject certificate nor the parent. |
| 951 |
*/ |
| 952 |
|
| 953 |
ret_ai = find_extension_auth_key_id(subject, &ai); |
| 954 |
if (ret_ai && ret_ai != HX509_EXTENSION_NOT_FOUND) |
| 955 |
return 1; |
| 956 |
ret_si = _hx509_find_extension_subject_key_id(issuer, &si); |
| 957 |
if (ret_si && ret_si != HX509_EXTENSION_NOT_FOUND) |
| 958 |
return -1; |
| 959 |
|
| 960 |
if (ret_si && ret_ai) |
| 961 |
goto out; |
| 962 |
if (ret_ai) |
| 963 |
goto out; |
| 964 |
if (ret_si) { |
| 965 |
if (allow_self_signed) { |
| 966 |
diff = 0; |
| 967 |
goto out; |
| 968 |
} else if (ai.keyIdentifier) { |
| 969 |
diff = -1; |
| 970 |
goto out; |
| 971 |
} |
| 972 |
} |
| 973 |
|
| 974 |
if (ai.keyIdentifier == NULL) { |
| 975 |
Name name; |
| 976 |
|
| 977 |
if (ai.authorityCertIssuer == NULL) |
| 978 |
return -1; |
| 979 |
if (ai.authorityCertSerialNumber == NULL) |
| 980 |
return -1; |
| 981 |
|
| 982 |
diff = der_heim_integer_cmp(ai.authorityCertSerialNumber, |
| 983 |
&issuer->tbsCertificate.serialNumber); |
| 984 |
if (diff) |
| 985 |
return diff; |
| 986 |
if (ai.authorityCertIssuer->len != 1) |
| 987 |
return -1; |
| 988 |
if (ai.authorityCertIssuer->val[0].element != choice_GeneralName_directoryName) |
| 989 |
return -1; |
| 990 |
|
| 991 |
name.element = (enum Name_enum) |
| 992 |
ai.authorityCertIssuer->val[0].u.directoryName.element; |
| 993 |
name.u.rdnSequence = |
| 994 |
ai.authorityCertIssuer->val[0].u.directoryName.u.rdnSequence; |
| 995 |
|
| 996 |
ret = _hx509_name_cmp(&issuer->tbsCertificate.subject, |
| 997 |
&name, |
| 998 |
&diff); |
| 999 |
if (ret) |
| 1000 |
return ret; |
| 1001 |
if (diff) |
| 1002 |
return diff; |
| 1003 |
diff = 0; |
| 1004 |
} else |
| 1005 |
diff = der_heim_octet_string_cmp(ai.keyIdentifier, &si); |
| 1006 |
if (diff) |
| 1007 |
goto out; |
| 1008 |
|
| 1009 |
out: |
| 1010 |
free_AuthorityKeyIdentifier(&ai); |
| 1011 |
free_SubjectKeyIdentifier(&si); |
| 1012 |
return diff; |
| 1013 |
} |
| 1014 |
|
| 1015 |
static int |
| 1016 |
certificate_is_anchor(hx509_context context, |
| 1017 |
hx509_certs trust_anchors, |
| 1018 |
const hx509_cert cert) |
| 1019 |
{ |
| 1020 |
hx509_query q; |
| 1021 |
hx509_cert c; |
| 1022 |
int ret; |
| 1023 |
|
| 1024 |
if (trust_anchors == NULL) |
| 1025 |
return 0; |
| 1026 |
|
| 1027 |
_hx509_query_clear(&q); |
| 1028 |
|
| 1029 |
q.match = HX509_QUERY_MATCH_CERTIFICATE; |
| 1030 |
q.certificate = _hx509_get_cert(cert); |
| 1031 |
|
| 1032 |
ret = hx509_certs_find(context, trust_anchors, &q, &c); |
| 1033 |
if (ret == 0) |
| 1034 |
hx509_cert_free(c); |
| 1035 |
return ret == 0; |
| 1036 |
} |
| 1037 |
|
| 1038 |
static int |
| 1039 |
certificate_is_self_signed(hx509_context context, |
| 1040 |
const Certificate *cert, |
| 1041 |
int *self_signed) |
| 1042 |
{ |
| 1043 |
int ret, diff; |
| 1044 |
ret = _hx509_name_cmp(&cert->tbsCertificate.subject, |
| 1045 |
&cert->tbsCertificate.issuer, &diff); |
| 1046 |
*self_signed = (diff == 0); |
| 1047 |
if (ret) { |
| 1048 |
hx509_set_error_string(context, 0, ret, |
| 1049 |
"Failed to check if self signed"); |
| 1050 |
} else |
| 1051 |
ret = _hx509_self_signed_valid(context, &cert->signatureAlgorithm); |
| 1052 |
|
| 1053 |
return ret; |
| 1054 |
} |
| 1055 |
|
| 1056 |
/* |
| 1057 |
* The subjectName is "null" when it's empty set of relative DBs. |
| 1058 |
*/ |
| 1059 |
|
| 1060 |
static int |
| 1061 |
subject_null_p(const Certificate *c) |
| 1062 |
{ |
| 1063 |
return c->tbsCertificate.subject.u.rdnSequence.len == 0; |
| 1064 |
} |
| 1065 |
|
| 1066 |
|
| 1067 |
static int |
| 1068 |
find_parent(hx509_context context, |
| 1069 |
time_t time_now, |
| 1070 |
hx509_certs trust_anchors, |
| 1071 |
hx509_path *path, |
| 1072 |
hx509_certs pool, |
| 1073 |
hx509_cert current, |
| 1074 |
hx509_cert *parent) |
| 1075 |
{ |
| 1076 |
AuthorityKeyIdentifier ai; |
| 1077 |
hx509_query q; |
| 1078 |
int ret; |
| 1079 |
|
| 1080 |
*parent = NULL; |
| 1081 |
memset(&ai, 0, sizeof(ai)); |
| 1082 |
|
| 1083 |
_hx509_query_clear(&q); |
| 1084 |
|
| 1085 |
if (!subject_null_p(current->data)) { |
| 1086 |
q.match |= HX509_QUERY_FIND_ISSUER_CERT; |
| 1087 |
q.subject = _hx509_get_cert(current); |
| 1088 |
} else { |
| 1089 |
ret = find_extension_auth_key_id(current->data, &ai); |
| 1090 |
if (ret) { |
| 1091 |
hx509_set_error_string(context, 0, HX509_CERTIFICATE_MALFORMED, |
| 1092 |
"Subjectless certificate missing AuthKeyID"); |
| 1093 |
return HX509_CERTIFICATE_MALFORMED; |
| 1094 |
} |
| 1095 |
|
| 1096 |
if (ai.keyIdentifier == NULL) { |
| 1097 |
free_AuthorityKeyIdentifier(&ai); |
| 1098 |
hx509_set_error_string(context, 0, HX509_CERTIFICATE_MALFORMED, |
| 1099 |
"Subjectless certificate missing keyIdentifier " |
| 1100 |
"inside AuthKeyID"); |
| 1101 |
return HX509_CERTIFICATE_MALFORMED; |
| 1102 |
} |
| 1103 |
|
| 1104 |
q.subject_id = ai.keyIdentifier; |
| 1105 |
q.match = HX509_QUERY_MATCH_SUBJECT_KEY_ID; |
| 1106 |
} |
| 1107 |
|
| 1108 |
q.path = path; |
| 1109 |
q.match |= HX509_QUERY_NO_MATCH_PATH; |
| 1110 |
|
| 1111 |
if (pool) { |
| 1112 |
q.timenow = time_now; |
| 1113 |
q.match |= HX509_QUERY_MATCH_TIME; |
| 1114 |
|
| 1115 |
ret = hx509_certs_find(context, pool, &q, parent); |
| 1116 |
if (ret == 0) { |
| 1117 |
free_AuthorityKeyIdentifier(&ai); |
| 1118 |
return 0; |
| 1119 |
} |
| 1120 |
q.match &= ~HX509_QUERY_MATCH_TIME; |
| 1121 |
} |
| 1122 |
|
| 1123 |
if (trust_anchors) { |
| 1124 |
ret = hx509_certs_find(context, trust_anchors, &q, parent); |
| 1125 |
if (ret == 0) { |
| 1126 |
free_AuthorityKeyIdentifier(&ai); |
| 1127 |
return ret; |
| 1128 |
} |
| 1129 |
} |
| 1130 |
free_AuthorityKeyIdentifier(&ai); |
| 1131 |
|
| 1132 |
{ |
| 1133 |
hx509_name name; |
| 1134 |
char *str; |
| 1135 |
|
| 1136 |
ret = hx509_cert_get_subject(current, &name); |
| 1137 |
if (ret) { |
| 1138 |
hx509_clear_error_string(context); |
| 1139 |
return HX509_ISSUER_NOT_FOUND; |
| 1140 |
} |
| 1141 |
ret = hx509_name_to_string(name, &str); |
| 1142 |
hx509_name_free(&name); |
| 1143 |
if (ret) { |
| 1144 |
hx509_clear_error_string(context); |
| 1145 |
return HX509_ISSUER_NOT_FOUND; |
| 1146 |
} |
| 1147 |
|
| 1148 |
hx509_set_error_string(context, 0, HX509_ISSUER_NOT_FOUND, |
| 1149 |
"Failed to find issuer for " |
| 1150 |
"certificate with subject: '%s'", str); |
| 1151 |
free(str); |
| 1152 |
} |
| 1153 |
return HX509_ISSUER_NOT_FOUND; |
| 1154 |
} |
| 1155 |
|
| 1156 |
/* |
| 1157 |
* |
| 1158 |
*/ |
| 1159 |
|
| 1160 |
static int |
| 1161 |
is_proxy_cert(hx509_context context, |
| 1162 |
const Certificate *cert, |
| 1163 |
ProxyCertInfo *rinfo) |
| 1164 |
{ |
| 1165 |
ProxyCertInfo info; |
| 1166 |
const Extension *e; |
| 1167 |
size_t size; |
| 1168 |
int ret; |
| 1169 |
size_t i = 0; |
| 1170 |
|
| 1171 |
if (rinfo) |
| 1172 |
memset(rinfo, 0, sizeof(*rinfo)); |
| 1173 |
|
| 1174 |
e = find_extension(cert, &asn1_oid_id_pkix_pe_proxyCertInfo, &i); |
| 1175 |
if (e == NULL) { |
| 1176 |
hx509_clear_error_string(context); |
| 1177 |
return HX509_EXTENSION_NOT_FOUND; |
| 1178 |
} |
| 1179 |
|
| 1180 |
ret = decode_ProxyCertInfo(e->extnValue.data, |
| 1181 |
e->extnValue.length, |
| 1182 |
&info, |
| 1183 |
&size); |
| 1184 |
if (ret) { |
| 1185 |
hx509_clear_error_string(context); |
| 1186 |
return ret; |
| 1187 |
} |
| 1188 |
if (size != e->extnValue.length) { |
| 1189 |
free_ProxyCertInfo(&info); |
| 1190 |
hx509_clear_error_string(context); |
| 1191 |
return HX509_EXTRA_DATA_AFTER_STRUCTURE; |
| 1192 |
} |
| 1193 |
if (rinfo == NULL) |
| 1194 |
free_ProxyCertInfo(&info); |
| 1195 |
else |
| 1196 |
*rinfo = info; |
| 1197 |
|
| 1198 |
return 0; |
| 1199 |
} |
| 1200 |
|
| 1201 |
/* |
| 1202 |
* Path operations are like MEMORY based keyset, but with exposed |
| 1203 |
* internal so we can do easy searches. |
| 1204 |
*/ |
| 1205 |
|
| 1206 |
int |
| 1207 |
_hx509_path_append(hx509_context context, hx509_path *path, hx509_cert cert) |
| 1208 |
{ |
| 1209 |
hx509_cert *val; |
| 1210 |
val = realloc(path->val, (path->len + 1) * sizeof(path->val[0])); |
| 1211 |
if (val == NULL) { |
| 1212 |
hx509_set_error_string(context, 0, ENOMEM, "out of memory"); |
| 1213 |
return ENOMEM; |
| 1214 |
} |
| 1215 |
|
| 1216 |
path->val = val; |
| 1217 |
path->val[path->len] = hx509_cert_ref(cert); |
| 1218 |
path->len++; |
| 1219 |
|
| 1220 |
return 0; |
| 1221 |
} |
| 1222 |
|
| 1223 |
void |
| 1224 |
_hx509_path_free(hx509_path *path) |
| 1225 |
{ |
| 1226 |
unsigned i; |
| 1227 |
|
| 1228 |
for (i = 0; i < path->len; i++) |
| 1229 |
hx509_cert_free(path->val[i]); |
| 1230 |
free(path->val); |
| 1231 |
path->val = NULL; |
| 1232 |
path->len = 0; |
| 1233 |
} |
| 1234 |
|
| 1235 |
/* |
| 1236 |
* Find path by looking up issuer for the top certificate and continue |
| 1237 |
* until an anchor certificate is found or max limit is found. A |
| 1238 |
* certificate never included twice in the path. |
| 1239 |
* |
| 1240 |
* If the trust anchors are not given, calculate optimistic path, just |
| 1241 |
* follow the chain upward until we no longer find a parent or we hit |
| 1242 |
* the max path limit. In this case, a failure will always be returned |
| 1243 |
* depending on what error condition is hit first. |
| 1244 |
* |
| 1245 |
* The path includes a path from the top certificate to the anchor |
| 1246 |
* certificate. |
| 1247 |
* |
| 1248 |
* The caller needs to free `path´ both on successful built path and |
| 1249 |
* failure. |
| 1250 |
*/ |
| 1251 |
|
| 1252 |
int |
| 1253 |
_hx509_calculate_path(hx509_context context, |
| 1254 |
int flags, |
| 1255 |
time_t time_now, |
| 1256 |
hx509_certs anchors, |
| 1257 |
unsigned int max_depth, |
| 1258 |
hx509_cert cert, |
| 1259 |
hx509_certs pool, |
| 1260 |
hx509_path *path) |
| 1261 |
{ |
| 1262 |
hx509_cert parent, current; |
| 1263 |
int ret; |
| 1264 |
|
| 1265 |
if (max_depth == 0) |
| 1266 |
max_depth = HX509_VERIFY_MAX_DEPTH; |
| 1267 |
|
| 1268 |
ret = _hx509_path_append(context, path, cert); |
| 1269 |
if (ret) |
| 1270 |
return ret; |
| 1271 |
|
| 1272 |
current = hx509_cert_ref(cert); |
| 1273 |
|
| 1274 |
while (!certificate_is_anchor(context, anchors, current)) { |
| 1275 |
|
| 1276 |
ret = find_parent(context, time_now, anchors, path, |
| 1277 |
pool, current, &parent); |
| 1278 |
hx509_cert_free(current); |
| 1279 |
if (ret) |
| 1280 |
return ret; |
| 1281 |
|
| 1282 |
ret = _hx509_path_append(context, path, parent); |
| 1283 |
if (ret) |
| 1284 |
return ret; |
| 1285 |
current = parent; |
| 1286 |
|
| 1287 |
if (path->len > max_depth) { |
| 1288 |
hx509_cert_free(current); |
| 1289 |
hx509_set_error_string(context, 0, HX509_PATH_TOO_LONG, |
| 1290 |
"Path too long while bulding " |
| 1291 |
"certificate chain"); |
| 1292 |
return HX509_PATH_TOO_LONG; |
| 1293 |
} |
| 1294 |
} |
| 1295 |
|
| 1296 |
if ((flags & HX509_CALCULATE_PATH_NO_ANCHOR) && |
| 1297 |
path->len > 0 && |
| 1298 |
certificate_is_anchor(context, anchors, path->val[path->len - 1])) |
| 1299 |
{ |
| 1300 |
hx509_cert_free(path->val[path->len - 1]); |
| 1301 |
path->len--; |
| 1302 |
} |
| 1303 |
|
| 1304 |
hx509_cert_free(current); |
| 1305 |
return 0; |
| 1306 |
} |
| 1307 |
|
| 1308 |
int |
| 1309 |
_hx509_AlgorithmIdentifier_cmp(const AlgorithmIdentifier *p, |
| 1310 |
const AlgorithmIdentifier *q) |
| 1311 |
{ |
| 1312 |
int diff; |
| 1313 |
diff = der_heim_oid_cmp(&p->algorithm, &q->algorithm); |
| 1314 |
if (diff) |
| 1315 |
return diff; |
| 1316 |
if (p->parameters) { |
| 1317 |
if (q->parameters) |
| 1318 |
return heim_any_cmp(p->parameters, |
| 1319 |
q->parameters); |
| 1320 |
else |
| 1321 |
return 1; |
| 1322 |
} else { |
| 1323 |
if (q->parameters) |
| 1324 |
return -1; |
| 1325 |
else |
| 1326 |
return 0; |
| 1327 |
} |
| 1328 |
} |
| 1329 |
|
| 1330 |
int |
| 1331 |
_hx509_Certificate_cmp(const Certificate *p, const Certificate *q) |
| 1332 |
{ |
| 1333 |
int diff; |
| 1334 |
diff = der_heim_bit_string_cmp(&p->signatureValue, &q->signatureValue); |
| 1335 |
if (diff) |
| 1336 |
return diff; |
| 1337 |
diff = _hx509_AlgorithmIdentifier_cmp(&p->signatureAlgorithm, |
| 1338 |
&q->signatureAlgorithm); |
| 1339 |
if (diff) |
| 1340 |
return diff; |
| 1341 |
diff = der_heim_octet_string_cmp(&p->tbsCertificate._save, |
| 1342 |
&q->tbsCertificate._save); |
| 1343 |
return diff; |
| 1344 |
} |
| 1345 |
|
| 1346 |
/** |
| 1347 |
* Compare to hx509 certificate object, useful for sorting. |
| 1348 |
* |
| 1349 |
* @param p a hx509 certificate object. |
| 1350 |
* @param q a hx509 certificate object. |
| 1351 |
* |
| 1352 |
* @return 0 the objects are the same, returns > 0 is p is "larger" |
| 1353 |
* then q, < 0 if p is "smaller" then q. |
| 1354 |
* |
| 1355 |
* @ingroup hx509_cert |
| 1356 |
*/ |
| 1357 |
|
| 1358 |
int |
| 1359 |
hx509_cert_cmp(hx509_cert p, hx509_cert q) |
| 1360 |
{ |
| 1361 |
return _hx509_Certificate_cmp(p->data, q->data); |
| 1362 |
} |
| 1363 |
|
| 1364 |
/** |
| 1365 |
* Return the name of the issuer of the hx509 certificate. |
| 1366 |
* |
| 1367 |
* @param p a hx509 certificate object. |
| 1368 |
* @param name a pointer to a hx509 name, should be freed by |
| 1369 |
* hx509_name_free(). |
| 1370 |
* |
| 1371 |
* @return An hx509 error code, see hx509_get_error_string(). |
| 1372 |
* |
| 1373 |
* @ingroup hx509_cert |
| 1374 |
*/ |
| 1375 |
|
| 1376 |
int |
| 1377 |
hx509_cert_get_issuer(hx509_cert p, hx509_name *name) |
| 1378 |
{ |
| 1379 |
return _hx509_name_from_Name(&p->data->tbsCertificate.issuer, name); |
| 1380 |
} |
| 1381 |
|
| 1382 |
/** |
| 1383 |
* Return the name of the subject of the hx509 certificate. |
| 1384 |
* |
| 1385 |
* @param p a hx509 certificate object. |
| 1386 |
* @param name a pointer to a hx509 name, should be freed by |
| 1387 |
* hx509_name_free(). See also hx509_cert_get_base_subject(). |
| 1388 |
* |
| 1389 |
* @return An hx509 error code, see hx509_get_error_string(). |
| 1390 |
* |
| 1391 |
* @ingroup hx509_cert |
| 1392 |
*/ |
| 1393 |
|
| 1394 |
int |
| 1395 |
hx509_cert_get_subject(hx509_cert p, hx509_name *name) |
| 1396 |
{ |
| 1397 |
return _hx509_name_from_Name(&p->data->tbsCertificate.subject, name); |
| 1398 |
} |
| 1399 |
|
| 1400 |
/** |
| 1401 |
* Return the name of the base subject of the hx509 certificate. If |
| 1402 |
* the certiicate is a verified proxy certificate, the this function |
| 1403 |
* return the base certificate (root of the proxy chain). If the proxy |
| 1404 |
* certificate is not verified with the base certificate |
| 1405 |
* HX509_PROXY_CERTIFICATE_NOT_CANONICALIZED is returned. |
| 1406 |
* |
| 1407 |
* @param context a hx509 context. |
| 1408 |
* @param c a hx509 certificate object. |
| 1409 |
* @param name a pointer to a hx509 name, should be freed by |
| 1410 |
* hx509_name_free(). See also hx509_cert_get_subject(). |
| 1411 |
* |
| 1412 |
* @return An hx509 error code, see hx509_get_error_string(). |
| 1413 |
* |
| 1414 |
* @ingroup hx509_cert |
| 1415 |
*/ |
| 1416 |
|
| 1417 |
int |
| 1418 |
hx509_cert_get_base_subject(hx509_context context, hx509_cert c, |
| 1419 |
hx509_name *name) |
| 1420 |
{ |
| 1421 |
if (c->basename) |
| 1422 |
return hx509_name_copy(context, c->basename, name); |
| 1423 |
if (is_proxy_cert(context, c->data, NULL) == 0) { |
| 1424 |
int ret = HX509_PROXY_CERTIFICATE_NOT_CANONICALIZED; |
| 1425 |
hx509_set_error_string(context, 0, ret, |
| 1426 |
"Proxy certificate have not been " |
| 1427 |
"canonicalize yet, no base name"); |
| 1428 |
return ret; |
| 1429 |
} |
| 1430 |
return _hx509_name_from_Name(&c->data->tbsCertificate.subject, name); |
| 1431 |
} |
| 1432 |
|
| 1433 |
/** |
| 1434 |
* Get serial number of the certificate. |
| 1435 |
* |
| 1436 |
* @param p a hx509 certificate object. |
| 1437 |
* @param i serial number, should be freed ith der_free_heim_integer(). |
| 1438 |
* |
| 1439 |
* @return An hx509 error code, see hx509_get_error_string(). |
| 1440 |
* |
| 1441 |
* @ingroup hx509_cert |
| 1442 |
*/ |
| 1443 |
|
| 1444 |
int |
| 1445 |
hx509_cert_get_serialnumber(hx509_cert p, heim_integer *i) |
| 1446 |
{ |
| 1447 |
return der_copy_heim_integer(&p->data->tbsCertificate.serialNumber, i); |
| 1448 |
} |
| 1449 |
|
| 1450 |
/** |
| 1451 |
* Get notBefore time of the certificate. |
| 1452 |
* |
| 1453 |
* @param p a hx509 certificate object. |
| 1454 |
* |
| 1455 |
* @return return not before time |
| 1456 |
* |
| 1457 |
* @ingroup hx509_cert |
| 1458 |
*/ |
| 1459 |
|
| 1460 |
time_t |
| 1461 |
hx509_cert_get_notBefore(hx509_cert p) |
| 1462 |
{ |
| 1463 |
return _hx509_Time2time_t(&p->data->tbsCertificate.validity.notBefore); |
| 1464 |
} |
| 1465 |
|
| 1466 |
/** |
| 1467 |
* Get notAfter time of the certificate. |
| 1468 |
* |
| 1469 |
* @param p a hx509 certificate object. |
| 1470 |
* |
| 1471 |
* @return return not after time. |
| 1472 |
* |
| 1473 |
* @ingroup hx509_cert |
| 1474 |
*/ |
| 1475 |
|
| 1476 |
time_t |
| 1477 |
hx509_cert_get_notAfter(hx509_cert p) |
| 1478 |
{ |
| 1479 |
return _hx509_Time2time_t(&p->data->tbsCertificate.validity.notAfter); |
| 1480 |
} |
| 1481 |
|
| 1482 |
/** |
| 1483 |
* Get the SubjectPublicKeyInfo structure from the hx509 certificate. |
| 1484 |
* |
| 1485 |
* @param context a hx509 context. |
| 1486 |
* @param p a hx509 certificate object. |
| 1487 |
* @param spki SubjectPublicKeyInfo, should be freed with |
| 1488 |
* free_SubjectPublicKeyInfo(). |
| 1489 |
* |
| 1490 |
* @return An hx509 error code, see hx509_get_error_string(). |
| 1491 |
* |
| 1492 |
* @ingroup hx509_cert |
| 1493 |
*/ |
| 1494 |
|
| 1495 |
int |
| 1496 |
hx509_cert_get_SPKI(hx509_context context, hx509_cert p, SubjectPublicKeyInfo *spki) |
| 1497 |
{ |
| 1498 |
int ret; |
| 1499 |
|
| 1500 |
ret = copy_SubjectPublicKeyInfo(&p->data->tbsCertificate.subjectPublicKeyInfo, spki); |
| 1501 |
if (ret) |
| 1502 |
hx509_set_error_string(context, 0, ret, "Failed to copy SPKI"); |
| 1503 |
return ret; |
| 1504 |
} |
| 1505 |
|
| 1506 |
/** |
| 1507 |
* Get the AlgorithmIdentifier from the hx509 certificate. |
| 1508 |
* |
| 1509 |
* @param context a hx509 context. |
| 1510 |
* @param p a hx509 certificate object. |
| 1511 |
* @param alg AlgorithmIdentifier, should be freed with |
| 1512 |
* free_AlgorithmIdentifier(). The algorithmidentifier is |
| 1513 |
* typicly rsaEncryption, or id-ecPublicKey, or some other |
| 1514 |
* public key mechanism. |
| 1515 |
* |
| 1516 |
* @return An hx509 error code, see hx509_get_error_string(). |
| 1517 |
* |
| 1518 |
* @ingroup hx509_cert |
| 1519 |
*/ |
| 1520 |
|
| 1521 |
int |
| 1522 |
hx509_cert_get_SPKI_AlgorithmIdentifier(hx509_context context, |
| 1523 |
hx509_cert p, |
| 1524 |
AlgorithmIdentifier *alg) |
| 1525 |
{ |
| 1526 |
int ret; |
| 1527 |
|
| 1528 |
ret = copy_AlgorithmIdentifier(&p->data->tbsCertificate.subjectPublicKeyInfo.algorithm, alg); |
| 1529 |
if (ret) |
| 1530 |
hx509_set_error_string(context, 0, ret, |
| 1531 |
"Failed to copy SPKI AlgorithmIdentifier"); |
| 1532 |
return ret; |
| 1533 |
} |
| 1534 |
|
| 1535 |
static int |
| 1536 |
get_x_unique_id(hx509_context context, const char *name, |
| 1537 |
const heim_bit_string *cert, heim_bit_string *subject) |
| 1538 |
{ |
| 1539 |
int ret; |
| 1540 |
|
| 1541 |
if (cert == NULL) { |
| 1542 |
ret = HX509_EXTENSION_NOT_FOUND; |
| 1543 |
hx509_set_error_string(context, 0, ret, "%s unique id doesn't exists", name); |
| 1544 |
return ret; |
| 1545 |
} |
| 1546 |
ret = der_copy_bit_string(cert, subject); |
| 1547 |
if (ret) { |
| 1548 |
hx509_set_error_string(context, 0, ret, "malloc out of memory", name); |
| 1549 |
return ret; |
| 1550 |
} |
| 1551 |
return 0; |
| 1552 |
} |
| 1553 |
|
| 1554 |
/** |
| 1555 |
* Get a copy of the Issuer Unique ID |
| 1556 |
* |
| 1557 |
* @param context a hx509_context |
| 1558 |
* @param p a hx509 certificate |
| 1559 |
* @param issuer the issuer id returned, free with der_free_bit_string() |
| 1560 |
* |
| 1561 |
* @return An hx509 error code, see hx509_get_error_string(). The |
| 1562 |
* error code HX509_EXTENSION_NOT_FOUND is returned if the certificate |
| 1563 |
* doesn't have a issuerUniqueID |
| 1564 |
* |
| 1565 |
* @ingroup hx509_cert |
| 1566 |
*/ |
| 1567 |
|
| 1568 |
int |
| 1569 |
hx509_cert_get_issuer_unique_id(hx509_context context, hx509_cert p, heim_bit_string *issuer) |
| 1570 |
{ |
| 1571 |
return get_x_unique_id(context, "issuer", p->data->tbsCertificate.issuerUniqueID, issuer); |
| 1572 |
} |
| 1573 |
|
| 1574 |
/** |
| 1575 |
* Get a copy of the Subect Unique ID |
| 1576 |
* |
| 1577 |
* @param context a hx509_context |
| 1578 |
* @param p a hx509 certificate |
| 1579 |
* @param subject the subject id returned, free with der_free_bit_string() |
| 1580 |
* |
| 1581 |
* @return An hx509 error code, see hx509_get_error_string(). The |
| 1582 |
* error code HX509_EXTENSION_NOT_FOUND is returned if the certificate |
| 1583 |
* doesn't have a subjectUniqueID |
| 1584 |
* |
| 1585 |
* @ingroup hx509_cert |
| 1586 |
*/ |
| 1587 |
|
| 1588 |
int |
| 1589 |
hx509_cert_get_subject_unique_id(hx509_context context, hx509_cert p, heim_bit_string *subject) |
| 1590 |
{ |
| 1591 |
return get_x_unique_id(context, "subject", p->data->tbsCertificate.subjectUniqueID, subject); |
| 1592 |
} |
| 1593 |
|
| 1594 |
|
| 1595 |
hx509_private_key |
| 1596 |
_hx509_cert_private_key(hx509_cert p) |
| 1597 |
{ |
| 1598 |
return p->private_key; |
| 1599 |
} |
| 1600 |
|
| 1601 |
int |
| 1602 |
hx509_cert_have_private_key(hx509_cert p) |
| 1603 |
{ |
| 1604 |
return p->private_key ? 1 : 0; |
| 1605 |
} |
| 1606 |
|
| 1607 |
|
| 1608 |
int |
| 1609 |
_hx509_cert_private_key_exportable(hx509_cert p) |
| 1610 |
{ |
| 1611 |
if (p->private_key == NULL) |
| 1612 |
return 0; |
| 1613 |
return _hx509_private_key_exportable(p->private_key); |
| 1614 |
} |
| 1615 |
|
| 1616 |
int |
| 1617 |
_hx509_cert_private_decrypt(hx509_context context, |
| 1618 |
const heim_octet_string *ciphertext, |
| 1619 |
const heim_oid *encryption_oid, |
| 1620 |
hx509_cert p, |
| 1621 |
heim_octet_string *cleartext) |
| 1622 |
{ |
| 1623 |
cleartext->data = NULL; |
| 1624 |
cleartext->length = 0; |
| 1625 |
|
| 1626 |
if (p->private_key == NULL) { |
| 1627 |
hx509_set_error_string(context, 0, HX509_PRIVATE_KEY_MISSING, |
| 1628 |
"Private key missing"); |
| 1629 |
return HX509_PRIVATE_KEY_MISSING; |
| 1630 |
} |
| 1631 |
|
| 1632 |
return hx509_private_key_private_decrypt(context, |
| 1633 |
ciphertext, |
| 1634 |
encryption_oid, |
| 1635 |
p->private_key, |
| 1636 |
cleartext); |
| 1637 |
} |
| 1638 |
|
| 1639 |
int |
| 1640 |
hx509_cert_public_encrypt(hx509_context context, |
| 1641 |
const heim_octet_string *cleartext, |
| 1642 |
const hx509_cert p, |
| 1643 |
heim_oid *encryption_oid, |
| 1644 |
heim_octet_string *ciphertext) |
| 1645 |
{ |
| 1646 |
return _hx509_public_encrypt(context, |
| 1647 |
cleartext, p->data, |
| 1648 |
encryption_oid, ciphertext); |
| 1649 |
} |
| 1650 |
|
| 1651 |
/* |
| 1652 |
* |
| 1653 |
*/ |
| 1654 |
|
| 1655 |
time_t |
| 1656 |
_hx509_Time2time_t(const Time *t) |
| 1657 |
{ |
| 1658 |
switch(t->element) { |
| 1659 |
case choice_Time_utcTime: |
| 1660 |
return t->u.utcTime; |
| 1661 |
case choice_Time_generalTime: |
| 1662 |
return t->u.generalTime; |
| 1663 |
} |
| 1664 |
return 0; |
| 1665 |
} |
| 1666 |
|
| 1667 |
/* |
| 1668 |
* |
| 1669 |
*/ |
| 1670 |
|
| 1671 |
static int |
| 1672 |
init_name_constraints(hx509_name_constraints *nc) |
| 1673 |
{ |
| 1674 |
memset(nc, 0, sizeof(*nc)); |
| 1675 |
return 0; |
| 1676 |
} |
| 1677 |
|
| 1678 |
static int |
| 1679 |
add_name_constraints(hx509_context context, const Certificate *c, int not_ca, |
| 1680 |
hx509_name_constraints *nc) |
| 1681 |
{ |
| 1682 |
NameConstraints tnc; |
| 1683 |
int ret; |
| 1684 |
|
| 1685 |
ret = find_extension_name_constraints(c, &tnc); |
| 1686 |
if (ret == HX509_EXTENSION_NOT_FOUND) |
| 1687 |
return 0; |
| 1688 |
else if (ret) { |
| 1689 |
hx509_set_error_string(context, 0, ret, "Failed getting NameConstraints"); |
| 1690 |
return ret; |
| 1691 |
} else if (not_ca) { |
| 1692 |
ret = HX509_VERIFY_CONSTRAINTS; |
| 1693 |
hx509_set_error_string(context, 0, ret, "Not a CA and " |
| 1694 |
"have NameConstraints"); |
| 1695 |
} else { |
| 1696 |
NameConstraints *val; |
| 1697 |
val = realloc(nc->val, sizeof(nc->val[0]) * (nc->len + 1)); |
| 1698 |
if (val == NULL) { |
| 1699 |
hx509_clear_error_string(context); |
| 1700 |
ret = ENOMEM; |
| 1701 |
goto out; |
| 1702 |
} |
| 1703 |
nc->val = val; |
| 1704 |
ret = copy_NameConstraints(&tnc, &nc->val[nc->len]); |
| 1705 |
if (ret) { |
| 1706 |
hx509_clear_error_string(context); |
| 1707 |
goto out; |
| 1708 |
} |
| 1709 |
nc->len += 1; |
| 1710 |
} |
| 1711 |
out: |
| 1712 |
free_NameConstraints(&tnc); |
| 1713 |
return ret; |
| 1714 |
} |
| 1715 |
|
| 1716 |
static int |
| 1717 |
match_RDN(const RelativeDistinguishedName *c, |
| 1718 |
const RelativeDistinguishedName *n) |
| 1719 |
{ |
| 1720 |
size_t i; |
| 1721 |
|
| 1722 |
if (c->len != n->len) |
| 1723 |
return HX509_NAME_CONSTRAINT_ERROR; |
| 1724 |
|
| 1725 |
for (i = 0; i < n->len; i++) { |
| 1726 |
int diff, ret; |
| 1727 |
|
| 1728 |
if (der_heim_oid_cmp(&c->val[i].type, &n->val[i].type) != 0) |
| 1729 |
return HX509_NAME_CONSTRAINT_ERROR; |
| 1730 |
ret = _hx509_name_ds_cmp(&c->val[i].value, &n->val[i].value, &diff); |
| 1731 |
if (ret) |
| 1732 |
return ret; |
| 1733 |
if (diff != 0) |
| 1734 |
return HX509_NAME_CONSTRAINT_ERROR; |
| 1735 |
} |
| 1736 |
return 0; |
| 1737 |
} |
| 1738 |
|
| 1739 |
static int |
| 1740 |
match_X501Name(const Name *c, const Name *n) |
| 1741 |
{ |
| 1742 |
size_t i; |
| 1743 |
int ret; |
| 1744 |
|
| 1745 |
if (c->element != choice_Name_rdnSequence |
| 1746 |
|| n->element != choice_Name_rdnSequence) |
| 1747 |
return 0; |
| 1748 |
if (c->u.rdnSequence.len > n->u.rdnSequence.len) |
| 1749 |
return HX509_NAME_CONSTRAINT_ERROR; |
| 1750 |
for (i = 0; i < c->u.rdnSequence.len; i++) { |
| 1751 |
ret = match_RDN(&c->u.rdnSequence.val[i], &n->u.rdnSequence.val[i]); |
| 1752 |
if (ret) |
| 1753 |
return ret; |
| 1754 |
} |
| 1755 |
return 0; |
| 1756 |
} |
| 1757 |
|
| 1758 |
|
| 1759 |
static int |
| 1760 |
match_general_name(const GeneralName *c, const GeneralName *n, int *match) |
| 1761 |
{ |
| 1762 |
/* |
| 1763 |
* Name constraints only apply to the same name type, see RFC3280, |
| 1764 |
* 4.2.1.11. |
| 1765 |
*/ |
| 1766 |
assert(c->element == n->element); |
| 1767 |
|
| 1768 |
switch(c->element) { |
| 1769 |
case choice_GeneralName_otherName: |
| 1770 |
if (der_heim_oid_cmp(&c->u.otherName.type_id, |
| 1771 |
&n->u.otherName.type_id) != 0) |
| 1772 |
return HX509_NAME_CONSTRAINT_ERROR; |
| 1773 |
if (heim_any_cmp(&c->u.otherName.value, |
| 1774 |
&n->u.otherName.value) != 0) |
| 1775 |
return HX509_NAME_CONSTRAINT_ERROR; |
| 1776 |
*match = 1; |
| 1777 |
return 0; |
| 1778 |
case choice_GeneralName_rfc822Name: { |
| 1779 |
const char *s; |
| 1780 |
size_t len1, len2; |
| 1781 |
s = memchr(c->u.rfc822Name.data, '@', c->u.rfc822Name.length); |
| 1782 |
if (s) { |
| 1783 |
if (der_printable_string_cmp(&c->u.rfc822Name, &n->u.rfc822Name) != 0) |
| 1784 |
return HX509_NAME_CONSTRAINT_ERROR; |
| 1785 |
} else { |
| 1786 |
s = memchr(n->u.rfc822Name.data, '@', n->u.rfc822Name.length); |
| 1787 |
if (s == NULL) |
| 1788 |
return HX509_NAME_CONSTRAINT_ERROR; |
| 1789 |
len1 = c->u.rfc822Name.length; |
| 1790 |
len2 = n->u.rfc822Name.length - |
| 1791 |
(s - ((char *)n->u.rfc822Name.data)); |
| 1792 |
if (len1 > len2) |
| 1793 |
return HX509_NAME_CONSTRAINT_ERROR; |
| 1794 |
if (memcmp(s + 1 + len2 - len1, c->u.rfc822Name.data, len1) != 0) |
| 1795 |
return HX509_NAME_CONSTRAINT_ERROR; |
| 1796 |
if (len1 < len2 && s[len2 - len1 + 1] != '.') |
| 1797 |
return HX509_NAME_CONSTRAINT_ERROR; |
| 1798 |
} |
| 1799 |
*match = 1; |
| 1800 |
return 0; |
| 1801 |
} |
| 1802 |
case choice_GeneralName_dNSName: { |
| 1803 |
size_t lenc, lenn; |
| 1804 |
char *ptr; |
| 1805 |
|
| 1806 |
lenc = c->u.dNSName.length; |
| 1807 |
lenn = n->u.dNSName.length; |
| 1808 |
if (lenc > lenn) |
| 1809 |
return HX509_NAME_CONSTRAINT_ERROR; |
| 1810 |
ptr = n->u.dNSName.data; |
| 1811 |
if (memcmp(&ptr[lenn - lenc], c->u.dNSName.data, lenc) != 0) |
| 1812 |
return HX509_NAME_CONSTRAINT_ERROR; |
| 1813 |
if (lenn != lenc && ptr[lenn - lenc - 1] != '.') |
| 1814 |
return HX509_NAME_CONSTRAINT_ERROR; |
| 1815 |
*match = 1; |
| 1816 |
return 0; |
| 1817 |
} |
| 1818 |
case choice_GeneralName_directoryName: { |
| 1819 |
Name c_name, n_name; |
| 1820 |
int ret; |
| 1821 |
|
| 1822 |
c_name._save.data = NULL; |
| 1823 |
c_name._save.length = 0; |
| 1824 |
c_name.element = (enum Name_enum)c->u.directoryName.element; |
| 1825 |
c_name.u.rdnSequence = c->u.directoryName.u.rdnSequence; |
| 1826 |
|
| 1827 |
n_name._save.data = NULL; |
| 1828 |
n_name._save.length = 0; |
| 1829 |
n_name.element = (enum Name_enum)n->u.directoryName.element; |
| 1830 |
n_name.u.rdnSequence = n->u.directoryName.u.rdnSequence; |
| 1831 |
|
| 1832 |
ret = match_X501Name(&c_name, &n_name); |
| 1833 |
if (ret == 0) |
| 1834 |
*match = 1; |
| 1835 |
return ret; |
| 1836 |
} |
| 1837 |
case choice_GeneralName_uniformResourceIdentifier: |
| 1838 |
case choice_GeneralName_iPAddress: |
| 1839 |
case choice_GeneralName_registeredID: |
| 1840 |
default: |
| 1841 |
return HX509_NAME_CONSTRAINT_ERROR; |
| 1842 |
} |
| 1843 |
} |
| 1844 |
|
| 1845 |
static int |
| 1846 |
match_alt_name(const GeneralName *n, const Certificate *c, |
| 1847 |
int *same, int *match) |
| 1848 |
{ |
| 1849 |
GeneralNames sa; |
| 1850 |
int ret = 0; |
| 1851 |
size_t i, j; |
| 1852 |
|
| 1853 |
i = 0; |
| 1854 |
do { |
| 1855 |
ret = find_extension_subject_alt_name(c, &i, &sa); |
| 1856 |
if (ret == HX509_EXTENSION_NOT_FOUND) { |
| 1857 |
ret = 0; |
| 1858 |
break; |
| 1859 |
} else if (ret != 0) |
| 1860 |
break; |
| 1861 |
|
| 1862 |
for (j = 0; j < sa.len; j++) { |
| 1863 |
if (n->element == sa.val[j].element) { |
| 1864 |
*same = 1; |
| 1865 |
match_general_name(n, &sa.val[j], match); |
| 1866 |
} |
| 1867 |
} |
| 1868 |
free_GeneralNames(&sa); |
| 1869 |
} while (1); |
| 1870 |
return ret; |
| 1871 |
} |
| 1872 |
|
| 1873 |
|
| 1874 |
static int |
| 1875 |
match_tree(const GeneralSubtrees *t, const Certificate *c, int *match) |
| 1876 |
{ |
| 1877 |
int name, alt_name, same; |
| 1878 |
unsigned int i; |
| 1879 |
int ret = 0; |
| 1880 |
|
| 1881 |
name = alt_name = same = *match = 0; |
| 1882 |
for (i = 0; i < t->len; i++) { |
| 1883 |
if (t->val[i].minimum && t->val[i].maximum) |
| 1884 |
return HX509_RANGE; |
| 1885 |
|
| 1886 |
/* |
| 1887 |
* If the constraint apply to directoryNames, test is with |
| 1888 |
* subjectName of the certificate if the certificate have a |
| 1889 |
* non-null (empty) subjectName. |
| 1890 |
*/ |
| 1891 |
|
| 1892 |
if (t->val[i].base.element == choice_GeneralName_directoryName |
| 1893 |
&& !subject_null_p(c)) |
| 1894 |
{ |
| 1895 |
GeneralName certname; |
| 1896 |
|
| 1897 |
memset(&certname, 0, sizeof(certname)); |
| 1898 |
certname.element = choice_GeneralName_directoryName; |
| 1899 |
certname.u.directoryName.element = (enum GeneralName_directoryName_enum) |
| 1900 |
c->tbsCertificate.subject.element; |
| 1901 |
certname.u.directoryName.u.rdnSequence = |
| 1902 |
c->tbsCertificate.subject.u.rdnSequence; |
| 1903 |
|
| 1904 |
match_general_name(&t->val[i].base, &certname, &name); |
| 1905 |
} |
| 1906 |
|
| 1907 |
/* Handle subjectAltNames, this is icky since they |
| 1908 |
* restrictions only apply if the subjectAltName is of the |
| 1909 |
* same type. So if there have been a match of type, require |
| 1910 |
* altname to be set. |
| 1911 |
*/ |
| 1912 |
match_alt_name(&t->val[i].base, c, &same, &alt_name); |
| 1913 |
} |
| 1914 |
if (name && (!same || alt_name)) |
| 1915 |
*match = 1; |
| 1916 |
return ret; |
| 1917 |
} |
| 1918 |
|
| 1919 |
static int |
| 1920 |
check_name_constraints(hx509_context context, |
| 1921 |
const hx509_name_constraints *nc, |
| 1922 |
const Certificate *c) |
| 1923 |
{ |
| 1924 |
int match, ret; |
| 1925 |
size_t i; |
| 1926 |
|
| 1927 |
for (i = 0 ; i < nc->len; i++) { |
| 1928 |
GeneralSubtrees gs; |
| 1929 |
|
| 1930 |
if (nc->val[i].permittedSubtrees) { |
| 1931 |
GeneralSubtrees_SET(&gs, nc->val[i].permittedSubtrees); |
| 1932 |
ret = match_tree(&gs, c, &match); |
| 1933 |
if (ret) { |
| 1934 |
hx509_clear_error_string(context); |
| 1935 |
return ret; |
| 1936 |
} |
| 1937 |
/* allow null subjectNames, they wont matches anything */ |
| 1938 |
if (match == 0 && !subject_null_p(c)) { |
| 1939 |
hx509_set_error_string(context, 0, HX509_VERIFY_CONSTRAINTS, |
| 1940 |
"Error verify constraints, " |
| 1941 |
"certificate didn't match any " |
| 1942 |
"permitted subtree"); |
| 1943 |
return HX509_VERIFY_CONSTRAINTS; |
| 1944 |
} |
| 1945 |
} |
| 1946 |
if (nc->val[i].excludedSubtrees) { |
| 1947 |
GeneralSubtrees_SET(&gs, nc->val[i].excludedSubtrees); |
| 1948 |
ret = match_tree(&gs, c, &match); |
| 1949 |
if (ret) { |
| 1950 |
hx509_clear_error_string(context); |
| 1951 |
return ret; |
| 1952 |
} |
| 1953 |
if (match) { |
| 1954 |
hx509_set_error_string(context, 0, HX509_VERIFY_CONSTRAINTS, |
| 1955 |
"Error verify constraints, " |
| 1956 |
"certificate included in excluded " |
| 1957 |
"subtree"); |
| 1958 |
return HX509_VERIFY_CONSTRAINTS; |
| 1959 |
} |
| 1960 |
} |
| 1961 |
} |
| 1962 |
return 0; |
| 1963 |
} |
| 1964 |
|
| 1965 |
static void |
| 1966 |
free_name_constraints(hx509_name_constraints *nc) |
| 1967 |
{ |
| 1968 |
size_t i; |
| 1969 |
|
| 1970 |
for (i = 0 ; i < nc->len; i++) |
| 1971 |
free_NameConstraints(&nc->val[i]); |
| 1972 |
free(nc->val); |
| 1973 |
} |
| 1974 |
|
| 1975 |
/** |
| 1976 |
* Build and verify the path for the certificate to the trust anchor |
| 1977 |
* specified in the verify context. The path is constructed from the |
| 1978 |
* certificate, the pool and the trust anchors. |
| 1979 |
* |
| 1980 |
* @param context A hx509 context. |
| 1981 |
* @param ctx A hx509 verification context. |
| 1982 |
* @param cert the certificate to build the path from. |
| 1983 |
* @param pool A keyset of certificates to build the chain from. |
| 1984 |
* |
| 1985 |
* @return An hx509 error code, see hx509_get_error_string(). |
| 1986 |
* |
| 1987 |
* @ingroup hx509_verify |
| 1988 |
*/ |
| 1989 |
|
| 1990 |
int |
| 1991 |
hx509_verify_path(hx509_context context, |
| 1992 |
hx509_verify_ctx ctx, |
| 1993 |
hx509_cert cert, |
| 1994 |
hx509_certs pool) |
| 1995 |
{ |
| 1996 |
hx509_name_constraints nc; |
| 1997 |
hx509_path path; |
| 1998 |
int ret, proxy_cert_depth, selfsigned_depth, diff; |
| 1999 |
size_t i, k; |
| 2000 |
enum certtype type; |
| 2001 |
Name proxy_issuer; |
| 2002 |
hx509_certs anchors = NULL; |
| 2003 |
|
| 2004 |
memset(&proxy_issuer, 0, sizeof(proxy_issuer)); |
| 2005 |
|
| 2006 |
if ((ctx->flags & HX509_VERIFY_CTX_F_ALLOW_PROXY_CERTIFICATE) == 0 && |
| 2007 |
is_proxy_cert(context, cert->data, NULL) == 0) |
| 2008 |
{ |
| 2009 |
ret = HX509_PROXY_CERT_INVALID; |
| 2010 |
hx509_set_error_string(context, 0, ret, |
| 2011 |
"Proxy certificate is not allowed as an EE " |
| 2012 |
"certificae if proxy certificate is disabled"); |
| 2013 |
return ret; |
| 2014 |
} |
| 2015 |
|
| 2016 |
ret = init_name_constraints(&nc); |
| 2017 |
if (ret) |
| 2018 |
return ret; |
| 2019 |
|
| 2020 |
path.val = NULL; |
| 2021 |
path.len = 0; |
| 2022 |
|
| 2023 |
if ((ctx->flags & HX509_VERIFY_CTX_F_TIME_SET) == 0) |
| 2024 |
ctx->time_now = time(NULL); |
| 2025 |
|
| 2026 |
/* |
| 2027 |
* |
| 2028 |
*/ |
| 2029 |
if (ctx->trust_anchors) |
| 2030 |
anchors = hx509_certs_ref(ctx->trust_anchors); |
| 2031 |
else if (context->default_trust_anchors && ALLOW_DEF_TA(ctx)) |
| 2032 |
anchors = hx509_certs_ref(context->default_trust_anchors); |
| 2033 |
else { |
| 2034 |
ret = hx509_certs_init(context, "MEMORY:no-TA", 0, NULL, &anchors); |
| 2035 |
if (ret) |
| 2036 |
goto out; |
| 2037 |
} |
| 2038 |
|
| 2039 |
/* |
| 2040 |
* Calculate the path from the certificate user presented to the |
| 2041 |
* to an anchor. |
| 2042 |
*/ |
| 2043 |
ret = _hx509_calculate_path(context, 0, ctx->time_now, |
| 2044 |
anchors, ctx->max_depth, |
| 2045 |
cert, pool, &path); |
| 2046 |
if (ret) |
| 2047 |
goto out; |
| 2048 |
|
| 2049 |
/* |
| 2050 |
* Check CA and proxy certificate chain from the top of the |
| 2051 |
* certificate chain. Also check certificate is valid with respect |
| 2052 |
* to the current time. |
| 2053 |
* |
| 2054 |
*/ |
| 2055 |
|
| 2056 |
proxy_cert_depth = 0; |
| 2057 |
selfsigned_depth = 0; |
| 2058 |
|
| 2059 |
if (ctx->flags & HX509_VERIFY_CTX_F_ALLOW_PROXY_CERTIFICATE) |
| 2060 |
type = PROXY_CERT; |
| 2061 |
else |
| 2062 |
type = EE_CERT; |
| 2063 |
|
| 2064 |
for (i = 0; i < path.len; i++) { |
| 2065 |
Certificate *c; |
| 2066 |
time_t t; |
| 2067 |
|
| 2068 |
c = _hx509_get_cert(path.val[i]); |
| 2069 |
|
| 2070 |
/* |
| 2071 |
* Lets do some basic check on issuer like |
| 2072 |
* keyUsage.keyCertSign and basicConstraints.cA bit depending |
| 2073 |
* on what type of certificate this is. |
| 2074 |
*/ |
| 2075 |
|
| 2076 |
switch (type) { |
| 2077 |
case CA_CERT: |
| 2078 |
|
| 2079 |
/* XXX make constants for keyusage */ |
| 2080 |
ret = check_key_usage(context, c, 1 << 5, |
| 2081 |
REQUIRE_RFC3280(ctx) ? TRUE : FALSE); |
| 2082 |
if (ret) { |
| 2083 |
hx509_set_error_string(context, HX509_ERROR_APPEND, ret, |
| 2084 |
"Key usage missing from CA certificate"); |
| 2085 |
goto out; |
| 2086 |
} |
| 2087 |
|
| 2088 |
/* self signed cert doesn't add to path length */ |
| 2089 |
if (i + 1 != path.len) { |
| 2090 |
int selfsigned; |
| 2091 |
|
| 2092 |
ret = certificate_is_self_signed(context, c, &selfsigned); |
| 2093 |
if (ret) |
| 2094 |
goto out; |
| 2095 |
if (selfsigned) |
| 2096 |
selfsigned_depth++; |
| 2097 |
} |
| 2098 |
|
| 2099 |
break; |
| 2100 |
case PROXY_CERT: { |
| 2101 |
ProxyCertInfo info; |
| 2102 |
|
| 2103 |
if (is_proxy_cert(context, c, &info) == 0) { |
| 2104 |
size_t j; |
| 2105 |
|
| 2106 |
if (info.pCPathLenConstraint != NULL && |
| 2107 |
*info.pCPathLenConstraint < i) |
| 2108 |
{ |
| 2109 |
free_ProxyCertInfo(&info); |
| 2110 |
ret = HX509_PATH_TOO_LONG; |
| 2111 |
hx509_set_error_string(context, 0, ret, |
| 2112 |
"Proxy certificate chain " |
| 2113 |
"longer then allowed"); |
| 2114 |
goto out; |
| 2115 |
} |
| 2116 |
/* XXX MUST check info.proxyPolicy */ |
| 2117 |
free_ProxyCertInfo(&info); |
| 2118 |
|
| 2119 |
j = 0; |
| 2120 |
if (find_extension(c, &asn1_oid_id_x509_ce_subjectAltName, &j)) { |
| 2121 |
ret = HX509_PROXY_CERT_INVALID; |
| 2122 |
hx509_set_error_string(context, 0, ret, |
| 2123 |
"Proxy certificate have explicity " |
| 2124 |
"forbidden subjectAltName"); |
| 2125 |
goto out; |
| 2126 |
} |
| 2127 |
|
| 2128 |
j = 0; |
| 2129 |
if (find_extension(c, &asn1_oid_id_x509_ce_issuerAltName, &j)) { |
| 2130 |
ret = HX509_PROXY_CERT_INVALID; |
| 2131 |
hx509_set_error_string(context, 0, ret, |
| 2132 |
"Proxy certificate have explicity " |
| 2133 |
"forbidden issuerAltName"); |
| 2134 |
goto out; |
| 2135 |
} |
| 2136 |
|
| 2137 |
/* |
| 2138 |
* The subject name of the proxy certificate should be |
| 2139 |
* CN=XXX,<proxy issuer>, prune of CN and check if its |
| 2140 |
* the same over the whole chain of proxy certs and |
| 2141 |
* then check with the EE cert when we get to it. |
| 2142 |
*/ |
| 2143 |
|
| 2144 |
if (proxy_cert_depth) { |
| 2145 |
ret = _hx509_name_cmp(&proxy_issuer, &c->tbsCertificate.subject, &diff); |
| 2146 |
if (ret) { |
| 2147 |
hx509_set_error_string(context, 0, ret, "Out of memory"); |
| 2148 |
goto out; |
| 2149 |
} |
| 2150 |
if (diff) { |
| 2151 |
ret = HX509_PROXY_CERT_NAME_WRONG; |
| 2152 |
hx509_set_error_string(context, 0, ret, |
| 2153 |
"Base proxy name not right"); |
| 2154 |
goto out; |
| 2155 |
} |
| 2156 |
} |
| 2157 |
|
| 2158 |
free_Name(&proxy_issuer); |
| 2159 |
|
| 2160 |
ret = copy_Name(&c->tbsCertificate.subject, &proxy_issuer); |
| 2161 |
if (ret) { |
| 2162 |
hx509_clear_error_string(context); |
| 2163 |
goto out; |
| 2164 |
} |
| 2165 |
|
| 2166 |
j = proxy_issuer.u.rdnSequence.len; |
| 2167 |
if (proxy_issuer.u.rdnSequence.len < 2 |
| 2168 |
|| proxy_issuer.u.rdnSequence.val[j - 1].len > 1 |
| 2169 |
|| der_heim_oid_cmp(&proxy_issuer.u.rdnSequence.val[j - 1].val[0].type, |
| 2170 |
&asn1_oid_id_at_commonName)) |
| 2171 |
{ |
| 2172 |
ret = HX509_PROXY_CERT_NAME_WRONG; |
| 2173 |
hx509_set_error_string(context, 0, ret, |
| 2174 |
"Proxy name too short or " |
| 2175 |
"does not have Common name " |
| 2176 |
"at the top"); |
| 2177 |
goto out; |
| 2178 |
} |
| 2179 |
|
| 2180 |
free_RelativeDistinguishedName(&proxy_issuer.u.rdnSequence.val[j - 1]); |
| 2181 |
proxy_issuer.u.rdnSequence.len -= 1; |
| 2182 |
|
| 2183 |
ret = _hx509_name_cmp(&proxy_issuer, &c->tbsCertificate.issuer, &diff); |
| 2184 |
if (ret) { |
| 2185 |
hx509_set_error_string(context, 0, ret, "Out of memory"); |
| 2186 |
goto out; |
| 2187 |
} |
| 2188 |
if (diff != 0) { |
| 2189 |
ret = HX509_PROXY_CERT_NAME_WRONG; |
| 2190 |
hx509_set_error_string(context, 0, ret, |
| 2191 |
"Proxy issuer name not as expected"); |
| 2192 |
goto out; |
| 2193 |
} |
| 2194 |
|
| 2195 |
break; |
| 2196 |
} else { |
| 2197 |
/* |
| 2198 |
* Now we are done with the proxy certificates, this |
| 2199 |
* cert was an EE cert and we we will fall though to |
| 2200 |
* EE checking below. |
| 2201 |
*/ |
| 2202 |
type = EE_CERT; |
| 2203 |
} |
| 2204 |
} |
| 2205 |
/* FALLTHROUGH */ |
| 2206 |
case EE_CERT: |
| 2207 |
/* |
| 2208 |
* If there where any proxy certificates in the chain |
| 2209 |
* (proxy_cert_depth > 0), check that the proxy issuer |
| 2210 |
* matched proxy certificates "base" subject. |
| 2211 |
*/ |
| 2212 |
if (proxy_cert_depth) { |
| 2213 |
|
| 2214 |
ret = _hx509_name_cmp(&proxy_issuer, |
| 2215 |
&c->tbsCertificate.subject, &diff); |
| 2216 |
if (ret) { |
| 2217 |
hx509_set_error_string(context, 0, ret, "out of memory"); |
| 2218 |
goto out; |
| 2219 |
} |
| 2220 |
if (diff) { |
| 2221 |
ret = HX509_PROXY_CERT_NAME_WRONG; |
| 2222 |
hx509_clear_error_string(context); |
| 2223 |
goto out; |
| 2224 |
} |
| 2225 |
if (cert->basename) |
| 2226 |
hx509_name_free(&cert->basename); |
| 2227 |
|
| 2228 |
ret = _hx509_name_from_Name(&proxy_issuer, &cert->basename); |
| 2229 |
if (ret) { |
| 2230 |
hx509_clear_error_string(context); |
| 2231 |
goto out; |
| 2232 |
} |
| 2233 |
} |
| 2234 |
|
| 2235 |
break; |
| 2236 |
} |
| 2237 |
|
| 2238 |
ret = check_basic_constraints(context, c, type, |
| 2239 |
i - proxy_cert_depth - selfsigned_depth); |
| 2240 |
if (ret) |
| 2241 |
goto out; |
| 2242 |
|
| 2243 |
/* |
| 2244 |
* Don't check the trust anchors expiration time since they |
| 2245 |
* are transported out of band, from RFC3820. |
| 2246 |
*/ |
| 2247 |
if (i + 1 != path.len || CHECK_TA(ctx)) { |
| 2248 |
|
| 2249 |
t = _hx509_Time2time_t(&c->tbsCertificate.validity.notBefore); |
| 2250 |
if (t > ctx->time_now) { |
| 2251 |
ret = HX509_CERT_USED_BEFORE_TIME; |
| 2252 |
hx509_clear_error_string(context); |
| 2253 |
goto out; |
| 2254 |
} |
| 2255 |
t = _hx509_Time2time_t(&c->tbsCertificate.validity.notAfter); |
| 2256 |
if (t < ctx->time_now) { |
| 2257 |
ret = HX509_CERT_USED_AFTER_TIME; |
| 2258 |
hx509_clear_error_string(context); |
| 2259 |
goto out; |
| 2260 |
} |
| 2261 |
} |
| 2262 |
|
| 2263 |
if (type == EE_CERT) |
| 2264 |
type = CA_CERT; |
| 2265 |
else if (type == PROXY_CERT) |
| 2266 |
proxy_cert_depth++; |
| 2267 |
} |
| 2268 |
|
| 2269 |
/* |
| 2270 |
* Verify constraints, do this backward so path constraints are |
| 2271 |
* checked in the right order. |
| 2272 |
*/ |
| 2273 |
|
| 2274 |
for (ret = 0, k = path.len; k > 0; k--) { |
| 2275 |
Certificate *c; |
| 2276 |
int selfsigned; |
| 2277 |
i = k - 1; |
| 2278 |
|
| 2279 |
c = _hx509_get_cert(path.val[i]); |
| 2280 |
|
| 2281 |
ret = certificate_is_self_signed(context, c, &selfsigned); |
| 2282 |
if (ret) |
| 2283 |
goto out; |
| 2284 |
|
| 2285 |
/* verify name constraints, not for selfsigned and anchor */ |
| 2286 |
if (!selfsigned || i + 1 != path.len) { |
| 2287 |
ret = check_name_constraints(context, &nc, c); |
| 2288 |
if (ret) { |
| 2289 |
goto out; |
| 2290 |
} |
| 2291 |
} |
| 2292 |
ret = add_name_constraints(context, c, i == 0, &nc); |
| 2293 |
if (ret) |
| 2294 |
goto out; |
| 2295 |
|
| 2296 |
/* XXX verify all other silly constraints */ |
| 2297 |
|
| 2298 |
} |
| 2299 |
|
| 2300 |
/* |
| 2301 |
* Verify that no certificates has been revoked. |
| 2302 |
*/ |
| 2303 |
|
| 2304 |
if (ctx->revoke_ctx) { |
| 2305 |
hx509_certs certs; |
| 2306 |
|
| 2307 |
ret = hx509_certs_init(context, "MEMORY:revoke-certs", 0, |
| 2308 |
NULL, &certs); |
| 2309 |
if (ret) |
| 2310 |
goto out; |
| 2311 |
|
| 2312 |
for (i = 0; i < path.len; i++) { |
| 2313 |
ret = hx509_certs_add(context, certs, path.val[i]); |
| 2314 |
if (ret) { |
| 2315 |
hx509_certs_free(&certs); |
| 2316 |
goto out; |
| 2317 |
} |
| 2318 |
} |
| 2319 |
ret = hx509_certs_merge(context, certs, pool); |
| 2320 |
if (ret) { |
| 2321 |
hx509_certs_free(&certs); |
| 2322 |
goto out; |
| 2323 |
} |
| 2324 |
|
| 2325 |
for (i = 0; i < path.len - 1; i++) { |
| 2326 |
size_t parent = (i < path.len - 1) ? i + 1 : i; |
| 2327 |
|
| 2328 |
ret = hx509_revoke_verify(context, |
| 2329 |
ctx->revoke_ctx, |
| 2330 |
certs, |
| 2331 |
ctx->time_now, |
| 2332 |
path.val[i], |
| 2333 |
path.val[parent]); |
| 2334 |
if (ret) { |
| 2335 |
hx509_certs_free(&certs); |
| 2336 |
goto out; |
| 2337 |
} |
| 2338 |
} |
| 2339 |
hx509_certs_free(&certs); |
| 2340 |
} |
| 2341 |
|
| 2342 |
/* |
| 2343 |
* Verify signatures, do this backward so public key working |
| 2344 |
* parameter is passed up from the anchor up though the chain. |
| 2345 |
*/ |
| 2346 |
|
| 2347 |
for (k = path.len; k > 0; k--) { |
| 2348 |
hx509_cert signer; |
| 2349 |
Certificate *c; |
| 2350 |
i = k - 1; |
| 2351 |
|
| 2352 |
c = _hx509_get_cert(path.val[i]); |
| 2353 |
|
| 2354 |
/* is last in chain (trust anchor) */ |
| 2355 |
if (i + 1 == path.len) { |
| 2356 |
int selfsigned; |
| 2357 |
|
| 2358 |
signer = path.val[i]; |
| 2359 |
|
| 2360 |
ret = certificate_is_self_signed(context, signer->data, &selfsigned); |
| 2361 |
if (ret) |
| 2362 |
goto out; |
| 2363 |
|
| 2364 |
/* if trust anchor is not self signed, don't check sig */ |
| 2365 |
if (!selfsigned) |
| 2366 |
continue; |
| 2367 |
} else { |
| 2368 |
/* take next certificate in chain */ |
| 2369 |
signer = path.val[i + 1]; |
| 2370 |
} |
| 2371 |
|
| 2372 |
/* verify signatureValue */ |
| 2373 |
ret = _hx509_verify_signature_bitstring(context, |
| 2374 |
signer, |
| 2375 |
&c->signatureAlgorithm, |
| 2376 |
&c->tbsCertificate._save, |
| 2377 |
&c->signatureValue); |
| 2378 |
if (ret) { |
| 2379 |
hx509_set_error_string(context, HX509_ERROR_APPEND, ret, |
| 2380 |
"Failed to verify signature of certificate"); |
| 2381 |
goto out; |
| 2382 |
} |
| 2383 |
/* |
| 2384 |
* Verify that the sigature algorithm is not weak. Ignore |
| 2385 |
* trust anchors since they are provisioned by the user. |
| 2386 |
*/ |
| 2387 |
|
| 2388 |
if (i + 1 != path.len && (ctx->flags & HX509_VERIFY_CTX_F_NO_BEST_BEFORE_CHECK) == 0) { |
| 2389 |
ret = _hx509_signature_is_weak(context, &c->signatureAlgorithm); |
| 2390 |
if (ret) |
| 2391 |
goto out; |
| 2392 |
} |
| 2393 |
} |
| 2394 |
|
| 2395 |
out: |
| 2396 |
hx509_certs_free(&anchors); |
| 2397 |
free_Name(&proxy_issuer); |
| 2398 |
free_name_constraints(&nc); |
| 2399 |
_hx509_path_free(&path); |
| 2400 |
|
| 2401 |
return ret; |
| 2402 |
} |
| 2403 |
|
| 2404 |
/** |
| 2405 |
* Verify a signature made using the private key of an certificate. |
| 2406 |
* |
| 2407 |
* @param context A hx509 context. |
| 2408 |
* @param signer the certificate that made the signature. |
| 2409 |
* @param alg algorthm that was used to sign the data. |
| 2410 |
* @param data the data that was signed. |
| 2411 |
* @param sig the sigature to verify. |
| 2412 |
* |
| 2413 |
* @return An hx509 error code, see hx509_get_error_string(). |
| 2414 |
* |
| 2415 |
* @ingroup hx509_crypto |
| 2416 |
*/ |
| 2417 |
|
| 2418 |
int |
| 2419 |
hx509_verify_signature(hx509_context context, |
| 2420 |
const hx509_cert signer, |
| 2421 |
const AlgorithmIdentifier *alg, |
| 2422 |
const heim_octet_string *data, |
| 2423 |
const heim_octet_string *sig) |
| 2424 |
{ |
| 2425 |
return _hx509_verify_signature(context, signer, alg, data, sig); |
| 2426 |
} |
| 2427 |
|
| 2428 |
int |
| 2429 |
_hx509_verify_signature_bitstring(hx509_context context, |
| 2430 |
const hx509_cert signer, |
| 2431 |
const AlgorithmIdentifier *alg, |
| 2432 |
const heim_octet_string *data, |
| 2433 |
const heim_bit_string *sig) |
| 2434 |
{ |
| 2435 |
heim_octet_string os; |
| 2436 |
|
| 2437 |
if (sig->length & 7) { |
| 2438 |
hx509_set_error_string(context, 0, HX509_CRYPTO_SIG_INVALID_FORMAT, |
| 2439 |
"signature not multiple of 8 bits"); |
| 2440 |
return HX509_CRYPTO_SIG_INVALID_FORMAT; |
| 2441 |
} |
| 2442 |
|
| 2443 |
os.data = sig->data; |
| 2444 |
os.length = sig->length / 8; |
| 2445 |
|
| 2446 |
return _hx509_verify_signature(context, signer, alg, data, &os); |
| 2447 |
} |
| 2448 |
|
| 2449 |
|
| 2450 |
|
| 2451 |
/** |
| 2452 |
* Verify that the certificate is allowed to be used for the hostname |
| 2453 |
* and address. |
| 2454 |
* |
| 2455 |
* @param context A hx509 context. |
| 2456 |
* @param cert the certificate to match with |
| 2457 |
* @param flags Flags to modify the behavior: |
| 2458 |
* - HX509_VHN_F_ALLOW_NO_MATCH no match is ok |
| 2459 |
* @param type type of hostname: |
| 2460 |
* - HX509_HN_HOSTNAME for plain hostname. |
| 2461 |
* - HX509_HN_DNSSRV for DNS SRV names. |
| 2462 |
* @param hostname the hostname to check |
| 2463 |
* @param sa address of the host |
| 2464 |
* @param sa_size length of address |
| 2465 |
* |
| 2466 |
* @return An hx509 error code, see hx509_get_error_string(). |
| 2467 |
* |
| 2468 |
* @ingroup hx509_cert |
| 2469 |
*/ |
| 2470 |
|
| 2471 |
int |
| 2472 |
hx509_verify_hostname(hx509_context context, |
| 2473 |
const hx509_cert cert, |
| 2474 |
int flags, |
| 2475 |
hx509_hostname_type type, |
| 2476 |
const char *hostname, |
| 2477 |
const struct sockaddr *sa, |
| 2478 |
/* XXX krb5_socklen_t */ int sa_size) |
| 2479 |
{ |
| 2480 |
GeneralNames san; |
| 2481 |
const Name *name; |
| 2482 |
int ret; |
| 2483 |
size_t i, j, k; |
| 2484 |
|
| 2485 |
if (sa && sa_size <= 0) |
| 2486 |
return EINVAL; |
| 2487 |
|
| 2488 |
memset(&san, 0, sizeof(san)); |
| 2489 |
|
| 2490 |
i = 0; |
| 2491 |
do { |
| 2492 |
ret = find_extension_subject_alt_name(cert->data, &i, &san); |
| 2493 |
if (ret == HX509_EXTENSION_NOT_FOUND) |
| 2494 |
break; |
| 2495 |
else if (ret != 0) |
| 2496 |
return HX509_PARSING_NAME_FAILED; |
| 2497 |
|
| 2498 |
for (j = 0; j < san.len; j++) { |
| 2499 |
switch (san.val[j].element) { |
| 2500 |
case choice_GeneralName_dNSName: { |
| 2501 |
heim_printable_string hn; |
| 2502 |
hn.data = rk_UNCONST(hostname); |
| 2503 |
hn.length = strlen(hostname); |
| 2504 |
|
| 2505 |
if (der_printable_string_cmp(&san.val[j].u.dNSName, &hn) == 0) { |
| 2506 |
free_GeneralNames(&san); |
| 2507 |
return 0; |
| 2508 |
} |
| 2509 |
break; |
| 2510 |
} |
| 2511 |
default: |
| 2512 |
break; |
| 2513 |
} |
| 2514 |
} |
| 2515 |
free_GeneralNames(&san); |
| 2516 |
} while (1); |
| 2517 |
|
| 2518 |
name = &cert->data->tbsCertificate.subject; |
| 2519 |
|
| 2520 |
/* Find first CN= in the name, and try to match the hostname on that */ |
| 2521 |
for (ret = 0, k = name->u.rdnSequence.len; ret == 0 && k > 0; k--) { |
| 2522 |
i = k - 1; |
| 2523 |
for (j = 0; ret == 0 && j < name->u.rdnSequence.val[i].len; j++) { |
| 2524 |
AttributeTypeAndValue *n = &name->u.rdnSequence.val[i].val[j]; |
| 2525 |
|
| 2526 |
if (der_heim_oid_cmp(&n->type, &asn1_oid_id_at_commonName) == 0) { |
| 2527 |
DirectoryString *ds = &n->value; |
| 2528 |
switch (ds->element) { |
| 2529 |
case choice_DirectoryString_printableString: { |
| 2530 |
heim_printable_string hn; |
| 2531 |
hn.data = rk_UNCONST(hostname); |
| 2532 |
hn.length = strlen(hostname); |
| 2533 |
|
| 2534 |
if (der_printable_string_cmp(&ds->u.printableString, &hn) == 0) |
| 2535 |
return 0; |
| 2536 |
break; |
| 2537 |
} |
| 2538 |
case choice_DirectoryString_ia5String: { |
| 2539 |
heim_ia5_string hn; |
| 2540 |
hn.data = rk_UNCONST(hostname); |
| 2541 |
hn.length = strlen(hostname); |
| 2542 |
|
| 2543 |
if (der_ia5_string_cmp(&ds->u.ia5String, &hn) == 0) |
| 2544 |
return 0; |
| 2545 |
break; |
| 2546 |
} |
| 2547 |
case choice_DirectoryString_utf8String: |
| 2548 |
if (strcasecmp(ds->u.utf8String, hostname) == 0) |
| 2549 |
return 0; |
| 2550 |
default: |
| 2551 |
break; |
| 2552 |
} |
| 2553 |
ret = HX509_NAME_CONSTRAINT_ERROR; |
| 2554 |
} |
| 2555 |
} |
| 2556 |
} |
| 2557 |
|
| 2558 |
if ((flags & HX509_VHN_F_ALLOW_NO_MATCH) == 0) |
| 2559 |
ret = HX509_NAME_CONSTRAINT_ERROR; |
| 2560 |
|
| 2561 |
return ret; |
| 2562 |
} |
| 2563 |
|
| 2564 |
int |
| 2565 |
_hx509_set_cert_attribute(hx509_context context, |
| 2566 |
hx509_cert cert, |
| 2567 |
const heim_oid *oid, |
| 2568 |
const heim_octet_string *attr) |
| 2569 |
{ |
| 2570 |
hx509_cert_attribute a; |
| 2571 |
void *d; |
| 2572 |
|
| 2573 |
if (hx509_cert_get_attribute(cert, oid) != NULL) |
| 2574 |
return 0; |
| 2575 |
|
| 2576 |
d = realloc(cert->attrs.val, |
| 2577 |
sizeof(cert->attrs.val[0]) * (cert->attrs.len + 1)); |
| 2578 |
if (d == NULL) { |
| 2579 |
hx509_clear_error_string(context); |
| 2580 |
return ENOMEM; |
| 2581 |
} |
| 2582 |
cert->attrs.val = d; |
| 2583 |
|
| 2584 |
a = malloc(sizeof(*a)); |
| 2585 |
if (a == NULL) |
| 2586 |
return ENOMEM; |
| 2587 |
|
| 2588 |
der_copy_octet_string(attr, &a->data); |
| 2589 |
der_copy_oid(oid, &a->oid); |
| 2590 |
|
| 2591 |
cert->attrs.val[cert->attrs.len] = a; |
| 2592 |
cert->attrs.len++; |
| 2593 |
|
| 2594 |
return 0; |
| 2595 |
} |
| 2596 |
|
| 2597 |
/** |
| 2598 |
* Get an external attribute for the certificate, examples are |
| 2599 |
* friendly name and id. |
| 2600 |
* |
| 2601 |
* @param cert hx509 certificate object to search |
| 2602 |
* @param oid an oid to search for. |
| 2603 |
* |
| 2604 |
* @return an hx509_cert_attribute, only valid as long as the |
| 2605 |
* certificate is referenced. |
| 2606 |
* |
| 2607 |
* @ingroup hx509_cert |
| 2608 |
*/ |
| 2609 |
|
| 2610 |
hx509_cert_attribute |
| 2611 |
hx509_cert_get_attribute(hx509_cert cert, const heim_oid *oid) |
| 2612 |
{ |
| 2613 |
size_t i; |
| 2614 |
for (i = 0; i < cert->attrs.len; i++) |
| 2615 |
if (der_heim_oid_cmp(oid, &cert->attrs.val[i]->oid) == 0) |
| 2616 |
return cert->attrs.val[i]; |
| 2617 |
return NULL; |
| 2618 |
} |
| 2619 |
|
| 2620 |
/** |
| 2621 |
* Set the friendly name on the certificate. |
| 2622 |
* |
| 2623 |
* @param cert The certificate to set the friendly name on |
| 2624 |
* @param name Friendly name. |
| 2625 |
* |
| 2626 |
* @return An hx509 error code, see hx509_get_error_string(). |
| 2627 |
* |
| 2628 |
* @ingroup hx509_cert |
| 2629 |
*/ |
| 2630 |
|
| 2631 |
int |
| 2632 |
hx509_cert_set_friendly_name(hx509_cert cert, const char *name) |
| 2633 |
{ |
| 2634 |
if (cert->friendlyname) |
| 2635 |
free(cert->friendlyname); |
| 2636 |
cert->friendlyname = strdup(name); |
| 2637 |
if (cert->friendlyname == NULL) |
| 2638 |
return ENOMEM; |
| 2639 |
return 0; |
| 2640 |
} |
| 2641 |
|
| 2642 |
/** |
| 2643 |
* Get friendly name of the certificate. |
| 2644 |
* |
| 2645 |
* @param cert cert to get the friendly name from. |
| 2646 |
* |
| 2647 |
* @return an friendly name or NULL if there is. The friendly name is |
| 2648 |
* only valid as long as the certificate is referenced. |
| 2649 |
* |
| 2650 |
* @ingroup hx509_cert |
| 2651 |
*/ |
| 2652 |
|
| 2653 |
const char * |
| 2654 |
hx509_cert_get_friendly_name(hx509_cert cert) |
| 2655 |
{ |
| 2656 |
hx509_cert_attribute a; |
| 2657 |
PKCS9_friendlyName n; |
| 2658 |
size_t sz; |
| 2659 |
int ret; |
| 2660 |
size_t i; |
| 2661 |
|
| 2662 |
if (cert->friendlyname) |
| 2663 |
return cert->friendlyname; |
| 2664 |
|
| 2665 |
a = hx509_cert_get_attribute(cert, &asn1_oid_id_pkcs_9_at_friendlyName); |
| 2666 |
if (a == NULL) { |
| 2667 |
hx509_name name; |
| 2668 |
|
| 2669 |
ret = hx509_cert_get_subject(cert, &name); |
| 2670 |
if (ret) |
| 2671 |
return NULL; |
| 2672 |
ret = hx509_name_to_string(name, &cert->friendlyname); |
| 2673 |
hx509_name_free(&name); |
| 2674 |
if (ret) |
| 2675 |
return NULL; |
| 2676 |
return cert->friendlyname; |
| 2677 |
} |
| 2678 |
|
| 2679 |
ret = decode_PKCS9_friendlyName(a->data.data, a->data.length, &n, &sz); |
| 2680 |
if (ret) |
| 2681 |
return NULL; |
| 2682 |
|
| 2683 |
if (n.len != 1) { |
| 2684 |
free_PKCS9_friendlyName(&n); |
| 2685 |
return NULL; |
| 2686 |
} |
| 2687 |
|
| 2688 |
cert->friendlyname = malloc(n.val[0].length + 1); |
| 2689 |
if (cert->friendlyname == NULL) { |
| 2690 |
free_PKCS9_friendlyName(&n); |
| 2691 |
return NULL; |
| 2692 |
} |
| 2693 |
|
| 2694 |
for (i = 0; i < n.val[0].length; i++) { |
| 2695 |
if (n.val[0].data[i] <= 0xff) |
| 2696 |
cert->friendlyname[i] = n.val[0].data[i] & 0xff; |
| 2697 |
else |
| 2698 |
cert->friendlyname[i] = 'X'; |
| 2699 |
} |
| 2700 |
cert->friendlyname[i] = '\0'; |
| 2701 |
free_PKCS9_friendlyName(&n); |
| 2702 |
|
| 2703 |
return cert->friendlyname; |
| 2704 |
} |
| 2705 |
|
| 2706 |
void |
| 2707 |
_hx509_query_clear(hx509_query *q) |
| 2708 |
{ |
| 2709 |
memset(q, 0, sizeof(*q)); |
| 2710 |
} |
| 2711 |
|
| 2712 |
/** |
| 2713 |
* Allocate an query controller. Free using hx509_query_free(). |
| 2714 |
* |
| 2715 |
* @param context A hx509 context. |
| 2716 |
* @param q return pointer to a hx509_query. |
| 2717 |
* |
| 2718 |
* @return An hx509 error code, see hx509_get_error_string(). |
| 2719 |
* |
| 2720 |
* @ingroup hx509_cert |
| 2721 |
*/ |
| 2722 |
|
| 2723 |
int |
| 2724 |
hx509_query_alloc(hx509_context context, hx509_query **q) |
| 2725 |
{ |
| 2726 |
*q = calloc(1, sizeof(**q)); |
| 2727 |
if (*q == NULL) |
| 2728 |
return ENOMEM; |
| 2729 |
return 0; |
| 2730 |
} |
| 2731 |
|
| 2732 |
|
| 2733 |
/** |
| 2734 |
* Set match options for the hx509 query controller. |
| 2735 |
* |
| 2736 |
* @param q query controller. |
| 2737 |
* @param option options to control the query controller. |
| 2738 |
* |
| 2739 |
* @return An hx509 error code, see hx509_get_error_string(). |
| 2740 |
* |
| 2741 |
* @ingroup hx509_cert |
| 2742 |
*/ |
| 2743 |
|
| 2744 |
void |
| 2745 |
hx509_query_match_option(hx509_query *q, hx509_query_option option) |
| 2746 |
{ |
| 2747 |
switch(option) { |
| 2748 |
case HX509_QUERY_OPTION_PRIVATE_KEY: |
| 2749 |
q->match |= HX509_QUERY_PRIVATE_KEY; |
| 2750 |
break; |
| 2751 |
case HX509_QUERY_OPTION_KU_ENCIPHERMENT: |
| 2752 |
q->match |= HX509_QUERY_KU_ENCIPHERMENT; |
| 2753 |
break; |
| 2754 |
case HX509_QUERY_OPTION_KU_DIGITALSIGNATURE: |
| 2755 |
q->match |= HX509_QUERY_KU_DIGITALSIGNATURE; |
| 2756 |
break; |
| 2757 |
case HX509_QUERY_OPTION_KU_KEYCERTSIGN: |
| 2758 |
q->match |= HX509_QUERY_KU_KEYCERTSIGN; |
| 2759 |
break; |
| 2760 |
case HX509_QUERY_OPTION_END: |
| 2761 |
default: |
| 2762 |
break; |
| 2763 |
} |
| 2764 |
} |
| 2765 |
|
| 2766 |
/** |
| 2767 |
* Set the issuer and serial number of match in the query |
| 2768 |
* controller. The function make copies of the isser and serial number. |
| 2769 |
* |
| 2770 |
* @param q a hx509 query controller |
| 2771 |
* @param issuer issuer to search for |
| 2772 |
* @param serialNumber the serialNumber of the issuer. |
| 2773 |
* |
| 2774 |
* @return An hx509 error code, see hx509_get_error_string(). |
| 2775 |
* |
| 2776 |
* @ingroup hx509_cert |
| 2777 |
*/ |
| 2778 |
|
| 2779 |
int |
| 2780 |
hx509_query_match_issuer_serial(hx509_query *q, |
| 2781 |
const Name *issuer, |
| 2782 |
const heim_integer *serialNumber) |
| 2783 |
{ |
| 2784 |
int ret; |
| 2785 |
if (q->serial) { |
| 2786 |
der_free_heim_integer(q->serial); |
| 2787 |
free(q->serial); |
| 2788 |
} |
| 2789 |
q->serial = malloc(sizeof(*q->serial)); |
| 2790 |
if (q->serial == NULL) |
| 2791 |
return ENOMEM; |
| 2792 |
ret = der_copy_heim_integer(serialNumber, q->serial); |
| 2793 |
if (ret) { |
| 2794 |
free(q->serial); |
| 2795 |
q->serial = NULL; |
| 2796 |
return ret; |
| 2797 |
} |
| 2798 |
if (q->issuer_name) { |
| 2799 |
free_Name(q->issuer_name); |
| 2800 |
free(q->issuer_name); |
| 2801 |
} |
| 2802 |
q->issuer_name = malloc(sizeof(*q->issuer_name)); |
| 2803 |
if (q->issuer_name == NULL) |
| 2804 |
return ENOMEM; |
| 2805 |
ret = copy_Name(issuer, q->issuer_name); |
| 2806 |
if (ret) { |
| 2807 |
free(q->issuer_name); |
| 2808 |
q->issuer_name = NULL; |
| 2809 |
return ret; |
| 2810 |
} |
| 2811 |
q->match |= HX509_QUERY_MATCH_SERIALNUMBER|HX509_QUERY_MATCH_ISSUER_NAME; |
| 2812 |
return 0; |
| 2813 |
} |
| 2814 |
|
| 2815 |
/** |
| 2816 |
* Set the query controller to match on a friendly name |
| 2817 |
* |
| 2818 |
* @param q a hx509 query controller. |
| 2819 |
* @param name a friendly name to match on |
| 2820 |
* |
| 2821 |
* @return An hx509 error code, see hx509_get_error_string(). |
| 2822 |
* |
| 2823 |
* @ingroup hx509_cert |
| 2824 |
*/ |
| 2825 |
|
| 2826 |
int |
| 2827 |
hx509_query_match_friendly_name(hx509_query *q, const char *name) |
| 2828 |
{ |
| 2829 |
if (q->friendlyname) |
| 2830 |
free(q->friendlyname); |
| 2831 |
q->friendlyname = strdup(name); |
| 2832 |
if (q->friendlyname == NULL) |
| 2833 |
return ENOMEM; |
| 2834 |
q->match |= HX509_QUERY_MATCH_FRIENDLY_NAME; |
| 2835 |
return 0; |
| 2836 |
} |
| 2837 |
|
| 2838 |
/** |
| 2839 |
* Set the query controller to require an one specific EKU (extended |
| 2840 |
* key usage). Any previous EKU matching is overwitten. If NULL is |
| 2841 |
* passed in as the eku, the EKU requirement is reset. |
| 2842 |
* |
| 2843 |
* @param q a hx509 query controller. |
| 2844 |
* @param eku an EKU to match on. |
| 2845 |
* |
| 2846 |
* @return An hx509 error code, see hx509_get_error_string(). |
| 2847 |
* |
| 2848 |
* @ingroup hx509_cert |
| 2849 |
*/ |
| 2850 |
|
| 2851 |
int |
| 2852 |
hx509_query_match_eku(hx509_query *q, const heim_oid *eku) |
| 2853 |
{ |
| 2854 |
int ret; |
| 2855 |
|
| 2856 |
if (eku == NULL) { |
| 2857 |
if (q->eku) { |
| 2858 |
der_free_oid(q->eku); |
| 2859 |
free(q->eku); |
| 2860 |
q->eku = NULL; |
| 2861 |
} |
| 2862 |
q->match &= ~HX509_QUERY_MATCH_EKU; |
| 2863 |
} else { |
| 2864 |
if (q->eku) { |
| 2865 |
der_free_oid(q->eku); |
| 2866 |
} else { |
| 2867 |
q->eku = calloc(1, sizeof(*q->eku)); |
| 2868 |
if (q->eku == NULL) |
| 2869 |
return ENOMEM; |
| 2870 |
} |
| 2871 |
ret = der_copy_oid(eku, q->eku); |
| 2872 |
if (ret) { |
| 2873 |
free(q->eku); |
| 2874 |
q->eku = NULL; |
| 2875 |
return ret; |
| 2876 |
} |
| 2877 |
q->match |= HX509_QUERY_MATCH_EKU; |
| 2878 |
} |
| 2879 |
return 0; |
| 2880 |
} |
| 2881 |
|
| 2882 |
int |
| 2883 |
hx509_query_match_expr(hx509_context context, hx509_query *q, const char *expr) |
| 2884 |
{ |
| 2885 |
if (q->expr) { |
| 2886 |
_hx509_expr_free(q->expr); |
| 2887 |
q->expr = NULL; |
| 2888 |
} |
| 2889 |
|
| 2890 |
if (expr == NULL) { |
| 2891 |
q->match &= ~HX509_QUERY_MATCH_EXPR; |
| 2892 |
} else { |
| 2893 |
q->expr = _hx509_expr_parse(expr); |
| 2894 |
if (q->expr) |
| 2895 |
q->match |= HX509_QUERY_MATCH_EXPR; |
| 2896 |
} |
| 2897 |
|
| 2898 |
return 0; |
| 2899 |
} |
| 2900 |
|
| 2901 |
/** |
| 2902 |
* Set the query controller to match using a specific match function. |
| 2903 |
* |
| 2904 |
* @param q a hx509 query controller. |
| 2905 |
* @param func function to use for matching, if the argument is NULL, |
| 2906 |
* the match function is removed. |
| 2907 |
* @param ctx context passed to the function. |
| 2908 |
* |
| 2909 |
* @return An hx509 error code, see hx509_get_error_string(). |
| 2910 |
* |
| 2911 |
* @ingroup hx509_cert |
| 2912 |
*/ |
| 2913 |
|
| 2914 |
int |
| 2915 |
hx509_query_match_cmp_func(hx509_query *q, |
| 2916 |
int (*func)(hx509_context, hx509_cert, void *), |
| 2917 |
void *ctx) |
| 2918 |
{ |
| 2919 |
if (func) |
| 2920 |
q->match |= HX509_QUERY_MATCH_FUNCTION; |
| 2921 |
else |
| 2922 |
q->match &= ~HX509_QUERY_MATCH_FUNCTION; |
| 2923 |
q->cmp_func = func; |
| 2924 |
q->cmp_func_ctx = ctx; |
| 2925 |
return 0; |
| 2926 |
} |
| 2927 |
|
| 2928 |
/** |
| 2929 |
* Free the query controller. |
| 2930 |
* |
| 2931 |
* @param context A hx509 context. |
| 2932 |
* @param q a pointer to the query controller. |
| 2933 |
* |
| 2934 |
* @ingroup hx509_cert |
| 2935 |
*/ |
| 2936 |
|
| 2937 |
void |
| 2938 |
hx509_query_free(hx509_context context, hx509_query *q) |
| 2939 |
{ |
| 2940 |
if (q == NULL) |
| 2941 |
return; |
| 2942 |
|
| 2943 |
if (q->serial) { |
| 2944 |
der_free_heim_integer(q->serial); |
| 2945 |
free(q->serial); |
| 2946 |
} |
| 2947 |
if (q->issuer_name) { |
| 2948 |
free_Name(q->issuer_name); |
| 2949 |
free(q->issuer_name); |
| 2950 |
} |
| 2951 |
if (q->eku) { |
| 2952 |
der_free_oid(q->eku); |
| 2953 |
free(q->eku); |
| 2954 |
} |
| 2955 |
if (q->friendlyname) |
| 2956 |
free(q->friendlyname); |
| 2957 |
if (q->expr) |
| 2958 |
_hx509_expr_free(q->expr); |
| 2959 |
|
| 2960 |
memset(q, 0, sizeof(*q)); |
| 2961 |
free(q); |
| 2962 |
} |
| 2963 |
|
| 2964 |
int |
| 2965 |
_hx509_query_match_cert(hx509_context context, const hx509_query *q, hx509_cert cert) |
| 2966 |
{ |
| 2967 |
Certificate *c = _hx509_get_cert(cert); |
| 2968 |
int ret, diff; |
| 2969 |
|
| 2970 |
_hx509_query_statistic(context, 1, q); |
| 2971 |
|
| 2972 |
if ((q->match & HX509_QUERY_FIND_ISSUER_CERT) && |
| 2973 |
_hx509_cert_is_parent_cmp(q->subject, c, 0) != 0) |
| 2974 |
return 0; |
| 2975 |
|
| 2976 |
if ((q->match & HX509_QUERY_MATCH_CERTIFICATE) && |
| 2977 |
_hx509_Certificate_cmp(q->certificate, c) != 0) |
| 2978 |
return 0; |
| 2979 |
|
| 2980 |
if ((q->match & HX509_QUERY_MATCH_SERIALNUMBER) |
| 2981 |
&& der_heim_integer_cmp(&c->tbsCertificate.serialNumber, q->serial) != 0) |
| 2982 |
return 0; |
| 2983 |
|
| 2984 |
if (q->match & HX509_QUERY_MATCH_ISSUER_NAME) { |
| 2985 |
ret = _hx509_name_cmp(&c->tbsCertificate.issuer, q->issuer_name, &diff); |
| 2986 |
if (ret || diff) |
| 2987 |
return 0; |
| 2988 |
} |
| 2989 |
|
| 2990 |
if (q->match & HX509_QUERY_MATCH_SUBJECT_NAME) { |
| 2991 |
ret = _hx509_name_cmp(&c->tbsCertificate.subject, q->subject_name, &diff); |
| 2992 |
if (ret || diff) |
| 2993 |
return 0; |
| 2994 |
} |
| 2995 |
|
| 2996 |
if (q->match & HX509_QUERY_MATCH_SUBJECT_KEY_ID) { |
| 2997 |
SubjectKeyIdentifier si; |
| 2998 |
|
| 2999 |
ret = _hx509_find_extension_subject_key_id(c, &si); |
| 3000 |
if (ret == 0) { |
| 3001 |
if (der_heim_octet_string_cmp(&si, q->subject_id) != 0) |
| 3002 |
ret = 1; |
| 3003 |
free_SubjectKeyIdentifier(&si); |
| 3004 |
} |
| 3005 |
if (ret) |
| 3006 |
return 0; |
| 3007 |
} |
| 3008 |
if ((q->match & HX509_QUERY_MATCH_ISSUER_ID)) |
| 3009 |
return 0; |
| 3010 |
if ((q->match & HX509_QUERY_PRIVATE_KEY) && |
| 3011 |
_hx509_cert_private_key(cert) == NULL) |
| 3012 |
return 0; |
| 3013 |
|
| 3014 |
{ |
| 3015 |
unsigned ku = 0; |
| 3016 |
if (q->match & HX509_QUERY_KU_DIGITALSIGNATURE) |
| 3017 |
ku |= (1 << 0); |
| 3018 |
if (q->match & HX509_QUERY_KU_NONREPUDIATION) |
| 3019 |
ku |= (1 << 1); |
| 3020 |
if (q->match & HX509_QUERY_KU_ENCIPHERMENT) |
| 3021 |
ku |= (1 << 2); |
| 3022 |
if (q->match & HX509_QUERY_KU_DATAENCIPHERMENT) |
| 3023 |
ku |= (1 << 3); |
| 3024 |
if (q->match & HX509_QUERY_KU_KEYAGREEMENT) |
| 3025 |
ku |= (1 << 4); |
| 3026 |
if (q->match & HX509_QUERY_KU_KEYCERTSIGN) |
| 3027 |
ku |= (1 << 5); |
| 3028 |
if (q->match & HX509_QUERY_KU_CRLSIGN) |
| 3029 |
ku |= (1 << 6); |
| 3030 |
if (ku && check_key_usage(context, c, ku, TRUE)) |
| 3031 |
return 0; |
| 3032 |
} |
| 3033 |
if ((q->match & HX509_QUERY_ANCHOR)) |
| 3034 |
return 0; |
| 3035 |
|
| 3036 |
if (q->match & HX509_QUERY_MATCH_LOCAL_KEY_ID) { |
| 3037 |
hx509_cert_attribute a; |
| 3038 |
|
| 3039 |
a = hx509_cert_get_attribute(cert, &asn1_oid_id_pkcs_9_at_localKeyId); |
| 3040 |
if (a == NULL) |
| 3041 |
return 0; |
| 3042 |
if (der_heim_octet_string_cmp(&a->data, q->local_key_id) != 0) |
| 3043 |
return 0; |
| 3044 |
} |
| 3045 |
|
| 3046 |
if (q->match & HX509_QUERY_NO_MATCH_PATH) { |
| 3047 |
size_t i; |
| 3048 |
|
| 3049 |
for (i = 0; i < q->path->len; i++) |
| 3050 |
if (hx509_cert_cmp(q->path->val[i], cert) == 0) |
| 3051 |
return 0; |
| 3052 |
} |
| 3053 |
if (q->match & HX509_QUERY_MATCH_FRIENDLY_NAME) { |
| 3054 |
const char *name = hx509_cert_get_friendly_name(cert); |
| 3055 |
if (name == NULL) |
| 3056 |
return 0; |
| 3057 |
if (strcasecmp(q->friendlyname, name) != 0) |
| 3058 |
return 0; |
| 3059 |
} |
| 3060 |
if (q->match & HX509_QUERY_MATCH_FUNCTION) { |
| 3061 |
ret = (*q->cmp_func)(context, cert, q->cmp_func_ctx); |
| 3062 |
if (ret != 0) |
| 3063 |
return 0; |
| 3064 |
} |
| 3065 |
|
| 3066 |
if (q->match & HX509_QUERY_MATCH_KEY_HASH_SHA1) { |
| 3067 |
heim_octet_string os; |
| 3068 |
|
| 3069 |
os.data = c->tbsCertificate.subjectPublicKeyInfo.subjectPublicKey.data; |
| 3070 |
os.length = |
| 3071 |
c->tbsCertificate.subjectPublicKeyInfo.subjectPublicKey.length / 8; |
| 3072 |
|
| 3073 |
ret = _hx509_verify_signature(context, |
| 3074 |
NULL, |
| 3075 |
hx509_signature_sha1(), |
| 3076 |
&os, |
| 3077 |
q->keyhash_sha1); |
| 3078 |
if (ret != 0) |
| 3079 |
return 0; |
| 3080 |
} |
| 3081 |
|
| 3082 |
if (q->match & HX509_QUERY_MATCH_TIME) { |
| 3083 |
time_t t; |
| 3084 |
t = _hx509_Time2time_t(&c->tbsCertificate.validity.notBefore); |
| 3085 |
if (t > q->timenow) |
| 3086 |
return 0; |
| 3087 |
t = _hx509_Time2time_t(&c->tbsCertificate.validity.notAfter); |
| 3088 |
if (t < q->timenow) |
| 3089 |
return 0; |
| 3090 |
} |
| 3091 |
|
| 3092 |
/* If an EKU is required, check the cert for it. */ |
| 3093 |
if ((q->match & HX509_QUERY_MATCH_EKU) && |
| 3094 |
hx509_cert_check_eku(context, cert, q->eku, 0)) |
| 3095 |
return 0; |
| 3096 |
|
| 3097 |
if ((q->match & HX509_QUERY_MATCH_EXPR)) { |
| 3098 |
hx509_env env = NULL; |
| 3099 |
|
| 3100 |
ret = _hx509_cert_to_env(context, cert, &env); |
| 3101 |
if (ret) |
| 3102 |
return 0; |
| 3103 |
|
| 3104 |
ret = _hx509_expr_eval(context, env, q->expr); |
| 3105 |
hx509_env_free(&env); |
| 3106 |
if (ret == 0) |
| 3107 |
return 0; |
| 3108 |
} |
| 3109 |
|
| 3110 |
if (q->match & ~HX509_QUERY_MASK) |
| 3111 |
return 0; |
| 3112 |
|
| 3113 |
return 1; |
| 3114 |
} |
| 3115 |
|
| 3116 |
/** |
| 3117 |
* Set a statistic file for the query statistics. |
| 3118 |
* |
| 3119 |
* @param context A hx509 context. |
| 3120 |
* @param fn statistics file name |
| 3121 |
* |
| 3122 |
* @ingroup hx509_cert |
| 3123 |
*/ |
| 3124 |
|
| 3125 |
void |
| 3126 |
hx509_query_statistic_file(hx509_context context, const char *fn) |
| 3127 |
{ |
| 3128 |
if (context->querystat) |
| 3129 |
free(context->querystat); |
| 3130 |
context->querystat = strdup(fn); |
| 3131 |
} |
| 3132 |
|
| 3133 |
void |
| 3134 |
_hx509_query_statistic(hx509_context context, int type, const hx509_query *q) |
| 3135 |
{ |
| 3136 |
FILE *f; |
| 3137 |
if (context->querystat == NULL) |
| 3138 |
return; |
| 3139 |
f = fopen(context->querystat, "a"); |
| 3140 |
if (f == NULL) |
| 3141 |
return; |
| 3142 |
rk_cloexec_file(f); |
| 3143 |
fprintf(f, "%d %d\n", type, q->match); |
| 3144 |
fclose(f); |
| 3145 |
} |
| 3146 |
|
| 3147 |
static const char *statname[] = { |
| 3148 |
"find issuer cert", |
| 3149 |
"match serialnumber", |
| 3150 |
"match issuer name", |
| 3151 |
"match subject name", |
| 3152 |
"match subject key id", |
| 3153 |
"match issuer id", |
| 3154 |
"private key", |
| 3155 |
"ku encipherment", |
| 3156 |
"ku digitalsignature", |
| 3157 |
"ku keycertsign", |
| 3158 |
"ku crlsign", |
| 3159 |
"ku nonrepudiation", |
| 3160 |
"ku keyagreement", |
| 3161 |
"ku dataencipherment", |
| 3162 |
"anchor", |
| 3163 |
"match certificate", |
| 3164 |
"match local key id", |
| 3165 |
"no match path", |
| 3166 |
"match friendly name", |
| 3167 |
"match function", |
| 3168 |
"match key hash sha1", |
| 3169 |
"match time" |
| 3170 |
}; |
| 3171 |
|
| 3172 |
struct stat_el { |
| 3173 |
unsigned long stats; |
| 3174 |
unsigned int index; |
| 3175 |
}; |
| 3176 |
|
| 3177 |
|
| 3178 |
static int |
| 3179 |
stat_sort(const void *a, const void *b) |
| 3180 |
{ |
| 3181 |
const struct stat_el *ae = a; |
| 3182 |
const struct stat_el *be = b; |
| 3183 |
return be->stats - ae->stats; |
| 3184 |
} |
| 3185 |
|
| 3186 |
/** |
| 3187 |
* Unparse the statistics file and print the result on a FILE descriptor. |
| 3188 |
* |
| 3189 |
* @param context A hx509 context. |
| 3190 |
* @param printtype tyep to print |
| 3191 |
* @param out the FILE to write the data on. |
| 3192 |
* |
| 3193 |
* @ingroup hx509_cert |
| 3194 |
*/ |
| 3195 |
|
| 3196 |
void |
| 3197 |
hx509_query_unparse_stats(hx509_context context, int printtype, FILE *out) |
| 3198 |
{ |
| 3199 |
rtbl_t t; |
| 3200 |
FILE *f; |
| 3201 |
int type, mask, num; |
| 3202 |
size_t i; |
| 3203 |
unsigned long multiqueries = 0, totalqueries = 0; |
| 3204 |
struct stat_el stats[32]; |
| 3205 |
|
| 3206 |
if (context->querystat == NULL) |
| 3207 |
return; |
| 3208 |
f = fopen(context->querystat, "r"); |
| 3209 |
if (f == NULL) { |
| 3210 |
fprintf(out, "No statistic file %s: %s.\n", |
| 3211 |
context->querystat, strerror(errno)); |
| 3212 |
return; |
| 3213 |
} |
| 3214 |
rk_cloexec_file(f); |
| 3215 |
|
| 3216 |
for (i = 0; i < sizeof(stats)/sizeof(stats[0]); i++) { |
| 3217 |
stats[i].index = i; |
| 3218 |
stats[i].stats = 0; |
| 3219 |
} |
| 3220 |
|
| 3221 |
while (fscanf(f, "%d %d\n", &type, &mask) == 2) { |
| 3222 |
if (type != printtype) |
| 3223 |
continue; |
| 3224 |
num = i = 0; |
| 3225 |
while (mask && i < sizeof(stats)/sizeof(stats[0])) { |
| 3226 |
if (mask & 1) { |
| 3227 |
stats[i].stats++; |
| 3228 |
num++; |
| 3229 |
} |
| 3230 |
mask = mask >>1 ; |
| 3231 |
i++; |
| 3232 |
} |
| 3233 |
if (num > 1) |
| 3234 |
multiqueries++; |
| 3235 |
totalqueries++; |
| 3236 |
} |
| 3237 |
fclose(f); |
| 3238 |
|
| 3239 |
qsort(stats, sizeof(stats)/sizeof(stats[0]), sizeof(stats[0]), stat_sort); |
| 3240 |
|
| 3241 |
t = rtbl_create(); |
| 3242 |
if (t == NULL) |
| 3243 |
errx(1, "out of memory"); |
| 3244 |
|
| 3245 |
rtbl_set_separator (t, " "); |
| 3246 |
|
| 3247 |
rtbl_add_column_by_id (t, 0, "Name", 0); |
| 3248 |
rtbl_add_column_by_id (t, 1, "Counter", 0); |
| 3249 |
|
| 3250 |
|
| 3251 |
for (i = 0; i < sizeof(stats)/sizeof(stats[0]); i++) { |
| 3252 |
char str[10]; |
| 3253 |
|
| 3254 |
if (stats[i].index < sizeof(statname)/sizeof(statname[0])) |
| 3255 |
rtbl_add_column_entry_by_id (t, 0, statname[stats[i].index]); |
| 3256 |
else { |
| 3257 |
snprintf(str, sizeof(str), "%d", stats[i].index); |
| 3258 |
rtbl_add_column_entry_by_id (t, 0, str); |
| 3259 |
} |
| 3260 |
snprintf(str, sizeof(str), "%lu", stats[i].stats); |
| 3261 |
rtbl_add_column_entry_by_id (t, 1, str); |
| 3262 |
} |
| 3263 |
|
| 3264 |
rtbl_format(t, out); |
| 3265 |
rtbl_destroy(t); |
| 3266 |
|
| 3267 |
fprintf(out, "\nQueries: multi %lu total %lu\n", |
| 3268 |
multiqueries, totalqueries); |
| 3269 |
} |
| 3270 |
|
| 3271 |
/** |
| 3272 |
* Check the extended key usage on the hx509 certificate. |
| 3273 |
* |
| 3274 |
* @param context A hx509 context. |
| 3275 |
* @param cert A hx509 context. |
| 3276 |
* @param eku the EKU to check for |
| 3277 |
* @param allow_any_eku if the any EKU is set, allow that to be a |
| 3278 |
* substitute. |
| 3279 |
* |
| 3280 |
* @return An hx509 error code, see hx509_get_error_string(). |
| 3281 |
* |
| 3282 |
* @ingroup hx509_cert |
| 3283 |
*/ |
| 3284 |
|
| 3285 |
int |
| 3286 |
hx509_cert_check_eku(hx509_context context, hx509_cert cert, |
| 3287 |
const heim_oid *eku, int allow_any_eku) |
| 3288 |
{ |
| 3289 |
ExtKeyUsage e; |
| 3290 |
int ret; |
| 3291 |
size_t i; |
| 3292 |
|
| 3293 |
ret = find_extension_eku(_hx509_get_cert(cert), &e); |
| 3294 |
if (ret) { |
| 3295 |
hx509_clear_error_string(context); |
| 3296 |
return ret; |
| 3297 |
} |
| 3298 |
|
| 3299 |
for (i = 0; i < e.len; i++) { |
| 3300 |
if (der_heim_oid_cmp(eku, &e.val[i]) == 0) { |
| 3301 |
free_ExtKeyUsage(&e); |
| 3302 |
return 0; |
| 3303 |
} |
| 3304 |
if (allow_any_eku) { |
| 3305 |
#if 0 |
| 3306 |
if (der_heim_oid_cmp(id_any_eku, &e.val[i]) == 0) { |
| 3307 |
free_ExtKeyUsage(&e); |
| 3308 |
return 0; |
| 3309 |
} |
| 3310 |
#endif |
| 3311 |
} |
| 3312 |
} |
| 3313 |
free_ExtKeyUsage(&e); |
| 3314 |
hx509_clear_error_string(context); |
| 3315 |
return HX509_CERTIFICATE_MISSING_EKU; |
| 3316 |
} |
| 3317 |
|
| 3318 |
int |
| 3319 |
_hx509_cert_get_keyusage(hx509_context context, |
| 3320 |
hx509_cert c, |
| 3321 |
KeyUsage *ku) |
| 3322 |
{ |
| 3323 |
Certificate *cert; |
| 3324 |
const Extension *e; |
| 3325 |
size_t size; |
| 3326 |
int ret; |
| 3327 |
size_t i = 0; |
| 3328 |
|
| 3329 |
memset(ku, 0, sizeof(*ku)); |
| 3330 |
|
| 3331 |
cert = _hx509_get_cert(c); |
| 3332 |
|
| 3333 |
if (_hx509_cert_get_version(cert) < 3) |
| 3334 |
return 0; |
| 3335 |
|
| 3336 |
e = find_extension(cert, &asn1_oid_id_x509_ce_keyUsage, &i); |
| 3337 |
if (e == NULL) |
| 3338 |
return HX509_KU_CERT_MISSING; |
| 3339 |
|
| 3340 |
ret = decode_KeyUsage(e->extnValue.data, e->extnValue.length, ku, &size); |
| 3341 |
if (ret) |
| 3342 |
return ret; |
| 3343 |
return 0; |
| 3344 |
} |
| 3345 |
|
| 3346 |
int |
| 3347 |
_hx509_cert_get_eku(hx509_context context, |
| 3348 |
hx509_cert cert, |
| 3349 |
ExtKeyUsage *e) |
| 3350 |
{ |
| 3351 |
int ret; |
| 3352 |
|
| 3353 |
memset(e, 0, sizeof(*e)); |
| 3354 |
|
| 3355 |
ret = find_extension_eku(_hx509_get_cert(cert), e); |
| 3356 |
if (ret && ret != HX509_EXTENSION_NOT_FOUND) { |
| 3357 |
hx509_clear_error_string(context); |
| 3358 |
return ret; |
| 3359 |
} |
| 3360 |
return 0; |
| 3361 |
} |
| 3362 |
|
| 3363 |
/** |
| 3364 |
* Encodes the hx509 certificate as a DER encode binary. |
| 3365 |
* |
| 3366 |
* @param context A hx509 context. |
| 3367 |
* @param c the certificate to encode. |
| 3368 |
* @param os the encode certificate, set to NULL, 0 on case of |
| 3369 |
* error. Free the os->data with hx509_xfree(). |
| 3370 |
* |
| 3371 |
* @return An hx509 error code, see hx509_get_error_string(). |
| 3372 |
* |
| 3373 |
* @ingroup hx509_cert |
| 3374 |
*/ |
| 3375 |
|
| 3376 |
int |
| 3377 |
hx509_cert_binary(hx509_context context, hx509_cert c, heim_octet_string *os) |
| 3378 |
{ |
| 3379 |
size_t size; |
| 3380 |
int ret; |
| 3381 |
|
| 3382 |
os->data = NULL; |
| 3383 |
os->length = 0; |
| 3384 |
|
| 3385 |
ASN1_MALLOC_ENCODE(Certificate, os->data, os->length, |
| 3386 |
_hx509_get_cert(c), &size, ret); |
| 3387 |
if (ret) { |
| 3388 |
os->data = NULL; |
| 3389 |
os->length = 0; |
| 3390 |
return ret; |
| 3391 |
} |
| 3392 |
if (os->length != size) |
| 3393 |
_hx509_abort("internal ASN.1 encoder error"); |
| 3394 |
|
| 3395 |
return ret; |
| 3396 |
} |
| 3397 |
|
| 3398 |
/* |
| 3399 |
* Last to avoid lost __attribute__s due to #undef. |
| 3400 |
*/ |
| 3401 |
|
| 3402 |
#undef __attribute__ |
| 3403 |
#define __attribute__(X) |
| 3404 |
|
| 3405 |
void |
| 3406 |
_hx509_abort(const char *fmt, ...) |
| 3407 |
__attribute__ ((__noreturn__, __format__ (__printf__, 1, 2))) |
| 3408 |
{ |
| 3409 |
va_list ap; |
| 3410 |
va_start(ap, fmt); |
| 3411 |
vprintf(fmt, ap); |
| 3412 |
va_end(ap); |
| 3413 |
printf("\n"); |
| 3414 |
fflush(stdout); |
| 3415 |
abort(); |
| 3416 |
} |
| 3417 |
|
| 3418 |
/** |
| 3419 |
* Free a data element allocated in the library. |
| 3420 |
* |
| 3421 |
* @param ptr data to be freed. |
| 3422 |
* |
| 3423 |
* @ingroup hx509_misc |
| 3424 |
*/ |
| 3425 |
|
| 3426 |
void |
| 3427 |
hx509_xfree(void *ptr) |
| 3428 |
{ |
| 3429 |
free(ptr); |
| 3430 |
} |
| 3431 |
|
| 3432 |
/** |
| 3433 |
* |
| 3434 |
*/ |
| 3435 |
|
| 3436 |
int |
| 3437 |
_hx509_cert_to_env(hx509_context context, hx509_cert cert, hx509_env *env) |
| 3438 |
{ |
| 3439 |
ExtKeyUsage eku; |
| 3440 |
hx509_name name; |
| 3441 |
char *buf; |
| 3442 |
int ret; |
| 3443 |
hx509_env envcert = NULL; |
| 3444 |
|
| 3445 |
*env = NULL; |
| 3446 |
|
| 3447 |
/* version */ |
| 3448 |
ret = asprintf(&buf, "%d", _hx509_cert_get_version(_hx509_get_cert(cert))); |
| 3449 |
if (ret == -1) |
| 3450 |
goto out; |
| 3451 |
ret = hx509_env_add(context, &envcert, "version", buf); |
| 3452 |
free(buf); |
| 3453 |
if (ret) |
| 3454 |
goto out; |
| 3455 |
|
| 3456 |
/* subject */ |
| 3457 |
ret = hx509_cert_get_subject(cert, &name); |
| 3458 |
if (ret) |
| 3459 |
goto out; |
| 3460 |
|
| 3461 |
ret = hx509_name_to_string(name, &buf); |
| 3462 |
if (ret) { |
| 3463 |
hx509_name_free(&name); |
| 3464 |
goto out; |
| 3465 |
} |
| 3466 |
|
| 3467 |
ret = hx509_env_add(context, &envcert, "subject", buf); |
| 3468 |
hx509_name_free(&name); |
| 3469 |
if (ret) |
| 3470 |
goto out; |
| 3471 |
|
| 3472 |
/* issuer */ |
| 3473 |
ret = hx509_cert_get_issuer(cert, &name); |
| 3474 |
if (ret) |
| 3475 |
goto out; |
| 3476 |
|
| 3477 |
ret = hx509_name_to_string(name, &buf); |
| 3478 |
hx509_name_free(&name); |
| 3479 |
if (ret) |
| 3480 |
goto out; |
| 3481 |
|
| 3482 |
ret = hx509_env_add(context, &envcert, "issuer", buf); |
| 3483 |
hx509_xfree(buf); |
| 3484 |
if (ret) |
| 3485 |
goto out; |
| 3486 |
|
| 3487 |
/* eku */ |
| 3488 |
|
| 3489 |
ret = _hx509_cert_get_eku(context, cert, &eku); |
| 3490 |
if (ret == HX509_EXTENSION_NOT_FOUND) |
| 3491 |
; |
| 3492 |
else if (ret != 0) |
| 3493 |
goto out; |
| 3494 |
else { |
| 3495 |
size_t i; |
| 3496 |
hx509_env enveku = NULL; |
| 3497 |
|
| 3498 |
for (i = 0; i < eku.len; i++) { |
| 3499 |
|
| 3500 |
ret = der_print_heim_oid(&eku.val[i], '.', &buf); |
| 3501 |
if (ret) { |
| 3502 |
free_ExtKeyUsage(&eku); |
| 3503 |
hx509_env_free(&enveku); |
| 3504 |
goto out; |
| 3505 |
} |
| 3506 |
ret = hx509_env_add(context, &enveku, buf, "oid-name-here"); |
| 3507 |
free(buf); |
| 3508 |
if (ret) { |
| 3509 |
free_ExtKeyUsage(&eku); |
| 3510 |
hx509_env_free(&enveku); |
| 3511 |
goto out; |
| 3512 |
} |
| 3513 |
} |
| 3514 |
free_ExtKeyUsage(&eku); |
| 3515 |
|
| 3516 |
ret = hx509_env_add_binding(context, &envcert, "eku", enveku); |
| 3517 |
if (ret) { |
| 3518 |
hx509_env_free(&enveku); |
| 3519 |
goto out; |
| 3520 |
} |
| 3521 |
} |
| 3522 |
|
| 3523 |
{ |
| 3524 |
Certificate *c = _hx509_get_cert(cert); |
| 3525 |
heim_octet_string os, sig; |
| 3526 |
hx509_env envhash = NULL; |
| 3527 |
|
| 3528 |
os.data = c->tbsCertificate.subjectPublicKeyInfo.subjectPublicKey.data; |
| 3529 |
os.length = |
| 3530 |
c->tbsCertificate.subjectPublicKeyInfo.subjectPublicKey.length / 8; |
| 3531 |
|
| 3532 |
ret = _hx509_create_signature(context, |
| 3533 |
NULL, |
| 3534 |
hx509_signature_sha1(), |
| 3535 |
&os, |
| 3536 |
NULL, |
| 3537 |
&sig); |
| 3538 |
if (ret != 0) |
| 3539 |
goto out; |
| 3540 |
|
| 3541 |
ret = hex_encode(sig.data, sig.length, &buf); |
| 3542 |
der_free_octet_string(&sig); |
| 3543 |
if (ret < 0) { |
| 3544 |
ret = ENOMEM; |
| 3545 |
hx509_set_error_string(context, 0, ret, |
| 3546 |
"Out of memory"); |
| 3547 |
goto out; |
| 3548 |
} |
| 3549 |
|
| 3550 |
ret = hx509_env_add(context, &envhash, "sha1", buf); |
| 3551 |
free(buf); |
| 3552 |
if (ret) |
| 3553 |
goto out; |
| 3554 |
|
| 3555 |
ret = hx509_env_add_binding(context, &envcert, "hash", envhash); |
| 3556 |
if (ret) { |
| 3557 |
hx509_env_free(&envhash); |
| 3558 |
goto out; |
| 3559 |
} |
| 3560 |
} |
| 3561 |
|
| 3562 |
ret = hx509_env_add_binding(context, env, "certificate", envcert); |
| 3563 |
if (ret) |
| 3564 |
goto out; |
| 3565 |
|
| 3566 |
return 0; |
| 3567 |
|
| 3568 |
out: |
| 3569 |
hx509_env_free(&envcert); |
| 3570 |
return ret; |
| 3571 |
} |
| 3572 |
|
| 3573 |
/** |
| 3574 |
* Print a simple representation of a certificate |
| 3575 |
* |
| 3576 |
* @param context A hx509 context, can be NULL |
| 3577 |
* @param cert certificate to print |
| 3578 |
* @param out the stdio output stream, if NULL, stdout is used |
| 3579 |
* |
| 3580 |
* @return An hx509 error code |
| 3581 |
* |
| 3582 |
* @ingroup hx509_cert |
| 3583 |
*/ |
| 3584 |
|
| 3585 |
int |
| 3586 |
hx509_print_cert(hx509_context context, hx509_cert cert, FILE *out) |
| 3587 |
{ |
| 3588 |
hx509_name name; |
| 3589 |
char *str; |
| 3590 |
int ret; |
| 3591 |
|
| 3592 |
if (out == NULL) |
| 3593 |
out = stderr; |
| 3594 |
|
| 3595 |
ret = hx509_cert_get_issuer(cert, &name); |
| 3596 |
if (ret) |
| 3597 |
return ret; |
| 3598 |
hx509_name_to_string(name, &str); |
| 3599 |
hx509_name_free(&name); |
| 3600 |
fprintf(out, " issuer: \"%s\"\n", str); |
| 3601 |
free(str); |
| 3602 |
|
| 3603 |
ret = hx509_cert_get_subject(cert, &name); |
| 3604 |
if (ret) |
| 3605 |
return ret; |
| 3606 |
hx509_name_to_string(name, &str); |
| 3607 |
hx509_name_free(&name); |
| 3608 |
fprintf(out, " subject: \"%s\"\n", str); |
| 3609 |
free(str); |
| 3610 |
|
| 3611 |
{ |
| 3612 |
heim_integer serialNumber; |
| 3613 |
|
| 3614 |
ret = hx509_cert_get_serialnumber(cert, &serialNumber); |
| 3615 |
if (ret) |
| 3616 |
return ret; |
| 3617 |
ret = der_print_hex_heim_integer(&serialNumber, &str); |
| 3618 |
if (ret) |
| 3619 |
return ret; |
| 3620 |
der_free_heim_integer(&serialNumber); |
| 3621 |
fprintf(out, " serial: %s\n", str); |
| 3622 |
free(str); |
| 3623 |
} |
| 3624 |
|
| 3625 |
printf(" keyusage: "); |
| 3626 |
ret = hx509_cert_keyusage_print(context, cert, &str); |
| 3627 |
if (ret == 0) { |
| 3628 |
fprintf(out, "%s\n", str); |
| 3629 |
free(str); |
| 3630 |
} else |
| 3631 |
fprintf(out, "no"); |
| 3632 |
|
| 3633 |
return 0; |
| 3634 |
} |