/src/p11-kit/fuzz/uri_fuzzer.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2026, David Korczynski. |
3 | | * |
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 | | * * Redistributions of source code must retain the above |
11 | | * copyright notice, this list of conditions and the |
12 | | * following disclaimer. |
13 | | * * Redistributions in binary form must reproduce the |
14 | | * above copyright notice, this list of conditions and |
15 | | * the following disclaimer in the documentation and/or |
16 | | * other materials provided with the distribution. |
17 | | * * The names of contributors to this software may not be |
18 | | * used to endorse or promote products derived from this |
19 | | * software without specific prior written permission. |
20 | | * |
21 | | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
22 | | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
23 | | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
24 | | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
25 | | * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
26 | | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
27 | | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS |
28 | | * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
29 | | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
30 | | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF |
31 | | * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH |
32 | | * DAMAGE. |
33 | | * |
34 | | */ |
35 | | |
36 | | #include "config.h" |
37 | | #include "test.h" |
38 | | |
39 | | #include "fuzz/fuzz.h" |
40 | | #include "p11-kit/uri.h" |
41 | | |
42 | | #include <stdlib.h> |
43 | | #include <string.h> |
44 | | |
45 | | #ifdef __cplusplus |
46 | | extern "C" |
47 | | #endif |
48 | | int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) |
49 | 1.88k | { |
50 | 1.88k | P11KitUri *uri; |
51 | 1.88k | char *string; |
52 | 1.88k | char *nul_terminated; |
53 | 1.88k | int ret; |
54 | | |
55 | | /* p11_kit_uri_parse expects a NUL-terminated string */ |
56 | 1.88k | nul_terminated = malloc(size + 1); |
57 | 1.88k | if (!nul_terminated) |
58 | 0 | return 0; |
59 | 1.88k | memcpy(nul_terminated, data, size); |
60 | 1.88k | nul_terminated[size] = '\0'; |
61 | | |
62 | 1.88k | uri = p11_kit_uri_new(); |
63 | 1.88k | if (!uri) { |
64 | 0 | free(nul_terminated); |
65 | 0 | return 0; |
66 | 0 | } |
67 | | |
68 | | /* Parse with P11_KIT_URI_FOR_ANY to exercise all attribute paths */ |
69 | 1.88k | ret = p11_kit_uri_parse(nul_terminated, P11_KIT_URI_FOR_ANY, uri); |
70 | | |
71 | 1.88k | if (ret == P11_KIT_URI_OK) { |
72 | | /* Exercise the format (serialization) round-trip */ |
73 | 1.26k | if (p11_kit_uri_format(uri, P11_KIT_URI_FOR_ANY, &string) == P11_KIT_URI_OK) |
74 | 1.26k | free(string); |
75 | | |
76 | | /* Exercise attribute accessors */ |
77 | 1.26k | p11_kit_uri_get_module_info(uri); |
78 | 1.26k | p11_kit_uri_get_slot_info(uri); |
79 | 1.26k | p11_kit_uri_get_token_info(uri); |
80 | 1.26k | p11_kit_uri_get_pin_value(uri); |
81 | 1.26k | p11_kit_uri_get_pin_source(uri); |
82 | 1.26k | p11_kit_uri_get_module_name(uri); |
83 | 1.26k | p11_kit_uri_get_module_path(uri); |
84 | 1.26k | p11_kit_uri_any_unrecognized(uri); |
85 | 1.26k | } |
86 | | |
87 | | /* Exercise the message function for all result codes */ |
88 | 1.88k | p11_kit_uri_message(ret); |
89 | | |
90 | 1.88k | p11_kit_uri_free(uri); |
91 | 1.88k | free(nul_terminated); |
92 | | |
93 | 1.88k | return 0; |
94 | 1.88k | } |