/src/libssh/tests/fuzz/ssh_privkey_fuzzer.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 2023 Jakub Jelen <jjelen@redhat.com> |
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 | 4 | { |
32 | 4 | ssh_finalize(); |
33 | 4 | } |
34 | | |
35 | | int LLVMFuzzerInitialize(int *argc, char ***argv) |
36 | 32 | { |
37 | 32 | (void)argc; |
38 | | |
39 | 32 | nalloc_init(*argv[0]); |
40 | | |
41 | 32 | ssh_init(); |
42 | | |
43 | 32 | atexit(_fuzz_finalize); |
44 | | |
45 | 32 | return 0; |
46 | 32 | } |
47 | | |
48 | | int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) |
49 | 1.71k | { |
50 | 1.71k | ssh_key pkey = NULL; |
51 | 1.71k | uint8_t *input = NULL; |
52 | 1.71k | int rc; |
53 | | |
54 | 1.71k | assert(nalloc_start(data, size) > 0); |
55 | | |
56 | 1.71k | input = bin_to_base64(data, size); |
57 | 1.71k | if (input == NULL) { |
58 | 103 | goto out; |
59 | 103 | } |
60 | | |
61 | 1.61k | rc = ssh_pki_import_privkey_base64((char *)input, NULL, NULL, NULL, &pkey); |
62 | 1.61k | free(input); |
63 | 1.61k | if (rc != SSH_OK) { |
64 | 1.61k | goto out; |
65 | 1.61k | } |
66 | 0 | ssh_key_free(pkey); |
67 | |
|
68 | 1.71k | out: |
69 | 1.71k | nalloc_end(); |
70 | 1.71k | return 0; |
71 | 0 | } |
72 | | |