Coverage Report

Created: 2025-06-13 06:57

/src/openssl/crypto/buffer/buffer.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
3
 *
4
 * Licensed under the Apache License 2.0 (the "License").  You may not use
5
 * this file except in compliance with the License.  You can obtain a copy
6
 * in the file LICENSE in the source distribution or at
7
 * https://www.openssl.org/source/license.html
8
 */
9
10
#include <stdio.h>
11
#include "internal/cryptlib.h"
12
#include <openssl/buffer.h>
13
14
/*
15
 * LIMIT_BEFORE_EXPANSION is the maximum n such that (n+3)/3*4 < 2**31. That
16
 * function is applied in several functions in this file and this limit
17
 * ensures that the result fits in an int.
18
 */
19
0
#define LIMIT_BEFORE_EXPANSION 0x5ffffffc
20
21
BUF_MEM *BUF_MEM_new_ex(unsigned long flags)
22
0
{
23
0
    BUF_MEM *ret;
24
25
0
    ret = BUF_MEM_new();
26
0
    if (ret != NULL)
27
0
        ret->flags = flags;
28
0
    return ret;
29
0
}
30
31
BUF_MEM *BUF_MEM_new(void)
32
0
{
33
0
    BUF_MEM *ret;
34
35
0
    ret = OPENSSL_zalloc(sizeof(*ret));
36
0
    if (ret == NULL)
37
0
        return NULL;
38
0
    return ret;
39
0
}
40
41
void BUF_MEM_free(BUF_MEM *a)
42
0
{
43
0
    if (a == NULL)
44
0
        return;
45
0
    if (a->data != NULL) {
46
0
        if (a->flags & BUF_MEM_FLAG_SECURE)
47
0
            OPENSSL_secure_clear_free(a->data, a->max);
48
0
        else
49
0
            OPENSSL_clear_free(a->data, a->max);
50
0
    }
51
0
    OPENSSL_free(a);
52
0
}
53
54
/* Allocate a block of secure memory; copy over old data if there
55
 * was any, and then free it. */
56
static char *sec_alloc_realloc(BUF_MEM *str, size_t len)
57
0
{
58
0
    char *ret;
59
60
0
    ret = OPENSSL_secure_malloc(len);
61
0
    if (str->data != NULL) {
62
0
        if (ret != NULL) {
63
0
            memcpy(ret, str->data, str->length);
64
0
            OPENSSL_secure_clear_free(str->data, str->length);
65
0
            str->data = NULL;
66
0
        }
67
0
    }
68
0
    return ret;
69
0
}
70
71
size_t BUF_MEM_grow(BUF_MEM *str, size_t len)
72
0
{
73
0
    char *ret;
74
0
    size_t n;
75
76
0
    if (str->length >= len) {
77
0
        str->length = len;
78
0
        return len;
79
0
    }
80
0
    if (str->max >= len) {
81
0
        if (str->data != NULL)
82
0
            memset(&str->data[str->length], 0, len - str->length);
83
0
        str->length = len;
84
0
        return len;
85
0
    }
86
    /* This limit is sufficient to ensure (len+3)/3*4 < 2**31 */
87
0
    if (len > LIMIT_BEFORE_EXPANSION) {
88
0
        ERR_raise(ERR_LIB_BUF, ERR_R_PASSED_INVALID_ARGUMENT);
89
0
        return 0;
90
0
    }
91
0
    n = (len + 3) / 3 * 4;
92
0
    if ((str->flags & BUF_MEM_FLAG_SECURE))
93
0
        ret = sec_alloc_realloc(str, n);
94
0
    else
95
0
        ret = OPENSSL_realloc(str->data, n);
96
0
    if (ret == NULL) {
97
0
        len = 0;
98
0
    } else {
99
0
        str->data = ret;
100
0
        str->max = n;
101
0
        memset(&str->data[str->length], 0, len - str->length);
102
0
        str->length = len;
103
0
    }
104
0
    return len;
105
0
}
106
107
size_t BUF_MEM_grow_clean(BUF_MEM *str, size_t len)
108
0
{
109
0
    char *ret;
110
0
    size_t n;
111
112
0
    if (str->length >= len) {
113
0
        if (str->data != NULL)
114
0
            memset(&str->data[len], 0, str->length - len);
115
0
        str->length = len;
116
0
        return len;
117
0
    }
118
0
    if (str->max >= len) {
119
0
        memset(&str->data[str->length], 0, len - str->length);
120
0
        str->length = len;
121
0
        return len;
122
0
    }
123
    /* This limit is sufficient to ensure (len+3)/3*4 < 2**31 */
124
0
    if (len > LIMIT_BEFORE_EXPANSION) {
125
0
        ERR_raise(ERR_LIB_BUF, ERR_R_PASSED_INVALID_ARGUMENT);
126
0
        return 0;
127
0
    }
128
0
    n = (len + 3) / 3 * 4;
129
0
    if ((str->flags & BUF_MEM_FLAG_SECURE))
130
0
        ret = sec_alloc_realloc(str, n);
131
0
    else
132
0
        ret = OPENSSL_clear_realloc(str->data, str->max, n);
133
0
    if (ret == NULL) {
134
0
        len = 0;
135
0
    } else {
136
0
        str->data = ret;
137
0
        str->max = n;
138
0
        memset(&str->data[str->length], 0, len - str->length);
139
0
        str->length = len;
140
0
    }
141
0
    return len;
142
0
}
143
144
void BUF_reverse(unsigned char *out, const unsigned char *in, size_t size)
145
0
{
146
0
    size_t i;
147
0
    if (in) {
148
0
        out += size - 1;
149
0
        for (i = 0; i < size; i++)
150
0
            *out-- = *in++;
151
0
    } else {
152
0
        unsigned char *q;
153
0
        char c;
154
0
        q = out + size - 1;
155
0
        for (i = 0; i < size / 2; i++) {
156
0
            c = *q;
157
0
            *q-- = *out;
158
0
            *out++ = c;
159
0
        }
160
0
    }
161
0
}