xref: /freebsd/crypto/krb5/src/tests/fuzzing/fuzz_krb5_ticket.c (revision f1c4c3daccbaf3820f0e2224de53df12fc952fcc)
1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /* tests/fuzzing/fuzz_krb5_ticket.c */
3 /*
4  * Copyright (C) 2024 by Arjun. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * * Redistributions of source code must retain the above copyright
11  *   notice, this list of conditions and the following disclaimer.
12  *
13  * * Redistributions in binary form must reproduce the above copyright
14  *   notice, this list of conditions and the following disclaimer in
15  *   the documentation and/or other materials provided with the
16  *   distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
23  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
29  * OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 /*
33  * Fuzzing harness implementation for krb5_decode_ticket.
34  */
35 
36 #include "autoconf.h"
37 #include <k5-int.h>
38 #include <krb5.h>
39 #include <string.h>
40 
41 #define kMinInputLength 2
42 #define kMaxInputLength 2048
43 
44 extern int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size);
45 
46 int
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)47 LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
48 {
49     krb5_error_code ret;
50     krb5_context context = NULL;
51     krb5_keytab defkt = NULL;
52     krb5_data data_in, *data_out;
53     krb5_ticket *ticket = NULL;
54 
55     if (size < kMinInputLength || size > kMaxInputLength)
56         return 0;
57 
58     data_in = make_data((void *)data, size);
59 
60     ret = krb5_init_context(&context);
61     if (ret)
62         return 0;
63 
64     ret = krb5_kt_default(context, &defkt);
65     if (ret)
66         goto cleanup;
67 
68     ret = krb5_decode_ticket(&data_in, &ticket);
69     if (ret)
70         goto cleanup;
71 
72     ret = encode_krb5_ticket(ticket, &data_out);
73     if (!ret)
74         krb5_free_data(context, data_out);
75 
76     krb5_server_decrypt_ticket_keytab(context, defkt, ticket);
77 
78 cleanup:
79     krb5_free_ticket(context, ticket);
80     if (defkt != NULL)
81         krb5_kt_close(context, defkt);
82     krb5_free_context(context);
83 
84     return 0;
85 }
86