/src/gnutls/fuzz/gnutls_srp_server_fuzzer.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright (C) 2017 Nikos Mavrogiannopoulos |
3 | | * |
4 | | * Permission is hereby granted, free of charge, to any person obtaining a |
5 | | * copy of this software and associated documentation files (the "Software"), |
6 | | * to deal in the Software without restriction, including without limitation |
7 | | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
8 | | * and/or sell copies of the Software, and to permit persons to whom the |
9 | | * Software is furnished to do so, subject to the following conditions: |
10 | | * |
11 | | * The above copyright notice and this permission notice shall be included in |
12 | | * all copies or substantial portions of the Software. |
13 | | * |
14 | | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
15 | | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
16 | | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
17 | | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
18 | | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
19 | | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
20 | | * DEALINGS IN THE SOFTWARE. |
21 | | * |
22 | | */ |
23 | | |
24 | | #include <assert.h> |
25 | | #include <stdint.h> |
26 | | #include <unistd.h> |
27 | | #include <string.h> |
28 | | #include <stdlib.h> |
29 | | |
30 | | #include <gnutls/gnutls.h> |
31 | | |
32 | | #include "certs.h" |
33 | | #include "srp.h" |
34 | | #include "mem.h" |
35 | | #include "fuzzer.h" |
36 | | |
37 | | static int srp_cb(gnutls_session_t session, const char *username, |
38 | | gnutls_datum_t *salt, gnutls_datum_t *verifier, |
39 | | gnutls_datum_t *generator, gnutls_datum_t *prime) |
40 | 0 | { |
41 | 0 | int ret; |
42 | |
|
43 | 0 | salt->data = (unsigned char *)gnutls_malloc(SALT_SIZE); |
44 | 0 | if (salt->data == NULL) |
45 | 0 | return -1; |
46 | | |
47 | 0 | memcpy(salt->data, (unsigned char *)SALT, SALT_SIZE); |
48 | 0 | salt->size = SALT_SIZE; |
49 | |
|
50 | 0 | generator->data = (unsigned char *)gnutls_malloc( |
51 | 0 | gnutls_srp_1024_group_generator.size); |
52 | 0 | if (generator->data == NULL) |
53 | 0 | return -1; |
54 | | |
55 | 0 | memcpy(generator->data, gnutls_srp_1024_group_generator.data, |
56 | 0 | gnutls_srp_1024_group_generator.size); |
57 | 0 | generator->size = gnutls_srp_1024_group_generator.size; |
58 | |
|
59 | 0 | prime->data = (unsigned char *)gnutls_malloc( |
60 | 0 | gnutls_srp_1024_group_prime.size); |
61 | 0 | if (prime->data == NULL) |
62 | 0 | return -1; |
63 | | |
64 | 0 | memcpy(prime->data, gnutls_srp_1024_group_prime.data, |
65 | 0 | gnutls_srp_1024_group_prime.size); |
66 | 0 | prime->size = gnutls_srp_1024_group_prime.size; |
67 | |
|
68 | 0 | ret = gnutls_srp_verifier(USERNAME, PASSWORD, salt, generator, prime, |
69 | 0 | verifier); |
70 | 0 | if (ret < 0) |
71 | 0 | return -1; |
72 | | |
73 | 0 | return 0; |
74 | 0 | } |
75 | | |
76 | | int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) |
77 | | { |
78 | | IGNORE_CERTS; |
79 | | int res; |
80 | | gnutls_datum_t rsa_cert, rsa_key; |
81 | | gnutls_datum_t ecdsa_cert, ecdsa_key; |
82 | | gnutls_datum_t ed25519_cert, ed25519_key; |
83 | | gnutls_session_t session; |
84 | | gnutls_certificate_credentials_t xcred; |
85 | | gnutls_srp_server_credentials_t pcred; |
86 | | struct mem_st memdata; |
87 | | |
88 | | res = gnutls_init(&session, GNUTLS_SERVER); |
89 | | assert(res >= 0); |
90 | | |
91 | | res = gnutls_certificate_allocate_credentials(&xcred); |
92 | | assert(res >= 0); |
93 | | |
94 | | res = gnutls_srp_allocate_server_credentials(&pcred); |
95 | | assert(res >= 0); |
96 | | |
97 | | gnutls_srp_set_server_credentials_function(pcred, srp_cb); |
98 | | |
99 | | rsa_cert.data = (unsigned char *)kRSACertificateDER; |
100 | | rsa_cert.size = sizeof(kRSACertificateDER); |
101 | | rsa_key.data = (unsigned char *)kRSAPrivateKeyDER; |
102 | | rsa_key.size = sizeof(kRSAPrivateKeyDER); |
103 | | |
104 | | ecdsa_cert.data = (unsigned char *)kECDSACertificateDER; |
105 | | ecdsa_cert.size = sizeof(kECDSACertificateDER); |
106 | | ecdsa_key.data = (unsigned char *)kECDSAPrivateKeyDER; |
107 | | ecdsa_key.size = sizeof(kECDSAPrivateKeyDER); |
108 | | |
109 | | ed25519_cert.data = (unsigned char *)kEd25519CertificateDER; |
110 | | ed25519_cert.size = sizeof(kEd25519CertificateDER); |
111 | | ed25519_key.data = (unsigned char *)kEd25519PrivateKeyDER; |
112 | | ed25519_key.size = sizeof(kEd25519PrivateKeyDER); |
113 | | |
114 | | res = gnutls_certificate_set_x509_key_mem(xcred, &rsa_cert, &rsa_key, |
115 | | GNUTLS_X509_FMT_DER); |
116 | | assert(res >= 0); |
117 | | |
118 | | res = gnutls_certificate_set_x509_key_mem( |
119 | | xcred, &ecdsa_cert, &ecdsa_key, GNUTLS_X509_FMT_DER); |
120 | | assert(res >= 0); |
121 | | |
122 | | res = gnutls_certificate_set_x509_key_mem( |
123 | | xcred, &ed25519_cert, &ed25519_key, GNUTLS_X509_FMT_DER); |
124 | | assert(res >= 0); |
125 | | |
126 | | gnutls_certificate_set_known_dh_params(xcred, GNUTLS_SEC_PARAM_MEDIUM); |
127 | | |
128 | | res = gnutls_credentials_set(session, GNUTLS_CRD_CERTIFICATE, xcred); |
129 | | assert(res >= 0); |
130 | | |
131 | | res = gnutls_credentials_set(session, GNUTLS_CRD_SRP, pcred); |
132 | | assert(res >= 0); |
133 | | |
134 | | res = gnutls_priority_set_direct( |
135 | | session, "NORMAL:-KX-ALL:+SRP:+SRP-RSA:+SRP-DSS", NULL); |
136 | | assert(res >= 0); |
137 | | |
138 | | memdata.data = data; |
139 | | memdata.size = size; |
140 | | |
141 | | gnutls_transport_set_push_function(session, mem_push); |
142 | | gnutls_transport_set_pull_function(session, mem_pull); |
143 | | gnutls_transport_set_pull_timeout_function(session, mem_pull_timeout); |
144 | | gnutls_transport_set_ptr(session, &memdata); |
145 | | |
146 | | do { |
147 | | res = gnutls_handshake(session); |
148 | | } while (res < 0 && gnutls_error_is_fatal(res) == 0); |
149 | | if (res >= 0) { |
150 | | for (;;) { |
151 | | char buf[16384]; |
152 | | res = gnutls_record_recv(session, buf, sizeof(buf)); |
153 | | if (res <= 0) { |
154 | | break; |
155 | | } |
156 | | } |
157 | | } |
158 | | |
159 | | gnutls_deinit(session); |
160 | | gnutls_certificate_free_credentials(xcred); |
161 | | gnutls_srp_free_server_credentials(pcred); |
162 | | return 0; |
163 | | } |