1 /* 2 * Copyright (c) 2019-2021 Yubico AB. All rights reserved. 3 * Use of this source code is governed by a BSD-style 4 * license that can be found in the LICENSE file. 5 */ 6 7 #include <sys/types.h> 8 #include <sys/socket.h> 9 10 #include <openssl/bn.h> 11 #include <openssl/evp.h> 12 #include <openssl/sha.h> 13 14 #include <cbor.h> 15 #include <stdbool.h> 16 #include <stdint.h> 17 #include <stdio.h> 18 #include <stdlib.h> 19 20 #include "mutator_aux.h" 21 22 extern int prng_up; 23 24 /* 25 * Build wrappers around functions of interest, and have them fail 26 * in a pseudo-random manner. 27 */ 28 29 #define WRAP(type, name, args, retval, param, prob) \ 30 extern type __wrap_##name args; \ 31 extern type __real_##name args; \ 32 type __wrap_##name args { \ 33 if (prng_up && uniform_random(400) < (prob)) { \ 34 return (retval); \ 35 } \ 36 \ 37 return (__real_##name param); \ 38 } 39 40 WRAP(void *, 41 malloc, 42 (size_t size), 43 NULL, 44 (size), 45 1 46 ) 47 48 WRAP(void *, 49 calloc, 50 (size_t nmemb, size_t size), 51 NULL, 52 (nmemb, size), 53 1 54 ) 55 56 WRAP(void *, 57 realloc, 58 (void *ptr, size_t size), 59 NULL, 60 (ptr, size), 61 1 62 ) 63 64 WRAP(char *, 65 strdup, 66 (const char *s), 67 NULL, 68 (s), 69 1 70 ) 71 72 WRAP(int, 73 EVP_Cipher, 74 (EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, 75 unsigned int inl), 76 -1, 77 (ctx, out, in, inl), 78 1 79 ) 80 81 WRAP(int, 82 EVP_CIPHER_CTX_ctrl, 83 (EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr), 84 0, 85 (ctx, type, arg, ptr), 86 1 87 ) 88 89 WRAP(EVP_CIPHER_CTX *, 90 EVP_CIPHER_CTX_new, 91 (void), 92 NULL, 93 (), 94 1 95 ) 96 97 WRAP(int, 98 EVP_CipherInit, 99 (EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher, 100 const unsigned char *key, const unsigned char *iv, int enc), 101 0, 102 (ctx, cipher, key, iv, enc), 103 1 104 ) 105 106 WRAP(RSA *, 107 EVP_PKEY_get0_RSA, 108 (EVP_PKEY *pkey), 109 NULL, 110 (pkey), 111 1 112 ) 113 114 WRAP(EC_KEY *, 115 EVP_PKEY_get0_EC_KEY, 116 (EVP_PKEY *pkey), 117 NULL, 118 (pkey), 119 1 120 ) 121 122 WRAP(int, 123 EVP_PKEY_get_raw_public_key, 124 (const EVP_PKEY *pkey, unsigned char *pub, size_t *len), 125 0, 126 (pkey, pub, len), 127 1 128 ) 129 130 WRAP(EVP_MD_CTX *, 131 EVP_MD_CTX_new, 132 (void), 133 NULL, 134 (), 135 1 136 ) 137 138 WRAP(int, 139 EVP_DigestVerifyInit, 140 (EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx, const EVP_MD *type, ENGINE *e, 141 EVP_PKEY *pkey), 142 0, 143 (ctx, pctx, type, e, pkey), 144 1 145 ) 146 147 WRAP(int, 148 EVP_DigestInit_ex, 149 (EVP_MD_CTX *ctx, const EVP_MD *type, ENGINE *impl), 150 0, 151 (ctx, type, impl), 152 1 153 ) 154 155 WRAP(int, 156 EVP_DigestUpdate, 157 (EVP_MD_CTX *ctx, const void *data, size_t count), 158 0, 159 (ctx, data, count), 160 1 161 ) 162 163 WRAP(int, 164 EVP_DigestFinal_ex, 165 (EVP_MD_CTX *ctx, unsigned char *md, unsigned int *isize), 166 0, 167 (ctx, md, isize), 168 1 169 ) 170 171 WRAP(BIGNUM *, 172 BN_bin2bn, 173 (const unsigned char *s, int len, BIGNUM *ret), 174 NULL, 175 (s, len, ret), 176 1 177 ) 178 179 WRAP(int, 180 BN_bn2bin, 181 (const BIGNUM *a, unsigned char *to), 182 -1, 183 (a, to), 184 1 185 ) 186 187 WRAP(BIGNUM *, 188 BN_CTX_get, 189 (BN_CTX *ctx), 190 NULL, 191 (ctx), 192 1 193 ) 194 195 WRAP(BN_CTX *, 196 BN_CTX_new, 197 (void), 198 NULL, 199 (), 200 1 201 ) 202 203 WRAP(BIGNUM *, 204 BN_new, 205 (void), 206 NULL, 207 (), 208 1 209 ) 210 211 WRAP(RSA *, 212 RSA_new, 213 (void), 214 NULL, 215 (), 216 1 217 ) 218 219 WRAP(int, 220 RSA_set0_key, 221 (RSA *r, BIGNUM *n, BIGNUM *e, BIGNUM *d), 222 0, 223 (r, n, e, d), 224 1 225 ) 226 227 WRAP(int, 228 RSA_pkey_ctx_ctrl, 229 (EVP_PKEY_CTX *ctx, int optype, int cmd, int p1, void *p2), 230 -1, 231 (ctx, optype, cmd, p1, p2), 232 1 233 ) 234 235 WRAP(EC_KEY *, 236 EC_KEY_new_by_curve_name, 237 (int nid), 238 NULL, 239 (nid), 240 1 241 ) 242 243 WRAP(const EC_GROUP *, 244 EC_KEY_get0_group, 245 (const EC_KEY *key), 246 NULL, 247 (key), 248 1 249 ) 250 251 WRAP(const BIGNUM *, 252 EC_KEY_get0_private_key, 253 (const EC_KEY *key), 254 NULL, 255 (key), 256 1 257 ) 258 259 WRAP(EC_POINT *, 260 EC_POINT_new, 261 (const EC_GROUP *group), 262 NULL, 263 (group), 264 1 265 ) 266 267 WRAP(int, 268 EC_POINT_get_affine_coordinates_GFp, 269 (const EC_GROUP *group, const EC_POINT *p, BIGNUM *x, BIGNUM *y, BN_CTX *ctx), 270 0, 271 (group, p, x, y, ctx), 272 1 273 ) 274 275 WRAP(EVP_PKEY *, 276 EVP_PKEY_new, 277 (void), 278 NULL, 279 (), 280 1 281 ) 282 283 WRAP(int, 284 EVP_PKEY_assign, 285 (EVP_PKEY *pkey, int type, void *key), 286 0, 287 (pkey, type, key), 288 1 289 ) 290 291 WRAP(int, 292 EVP_PKEY_keygen_init, 293 (EVP_PKEY_CTX *ctx), 294 0, 295 (ctx), 296 1 297 ) 298 299 WRAP(int, 300 EVP_PKEY_keygen, 301 (EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey), 302 0, 303 (ctx, ppkey), 304 1 305 ) 306 307 WRAP(int, 308 EVP_PKEY_paramgen_init, 309 (EVP_PKEY_CTX *ctx), 310 0, 311 (ctx), 312 1 313 ) 314 315 WRAP(int, 316 EVP_PKEY_paramgen, 317 (EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey), 318 0, 319 (ctx, ppkey), 320 1 321 ) 322 323 WRAP(EVP_PKEY *, 324 EVP_PKEY_new_raw_public_key, 325 (int type, ENGINE *e, const unsigned char *key, size_t keylen), 326 NULL, 327 (type, e, key, keylen), 328 1 329 ) 330 331 WRAP(EVP_PKEY_CTX *, 332 EVP_PKEY_CTX_new, 333 (EVP_PKEY *pkey, ENGINE *e), 334 NULL, 335 (pkey, e), 336 1 337 ) 338 339 WRAP(EVP_PKEY_CTX *, 340 EVP_PKEY_CTX_new_id, 341 (int id, ENGINE *e), 342 NULL, 343 (id, e), 344 1 345 ) 346 347 WRAP(int, 348 EVP_PKEY_derive, 349 (EVP_PKEY_CTX *ctx, unsigned char *key, size_t *pkeylen), 350 0, 351 (ctx, key, pkeylen), 352 1 353 ) 354 355 WRAP(int, 356 EVP_PKEY_derive_init, 357 (EVP_PKEY_CTX *ctx), 358 0, 359 (ctx), 360 1 361 ) 362 363 WRAP(int, 364 EVP_PKEY_derive_set_peer, 365 (EVP_PKEY_CTX *ctx, EVP_PKEY *peer), 366 0, 367 (ctx, peer), 368 1 369 ) 370 371 WRAP(int, 372 EVP_PKEY_verify_init, 373 (EVP_PKEY_CTX *ctx), 374 0, 375 (ctx), 376 1 377 ) 378 379 WRAP(int, 380 EVP_PKEY_CTX_ctrl, 381 (EVP_PKEY_CTX *ctx, int keytype, int optype, int cmd, int p1, void *p2), 382 -1, 383 (ctx, keytype, optype, cmd, p1, p2), 384 1 385 ) 386 387 WRAP(const EVP_MD *, 388 EVP_sha1, 389 (void), 390 NULL, 391 (), 392 1 393 ) 394 395 WRAP(const EVP_MD *, 396 EVP_sha256, 397 (void), 398 NULL, 399 (), 400 1 401 ) 402 403 WRAP(const EVP_CIPHER *, 404 EVP_aes_256_cbc, 405 (void), 406 NULL, 407 (), 408 1 409 ) 410 411 WRAP(const EVP_CIPHER *, 412 EVP_aes_256_gcm, 413 (void), 414 NULL, 415 (), 416 1 417 ) 418 419 WRAP(unsigned char *, 420 HMAC, 421 (const EVP_MD *evp_md, const void *key, int key_len, 422 const unsigned char *d, int n, unsigned char *md, 423 unsigned int *md_len), 424 NULL, 425 (evp_md, key, key_len, d, n, md, md_len), 426 1 427 ) 428 429 WRAP(HMAC_CTX *, 430 HMAC_CTX_new, 431 (void), 432 NULL, 433 (), 434 1 435 ) 436 437 WRAP(int, 438 HMAC_Init_ex, 439 (HMAC_CTX *ctx, const void *key, int key_len, const EVP_MD *md, 440 ENGINE *impl), 441 0, 442 (ctx, key, key_len, md, impl), 443 1 444 ) 445 446 WRAP(int, 447 HMAC_Update, 448 (HMAC_CTX *ctx, const unsigned char *data, int len), 449 0, 450 (ctx, data, len), 451 1 452 ) 453 454 WRAP(int, 455 HMAC_Final, 456 (HMAC_CTX *ctx, unsigned char *md, unsigned int *len), 457 0, 458 (ctx, md, len), 459 1 460 ) 461 462 WRAP(unsigned char *, 463 SHA1, 464 (const unsigned char *d, size_t n, unsigned char *md), 465 NULL, 466 (d, n, md), 467 1 468 ) 469 470 WRAP(unsigned char *, 471 SHA256, 472 (const unsigned char *d, size_t n, unsigned char *md), 473 NULL, 474 (d, n, md), 475 1 476 ) 477 478 WRAP(cbor_item_t *, 479 cbor_build_string, 480 (const char *val), 481 NULL, 482 (val), 483 1 484 ) 485 486 WRAP(cbor_item_t *, 487 cbor_build_bytestring, 488 (cbor_data handle, size_t length), 489 NULL, 490 (handle, length), 491 1 492 ) 493 494 WRAP(cbor_item_t *, 495 cbor_build_bool, 496 (bool value), 497 NULL, 498 (value), 499 1 500 ) 501 502 WRAP(cbor_item_t *, 503 cbor_build_negint8, 504 (uint8_t value), 505 NULL, 506 (value), 507 1 508 ) 509 510 WRAP(cbor_item_t *, 511 cbor_build_negint16, 512 (uint16_t value), 513 NULL, 514 (value), 515 1 516 ) 517 518 WRAP(cbor_item_t *, 519 cbor_load, 520 (cbor_data source, size_t source_size, struct cbor_load_result *result), 521 NULL, 522 (source, source_size, result), 523 1 524 ) 525 526 WRAP(cbor_item_t *, 527 cbor_build_uint8, 528 (uint8_t value), 529 NULL, 530 (value), 531 1 532 ) 533 534 WRAP(cbor_item_t *, 535 cbor_build_uint16, 536 (uint16_t value), 537 NULL, 538 (value), 539 1 540 ) 541 542 WRAP(cbor_item_t *, 543 cbor_build_uint32, 544 (uint32_t value), 545 NULL, 546 (value), 547 1 548 ) 549 550 WRAP(cbor_item_t *, 551 cbor_build_uint64, 552 (uint64_t value), 553 NULL, 554 (value), 555 1 556 ) 557 558 WRAP(struct cbor_pair *, 559 cbor_map_handle, 560 (const cbor_item_t *item), 561 NULL, 562 (item), 563 1 564 ) 565 566 WRAP(cbor_item_t **, 567 cbor_array_handle, 568 (const cbor_item_t *item), 569 NULL, 570 (item), 571 1 572 ) 573 574 WRAP(bool, 575 cbor_array_push, 576 (cbor_item_t *array, cbor_item_t *pushee), 577 false, 578 (array, pushee), 579 1 580 ) 581 582 WRAP(bool, 583 cbor_map_add, 584 (cbor_item_t *item, struct cbor_pair pair), 585 false, 586 (item, pair), 587 1 588 ) 589 590 WRAP(cbor_item_t *, 591 cbor_new_definite_map, 592 (size_t size), 593 NULL, 594 (size), 595 1 596 ) 597 598 WRAP(cbor_item_t *, 599 cbor_new_definite_array, 600 (size_t size), 601 NULL, 602 (size), 603 1 604 ) 605 606 WRAP(cbor_item_t *, 607 cbor_new_definite_bytestring, 608 (void), 609 NULL, 610 (), 611 1 612 ) 613 614 WRAP(size_t, 615 cbor_serialize_alloc, 616 (const cbor_item_t *item, cbor_mutable_data *buffer, 617 size_t *buffer_size), 618 0, 619 (item, buffer, buffer_size), 620 1 621 ) 622 623 WRAP(int, 624 fido_tx, 625 (fido_dev_t *d, uint8_t cmd, const void *buf, size_t count, int *ms), 626 -1, 627 (d, cmd, buf, count, ms), 628 1 629 ) 630 631 WRAP(int, 632 bind, 633 (int sockfd, const struct sockaddr *addr, socklen_t addrlen), 634 -1, 635 (sockfd, addr, addrlen), 636 1 637 ) 638