Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update return type for EVP_EncodeUpdate #1481

Merged
merged 1 commit into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions crypto/base64/base64.c
Original file line number Diff line number Diff line change
Expand Up @@ -134,21 +134,21 @@ void EVP_EncodeInit(EVP_ENCODE_CTX *ctx) {
OPENSSL_memset(ctx, 0, sizeof(EVP_ENCODE_CTX));
}

void EVP_EncodeUpdate(EVP_ENCODE_CTX *ctx, uint8_t *out, int *out_len,
const uint8_t *in, size_t in_len) {
int EVP_EncodeUpdate(EVP_ENCODE_CTX *ctx, uint8_t *out, int *out_len,
const uint8_t *in, size_t in_len) {
size_t total = 0;

*out_len = 0;
if (in_len == 0) {
return;
return 0;
}

assert(ctx->data_used < sizeof(ctx->data));

if (sizeof(ctx->data) - ctx->data_used > in_len) {
OPENSSL_memcpy(&ctx->data[ctx->data_used], in, in_len);
ctx->data_used += (unsigned)in_len;
return;
return 1;
}

if (ctx->data_used != 0) {
Expand Down Expand Up @@ -178,7 +178,7 @@ void EVP_EncodeUpdate(EVP_ENCODE_CTX *ctx, uint8_t *out, int *out_len,

if (total + encoded + 1 < total) {
*out_len = 0;
return;
return 0;
}

total += encoded + 1;
Expand All @@ -194,8 +194,11 @@ void EVP_EncodeUpdate(EVP_ENCODE_CTX *ctx, uint8_t *out, int *out_len,
// We cannot signal an error, but we can at least avoid making *out_len
// negative.
total = 0;
samuel40791765 marked this conversation as resolved.
Show resolved Hide resolved
return 0;
}
*out_len = (int)total;

return 1;
}

void EVP_EncodeFinal(EVP_ENCODE_CTX *ctx, uint8_t *out, int *out_len) {
Expand Down
7 changes: 4 additions & 3 deletions crypto/base64/base64_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -206,9 +206,10 @@ TEST_P(Base64Test, EncodeDecode) {
EVP_EncodeInit(&ctx);

int out_len;
EVP_EncodeUpdate(&ctx, out, &out_len,
reinterpret_cast<const uint8_t *>(t.decoded),
decoded_len);
int ret = EVP_EncodeUpdate(&ctx, out, &out_len,
reinterpret_cast<const uint8_t *>(t.decoded),
decoded_len);
EXPECT_EQ(ret, (strlen(t.encoded) > 0 ? 1 : 0));
size_t total = out_len;

EVP_EncodeFinal(&ctx, out + total, &out_len);
Expand Down
6 changes: 4 additions & 2 deletions crypto/decrepit/bio/base64_bio.c
Original file line number Diff line number Diff line change
Expand Up @@ -396,8 +396,10 @@ static int b64_write(BIO *b, const char *in, int inl) {
ret += n;
}
} else {
EVP_EncodeUpdate(&(ctx->base64), (uint8_t *)ctx->buf, &ctx->buf_len,
(uint8_t *)in, n);
if(!EVP_EncodeUpdate(&(ctx->base64), (uint8_t *)ctx->buf, &ctx->buf_len,
(uint8_t *)in, n)) {
return ((ret == 0) ? -1 : ret);
}
assert(ctx->buf_len <= (int)sizeof(ctx->buf));
assert(ctx->buf_len >= ctx->buf_off);
ret += n;
Expand Down
4 changes: 3 additions & 1 deletion crypto/pem/pem_lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -561,7 +561,9 @@ int PEM_write_bio(BIO *bp, const char *name, const char *header,
i = j = 0;
while (len > 0) {
n = (int)((len > (PEM_BUFSIZE * 5)) ? (PEM_BUFSIZE * 5) : len);
EVP_EncodeUpdate(&ctx, buf, &outl, &(data[j]), n);
if(!EVP_EncodeUpdate(&ctx, buf, &outl, &(data[j]), n)) {
goto err;
}
if ((outl) && (BIO_write(bp, (char *)buf, outl) != outl)) {
goto err;
}
Expand Down
6 changes: 3 additions & 3 deletions include/openssl/base64.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,9 @@ OPENSSL_EXPORT void EVP_EncodeInit(EVP_ENCODE_CTX *ctx);
// version of them to |out| and sets |*out_len| to the number of bytes written.
// Some state may be contained in |ctx| so |EVP_EncodeFinal| must be used to
// flush it before using the encoded data.
OPENSSL_EXPORT void EVP_EncodeUpdate(EVP_ENCODE_CTX *ctx, uint8_t *out,
int *out_len, const uint8_t *in,
size_t in_len);
OPENSSL_EXPORT int EVP_EncodeUpdate(EVP_ENCODE_CTX *ctx, uint8_t *out,
int *out_len, const uint8_t *in,
size_t in_len);

// EVP_EncodeFinal flushes any remaining output bytes from |ctx| to |out| and
// sets |*out_len| to the number of bytes written.
Expand Down
Loading