Backport libcrypto audit: check return values of EVP functions instead
authorDr. Stephen Henson <steve@openssl.org>
Fri, 3 Jun 2011 20:53:00 +0000 (20:53 +0000)
committerDr. Stephen Henson <steve@openssl.org>
Fri, 3 Jun 2011 20:53:00 +0000 (20:53 +0000)
of assuming they will always suceed.

31 files changed:
CHANGES
crypto/asn1/a_digest.c
crypto/asn1/a_sign.c
crypto/asn1/a_verify.c
crypto/asn1/n_pkey.c
crypto/asn1/p5_pbev2.c
crypto/asn1/t_x509.c
crypto/cms/cms_lib.c
crypto/cms/cms_sd.c
crypto/dsa/dsa_gen.c
crypto/evp/bio_md.c
crypto/evp/bio_ok.c
crypto/evp/e_rc2.c
crypto/evp/evp_key.c
crypto/evp/p5_crpt.c
crypto/evp/p5_crpt2.c
crypto/evp/p_open.c
crypto/evp/p_seal.c
crypto/evp/p_sign.c
crypto/evp/p_verify.c
crypto/hmac/hm_pmeth.c
crypto/ocsp/ocsp_lib.c
crypto/pem/pvkfmt.c
crypto/pkcs12/p12_decr.c
crypto/pkcs12/p12_key.c
crypto/pkcs12/p12_mutl.c
crypto/pkcs7/pk7_doit.c
crypto/rsa/rsa_oaep.c
crypto/ts/ts_rsp_verify.c
crypto/x509/x509_cmp.c
crypto/x509v3/v3_skey.c

diff --git a/CHANGES b/CHANGES
index eb5a06b..cea65cc 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -4,6 +4,10 @@
 
  Changes between 1.0.0d and 1.0.1  [xx XXX xxxx]
 
+  *) Backport libcrypto audit of return value checking from HEAD, not
+     all cases can be covered as some introduce binary incompatibilities.
+     [Steve Henson]
+
   *) Redirect RSA operations to FIPS module including keygen,
      encrypt, decrypt, sign and verify. Block use of non FIPS RSA methods.
      [Steve Henson]
index d00d9e2..cbdeea6 100644 (file)
@@ -87,7 +87,8 @@ int ASN1_digest(i2d_of_void *i2d, const EVP_MD *type, char *data,
        p=str;
        i2d(data,&p);
 
-       EVP_Digest(str, i, md, len, type, NULL);
+       if (!EVP_Digest(str, i, md, len, type, NULL))
+               return 0;
        OPENSSL_free(str);
        return(1);
        }
@@ -104,7 +105,8 @@ int ASN1_item_digest(const ASN1_ITEM *it, const EVP_MD *type, void *asn,
        i=ASN1_item_i2d(asn,&str, it);
        if (!str) return(0);
 
-       EVP_Digest(str, i, md, len, type, NULL);
+       if (!EVP_Digest(str, i, md, len, type, NULL))
+               return 0;
        OPENSSL_free(str);
        return(1);
        }
