1 // cc_fuzz_target test for public key parsing.
2 
3 #include <stddef.h>
4 #include <stdio.h>
5 #include <stdint.h>
6 #include <stdlib.h>
7 #include <string.h>
8 
9 extern "C" {
10 
11 #include "includes.h"
12 #include "sshkey.h"
13 #include "ssherr.h"
14 
15 static struct sshkey *generate_or_die(int type, unsigned bits) {
16   int r;
17   struct sshkey *ret;
18   if ((r = sshkey_generate(type, bits, &ret)) != 0) {
19     fprintf(stderr, "generate(%d, %u): %s", type, bits, ssh_err(r));
20     abort();
21   }
22   return ret;
23 }
24 
25 int LLVMFuzzerTestOneInput(const uint8_t* sig, size_t slen)
26 {
27 #ifdef WITH_OPENSSL
28   static struct sshkey *rsa = generate_or_die(KEY_RSA, 2048);
29   static struct sshkey *dsa = generate_or_die(KEY_DSA, 1024);
30   static struct sshkey *ecdsa256 = generate_or_die(KEY_ECDSA, 256);
31   static struct sshkey *ecdsa384 = generate_or_die(KEY_ECDSA, 384);
32   static struct sshkey *ecdsa521 = generate_or_die(KEY_ECDSA, 521);
33 #endif
34   struct sshkey_sig_details *details = NULL;
35   static struct sshkey *ed25519 = generate_or_die(KEY_ED25519, 0);
36   static const char *data = "If everyone started announcing his nose had "
37       "run away, I don’t know how it would all end";
38   static const size_t dlen = strlen(data);
39 
40 #ifdef WITH_OPENSSL
41   sshkey_verify(rsa, sig, slen, (const u_char *)data, dlen, NULL, 0, &details);
42   sshkey_sig_details_free(details);
43   details = NULL;
44   sshkey_verify(dsa, sig, slen, (const u_char *)data, dlen, NULL, 0, &details);
45   sshkey_sig_details_free(details);
46   details = NULL;
47   sshkey_verify(ecdsa256, sig, slen, (const u_char *)data, dlen, NULL, 0, &details);
48   sshkey_sig_details_free(details);
49   details = NULL;
50   sshkey_verify(ecdsa384, sig, slen, (const u_char *)data, dlen, NULL, 0, &details);
51   sshkey_sig_details_free(details);
52   details = NULL;
53   sshkey_verify(ecdsa521, sig, slen, (const u_char *)data, dlen, NULL, 0, &details);
54   sshkey_sig_details_free(details);
55   details = NULL;
56 #endif
57   sshkey_verify(ed25519, sig, slen, (const u_char *)data, dlen, NULL, 0, &details);
58   sshkey_sig_details_free(details);
59   return 0;
60 }
61 
62 } // extern
63