diff options
author | Richard Levitte <levitte@openssl.org> | 2021-02-23 22:42:18 +0100 |
---|---|---|
committer | Richard Levitte <levitte@openssl.org> | 2021-02-27 16:14:09 +0100 |
commit | 1d73e2adae9c80d359d6d85c9f65d97a86add542 (patch) | |
tree | 737601f26e2d22173595081da89b11c5e96dd6fa /crypto/asn1/i2d_evp.c | |
parent | c8182743a7764ba8c9e61665722cae06fa8edb62 (diff) |
crypto/asn1/i2d_evp.c: Fix i2d_provided() to return a proper length
Fixes #14258
Reviewed-by: Tomas Mraz <tomas@openssl.org>
Reviewed-by: Ben Kaduk <kaduk@mit.edu>
(Merged from https://github.com/openssl/openssl/pull/14291)
Diffstat (limited to 'crypto/asn1/i2d_evp.c')
-rw-r--r-- | crypto/asn1/i2d_evp.c | 14 |
1 files changed, 10 insertions, 4 deletions
diff --git a/crypto/asn1/i2d_evp.c b/crypto/asn1/i2d_evp.c index ffcb34aa20..2a101a6fa3 100644 --- a/crypto/asn1/i2d_evp.c +++ b/crypto/asn1/i2d_evp.c @@ -42,8 +42,10 @@ static int i2d_provided(const EVP_PKEY *a, int selection, output_info++) { /* * The i2d_ calls don't take a boundary length for *pp. However, - * OSSL_ENCODER_CTX_get_num_encoders() needs one, so we make one - * up. + * OSSL_ENCODER_to_data() needs one, so we make one up. Because + * OSSL_ENCODER_to_data() decrements this number by the amount of + * bytes written, we need to calculate the length written further + * down, when pp != NULL. */ size_t len = INT_MAX; @@ -53,8 +55,12 @@ static int i2d_provided(const EVP_PKEY *a, int selection, NULL); if (ctx == NULL) return -1; - if (OSSL_ENCODER_to_data(ctx, pp, &len)) - ret = (int)len; + if (OSSL_ENCODER_to_data(ctx, pp, &len)) { + if (pp == NULL) + ret = (int)len; + else + ret = INT_MAX - (int)len; + } OSSL_ENCODER_CTX_free(ctx); ctx = NULL; } |