Fix memory leaks: uninstantiate DRBG during health checks. Cleanup md_ctx
[openssl.git] / fips / rand / fips_drbg_selftest.c
index a3732c1a4abf0aaeefe279b4ff68ab95449621c2..496ea734817d99bda86dfc2ae5c1f7cdf3fccd48 100644 (file)
@@ -797,6 +797,8 @@ static int fips_drbg_single_kat(DRBG_CTX *dctx, DRBG_SELFTEST_DATA *td)
        rv = 1;
 
        err:
+       if (rv == 0)
+               FIPSerr(FIPS_F_FIPS_DRBG_SINGLE_KAT, FIPS_R_SELFTEST_FAILED);
        FIPS_drbg_uninstantiate(dctx);
        
        return rv;
@@ -857,6 +859,13 @@ static int fips_drbg_health_check(DRBG_CTX *dctx, DRBG_SELFTEST_DATA *td)
                goto err;
                }
 
+       dctx->flags &= ~DRBG_FLAG_NOERR;
+       if (!FIPS_drbg_uninstantiate(dctx))
+               {
+               FIPSerr(FIPS_F_FIPS_DRBG_HEALTH_CHECK, FIPS_R_UNINSTANTIATE_ERROR);
+               goto err;
+               }
+
        /* Instantiate with valid data. NB: errors now reported again */
        if (!FIPS_drbg_init(dctx, td->nid, td->flags))
                goto err;
@@ -909,6 +918,14 @@ static int fips_drbg_health_check(DRBG_CTX *dctx, DRBG_SELFTEST_DATA *td)
                goto err;
                }
                
+       dctx->flags &= ~DRBG_FLAG_NOERR;
+
+       if (!FIPS_drbg_uninstantiate(dctx))
+               {
+               FIPSerr(FIPS_F_FIPS_DRBG_HEALTH_CHECK, FIPS_R_UNINSTANTIATE_ERROR);
+               goto err;
+               }
+
 
        /* Instantiate again with valid data */
 
@@ -952,11 +969,11 @@ static int fips_drbg_health_check(DRBG_CTX *dctx, DRBG_SELFTEST_DATA *td)
                }
 
        FIPS_drbg_uninstantiate(dctx);
-       p = (unsigned char *)dctx;
+       p = (unsigned char *)&dctx->d;
        /* Standard says we have to check uninstantiate really zeroes
         * the data...
         */
-       for (i = 0; i < sizeof(DRBG_CTX); i++)
+       for (i = 0; i < sizeof(dctx->d); i++)
                {
                if (*p != 0)
                        {
@@ -978,7 +995,7 @@ static int fips_drbg_health_check(DRBG_CTX *dctx, DRBG_SELFTEST_DATA *td)
        return 0;
 
        }
-               
+
 
 int fips_drbg_kat(DRBG_CTX *dctx, int nid, unsigned int flags)
        {