/src/openssl34/fuzz/decoder.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 2023-2024 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * |
4 | | * Licensed under the Apache License 2.0 (the "License"); |
5 | | * you may not use this file except in compliance with the License. |
6 | | * You may obtain a copy of the License at |
7 | | * https://www.openssl.org/source/license.html |
8 | | * or in the file LICENSE in the source distribution. |
9 | | */ |
10 | | |
11 | | #include <openssl/decoder.h> |
12 | | #include <openssl/err.h> |
13 | | #include <openssl/rand.h> |
14 | | #include "fuzzer.h" |
15 | | |
16 | | static ASN1_PCTX *pctx; |
17 | | |
18 | | int FuzzerInitialize(int *argc, char ***argv) |
19 | 214 | { |
20 | 214 | FuzzerSetRand(); |
21 | | |
22 | 214 | OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CRYPTO_STRINGS |
23 | 214 | | OPENSSL_INIT_ADD_ALL_CIPHERS |
24 | 214 | | OPENSSL_INIT_ADD_ALL_DIGESTS, |
25 | 214 | NULL); |
26 | | |
27 | 214 | pctx = ASN1_PCTX_new(); |
28 | 214 | ASN1_PCTX_set_flags(pctx, ASN1_PCTX_FLAGS_SHOW_ABSENT | ASN1_PCTX_FLAGS_SHOW_SEQUENCE | ASN1_PCTX_FLAGS_SHOW_SSOF | ASN1_PCTX_FLAGS_SHOW_TYPE | ASN1_PCTX_FLAGS_SHOW_FIELD_STRUCT_NAME); |
29 | 214 | ASN1_PCTX_set_str_flags(pctx, ASN1_STRFLGS_UTF8_CONVERT | ASN1_STRFLGS_SHOW_TYPE | ASN1_STRFLGS_DUMP_ALL); |
30 | | |
31 | 214 | ERR_clear_error(); |
32 | 214 | CRYPTO_free_ex_index(0, -1); |
33 | 214 | return 1; |
34 | 214 | } |
35 | | |
36 | | int FuzzerTestOneInput(const uint8_t *buf, size_t len) |
37 | 39.9k | { |
38 | 39.9k | OSSL_DECODER_CTX *dctx; |
39 | 39.9k | EVP_PKEY *pkey = NULL; |
40 | 39.9k | EVP_PKEY_CTX *ctx = NULL; |
41 | 39.9k | BIO *bio; |
42 | | |
43 | 39.9k | bio = BIO_new(BIO_s_null()); |
44 | 39.9k | dctx = OSSL_DECODER_CTX_new_for_pkey(&pkey, NULL, NULL, NULL, 0, NULL, |
45 | 39.9k | NULL); |
46 | 39.9k | if (dctx == NULL) { |
47 | 0 | return 0; |
48 | 0 | } |
49 | 39.9k | if (OSSL_DECODER_from_data(dctx, &buf, &len)) { |
50 | 20.4k | EVP_PKEY *pkey2; |
51 | | |
52 | 20.4k | EVP_PKEY_print_public(bio, pkey, 1, pctx); |
53 | 20.4k | EVP_PKEY_print_private(bio, pkey, 1, pctx); |
54 | 20.4k | EVP_PKEY_print_params(bio, pkey, 1, pctx); |
55 | | |
56 | 20.4k | pkey2 = EVP_PKEY_dup(pkey); |
57 | 20.4k | OPENSSL_assert(pkey2 != NULL); |
58 | 20.4k | EVP_PKEY_eq(pkey, pkey2); |
59 | 20.4k | EVP_PKEY_free(pkey2); |
60 | | |
61 | 20.4k | ctx = EVP_PKEY_CTX_new(pkey, NULL); |
62 | | /* |
63 | | * Param check will take too long time on large DH parameters. |
64 | | * Skip it. |
65 | | */ |
66 | 20.4k | if ((!EVP_PKEY_is_a(pkey, "DH") && !EVP_PKEY_is_a(pkey, "DHX")) |
67 | 7.68k | || EVP_PKEY_get_bits(pkey) <= 2048) |
68 | 20.3k | EVP_PKEY_param_check(ctx); |
69 | | |
70 | 20.4k | EVP_PKEY_public_check(ctx); |
71 | | /* Private and pairwise checks are unbounded, skip for large keys. */ |
72 | 20.4k | if (EVP_PKEY_get_bits(pkey) <= 4096) { |
73 | 18.4k | EVP_PKEY_private_check(ctx); |
74 | 18.4k | EVP_PKEY_pairwise_check(ctx); |
75 | 18.4k | } |
76 | 20.4k | OPENSSL_assert(ctx != NULL); |
77 | 20.4k | EVP_PKEY_CTX_free(ctx); |
78 | 20.4k | EVP_PKEY_free(pkey); |
79 | 20.4k | } |
80 | 39.9k | OSSL_DECODER_CTX_free(dctx); |
81 | | |
82 | 39.9k | BIO_free(bio); |
83 | 39.9k | ERR_clear_error(); |
84 | 39.9k | return 0; |
85 | 39.9k | } |
86 | | |
87 | | void FuzzerCleanup(void) |
88 | 0 | { |
89 | 0 | ASN1_PCTX_free(pctx); |
90 | 0 | FuzzerClearRand(); |
91 | 0 | } |