提交 689f9fab 编写于 作者: N Nils Larsch

fix potential memory leaks

PR: 1462
上级 82686bdc
...@@ -331,7 +331,9 @@ BIO *PKCS7_dataInit(PKCS7 *p7, BIO *bio) ...@@ -331,7 +331,9 @@ BIO *PKCS7_dataInit(PKCS7 *p7, BIO *bio)
keylen=EVP_CIPHER_key_length(evp_cipher); keylen=EVP_CIPHER_key_length(evp_cipher);
ivlen=EVP_CIPHER_iv_length(evp_cipher); ivlen=EVP_CIPHER_iv_length(evp_cipher);
xalg->algorithm = OBJ_nid2obj(EVP_CIPHER_type(evp_cipher)); xalg->algorithm = OBJ_nid2obj(EVP_CIPHER_type(evp_cipher));
if (ivlen > 0) RAND_pseudo_bytes(iv,ivlen); if (ivlen > 0)
if (RAND_pseudo_bytes(iv,ivlen) <= 0)
goto err;
if (EVP_CipherInit_ex(ctx, evp_cipher, NULL, NULL, NULL, 1)<=0) if (EVP_CipherInit_ex(ctx, evp_cipher, NULL, NULL, NULL, 1)<=0)
goto err; goto err;
if (EVP_CIPHER_CTX_rand_key(ctx, key) <= 0) if (EVP_CIPHER_CTX_rand_key(ctx, key) <= 0)
...@@ -340,10 +342,13 @@ BIO *PKCS7_dataInit(PKCS7 *p7, BIO *bio) ...@@ -340,10 +342,13 @@ BIO *PKCS7_dataInit(PKCS7 *p7, BIO *bio)
goto err; goto err;
if (ivlen > 0) { if (ivlen > 0) {
if (xalg->parameter == NULL) if (xalg->parameter == NULL) {
xalg->parameter=ASN1_TYPE_new(); xalg->parameter = ASN1_TYPE_new();
if (xalg->parameter == NULL)
goto err;
}
if(EVP_CIPHER_param_to_asn1(ctx, xalg->parameter) < 0) if(EVP_CIPHER_param_to_asn1(ctx, xalg->parameter) < 0)
goto err; goto err;
} }
/* Lets do the pub key stuff :-) */ /* Lets do the pub key stuff :-) */
...@@ -371,6 +376,8 @@ BIO *PKCS7_dataInit(PKCS7 *p7, BIO *bio) ...@@ -371,6 +376,8 @@ BIO *PKCS7_dataInit(PKCS7 *p7, BIO *bio)
if(bio == NULL) if(bio == NULL)
{ {
bio=BIO_new(BIO_s_mem()); bio=BIO_new(BIO_s_mem());
if (bio == NULL)
goto err;
BIO_set_mem_eof_return(bio,0); BIO_set_mem_eof_return(bio,0);
} }
} }
...@@ -613,6 +620,8 @@ BIO *PKCS7_dataDecode(PKCS7 *p7, EVP_PKEY *pkey, BIO *in_bio, X509 *pcert) ...@@ -613,6 +620,8 @@ BIO *PKCS7_dataDecode(PKCS7 *p7, EVP_PKEY *pkey, BIO *in_bio, X509 *pcert)
bio=BIO_new(BIO_s_mem()); bio=BIO_new(BIO_s_mem());
BIO_set_mem_eof_return(bio,0); BIO_set_mem_eof_return(bio,0);
} }
if (bio == NULL)
goto err;
#endif #endif
} }
BIO_push(out,bio); BIO_push(out,bio);
...@@ -1080,8 +1089,13 @@ PKCS7_ISSUER_AND_SERIAL *PKCS7_get_issuer_and_serial(PKCS7 *p7, int idx) ...@@ -1080,8 +1089,13 @@ PKCS7_ISSUER_AND_SERIAL *PKCS7_get_issuer_and_serial(PKCS7 *p7, int idx)
int i; int i;
i=OBJ_obj2nid(p7->type); i=OBJ_obj2nid(p7->type);
if (i != NID_pkcs7_signedAndEnveloped) return(NULL); if (i != NID_pkcs7_signedAndEnveloped)
return NULL;
if (p7->d.signed_and_enveloped == NULL)
return NULL;
rsk=p7->d.signed_and_enveloped->recipientinfo; rsk=p7->d.signed_and_enveloped->recipientinfo;
if (rsk == NULL)
return NULL;
ri=sk_PKCS7_RECIP_INFO_value(rsk,0); ri=sk_PKCS7_RECIP_INFO_value(rsk,0);
if (sk_PKCS7_RECIP_INFO_num(rsk) <= idx) return(NULL); if (sk_PKCS7_RECIP_INFO_num(rsk) <= idx) return(NULL);
ri=sk_PKCS7_RECIP_INFO_value(rsk,idx); ri=sk_PKCS7_RECIP_INFO_value(rsk,idx);
...@@ -1135,6 +1149,8 @@ int PKCS7_set_signed_attributes(PKCS7_SIGNER_INFO *p7si, ...@@ -1135,6 +1149,8 @@ int PKCS7_set_signed_attributes(PKCS7_SIGNER_INFO *p7si,
if (p7si->auth_attr != NULL) if (p7si->auth_attr != NULL)
sk_X509_ATTRIBUTE_pop_free(p7si->auth_attr,X509_ATTRIBUTE_free); sk_X509_ATTRIBUTE_pop_free(p7si->auth_attr,X509_ATTRIBUTE_free);
p7si->auth_attr=sk_X509_ATTRIBUTE_dup(sk); p7si->auth_attr=sk_X509_ATTRIBUTE_dup(sk);
if (p7si->auth_attr == NULL)
return 0;
for (i=0; i<sk_X509_ATTRIBUTE_num(sk); i++) for (i=0; i<sk_X509_ATTRIBUTE_num(sk); i++)
{ {
if ((sk_X509_ATTRIBUTE_set(p7si->auth_attr,i, if ((sk_X509_ATTRIBUTE_set(p7si->auth_attr,i,
...@@ -1153,6 +1169,8 @@ int PKCS7_set_attributes(PKCS7_SIGNER_INFO *p7si, STACK_OF(X509_ATTRIBUTE) *sk) ...@@ -1153,6 +1169,8 @@ int PKCS7_set_attributes(PKCS7_SIGNER_INFO *p7si, STACK_OF(X509_ATTRIBUTE) *sk)
sk_X509_ATTRIBUTE_pop_free(p7si->unauth_attr, sk_X509_ATTRIBUTE_pop_free(p7si->unauth_attr,
X509_ATTRIBUTE_free); X509_ATTRIBUTE_free);
p7si->unauth_attr=sk_X509_ATTRIBUTE_dup(sk); p7si->unauth_attr=sk_X509_ATTRIBUTE_dup(sk);
if (p7si->unauth_attr == NULL)
return 0;
for (i=0; i<sk_X509_ATTRIBUTE_num(sk); i++) for (i=0; i<sk_X509_ATTRIBUTE_num(sk); i++)
{ {
if ((sk_X509_ATTRIBUTE_set(p7si->unauth_attr,i, if ((sk_X509_ATTRIBUTE_set(p7si->unauth_attr,i,
...@@ -1183,9 +1201,16 @@ static int add_attribute(STACK_OF(X509_ATTRIBUTE) **sk, int nid, int atrtype, ...@@ -1183,9 +1201,16 @@ static int add_attribute(STACK_OF(X509_ATTRIBUTE) **sk, int nid, int atrtype,
if (*sk == NULL) if (*sk == NULL)
{ {
*sk = sk_X509_ATTRIBUTE_new_null(); *sk = sk_X509_ATTRIBUTE_new_null();
if (*sk == NULL)
return 0;
new_attrib: new_attrib:
attr=X509_ATTRIBUTE_create(nid,atrtype,value); if (!(attr=X509_ATTRIBUTE_create(nid,atrtype,value)))
sk_X509_ATTRIBUTE_push(*sk,attr); return 0;
if (!sk_X509_ATTRIBUTE_push(*sk,attr))
{
X509_ATTRIBUTE_free(attr);
return 0;
}
} }
else else
{ {
...@@ -1198,7 +1223,13 @@ new_attrib: ...@@ -1198,7 +1223,13 @@ new_attrib:
{ {
X509_ATTRIBUTE_free(attr); X509_ATTRIBUTE_free(attr);
attr=X509_ATTRIBUTE_create(nid,atrtype,value); attr=X509_ATTRIBUTE_create(nid,atrtype,value);
sk_X509_ATTRIBUTE_set(*sk,i,attr); if (attr == NULL)
return 0;
if (!sk_X509_ATTRIBUTE_set(*sk,i,attr))
{
X509_ATTRIBUTE_free(attr);
return 0;
}
goto end; goto end;
} }
} }
......
...@@ -272,16 +272,23 @@ int PKCS7_add_signer(PKCS7 *p7, PKCS7_SIGNER_INFO *psi) ...@@ -272,16 +272,23 @@ int PKCS7_add_signer(PKCS7 *p7, PKCS7_SIGNER_INFO *psi)
if (!j) /* we need to add another algorithm */ if (!j) /* we need to add another algorithm */
{ {
if(!(alg=X509_ALGOR_new()) if(!(alg=X509_ALGOR_new())
|| !(alg->parameter = ASN1_TYPE_new())) { || !(alg->parameter = ASN1_TYPE_new()))
{
X509_ALGOR_free(alg);
PKCS7err(PKCS7_F_PKCS7_ADD_SIGNER,ERR_R_MALLOC_FAILURE); PKCS7err(PKCS7_F_PKCS7_ADD_SIGNER,ERR_R_MALLOC_FAILURE);
return(0); return(0);
} }
alg->algorithm=OBJ_nid2obj(nid); alg->algorithm=OBJ_nid2obj(nid);
alg->parameter->type = V_ASN1_NULL; alg->parameter->type = V_ASN1_NULL;
sk_X509_ALGOR_push(md_sk,alg); if (!sk_X509_ALGOR_push(md_sk,alg))
{
X509_ALGOR_free(alg);
return 0;
}
} }
sk_PKCS7_SIGNER_INFO_push(signer_sk,psi); if (!sk_PKCS7_SIGNER_INFO_push(signer_sk,psi))
return 0;
return(1); return(1);
} }
...@@ -306,8 +313,17 @@ int PKCS7_add_certificate(PKCS7 *p7, X509 *x509) ...@@ -306,8 +313,17 @@ int PKCS7_add_certificate(PKCS7 *p7, X509 *x509)
if (*sk == NULL) if (*sk == NULL)
*sk=sk_X509_new_null(); *sk=sk_X509_new_null();
if (*sk == NULL)
{
PKCS7err(PKCS7_F_PKCS7_ADD_CERTIFICATE, ERR_R_MALLOC_FAILURE);
return 0;
}
CRYPTO_add(&x509->references,1,CRYPTO_LOCK_X509); CRYPTO_add(&x509->references,1,CRYPTO_LOCK_X509);
sk_X509_push(*sk,x509); if (!sk_X509_push(*sk,x509))
{
X509_free(x509);
return 0;
}
return(1); return(1);
} }
...@@ -332,9 +348,18 @@ int PKCS7_add_crl(PKCS7 *p7, X509_CRL *crl) ...@@ -332,9 +348,18 @@ int PKCS7_add_crl(PKCS7 *p7, X509_CRL *crl)
if (*sk == NULL) if (*sk == NULL)
*sk=sk_X509_CRL_new_null(); *sk=sk_X509_CRL_new_null();
if (*sk == NULL)
{
PKCS7err(PKCS7_F_PKCS7_ADD_CERTIFICATE,ERR_R_MALLOC_FAILURE);
return 0;
}
CRYPTO_add(&crl->references,1,CRYPTO_LOCK_X509_CRL); CRYPTO_add(&crl->references,1,CRYPTO_LOCK_X509_CRL);
sk_X509_CRL_push(*sk,crl); if (!sk_X509_CRL_push(*sk,crl))
{
X509_CRL_free(crl);
return 0;
}
return(1); return(1);
} }
...@@ -496,7 +521,8 @@ int PKCS7_add_recipient_info(PKCS7 *p7, PKCS7_RECIP_INFO *ri) ...@@ -496,7 +521,8 @@ int PKCS7_add_recipient_info(PKCS7 *p7, PKCS7_RECIP_INFO *ri)
return(0); return(0);
} }
sk_PKCS7_RECIP_INFO_push(sk,ri); if (!sk_PKCS7_RECIP_INFO_push(sk,ri))
return 0;
return(1); return(1);
} }
......
...@@ -378,7 +378,8 @@ int PKCS7_verify(PKCS7 *p7, STACK_OF(X509) *certs, X509_STORE *store, ...@@ -378,7 +378,8 @@ int PKCS7_verify(PKCS7 *p7, STACK_OF(X509) *certs, X509_STORE *store,
tmpin = indata; tmpin = indata;
p7bio=PKCS7_dataInit(p7,tmpin); if (!(p7bio=PKCS7_dataInit(p7,tmpin)))
goto err;
if(flags & PKCS7_TEXT) { if(flags & PKCS7_TEXT) {
if(!(tmpout = BIO_new(BIO_s_mem()))) { if(!(tmpout = BIO_new(BIO_s_mem()))) {
...@@ -483,7 +484,10 @@ STACK_OF(X509) *PKCS7_get0_signers(PKCS7 *p7, STACK_OF(X509) *certs, int flags) ...@@ -483,7 +484,10 @@ STACK_OF(X509) *PKCS7_get0_signers(PKCS7 *p7, STACK_OF(X509) *certs, int flags)
return 0; return 0;
} }
sk_X509_push(signers, signer); if (!sk_X509_push(signers, signer)) {
sk_X509_free(signers);
return NULL;
}
} }
return signers; return signers;
} }
...@@ -503,8 +507,9 @@ PKCS7 *PKCS7_encrypt(STACK_OF(X509) *certs, BIO *in, const EVP_CIPHER *cipher, ...@@ -503,8 +507,9 @@ PKCS7 *PKCS7_encrypt(STACK_OF(X509) *certs, BIO *in, const EVP_CIPHER *cipher,
return NULL; return NULL;
} }
PKCS7_set_type(p7, NID_pkcs7_enveloped); if (!PKCS7_set_type(p7, NID_pkcs7_enveloped))
if(!PKCS7_set_cipher(p7, cipher)) { goto err;
if (!PKCS7_set_cipher(p7, cipher)) {
PKCS7err(PKCS7_F_PKCS7_ENCRYPT,PKCS7_R_ERROR_SETTING_CIPHER); PKCS7err(PKCS7_F_PKCS7_ENCRYPT,PKCS7_R_ERROR_SETTING_CIPHER);
goto err; goto err;
} }
...@@ -526,7 +531,7 @@ PKCS7 *PKCS7_encrypt(STACK_OF(X509) *certs, BIO *in, const EVP_CIPHER *cipher, ...@@ -526,7 +531,7 @@ PKCS7 *PKCS7_encrypt(STACK_OF(X509) *certs, BIO *in, const EVP_CIPHER *cipher,
err: err:
BIO_free(p7bio); BIO_free_all(p7bio);
PKCS7_free(p7); PKCS7_free(p7);
return NULL; return NULL;
...@@ -564,10 +569,13 @@ int PKCS7_decrypt(PKCS7 *p7, EVP_PKEY *pkey, X509 *cert, BIO *data, int flags) ...@@ -564,10 +569,13 @@ int PKCS7_decrypt(PKCS7 *p7, EVP_PKEY *pkey, X509 *cert, BIO *data, int flags)
/* Encrypt BIOs can't do BIO_gets() so add a buffer BIO */ /* Encrypt BIOs can't do BIO_gets() so add a buffer BIO */
if(!(tmpbuf = BIO_new(BIO_f_buffer()))) { if(!(tmpbuf = BIO_new(BIO_f_buffer()))) {
PKCS7err(PKCS7_F_PKCS7_DECRYPT, ERR_R_MALLOC_FAILURE); PKCS7err(PKCS7_F_PKCS7_DECRYPT, ERR_R_MALLOC_FAILURE);
BIO_free_all(tmpmem);
return 0; return 0;
} }
if(!(bread = BIO_push(tmpbuf, tmpmem))) { if(!(bread = BIO_push(tmpbuf, tmpmem))) {
PKCS7err(PKCS7_F_PKCS7_DECRYPT, ERR_R_MALLOC_FAILURE); PKCS7err(PKCS7_F_PKCS7_DECRYPT, ERR_R_MALLOC_FAILURE);
BIO_free_all(tmpbuf);
BIO_free_all(tmpmem);
return 0; return 0;
} }
ret = SMIME_text(bread, data); ret = SMIME_text(bread, data);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册