{
int nlen, n, i, j, outl;
unsigned char *buf = NULL;
- EVP_ENCODE_CTX ctx;
+ EVP_ENCODE_CTX *ctx = EVP_ENCODE_CTX_new();
int reason = ERR_R_BUF_LIB;
- EVP_EncodeInit(&ctx);
+ if (ctx == NULL) {
+ reason = ERR_R_MALLOC_FAILURE;
+ goto err;
+ }
+
+ EVP_EncodeInit(ctx);
nlen = strlen(name);
if ((BIO_write(bp, "-----BEGIN ", 11) != 11) ||
i = j = 0;
while (len > 0) {
n = (int)((len > (PEM_BUFSIZE * 5)) ? (PEM_BUFSIZE * 5) : len);
- EVP_EncodeUpdate(&ctx, buf, &outl, &(data[j]), n);
+ EVP_EncodeUpdate(ctx, buf, &outl, &(data[j]), n);
if ((outl) && (BIO_write(bp, (char *)buf, outl) != outl))
goto err;
i += outl;
len -= n;
j += n;
}
- EVP_EncodeFinal(&ctx, buf, &outl);
+ EVP_EncodeFinal(ctx, buf, &outl);
if ((outl > 0) && (BIO_write(bp, (char *)buf, outl) != outl))
goto err;
- OPENSSL_clear_free(buf, PEM_BUFSIZE * 8);
- buf = NULL;
if ((BIO_write(bp, "-----END ", 9) != 9) ||
(BIO_write(bp, name, nlen) != nlen) ||
(BIO_write(bp, "-----\n", 6) != 6))
goto err;
+ OPENSSL_clear_free(buf, PEM_BUFSIZE * 8);
+ EVP_ENCODE_CTX_free(ctx);
return (i + outl);
err:
OPENSSL_clear_free(buf, PEM_BUFSIZE * 8);
+ EVP_ENCODE_CTX_free(ctx);
PEMerr(PEM_F_PEM_WRITE_BIO, reason);
return (0);
}
int PEM_read_bio(BIO *bp, char **name, char **header, unsigned char **data,
long *len)
{
- EVP_ENCODE_CTX ctx;
+ EVP_ENCODE_CTX *ctx = EVP_ENCODE_CTX_new();
int end = 0, i, k, bl = 0, hl = 0, nohead = 0;
char buf[256];
BUF_MEM *nameB;
BUF_MEM *headerB;
BUF_MEM *dataB, *tmpB;
+ if (ctx == NULL) {
+ PEMerr(PEM_F_PEM_READ_BIO, ERR_R_MALLOC_FAILURE);
+ return (0);
+ }
+
nameB = BUF_MEM_new();
headerB = BUF_MEM_new();
dataB = BUF_MEM_new();
if ((nameB == NULL) || (headerB == NULL) || (dataB == NULL)) {
- BUF_MEM_free(nameB);
- BUF_MEM_free(headerB);
- BUF_MEM_free(dataB);
- PEMerr(PEM_F_PEM_READ_BIO, ERR_R_MALLOC_FAILURE);
- return (0);
+ goto err;
}
buf[254] = '\0';
goto err;
}
- EVP_DecodeInit(&ctx);
- i = EVP_DecodeUpdate(&ctx,
+ EVP_DecodeInit(ctx);
+ i = EVP_DecodeUpdate(ctx,
(unsigned char *)dataB->data, &bl,
(unsigned char *)dataB->data, bl);
if (i < 0) {
PEMerr(PEM_F_PEM_READ_BIO, PEM_R_BAD_BASE64_DECODE);
goto err;
}
- i = EVP_DecodeFinal(&ctx, (unsigned char *)&(dataB->data[bl]), &k);
+ i = EVP_DecodeFinal(ctx, (unsigned char *)&(dataB->data[bl]), &k);
if (i < 0) {
PEMerr(PEM_F_PEM_READ_BIO, PEM_R_BAD_BASE64_DECODE);
goto err;
OPENSSL_free(nameB);
OPENSSL_free(headerB);
OPENSSL_free(dataB);
+ EVP_ENCODE_CTX_free(ctx);
return (1);
err:
BUF_MEM_free(nameB);
BUF_MEM_free(headerB);
BUF_MEM_free(dataB);
+ EVP_ENCODE_CTX_free(ctx);
return (0);
}
goto err;
}
- EVP_EncodeInit(&ctx->encode);
+ ctx->encode = EVP_ENCODE_CTX_new();
+ EVP_EncodeInit(ctx->encode);
ctx->md = EVP_MD_CTX_new();
if (!EVP_SignInit(ctx->md, md_type))
i = inl;
if (!EVP_EncryptUpdate(&ctx->cipher, buffer, &j, in, i))
return 0;
- EVP_EncodeUpdate(&ctx->encode, out, &j, buffer, j);
+ EVP_EncodeUpdate(ctx->encode, out, &j, buffer, j);
*outl += j;
out += j;
in += i;
if (!EVP_EncryptFinal_ex(&ctx->cipher, s, (int *)&i))
goto err;
- EVP_EncodeUpdate(&ctx->encode, out, &j, s, i);
+ EVP_EncodeUpdate(ctx->encode, out, &j, s, i);
*outl = j;
out += j;
- EVP_EncodeFinal(&ctx->encode, out, &j);
+ EVP_EncodeFinal(ctx->encode, out, &j);
*outl += j;
if (!EVP_SignFinal(ctx->md, s, &i, priv))
ret = 1;
err:
+ EVP_ENCODE_CTX_free(ctx->encode);
EVP_MD_CTX_free(ctx->md);
EVP_CIPHER_CTX_cleanup(&ctx->cipher);
OPENSSL_free(s);