Debian Codegraph @

tests/fuzz/ssh_sshsig_fuzzer.c

raw
1 /*
2 * Copyright 2025 Jakub Jelen <[email protected]>
3 *
4 * Licensed under the Apache License, Version 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 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <assert.h>
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21
22 #define LIBSSH_STATIC 1
23 #include "libssh/libssh.h"
24
25 #include "nallocinc.c"
26
27 static void _fuzz_finalize(void)
28 {
29 ssh_finalize();
30 }
31
32 int LLVMFuzzerInitialize(int *argc, char ***argv)
33 {
34 (void)argc;
35
36 nalloc_init(*argv[0]);
37
38 ssh_init();
39
40 atexit(_fuzz_finalize);
41
42 return 0;
43 }
44
45 int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
46 {
47 ssh_key pkey = NULL;
48 const char input[] = "badc0de";
49 const char namespace[] = "namespace";
50 char *signature = NULL;
51 int rc;
52
53 assert(nalloc_start(data, size) > 0);
54
55 signature = (char *)malloc(size + 1);
56 if (signature == NULL) {
57 goto out;
58 }
59 strncpy(signature, (const char *)data, size);
60 signature[size] = '\0';
61
62 rc = sshsig_verify(input, sizeof(input), signature, namespace, &pkey);
63 free(signature);
64 if (rc != SSH_OK) {
65 goto out;
66 }
67 ssh_key_free(pkey);
68
69 out:
70 nalloc_end();
71 return 0;
72 }