Coverage Report

Created: 2024-02-05 19:20

/libfido2/src/compress.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright (c) 2020-2022 Yubico AB. All rights reserved.
3
 * Use of this source code is governed by a BSD-style
4
 * license that can be found in the LICENSE file.
5
 * SPDX-License-Identifier: BSD-2-Clause
6
 */
7
8
#include <zlib.h>
9
#include "fido.h"
10
11
35.3k
#define BOUND (1024UL * 1024UL)
12
13
/* zlib inflate (raw + headers) */
14
static int
15
rfc1950_inflate(fido_blob_t *out, const fido_blob_t *in, size_t origsiz)
16
323
{
17
323
        u_long ilen, olen;
18
323
        int z;
19
20
323
        memset(out, 0, sizeof(*out));
21
22
323
        if (in->len > ULONG_MAX || (ilen = (u_long)in->len) > BOUND ||
23
323
            origsiz > ULONG_MAX || (olen = (u_long)origsiz) > BOUND) {
24
0
                fido_log_debug("%s: in->len=%zu, origsiz=%zu", __func__,
25
0
                    in->len, origsiz);
26
0
                return FIDO_ERR_INVALID_ARGUMENT;
27
0
        }
28
29
323
        if ((out->ptr = calloc(1, olen)) == NULL)
30
3
                return FIDO_ERR_INTERNAL;
31
320
        out->len = olen;
32
33
320
        if ((z = uncompress(out->ptr, &olen, in->ptr, ilen)) != Z_OK ||
34
320
            olen > SIZE_MAX || olen != out->len) {
35
253
                fido_log_debug("%s: uncompress: %d, olen=%lu, out->len=%zu",
36
253
                    __func__, z, olen, out->len);
37
253
                fido_blob_reset(out);
38
253
                return FIDO_ERR_COMPRESS;
39
253
        }
40
41
67
        return FIDO_OK;
42
320
}
43
44
/* raw inflate */
45
static int
46
rfc1951_inflate(fido_blob_t *out, const fido_blob_t *in, size_t origsiz)
47
256
{
48
256
        z_stream zs;
49
256
        u_int ilen, olen;
50
256
        int r, z;
51
52
256
        memset(&zs, 0, sizeof(zs));
53
256
        memset(out, 0, sizeof(*out));
54
55
256
        if (in->len > UINT_MAX || (ilen = (u_int)in->len) > BOUND ||
56
256
            origsiz > UINT_MAX || (olen = (u_int)origsiz) > BOUND) {
57
0
                fido_log_debug("%s: in->len=%zu, origsiz=%zu", __func__,
58
0
                    in->len, origsiz);
59
0
                return FIDO_ERR_INVALID_ARGUMENT;
60
0
        }
61
256
        if ((z = inflateInit2(&zs, -MAX_WBITS)) != Z_OK) {
62
0
                fido_log_debug("%s: inflateInit2: %d", __func__, z);
63
0
                return FIDO_ERR_COMPRESS;
64
0
        }
65
66
256
        if ((out->ptr = calloc(1, olen)) == NULL) {
67
1
                r = FIDO_ERR_INTERNAL;
68
1
                goto fail;
69
1
        }
70
255
        out->len = olen;
71
255
        zs.next_in = in->ptr;
72
255
        zs.avail_in = ilen;
73
255
        zs.next_out = out->ptr;
74
255
        zs.avail_out = olen;
75
76
255
        if ((z = inflate(&zs, Z_FINISH)) != Z_STREAM_END) {
77
3
                fido_log_debug("%s: inflate: %d", __func__, z);
78
3
                r = FIDO_ERR_COMPRESS;
79
3
                goto fail;
80
3
        }
81
252
        if (zs.avail_out != 0) {
82
0
                fido_log_debug("%s: %u != 0", __func__, zs.avail_out);
83
0
                r = FIDO_ERR_COMPRESS;
84
0
                goto fail;
85
0
        }
86
87
252
        r = FIDO_OK;
88
256
fail:
89
256
        if ((z = inflateEnd(&zs)) != Z_OK) {
90
0
                fido_log_debug("%s: inflateEnd: %d", __func__, z);
91
0
                r = FIDO_ERR_COMPRESS;
92
0
        }
93
256
        if (r != FIDO_OK)
94
4
                fido_blob_reset(out);
95
96
256
        return r;
97
252
}
98
99
/* raw deflate */
100
static int
101
rfc1951_deflate(fido_blob_t *out, const fido_blob_t *in)
102
16.8k
{
103
16.8k
        z_stream zs;
104
16.8k
        u_int ilen, olen;
105
16.8k
        int r, z;
106
107
16.8k
        memset(&zs, 0, sizeof(zs));
108
16.8k
        memset(out, 0, sizeof(*out));
109
110
16.8k
        if (in->len > UINT_MAX || (ilen = (u_int)in->len) > BOUND) {
111
0
                fido_log_debug("%s: in->len=%zu", __func__, in->len);
112
0
                return FIDO_ERR_INVALID_ARGUMENT;
113
0
        }
114
16.8k
        if ((z = deflateInit2(&zs, Z_DEFAULT_COMPRESSION, Z_DEFLATED,
115
16.8k
            -MAX_WBITS, 8, Z_DEFAULT_STRATEGY)) != Z_OK) {
116
6
                fido_log_debug("%s: deflateInit2: %d", __func__, z);
117
6
                return FIDO_ERR_COMPRESS;
118
6
        }
119
120
16.7k
        olen = BOUND;
121
16.7k
        if ((out->ptr = calloc(1, olen)) == NULL) {
122
3
                r = FIDO_ERR_INTERNAL;
123
3
                goto fail;
124
3
        }
125
16.7k
        out->len = olen;
126
16.7k
        zs.next_in = in->ptr;
127
16.7k
        zs.avail_in = ilen;
128
16.7k
        zs.next_out = out->ptr;
129
16.7k
        zs.avail_out = olen;
130
131
16.7k
        if ((z = deflate(&zs, Z_FINISH)) != Z_STREAM_END) {
132
5
                fido_log_debug("%s: inflate: %d", __func__, z);
133
5
                r = FIDO_ERR_COMPRESS;
134
5
                goto fail;
135
5
        }
136
16.7k
        if (zs.avail_out >= out->len) {
137
11
                fido_log_debug("%s: %u > %zu", __func__, zs.avail_out,
138
11
                    out->len);
139
11
                r = FIDO_ERR_COMPRESS;
140
11
                goto fail;
141
11
        }
142
16.7k
        out->len -= zs.avail_out;
143
144
16.7k
        r = FIDO_OK;
145
16.7k
fail:
146
16.7k
        if ((z = deflateEnd(&zs)) != Z_OK) {
147
0
                fido_log_debug("%s: deflateEnd: %d", __func__, z);
148
0
                r = FIDO_ERR_COMPRESS;
149
0
        }
150
16.7k
        if (r != FIDO_OK)
151
19
                fido_blob_reset(out);
152
153
16.7k
        return r;
154
16.7k
}
155
156
int
157
fido_compress(fido_blob_t *out, const fido_blob_t *in)
158
16.8k
{
159
16.8k
        return rfc1951_deflate(out, in);
160
16.8k
}
161
162
int
163
fido_uncompress(fido_blob_t *out, const fido_blob_t *in, size_t origsiz)
164
323
{
165
323
        if (rfc1950_inflate(out, in, origsiz) == FIDO_OK)
166
67
                return FIDO_OK; /* backwards compat with libfido2 < 1.11 */
167
256
        return rfc1951_inflate(out, in, origsiz);
168
323
}