/src/openssl41/fuzz/pkcs12.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 2026 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 | | /* |
12 | | * Test PKCS12 parsing with fuzzed input. |
13 | | */ |
14 | | |
15 | | #include <openssl/bio.h> |
16 | | #include <openssl/err.h> |
17 | | #include <openssl/pkcs12.h> |
18 | | #include <openssl/x509.h> |
19 | | #include "fuzzer.h" |
20 | | |
21 | | int FuzzerInitialize(int *argc, char ***argv) |
22 | 254 | { |
23 | 254 | OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CRYPTO_STRINGS, NULL); |
24 | 254 | ERR_clear_error(); |
25 | 254 | CRYPTO_free_ex_index(0, -1); |
26 | | |
27 | 254 | return 1; |
28 | 254 | } |
29 | | |
30 | | int FuzzerTestOneInput(const uint8_t *buf, size_t len) |
31 | 8.88k | { |
32 | 8.88k | PKCS12 *p12; |
33 | 8.88k | BIO *in; |
34 | 8.88k | EVP_PKEY *pkey = NULL; |
35 | 8.88k | X509 *cert = NULL; |
36 | 8.88k | STACK_OF(X509) *ca = NULL; |
37 | | |
38 | 8.88k | if (len == 0 || len > INT_MAX) |
39 | 0 | return 0; |
40 | | |
41 | 8.88k | in = BIO_new(BIO_s_mem()); |
42 | 8.88k | if (in == NULL) { |
43 | 0 | ERR_clear_error(); |
44 | 0 | return 0; |
45 | 0 | } |
46 | 8.88k | if ((size_t)BIO_write(in, buf, (int)len) != len) { |
47 | 0 | BIO_free(in); |
48 | 0 | ERR_clear_error(); |
49 | 0 | return 0; |
50 | 0 | } |
51 | 8.88k | p12 = d2i_PKCS12_bio(in, NULL); |
52 | 8.88k | if (p12 != NULL) { |
53 | 2.60k | PKCS12_verify_mac(p12, NULL, 0); |
54 | 2.60k | PKCS12_verify_mac(p12, "", 0); |
55 | | |
56 | 2.60k | PKCS12_parse(p12, NULL, &pkey, &cert, &ca); |
57 | 2.60k | EVP_PKEY_free(pkey); |
58 | 2.60k | X509_free(cert); |
59 | 2.60k | OSSL_STACK_OF_X509_free(ca); |
60 | | |
61 | 2.60k | pkey = NULL; |
62 | 2.60k | cert = NULL; |
63 | 2.60k | ca = NULL; |
64 | 2.60k | PKCS12_parse(p12, "", &pkey, &cert, &ca); |
65 | 2.60k | EVP_PKEY_free(pkey); |
66 | 2.60k | X509_free(cert); |
67 | 2.60k | OSSL_STACK_OF_X509_free(ca); |
68 | | |
69 | 2.60k | PKCS12_free(p12); |
70 | 2.60k | } |
71 | | |
72 | 8.88k | BIO_free(in); |
73 | 8.88k | ERR_clear_error(); |
74 | | |
75 | 8.88k | return 0; |
76 | 8.88k | } |
77 | | |
78 | | void FuzzerCleanup(void) |
79 | 0 | { |
80 | 0 | } |