Debian Codegraph @

tests/fuzz/ssh_privkey_fuzzer.c

raw
1 /*
2 * Copyright 2023 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 "config.h"
18
19 #include <assert.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23
24 #define LIBSSH_STATIC 1
25 #include "libssh/libssh.h"
26 #include "libssh/priv.h"
27
28 #include "nallocinc.c"
29
30 static void _fuzz_finalize(void)
31 {
32 ssh_finalize();
33 }
34
35 int LLVMFuzzerInitialize(int *argc, char ***argv)
36 {
37 (void)argc;
38
39 nalloc_init(*argv[0]);
40
41 ssh_init();
42
43 atexit(_fuzz_finalize);
44
45 return 0;
46 }
47
48 int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
49 {
50 ssh_key pkey = NULL;
51 uint8_t *input = NULL;
52 int rc;
53
54 assert(nalloc_start(data, size) > 0);
55
56 input = bin_to_base64(data, size);
57 if (input == NULL) {
58 goto out;
59 }
60
61 rc = ssh_pki_import_privkey_base64((char *)input, NULL, NULL, NULL, &pkey);
62 free(input);
63 if (rc != SSH_OK) {
64 goto out;
65 }
66 ssh_key_free(pkey);
67
68 out:
69 nalloc_end();
70 return 0;
71 }
72