index ff63bfc..d96a622 100644 (file)
@@ -184,9 +184,9 @@ int ASN1_sign(i2d_of_void *i2d, X509_ALGOR *algor1, X509_ALGOR *algor2,
        p=buf_in;
 
        i2d(data,&p);
-       EVP_SignInit_ex(&ctx,type, NULL);
-       EVP_SignUpdate(&ctx,(unsigned char *)buf_in,inl);
-       if (!EVP_SignFinal(&ctx,(unsigned char *)buf_out,
+       if (!EVP_SignInit_ex(&ctx,type, NULL)
+               || !EVP_SignUpdate(&ctx,(unsigned char *)buf_in,inl)
+               || !EVP_SignFinal(&ctx,(unsigned char *)buf_out,
                        (unsigned int *)&outl,pkey))
                {
                outl=0;
@@ -270,9 +270,9 @@ int ASN1_item_sign(const ASN1_ITEM *it, X509_ALGOR *algor1, X509_ALGOR *algor2,
                goto err;
                }
 
-       EVP_SignInit_ex(&ctx,type, NULL);
-       EVP_SignUpdate(&ctx,(unsigned char *)buf_in,inl);
-       if (!EVP_SignFinal(&ctx,(unsigned char *)buf_out,
+       if (!EVP_SignInit_ex(&ctx,type, NULL)
+               || !EVP_SignUpdate(&ctx,(unsigned char *)buf_in,inl)
+               || !EVP_SignFinal(&ctx,(unsigned char *)buf_out,
                        (unsigned int *)&outl,pkey))
                {
                outl=0;
index cecdb13..d9332ee 100644 (file)
@@ -101,8 +101,13 @@ int ASN1_verify(i2d_of_void *i2d, X509_ALGOR *a, ASN1_BIT_STRING *signature,
        p=buf_in;
 
        i2d(data,&p);
-       EVP_VerifyInit_ex(&ctx,type, NULL);
-       EVP_VerifyUpdate(&ctx,(unsigned char *)buf_in,inl);
+       if (!EVP_VerifyInit_ex(&ctx,type, NULL)
+               || !EVP_VerifyUpdate(&ctx,(unsigned char *)buf_in,inl))
+               {
+               ASN1err(ASN1_F_ASN1_VERIFY,ERR_R_EVP_LIB);
+               ret=0;
+               goto err;
+               }
 
        OPENSSL_cleanse(buf_in,(unsigned int)inl);
        OPENSSL_free(buf_in);
@@ -173,7 +178,12 @@ int ASN1_item_verify(const ASN1_ITEM *it, X509_ALGOR *a, ASN1_BIT_STRING *signat
                goto err;
                }
 
-       EVP_VerifyUpdate(&ctx,(unsigned char *)buf_in,inl);
+       if (!EVP_VerifyUpdate(&ctx,(unsigned char *)buf_in,inl))
+               {
+               ASN1err(ASN1_F_ASN1_ITEM_VERIFY,ERR_R_EVP_LIB);
+               ret=0;
+               goto err;
+               }
 
        OPENSSL_cleanse(buf_in,(unsigned int)inl);
        OPENSSL_free(buf_in);
index e7d0439..e251739 100644 (file)
@@ -129,6 +129,7 @@ int i2d_RSA_NET(const RSA *a, unsigned char **pp,
        unsigned char buf[256],*zz;
        unsigned char key[EVP_MAX_KEY_LENGTH];
        EVP_CIPHER_CTX ctx;
+       EVP_CIPHER_CTX_init(&ctx);
 
        if (a == NULL) return(0);
 
@@ -206,24 +207,28 @@ int i2d_RSA_NET(const RSA *a, unsigned char **pp,
        i = strlen((char *)buf);
        /* If the key is used for SGC the algorithm is modified a little. */
        if(sgckey) {
-               EVP_Digest(buf, i, buf, NULL, EVP_md5(), NULL);
+               if (!EVP_Digest(buf, i, buf, NULL, EVP_md5(), NULL))
+                       goto err;
                memcpy(buf + 16, "SGCKEYSALT", 10);
                i = 26;
        }
 
-       EVP_BytesToKey(EVP_rc4(),EVP_md5(),NULL,buf,i,1,key,NULL);
+       if (!EVP_BytesToKey(EVP_rc4(),EVP_md5(),NULL,buf,i,1,key,NULL))
+               goto err;
        OPENSSL_cleanse(buf,256);
 
        /* Encrypt private key in place */
        zz = enckey->enckey->digest->data;
-       EVP_CIPHER_CTX_init(&ctx);
-       EVP_EncryptInit_ex(&ctx,EVP_rc4(),NULL,key,NULL);
-       EVP_EncryptUpdate(&ctx,zz,&i,zz,pkeylen);
-       EVP_EncryptFinal_ex(&ctx,zz + i,&j);
-       EVP_CIPHER_CTX_cleanup(&ctx);
+       if (!EVP_EncryptInit_ex(&ctx,EVP_rc4(),NULL,key,NULL))
+               goto err;
+       if (!EVP_EncryptUpdate(&ctx,zz,&i,zz,pkeylen))
+               goto err;
+       if (!EVP_EncryptFinal_ex(&ctx,zz + i,&j))
+               goto err;
 
        ret = i2d_NETSCAPE_ENCRYPTED_PKEY(enckey, pp);
 err:
+       EVP_CIPHER_CTX_cleanup(&ctx);
        NETSCAPE_ENCRYPTED_PKEY_free(enckey);
        NETSCAPE_PKEY_free(pkey);
        return(ret);
@@ -288,6 +293,7 @@ static RSA *d2i_RSA_NET_2(RSA **a, ASN1_OCTET_STRING *os,
        const unsigned char *zz;
        unsigned char key[EVP_MAX_KEY_LENGTH];
        EVP_CIPHER_CTX ctx;
+       EVP_CIPHER_CTX_init(&ctx);
 
        i=cb((char *)buf,256,"Enter Private Key password:",0);
        if (i != 0)
@@ -298,19 +304,22 @@ static RSA *d2i_RSA_NET_2(RSA **a, ASN1_OCTET_STRING *os,
 
        i = strlen((char *)buf);
        if(sgckey){
-               EVP_Digest(buf, i, buf, NULL, EVP_md5(), NULL);
+               if (!EVP_Digest(buf, i, buf, NULL, EVP_md5(), NULL))
+                       goto err;
                memcpy(buf + 16, "SGCKEYSALT", 10);
                i = 26;
        }
                
-       EVP_BytesToKey(EVP_rc4(),EVP_md5(),NULL,buf,i,1,key,NULL);
+       if (!EVP_BytesToKey(EVP_rc4(),EVP_md5(),NULL,buf,i,1,key,NULL))
+               goto err;
        OPENSSL_cleanse(buf,256);
 
-       EVP_CIPHER_CTX_init(&ctx);
-       EVP_DecryptInit_ex(&ctx,EVP_rc4(),NULL, key,NULL);
-       EVP_DecryptUpdate(&ctx,os->data,&i,os->data,os->length);
-       EVP_DecryptFinal_ex(&ctx,&(os->data[i]),&j);
-       EVP_CIPHER_CTX_cleanup(&ctx);
+       if (!EVP_DecryptInit_ex(&ctx,EVP_rc4(),NULL, key,NULL))
+               goto err;
+       if (!EVP_DecryptUpdate(&ctx,os->data,&i,os->data,os->length))
+               goto err;
+       if (!EVP_DecryptFinal_ex(&ctx,&(os->data[i]),&j))
+               goto err;
        os->length=i+j;
 
        zz=os->data;
@@ -328,6 +337,7 @@ static RSA *d2i_RSA_NET_2(RSA **a, ASN1_OCTET_STRING *os,
                goto err;
                }
 err:
+       EVP_CIPHER_CTX_cleanup(&ctx);
        NETSCAPE_PKEY_free(pkey);
        return(ret);
        }
index cb49b66..377edaf 100644 (file)
@@ -127,7 +127,8 @@ X509_ALGOR *PKCS5_pbe2_set_iv(const EVP_CIPHER *cipher, int iter,
        EVP_CIPHER_CTX_init(&ctx);
 
        /* Dummy cipherinit to just setup the IV, and PRF */
-       EVP_CipherInit_ex(&ctx, cipher, NULL, NULL, iv, 0);
+       if (!EVP_CipherInit_ex(&ctx, cipher, NULL, NULL, iv, 0))
+               goto err;
        if(EVP_CIPHER_param_to_asn1(&ctx, scheme->parameter) < 0) {
                ASN1err(ASN1_F_PKCS5_PBE2_SET_IV,
                                        ASN1_R_ERROR_SETTING_CIPHER_PARAMS);
index e061f2f..01cf9e4 100644 (file)
@@ -255,7 +255,8 @@ int X509_ocspid_print (BIO *bp, X509 *x)
                goto err;
        i2d_X509_NAME(x->cert_info->subject, &dertmp);
 
-       EVP_Digest(der, derlen, SHA1md, NULL, EVP_sha1(), NULL);
+       if (!EVP_Digest(der, derlen, SHA1md, NULL, EVP_sha1(), NULL))
+               goto err;
        for (i=0; i < SHA_DIGEST_LENGTH; i++)
                {
                if (BIO_printf(bp,"%02X",SHA1md[i]) <= 0) goto err;
@@ -268,8 +269,10 @@ int X509_ocspid_print (BIO *bp, X509 *x)
        if (BIO_printf(bp,"\n        Public key OCSP hash: ") <= 0)
                goto err;
 
-       EVP_Digest(x->cert_info->key->public_key->data,
-               x->cert_info->key->public_key->length, SHA1md, NULL, EVP_sha1(), NULL);
+       if (!EVP_Digest(x->cert_info->key->public_key->data,
+                       x->cert_info->key->public_key->length,
+                       SHA1md, NULL, EVP_sha1(), NULL))
+               goto err;
        for (i=0; i < SHA_DIGEST_LENGTH; i++)
                {
                if (BIO_printf(bp,"%02X",SHA1md[i]) <= 0)
index d00fe0f..f88e8f3 100644 (file)
@@ -412,8 +412,7 @@ int cms_DigestAlgorithm_find_ctx(EVP_MD_CTX *mctx, BIO *chain,
                 */
                        || EVP_MD_pkey_type(EVP_MD_CTX_md(mtmp)) == nid)
                        {
-                       EVP_MD_CTX_copy_ex(mctx, mtmp);
-                       return 1;
+                       return EVP_MD_CTX_copy_ex(mctx, mtmp);
                        }
                chain = BIO_next(chain);
                }
index e3192b9..77fbd13 100644 (file)
@@ -641,7 +641,8 @@ static int cms_SignerInfo_content_sign(CMS_ContentInfo *cms,
                        cms->d.signedData->encapContentInfo->eContentType; 
                unsigned char md[EVP_MAX_MD_SIZE];
                unsigned int mdlen;
-               EVP_DigestFinal_ex(&mctx, md, &mdlen);
+               if (!EVP_DigestFinal_ex(&mctx, md, &mdlen))
+                       goto err;
                if (!CMS_signed_add1_attr_by_NID(si, NID_pkcs9_messageDigest,
                                                V_ASN1_OCTET_STRING,
                                                md, mdlen))
index e509dc9..e6a5452 100644 (file)
@@ -202,8 +202,10 @@ int dsa_builtin_paramgen(DSA *ret, size_t bits, size_t qbits,
                                }
 
                        /* step 2 */
-                       EVP_Digest(seed, qsize, md,   NULL, evpmd, NULL);
-                       EVP_Digest(buf,  qsize, buf2, NULL, evpmd, NULL);
+                       if (!EVP_Digest(seed, qsize, md,   NULL, evpmd, NULL))
+                               goto err;
+                       if (!EVP_Digest(buf,  qsize, buf2, NULL, evpmd, NULL))
+                               goto err;
                        for (i = 0; i < qsize; i++)
                                md[i]^=buf2[i];
 
@@ -252,7 +254,9 @@ int dsa_builtin_paramgen(DSA *ret, size_t bits, size_t qbits,
                                                break;
                                        }
 
-                               EVP_Digest(buf, qsize, md ,NULL, evpmd, NULL);
+                               if (!EVP_Digest(buf, qsize, md ,NULL, evpmd,
+                                                                       NULL))
+                                       goto err;
 
                                /* step 8 */
                                if (!BN_bin2bn(md, qsize, r0))
index 9841e32..144fdfd 100644 (file)
@@ -153,8 +153,12 @@ static int md_write(BIO *b, const char *in, int inl)
                {
                if (ret > 0)
                        {
-                       EVP_DigestUpdate(ctx,(const unsigned char *)in,
-                               (unsigned int)ret);
+                       if (!EVP_DigestUpdate(ctx,(const unsigned char *)in,
+                               (unsigned int)ret))
+                               {
+                               BIO_clear_retry_flags(b);
+                               return 0;
+                               }
                        }
                }
        if(b->next_bio != NULL)
@@ -220,7 +224,8 @@ static long md_ctrl(BIO *b, int cmd, long num, void *ptr)
        case BIO_CTRL_DUP:
                dbio=ptr;
                dctx=dbio->ptr;
-               EVP_MD_CTX_copy_ex(dctx,ctx);
+               if (!EVP_MD_CTX_copy_ex(dctx,ctx))
+                       return 0;
                b->init=1;
                break;
        default:
index 98bc1ab..e643353 100644 (file)
@@ -133,10 +133,10 @@ static int ok_new(BIO *h);
 static int ok_free(BIO *data);
 static long ok_callback_ctrl(BIO *h, int cmd, bio_info_cb *fp);
 
-static void sig_out(BIO* b);
-static void sig_in(BIO* b);
-static void block_out(BIO* b);
-static void block_in(BIO* b);
+static int sig_out(BIO* b);
+static int sig_in(BIO* b);
+static int block_out(BIO* b);
+static int block_in(BIO* b);
 #define OK_BLOCK_SIZE  (1024*4)
 #define OK_BLOCK_BLOCK 4
 #define IOBS           (OK_BLOCK_SIZE+ OK_BLOCK_BLOCK+ 3*EVP_MAX_MD_SIZE)
@@ -266,10 +266,24 @@ static int ok_read(BIO *b, char *out, int outl)
                ctx->buf_len+= i;
 
                /* no signature yet -- check if we got one */
-               if (ctx->sigio == 1) sig_in(b);
+               if (ctx->sigio == 1)
+                       {
+                       if (!sig_in(b))
+                               {
+                               BIO_clear_retry_flags(b);
+                               return 0;
+                               }
+                       }
 
                /* signature ok -- check if we got block */
-               if (ctx->sigio == 0) block_in(b);
+               if (ctx->sigio == 0)
+                       {
+                       if (!block_in(b))
+                               {
+                               BIO_clear_retry_flags(b);
+                               return 0;
+                               }
+                       }
 
                /* invalid block -- cancel */
                if (ctx->cont <= 0) break;
@@ -293,7 +307,8 @@ static int ok_write(BIO *b, const char *in, int inl)
 
        if ((ctx == NULL) || (b->next_bio == NULL) || (b->init == 0)) return(0);
 
-       if(ctx->sigio) sig_out(b);
+       if(ctx->sigio && !sig_out(b))
+               return 0;
 
        do{
                BIO_clear_retry_flags(b);
@@ -332,7 +347,11 @@ static int ok_write(BIO *b, const char *in, int inl)
 
                if(ctx->buf_len >= OK_BLOCK_SIZE+ OK_BLOCK_BLOCK)
                        {
-                       block_out(b);
+                       if (!block_out(b))
+                               {
+                               BIO_clear_retry_flags(b);
+                               return 0;
+                               }
                        }
        }while(inl > 0);
 
@@ -379,7 +398,8 @@ static long ok_ctrl(BIO *b, int cmd, long num, void *ptr)
        case BIO_CTRL_FLUSH:
                /* do a final write */
                if(ctx->blockout == 0)
-                       block_out(b);
+                       if (!block_out(b))
+                               return 0;
 
                while (ctx->blockout)
                        {
@@ -408,7 +428,8 @@ static long ok_ctrl(BIO *b, int cmd, long num, void *ptr)
                break;
        case BIO_C_SET_MD:
                md=ptr;
-               EVP_DigestInit_ex(&ctx->md, md, NULL);
+               if (!EVP_DigestInit_ex(&ctx->md, md, NULL))
+                       return 0;
                b->init=1;
                break;
        case BIO_C_GET_MD:
@@ -455,7 +476,7 @@ static void longswap(void *_ptr, size_t len)
        }
 }
 
-static void sig_out(BIO* b)
+static int sig_out(BIO* b)
        {
        BIO_OK_CTX *ctx;
        EVP_MD_CTX *md;
@@ -463,9 +484,10 @@ static void sig_out(BIO* b)
        ctx=b->ptr;
        md=&ctx->md;
 
-       if(ctx->buf_len+ 2* md->digest->md_size > OK_BLOCK_SIZE) return;
+       if(ctx->buf_len+ 2* md->digest->md_size > OK_BLOCK_SIZE) return 1;
 
-       EVP_DigestInit_ex(md, md->digest, NULL);
+       if (!EVP_DigestInit_ex(md, md->digest, NULL))
+               goto berr;
        /* FIXME: there's absolutely no guarantee this makes any sense at all,
         * particularly now EVP_MD_CTX has been restructured.
         */
@@ -474,14 +496,20 @@ static void sig_out(BIO* b)
        longswap(&(ctx->buf[ctx->buf_len]), md->digest->md_size);
        ctx->buf_len+= md->digest->md_size;
 
-       EVP_DigestUpdate(md, WELLKNOWN, strlen(WELLKNOWN));
-       EVP_DigestFinal_ex(md, &(ctx->buf[ctx->buf_len]), NULL);
+       if (!EVP_DigestUpdate(md, WELLKNOWN, strlen(WELLKNOWN)))
+               goto berr;
+       if (!EVP_DigestFinal_ex(md, &(ctx->buf[ctx->buf_len]), NULL))
+               goto berr;
        ctx->buf_len+= md->digest->md_size;
        ctx->blockout= 1;
        ctx->sigio= 0;
+       return 1;
+       berr:
+       BIO_clear_retry_flags(b);
+       return 0;
        }
 
-static void sig_in(BIO* b)
+static int sig_in(BIO* b)
        {
        BIO_OK_CTX *ctx;
        EVP_MD_CTX *md;
@@ -491,15 +519,18 @@ static void sig_in(BIO* b)
        ctx=b->ptr;
        md=&ctx->md;
 
-       if((int)(ctx->buf_len-ctx->buf_off) < 2*md->digest->md_size) return;
+       if((int)(ctx->buf_len-ctx->buf_off) < 2*md->digest->md_size) return 1;
 
-       EVP_DigestInit_ex(md, md->digest, NULL);
+       if (!EVP_DigestInit_ex(md, md->digest, NULL))
+               goto berr;
        memcpy(md->md_data, &(ctx->buf[ctx->buf_off]), md->digest->md_size);
        longswap(md->md_data, md->digest->md_size);
        ctx->buf_off+= md->digest->md_size;
 
-       EVP_DigestUpdate(md, WELLKNOWN, strlen(WELLKNOWN));
-       EVP_DigestFinal_ex(md, tmp, NULL);
+       if (!EVP_DigestUpdate(md, WELLKNOWN, strlen(WELLKNOWN)))
+               goto berr;
+       if (!EVP_DigestFinal_ex(md, tmp, NULL))
+               goto berr;
        ret= memcmp(&(ctx->buf[ctx->buf_off]), tmp, md->digest->md_size) == 0;
        ctx->buf_off+= md->digest->md_size;
        if(ret == 1)
@@ -516,9 +547,13 @@ static void sig_in(BIO* b)
                {
                ctx->cont= 0;
                }
+       return 1;
+       berr:
+       BIO_clear_retry_flags(b);
+       return 0;
        }
 
-static void block_out(BIO* b)
+static int block_out(BIO* b)
        {
        BIO_OK_CTX *ctx;
        EVP_MD_CTX *md;
@@ -532,13 +567,20 @@ static void block_out(BIO* b)
        ctx->buf[1]=(unsigned char)(tl>>16);
        ctx->buf[2]=(unsigned char)(tl>>8);
        ctx->buf[3]=(unsigned char)(tl);
-       EVP_DigestUpdate(md, (unsigned char*) &(ctx->buf[OK_BLOCK_BLOCK]), tl);
-       EVP_DigestFinal_ex(md, &(ctx->buf[ctx->buf_len]), NULL);
+       if (!EVP_DigestUpdate(md,
+               (unsigned char*) &(ctx->buf[OK_BLOCK_BLOCK]), tl))
+               goto berr;
+       if (!EVP_DigestFinal_ex(md, &(ctx->buf[ctx->buf_len]), NULL))
+               goto berr;
        ctx->buf_len+= md->digest->md_size;
        ctx->blockout= 1;
+       return 1;
+       berr:
+       BIO_clear_retry_flags(b);
+       return 0;
        }
 
-static void block_in(BIO* b)
+static int block_in(BIO* b)
        {
        BIO_OK_CTX *ctx;
        EVP_MD_CTX *md;
@@ -554,10 +596,13 @@ static void block_in(BIO* b)
        tl|=ctx->buf[2]; tl<<=8;
        tl|=ctx->buf[3];
 
-       if (ctx->buf_len < tl+ OK_BLOCK_BLOCK+ md->digest->md_size) return;
+       if (ctx->buf_len < tl+ OK_BLOCK_BLOCK+ md->digest->md_size) return 1;
  
-       EVP_DigestUpdate(md, (unsigned char*) &(ctx->buf[OK_BLOCK_BLOCK]), tl);
-       EVP_DigestFinal_ex(md, tmp, NULL);
+       if (!EVP_DigestUpdate(md,
+                       (unsigned char*) &(ctx->buf[OK_BLOCK_BLOCK]), tl))
+               goto berr;
+       if (!EVP_DigestFinal_ex(md, tmp, NULL))
+               goto berr;
        if(memcmp(&(ctx->buf[tl+ OK_BLOCK_BLOCK]), tmp, md->digest->md_size) == 0)
                {
                /* there might be parts from next block lurking around ! */
@@ -571,5 +616,9 @@ static void block_in(BIO* b)
                {
                ctx->cont= 0;
                }
+       return 1;
+       berr:
+       BIO_clear_retry_flags(b);
+       return 0;
        }
 
index f78d781..d4c33b5 100644 (file)
@@ -183,7 +183,8 @@ static int rc2_get_asn1_type_and_iv(EVP_CIPHER_CTX *c, ASN1_TYPE *type)
                key_bits =rc2_magic_to_meth((int)num);
                if (!key_bits)
                        return(-1);
-               if(i > 0) EVP_CipherInit_ex(c, NULL, NULL, NULL, iv, -1);
+               if(i > 0 && !EVP_CipherInit_ex(c, NULL, NULL, NULL, iv, -1))
+                       return -1;
                EVP_CIPHER_CTX_ctrl(c, EVP_CTRL_SET_RC2_KEY_BITS, key_bits, NULL);
                EVP_CIPHER_CTX_set_key_length(c, key_bits / 8);
                }
index 839d6a3..7961fbe 100644 (file)
@@ -120,7 +120,7 @@ int EVP_BytesToKey(const EVP_CIPHER *type, const EVP_MD *md,
        unsigned char md_buf[EVP_MAX_MD_SIZE];
        int niv,nkey,addmd=0;
        unsigned int mds=0,i;
-
+       int rv = 0;
        nkey=type->key_len;
        niv=type->iv_len;
        OPENSSL_assert(nkey <= EVP_MAX_KEY_LENGTH);
@@ -134,17 +134,24 @@ int EVP_BytesToKey(const EVP_CIPHER *type, const EVP_MD *md,
                if (!EVP_DigestInit_ex(&c,md, NULL))
                        return 0;
                if (addmd++)
-                       EVP_DigestUpdate(&c,&(md_buf[0]),mds);
-               EVP_DigestUpdate(&c,data,datal);
+                       if (!EVP_DigestUpdate(&c,&(md_buf[0]),mds))
+                               goto err;
+               if (!EVP_DigestUpdate(&c,data,datal))
+                       goto err;
                if (salt != NULL)
-                       EVP_DigestUpdate(&c,salt,PKCS5_SALT_LEN);
-               EVP_DigestFinal_ex(&c,&(md_buf[0]),&mds);
+                       if (!EVP_DigestUpdate(&c,salt,PKCS5_SALT_LEN))
+                               goto err;
+               if (!EVP_DigestFinal_ex(&c,&(md_buf[0]),&mds))
+                       goto err;
 
                for (i=1; i<(unsigned int)count; i++)
                        {
-                       EVP_DigestInit_ex(&c,md, NULL);
-                       EVP_DigestUpdate(&c,&(md_buf[0]),mds);
-                       EVP_DigestFinal_ex(&c,&(md_buf[0]),&mds);
+                       if (!EVP_DigestInit_ex(&c,md, NULL))
+                               goto err;
+                       if (!EVP_DigestUpdate(&c,&(md_buf[0]),mds))
+                               goto err;
+                       if (!EVP_DigestFinal_ex(&c,&(md_buf[0]),&mds))
+                               goto err;
                        }
                i=0;
                if (nkey)
@@ -173,8 +180,10 @@ int EVP_BytesToKey(const EVP_CIPHER *type, const EVP_MD *md,
                        }
                if ((nkey == 0) && (niv == 0)) break;
                }
+       rv = type->key_len;
+       err:
        EVP_MD_CTX_cleanup(&c);
        OPENSSL_cleanse(&(md_buf[0]),EVP_MAX_MD_SIZE);
-       return(type->key_len);
+       return rv;
        }
 
index 7ecfa8d..7d9c1f0 100644 (file)
@@ -82,6 +82,8 @@ int PKCS5_PBE_keyivgen(EVP_CIPHER_CTX *cctx, const char *pass, int passlen,
        unsigned char *salt;
        const unsigned char *pbuf;
        int mdsize;
+       int rv = 0;
+       EVP_MD_CTX_init(&ctx);
 
        /* Extract useful info from parameter */
        if (param == NULL || param->type != V_ASN1_SEQUENCE ||
@@ -104,29 +106,37 @@ int PKCS5_PBE_keyivgen(EVP_CIPHER_CTX *cctx, const char *pass, int passlen,
        if(!pass) passlen = 0;
        else if(passlen == -1) passlen = strlen(pass);
 
-       EVP_MD_CTX_init(&ctx);
-       EVP_DigestInit_ex(&ctx, md, NULL);
-       EVP_DigestUpdate(&ctx, pass, passlen);
-       EVP_DigestUpdate(&ctx, salt, saltlen);
+       if (!EVP_DigestInit_ex(&ctx, md, NULL))
+               goto err;
+       if (!EVP_DigestUpdate(&ctx, pass, passlen))
+               goto err;
+       if (!EVP_DigestUpdate(&ctx, salt, saltlen))
+               goto err;
        PBEPARAM_free(pbe);
-       EVP_DigestFinal_ex(&ctx, md_tmp, NULL);
+       if (!EVP_DigestFinal_ex(&ctx, md_tmp, NULL))
+               goto err;
        mdsize = EVP_MD_size(md);
        if (mdsize < 0)
            return 0;
        for (i = 1; i < iter; i++) {
-               EVP_DigestInit_ex(&ctx, md, NULL);
-               EVP_DigestUpdate(&ctx, md_tmp, mdsize);
-               EVP_DigestFinal_ex (&ctx, md_tmp, NULL);
+               if (!EVP_DigestInit_ex(&ctx, md, NULL))
+                       goto err;
+               if (!EVP_DigestUpdate(&ctx, md_tmp, mdsize))
+                       goto err;
+               if (!EVP_DigestFinal_ex (&ctx, md_tmp, NULL))
+                       goto err;
        }
-       EVP_MD_CTX_cleanup(&ctx);
        OPENSSL_assert(EVP_CIPHER_key_length(cipher) <= (int)sizeof(md_tmp));
        memcpy(key, md_tmp, EVP_CIPHER_key_length(cipher));
        OPENSSL_assert(EVP_CIPHER_iv_length(cipher) <= 16);
        memcpy(iv, md_tmp + (16 - EVP_CIPHER_iv_length(cipher)),
                                                 EVP_CIPHER_iv_length(cipher));
-       EVP_CipherInit_ex(cctx, cipher, NULL, key, iv, en_de);
+       if (!EVP_CipherInit_ex(cctx, cipher, NULL, key, iv, en_de))
+               goto err;
        OPENSSL_cleanse(md_tmp, EVP_MAX_MD_SIZE);
        OPENSSL_cleanse(key, EVP_MAX_KEY_LENGTH);
        OPENSSL_cleanse(iv, EVP_MAX_IV_LENGTH);
-       return 1;
+       rv = 1;
+       err:
+       return rv;
 }
index 334379f..01b6b50 100644 (file)
@@ -110,10 +110,14 @@ int PKCS5_PBKDF2_HMAC(const char *pass, int passlen,
                itmp[1] = (unsigned char)((i >> 16) & 0xff);
                itmp[2] = (unsigned char)((i >> 8) & 0xff);
                itmp[3] = (unsigned char)(i & 0xff);
-               HMAC_Init_ex(&hctx, pass, passlen, digest, NULL);
-               HMAC_Update(&hctx, salt, saltlen);
-               HMAC_Update(&hctx, itmp, 4);
-               HMAC_Final(&hctx, digtmp, NULL);
+               if (!HMAC_Init_ex(&hctx, pass, passlen, digest, NULL)
+                       || !HMAC_Update(&hctx, salt, saltlen)
+                       || !HMAC_Update(&hctx, itmp, 4)
+                       || !HMAC_Final(&hctx, digtmp, NULL))
+                       {
+                       HMAC_CTX_cleanup(&hctx);
+                       return 0;
+                       }
                memcpy(p, digtmp, cplen);
                for(j = 1; j < iter; j++)
                        {
@@ -211,7 +215,11 @@ int PKCS5_v2_PBE_keyivgen(EVP_CIPHER_CTX *ctx, const char *pass, int passlen,
        }
 
        /* Fixup cipher based on AlgorithmIdentifier */
-       EVP_CipherInit_ex(ctx, cipher, NULL, NULL, NULL, en_de);
+       if (!EVP_CipherInit_ex(ctx, cipher, NULL, NULL, NULL, en_de))
+               {
+               EVPerr(EVP_F_PKCS5_V2_PBE_KEYIVGEN, ERR_R_EVP_LIB);
+               goto err;
+               }
        if(EVP_CIPHER_asn1_to_param(ctx, pbe2->encryption->parameter) < 0) {
                EVPerr(EVP_F_PKCS5_V2_PBE_KEYIVGEN,
                                        EVP_R_CIPHER_PARAMETER_ERROR);
@@ -278,7 +286,11 @@ int PKCS5_v2_PBE_keyivgen(EVP_CIPHER_CTX *ctx, const char *pass, int passlen,
        if(!PKCS5_PBKDF2_HMAC(pass, passlen, salt, saltlen, iter, prfmd,
                                                   keylen, key))
                goto err;
-       EVP_CipherInit_ex(ctx, NULL, NULL, key, NULL, en_de);
+       if (!EVP_CipherInit_ex(ctx, NULL, NULL, key, NULL, en_de))
+               {
+               EVPerr(EVP_F_PKCS5_V2_PBE_KEYIVGEN, ERR_R_EVP_LIB);
+               goto err;
+               }
        OPENSSL_cleanse(key, keylen);
        PBKDF2PARAM_free(kdf);
        return 1;
index 53a59a2..c748fbe 100644 (file)
@@ -115,7 +115,8 @@ int EVP_OpenFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
        int i;
 
        i=EVP_DecryptFinal_ex(ctx,out,outl);
-       EVP_DecryptInit_ex(ctx,NULL,NULL,NULL,NULL);
+       if (i)
+               i = EVP_DecryptInit_ex(ctx,NULL,NULL,NULL,NULL);
        return(i);
        }
 #else /* !OPENSSL_NO_RSA */
index d832452..e5919b0 100644 (file)
@@ -110,6 +110,7 @@ int EVP_SealFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
        {
        int i;
        i = EVP_EncryptFinal_ex(ctx,out,outl);
-       EVP_EncryptInit_ex(ctx,NULL,NULL,NULL,NULL);
+       if (i) 
+               i = EVP_EncryptInit_ex(ctx,NULL,NULL,NULL,NULL);
        return i;
        }
index bb893f5..ba3596b 100644 (file)
@@ -85,8 +85,10 @@ int EVP_SignFinal(EVP_MD_CTX *ctx, unsigned char *sigret, unsigned int *siglen,
 
        *siglen=0;
        EVP_MD_CTX_init(&tmp_ctx);
-       EVP_MD_CTX_copy_ex(&tmp_ctx,ctx);   
-       EVP_DigestFinal_ex(&tmp_ctx,&(m[0]),&m_len);
+       if (!EVP_MD_CTX_copy_ex(&tmp_ctx,ctx))
+               goto err;  
+       if (!EVP_DigestFinal_ex(&tmp_ctx,&(m[0]),&m_len))
+               goto err;
        EVP_MD_CTX_cleanup(&tmp_ctx);
 
        if (ctx->digest->flags & EVP_MD_FLAG_PKEY_METHOD_SIGNATURE)
index 41d4b67..d43f86e 100644 (file)
@@ -71,8 +71,10 @@ int EVP_VerifyFinal(EVP_MD_CTX *ctx, const unsigned char *sigbuf,
        EVP_MD_CTX tmp_ctx;
 
        EVP_MD_CTX_init(&tmp_ctx);
-       EVP_MD_CTX_copy_ex(&tmp_ctx,ctx);     
-       EVP_DigestFinal_ex(&tmp_ctx,&(m[0]),&m_len);
+       if (!EVP_MD_CTX_copy_ex(&tmp_ctx,ctx))
+               goto err;    
+       if (!EVP_DigestFinal_ex(&tmp_ctx,&(m[0]),&m_len))
+               goto err;
        EVP_MD_CTX_cleanup(&tmp_ctx);
 
        if (ctx->digest->flags & EVP_MD_FLAG_PKEY_METHOD_SIGNATURE)
index 71e8567..0daa445 100644 (file)
@@ -100,7 +100,8 @@ static int pkey_hmac_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src)
        dctx = dst->data;
        dctx->md = sctx->md;
        HMAC_CTX_init(&dctx->ctx);
-       HMAC_CTX_copy(&dctx->ctx, &sctx->ctx);
+       if (!HMAC_CTX_copy(&dctx->ctx, &sctx->ctx))
+               return 0;
        if (sctx->ktmp.data)
                {
                if (!ASN1_OCTET_STRING_set(&dctx->ktmp,
@@ -141,7 +142,8 @@ static int pkey_hmac_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
 static int int_update(EVP_MD_CTX *ctx,const void *data,size_t count)
        {
        HMAC_PKEY_CTX *hctx = ctx->pctx->data;
-       HMAC_Update(&hctx->ctx, data, count);
+       if (!HMAC_Update(&hctx->ctx, data, count))
+               return 0;
        return 1;
        }
 
@@ -167,7 +169,8 @@ static int hmac_signctx(EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen,
        if (!sig)
                return 1;
 
-       HMAC_Final(&hctx->ctx, sig, &hlen);
+       if (!HMAC_Final(&hctx->ctx, sig, &hlen))
+               return 0;
        *siglen = (size_t)hlen;
        return 1;
        }
@@ -192,8 +195,9 @@ static int pkey_hmac_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
 
                case EVP_PKEY_CTRL_DIGESTINIT:
                key = (ASN1_OCTET_STRING *)ctx->pkey->pkey.ptr;
-               HMAC_Init_ex(&hctx->ctx, key->data, key->length, hctx->md,
-                               ctx->engine);
+               if (!HMAC_Init_ex(&hctx->ctx, key->data, key->length, hctx->md,
+                               ctx->engine))
+                       return 0;
                break;
 
                default:
index e92b86c..a94dc83 100644 (file)
@@ -124,7 +124,8 @@ OCSP_CERTID *OCSP_cert_id_new(const EVP_MD *dgst,
        if (!(ASN1_OCTET_STRING_set(cid->issuerNameHash, md, i))) goto err;
 
        /* Calculate the issuerKey hash, excluding tag and length */
-       EVP_Digest(issuerKey->data, issuerKey->length, md, &i, dgst, NULL);
+       if (!EVP_Digest(issuerKey->data, issuerKey->length, md, &i, dgst, NULL))
+               goto err;
 
        if (!(ASN1_OCTET_STRING_set(cid->issuerKeyHash, md, i))) goto err;
 
index 5f130c4..b1bf71a 100644 (file)
@@ -709,13 +709,16 @@ static int derive_pvk_key(unsigned char *key,
                        const unsigned char *pass, int passlen)
        {
        EVP_MD_CTX mctx;
+       int rv = 1;
        EVP_MD_CTX_init(&mctx);
-       EVP_DigestInit_ex(&mctx, EVP_sha1(), NULL);
-       EVP_DigestUpdate(&mctx, salt, saltlen);
-       EVP_DigestUpdate(&mctx, pass, passlen);
-       EVP_DigestFinal_ex(&mctx, key, NULL);
+       if (!EVP_DigestInit_ex(&mctx, EVP_sha1(), NULL)
+               || !EVP_DigestUpdate(&mctx, salt, saltlen)
+               || !EVP_DigestUpdate(&mctx, pass, passlen)
+               || !EVP_DigestFinal_ex(&mctx, key, NULL))
+                       rv = 0;
+
        EVP_MD_CTX_cleanup(&mctx);
-       return 1;
+       return rv;
        }
        
 
@@ -727,11 +730,12 @@ static EVP_PKEY *do_PVK_body(const unsigned char **in,
        const unsigned char *p = *in;
        unsigned int magic;
        unsigned char *enctmp = NULL, *q;
+       EVP_CIPHER_CTX cctx;
+       EVP_CIPHER_CTX_init(&cctx);
        if (saltlen)
                {
                char psbuf[PEM_BUFSIZE];
                unsigned char keybuf[20];
-               EVP_CIPHER_CTX cctx;
                int enctmplen, inlen;
                if (cb)
                        inlen=cb(psbuf,PEM_BUFSIZE,0,u);
@@ -757,37 +761,41 @@ static EVP_PKEY *do_PVK_body(const unsigned char **in,
                p += 8;
                inlen = keylen - 8;
                q = enctmp + 8;
-               EVP_CIPHER_CTX_init(&cctx);
-               EVP_DecryptInit_ex(&cctx, EVP_rc4(), NULL, keybuf, NULL);
-               EVP_DecryptUpdate(&cctx, q, &enctmplen, p, inlen);
-               EVP_DecryptFinal_ex(&cctx, q + enctmplen, &enctmplen);
+               if (!EVP_DecryptInit_ex(&cctx, EVP_rc4(), NULL, keybuf, NULL))
+                       goto err;
+               if (!EVP_DecryptUpdate(&cctx, q, &enctmplen, p, inlen))
+                       goto err;
+               if (!EVP_DecryptFinal_ex(&cctx, q + enctmplen, &enctmplen))
+                       goto err;
                magic = read_ledword((const unsigned char **)&q);
                if (magic != MS_RSA2MAGIC && magic != MS_DSS2MAGIC)
                        {
                        q = enctmp + 8;
                        memset(keybuf + 5, 0, 11);
-                       EVP_DecryptInit_ex(&cctx, EVP_rc4(), NULL, keybuf,
-                                                               NULL);
+                       if (!EVP_DecryptInit_ex(&cctx, EVP_rc4(), NULL, keybuf,
+                                                               NULL))
+                               goto err;
                        OPENSSL_cleanse(keybuf, 20);
-                       EVP_DecryptUpdate(&cctx, q, &enctmplen, p, inlen);
-                       EVP_DecryptFinal_ex(&cctx, q + enctmplen,
-                                                               &enctmplen);
+                       if (!EVP_DecryptUpdate(&cctx, q, &enctmplen, p, inlen))
+                               goto err;
+                       if (!EVP_DecryptFinal_ex(&cctx, q + enctmplen,
+                                                               &enctmplen))
+                               goto err;
                        magic = read_ledword((const unsigned char **)&q);
                        if (magic != MS_RSA2MAGIC && magic != MS_DSS2MAGIC)
                                {
-                               EVP_CIPHER_CTX_cleanup(&cctx);
                                PEMerr(PEM_F_DO_PVK_BODY, PEM_R_BAD_DECRYPT);
                                goto err;
                                }
                        }
                else
                        OPENSSL_cleanse(keybuf, 20);
-               EVP_CIPHER_CTX_cleanup(&cctx);
                p = enctmp;
                }
 
        ret = b2i_PrivateKey(&p, keylen);
        err:
+       EVP_CIPHER_CTX_cleanup(&cctx);
        if (enctmp && saltlen)
                OPENSSL_free(enctmp);
        return ret;
@@ -841,6 +849,8 @@ static int i2b_PVK(unsigned char **out, EVP_PKEY*pk, int enclevel,
        {
        int outlen = 24, pklen;
        unsigned char *p, *salt = NULL;
+       EVP_CIPHER_CTX cctx;
+       EVP_CIPHER_CTX_init(&cctx);
        if (enclevel)
                outlen += PVK_SALTLEN;
        pklen = do_i2b(NULL, pk, 0);
@@ -885,7 +895,6 @@ static int i2b_PVK(unsigned char **out, EVP_PKEY*pk, int enclevel,
                {
                char psbuf[PEM_BUFSIZE];
                unsigned char keybuf[20];
-               EVP_CIPHER_CTX cctx;
                int enctmplen, inlen;
                if (cb)
                        inlen=cb(psbuf,PEM_BUFSIZE,1,u);
@@ -902,16 +911,19 @@ static int i2b_PVK(unsigned char **out, EVP_PKEY*pk, int enclevel,
                if (enclevel == 1)
                        memset(keybuf + 5, 0, 11);
                p = salt + PVK_SALTLEN + 8;
-               EVP_CIPHER_CTX_init(&cctx);
-               EVP_EncryptInit_ex(&cctx, EVP_rc4(), NULL, keybuf, NULL);
+               if (!EVP_EncryptInit_ex(&cctx, EVP_rc4(), NULL, keybuf, NULL))
+                       goto error;
                OPENSSL_cleanse(keybuf, 20);
-               EVP_DecryptUpdate(&cctx, p, &enctmplen, p, pklen - 8);
-               EVP_DecryptFinal_ex(&cctx, p + enctmplen, &enctmplen);
-               EVP_CIPHER_CTX_cleanup(&cctx);
+               if (!EVP_DecryptUpdate(&cctx, p, &enctmplen, p, pklen - 8))
+                       goto error;
+               if (!EVP_DecryptFinal_ex(&cctx, p + enctmplen, &enctmplen))
+                       goto error;
                }
+       EVP_CIPHER_CTX_cleanup(&cctx);
        return outlen;
 
        error:
+       EVP_CIPHER_CTX_cleanup(&cctx);
        return -1;
        }
 
index ba77dbb..9d3557e 100644 (file)
@@ -89,7 +89,14 @@ unsigned char * PKCS12_pbe_crypt(X509_ALGOR *algor, const char *pass,
                goto err;
        }
 
-       EVP_CipherUpdate(&ctx, out, &i, in, inlen);
+       if (!EVP_CipherUpdate(&ctx, out, &i, in, inlen))
+               {
+               OPENSSL_free(out);
+               out = NULL;
+               PKCS12err(PKCS12_F_PKCS12_PBE_CRYPT,ERR_R_EVP_LIB);
+               goto err;
+               }
+
        outlen = i;
        if(!EVP_CipherFinal_ex(&ctx, out + i, &i)) {
                OPENSSL_free(out);
index 424203f..c55c7b6 100644 (file)
@@ -152,14 +152,16 @@ int PKCS12_key_gen_uni(unsigned char *pass, int passlen, unsigned char *salt,
        for (i = 0; i < Slen; i++) *p++ = salt[i % saltlen];
        for (i = 0; i < Plen; i++) *p++ = pass[i % passlen];
        for (;;) {
-               EVP_DigestInit_ex(&ctx, md_type, NULL);
-               EVP_DigestUpdate(&ctx, D, v);
-               EVP_DigestUpdate(&ctx, I, Ilen);
-               EVP_DigestFinal_ex(&ctx, Ai, NULL);
+               if (!EVP_DigestInit_ex(&ctx, md_type, NULL)
+                       || !EVP_DigestUpdate(&ctx, D, v)
+                       || !EVP_DigestUpdate(&ctx, I, Ilen)
+                       || !EVP_DigestFinal_ex(&ctx, Ai, NULL))
+                       goto err;
                for (j = 1; j < iter; j++) {
-                       EVP_DigestInit_ex(&ctx, md_type, NULL);
-                       EVP_DigestUpdate(&ctx, Ai, u);
-                       EVP_DigestFinal_ex(&ctx, Ai, NULL);
+                       if (!EVP_DigestInit_ex(&ctx, md_type, NULL)
+                               || !EVP_DigestUpdate(&ctx, Ai, u)
+                               || !EVP_DigestFinal_ex(&ctx, Ai, NULL))
+                       goto err;
                }
                memcpy (out, Ai, min (n, u));
                if (u >= n) {
index 9ab740d..96de1bd 100644 (file)
@@ -97,10 +97,14 @@ int PKCS12_gen_mac(PKCS12 *p12, const char *pass, int passlen,
                return 0;
        }
        HMAC_CTX_init(&hmac);
-       HMAC_Init_ex(&hmac, key, md_size, md_type, NULL);
-       HMAC_Update(&hmac, p12->authsafes->d.data->data,
-                                        p12->authsafes->d.data->length);
-       HMAC_Final(&hmac, mac, maclen);
+       if (!HMAC_Init_ex(&hmac, key, md_size, md_type, NULL)
+               || !HMAC_Update(&hmac, p12->authsafes->d.data->data,
+                                        p12->authsafes->d.data->length)
+               || !HMAC_Final(&hmac, mac, maclen))
+               {
+               HMAC_CTX_cleanup(&hmac);
+               return 0;
+               }
        HMAC_CTX_cleanup(&hmac);
        return 1;
 }
index 3bf1a36..cb8434b 100644 (file)
@@ -676,7 +676,11 @@ static int do_pkcs7_signed_attrib(PKCS7_SIGNER_INFO *si, EVP_MD_CTX *mctx)
                }
 
        /* Add digest */
-       EVP_DigestFinal_ex(mctx, md_data,&md_len);
+       if (!EVP_DigestFinal_ex(mctx, md_data,&md_len))
+               {
+               PKCS7err(PKCS7_F_DO_PKCS7_SIGNED_ATTRIB, ERR_R_EVP_LIB);
+               return 0;
+               }
        if (!PKCS7_add1_attrib_digest(si, md_data, md_len))
                {
                PKCS7err(PKCS7_F_DO_PKCS7_SIGNED_ATTRIB, ERR_R_MALLOC_FAILURE);
@@ -784,7 +788,8 @@ int PKCS7_dataFinal(PKCS7 *p7, BIO *bio)
 
                        /* We now have the EVP_MD_CTX, lets do the
                         * signing. */
-                       EVP_MD_CTX_copy_ex(&ctx_tmp,mdc);
+                       if (!EVP_MD_CTX_copy_ex(&ctx_tmp,mdc))
+                               goto err;
 
                        sk=si->auth_attr;
 
@@ -822,7 +827,8 @@ int PKCS7_dataFinal(PKCS7 *p7, BIO *bio)
                if (!PKCS7_find_digest(&mdc, bio,
                                OBJ_obj2nid(p7->d.digest->md->algorithm)))
                        goto err;
-               EVP_DigestFinal_ex(mdc,md_data,&md_len);
+               if (!EVP_DigestFinal_ex(mdc,md_data,&md_len))
+                       goto err;
                M_ASN1_OCTET_STRING_set(p7->d.digest->digest, md_data, md_len);
                }
 
@@ -1015,7 +1021,8 @@ int PKCS7_signatureVerify(BIO *bio, PKCS7 *p7, PKCS7_SIGNER_INFO *si,
 
        /* mdc is the digest ctx that we want, unless there are attributes,
         * in which case the digest is the signed attributes */
-       EVP_MD_CTX_copy_ex(&mdc_tmp,mdc);
+       if (!EVP_MD_CTX_copy_ex(&mdc_tmp,mdc))
+               goto err;
 
        sk=si->auth_attr;
        if ((sk != NULL) && (sk_X509_ATTRIBUTE_num(sk) != 0))
@@ -1025,7 +1032,8 @@ int PKCS7_signatureVerify(BIO *bio, PKCS7 *p7, PKCS7_SIGNER_INFO *si,
                int alen;
                ASN1_OCTET_STRING *message_digest;
 
-               EVP_DigestFinal_ex(&mdc_tmp,md_dat,&md_len);
+               if (!EVP_DigestFinal_ex(&mdc_tmp,md_dat,&md_len))
+                       goto err;
                message_digest=PKCS7_digest_from_attributes(sk);
                if (!message_digest)
                        {
@@ -1050,7 +1058,8 @@ for (ii=0; ii<md_len; ii++) printf("%02X",md_dat[ii]); printf(" calc\n");
                        goto err;
                        }
 
-               EVP_VerifyInit_ex(&mdc_tmp,EVP_get_digestbynid(md_type), NULL);
+               if (!EVP_VerifyInit_ex(&mdc_tmp,EVP_get_digestbynid(md_type), NULL))
+                       goto err;
 
                alen = ASN1_item_i2d((ASN1_VALUE *)sk, &abuf,
                                                ASN1_ITEM_rptr(PKCS7_ATTR_VERIFY));
@@ -1060,7 +1069,8 @@ for (ii=0; ii<md_len; ii++) printf("%02X",md_dat[ii]); printf(" calc\n");
                        ret = -1;
                        goto err;
                        }
-               EVP_VerifyUpdate(&mdc_tmp, abuf, alen);
+               if (!EVP_VerifyUpdate(&mdc_tmp, abuf, alen))
+                       goto err;
 
                OPENSSL_free(abuf);
                }
index e238d10..553d212 100644 (file)
@@ -56,7 +56,8 @@ int RSA_padding_add_PKCS1_OAEP(unsigned char *to, int tlen,
        seed = to + 1;
        db = to + SHA_DIGEST_LENGTH + 1;
 
-       EVP_Digest((void *)param, plen, db, NULL, EVP_sha1(), NULL);
+       if (!EVP_Digest((void *)param, plen, db, NULL, EVP_sha1(), NULL))
+               return 0;
        memset(db + SHA_DIGEST_LENGTH, 0,
                emlen - flen - 2 * SHA_DIGEST_LENGTH - 1);
        db[emlen - flen - SHA_DIGEST_LENGTH - 1] = 0x01;
@@ -145,7 +146,8 @@ int RSA_padding_check_PKCS1_OAEP(unsigned char *to, int tlen,
        for (i = 0; i < dblen; i++)
                db[i] ^= maskeddb[i];
 
-       EVP_Digest((void *)param, plen, phash, NULL, EVP_sha1(), NULL);
+       if (!EVP_Digest((void *)param, plen, phash, NULL, EVP_sha1(), NULL))
+               return -1;
 
        if (memcmp(db, phash, SHA_DIGEST_LENGTH) != 0 || bad)
                goto decoding_err;
@@ -189,34 +191,40 @@ int PKCS1_MGF1(unsigned char *mask, long len,
        EVP_MD_CTX c;
        unsigned char md[EVP_MAX_MD_SIZE];
        int mdlen;
+       int rv = -1;
 
        EVP_MD_CTX_init(&c);
        mdlen = EVP_MD_size(dgst);
        if (mdlen < 0)
-               return -1;
+               goto err;
        for (i = 0; outlen < len; i++)
                {
                cnt[0] = (unsigned char)((i >> 24) & 255);
                cnt[1] = (unsigned char)((i >> 16) & 255);
                cnt[2] = (unsigned char)((i >> 8)) & 255;
                cnt[3] = (unsigned char)(i & 255);
-               EVP_DigestInit_ex(&c,dgst, NULL);
-               EVP_DigestUpdate(&c, seed, seedlen);
-               EVP_DigestUpdate(&c, cnt, 4);
+               if (!EVP_DigestInit_ex(&c,dgst, NULL)
+                       || !EVP_DigestUpdate(&c, seed, seedlen)
+                       || !EVP_DigestUpdate(&c, cnt, 4))
+                       goto err;
                if (outlen + mdlen <= len)
                        {
-                       EVP_DigestFinal_ex(&c, mask + outlen, NULL);
+                       if (!EVP_DigestFinal_ex(&c, mask + outlen, NULL))
+                               goto err;
                        outlen += mdlen;
                        }
                else
                        {
-                       EVP_DigestFinal_ex(&c, md, NULL);
+                       if (!EVP_DigestFinal_ex(&c, md, NULL))
+                               goto err;
                        memcpy(mask + outlen, md, len - outlen);
                        outlen = len;
                        }
                }
+       rv = 0;
+       err:
        EVP_MD_CTX_cleanup(&c);
-       return 0;
+       return rv;
        }
 
 static int MGF1(unsigned char *mask, long len, const unsigned char *seed,
index e1f3b53..afe16af 100644 (file)
@@ -614,12 +614,15 @@ static int TS_compute_imprint(BIO *data, TS_TST_INFO *tst_info,
                goto err;
                }
 
-       EVP_DigestInit(&md_ctx, md);
+       if (!EVP_DigestInit(&md_ctx, md))
+               goto err;
        while ((length = BIO_read(data, buffer, sizeof(buffer))) > 0)
                {
-               EVP_DigestUpdate(&md_ctx, buffer, length);
+               if (!EVP_DigestUpdate(&md_ctx, buffer, length))
+                       goto err;
                }
-       EVP_DigestFinal(&md_ctx, *imprint, NULL);
+       if (!EVP_DigestFinal(&md_ctx, *imprint, NULL))
+               goto err;
 
        return 1;
  err:
index 4bc9da0..a75876e 100644 (file)
@@ -87,15 +87,20 @@ unsigned long X509_issuer_and_serial_hash(X509 *a)
        EVP_MD_CTX_init(&ctx);
        f=X509_NAME_oneline(a->cert_info->issuer,NULL,0);
        ret=strlen(f);
-       EVP_DigestInit_ex(&ctx, EVP_md5(), NULL);
-       EVP_DigestUpdate(&ctx,(unsigned char *)f,ret);
+       if (!EVP_DigestInit_ex(&ctx, EVP_md5(), NULL))
+               goto err;
+       if (!EVP_DigestUpdate(&ctx,(unsigned char *)f,ret))
+               goto err;
        OPENSSL_free(f);
-       EVP_DigestUpdate(&ctx,(unsigned char *)a->cert_info->serialNumber->data,
-               (unsigned long)a->cert_info->serialNumber->length);
-       EVP_DigestFinal_ex(&ctx,&(md[0]),NULL);
+       if(!EVP_DigestUpdate(&ctx,(unsigned char *)a->cert_info->serialNumber->data,
+               (unsigned long)a->cert_info->serialNumber->length))
+               goto err;
+       if (!EVP_DigestFinal_ex(&ctx,&(md[0]),NULL))
+               goto err;
        ret=(   ((unsigned long)md[0]     )|((unsigned long)md[1]<<8L)|
                ((unsigned long)md[2]<<16L)|((unsigned long)md[3]<<24L)
                )&0xffffffffL;
+       err:
        EVP_MD_CTX_cleanup(&ctx);
        return(ret);
        }
@@ -219,7 +224,9 @@ unsigned long X509_NAME_hash(X509_NAME *x)
 
        /* Make sure X509_NAME structure contains valid cached encoding */
        i2d_X509_NAME(x,NULL);
-       EVP_Digest(x->canon_enc, x->canon_enclen, md, NULL, EVP_sha1(), NULL);
+       if (!EVP_Digest(x->canon_enc, x->canon_enclen, md, NULL, EVP_sha1(),
+               NULL))
+               return 0;
 
        ret=(   ((unsigned long)md[0]     )|((unsigned long)md[1]<<8L)|
                ((unsigned long)md[2]<<16L)|((unsigned long)md[3]<<24L)
@@ -239,7 +246,8 @@ unsigned long X509_NAME_hash_old(X509_NAME *x)
 
        /* Make sure X509_NAME structure contains valid cached encoding */
        i2d_X509_NAME(x,NULL);
-       EVP_Digest(x->bytes->data, x->bytes->length, md, NULL, EVP_md5(), NULL);
+       if (!EVP_Digest(x->bytes->data, x->bytes->length, md, NULL, EVP_md5(), NULL))
+               return 0;
 
        ret=(   ((unsigned long)md[0]     )|((unsigned long)md[1]<<8L)|
                ((unsigned long)md[2]<<16L)|((unsigned long)md[3]<<24L)
index 202c9e4..0a984fb 100644 (file)
@@ -129,7 +129,8 @@ static ASN1_OCTET_STRING *s2i_skey_id(X509V3_EXT_METHOD *method,
                goto err;
        }
 
-       EVP_Digest(pk->data, pk->length, pkey_dig, &diglen, EVP_sha1(), NULL);
+       if (!EVP_Digest(pk->data, pk->length, pkey_dig, &diglen, EVP_sha1(), NULL))
+               goto err;
 
        if(!M_ASN1_OCTET_STRING_set(oct, pkey_dig, diglen)) {
                X509V3err(X509V3_F_S2I_SKEY_ID,ERR_R_MALLOC_FAILURE);