APPS: make apps strict on app_RAND_load() and app_RAND_write() failure
authorDr. David von Oheimb <David.von.Oheimb@siemens.com>
Sat, 3 Apr 2021 10:53:51 +0000 (12:53 +0200)
committerDr. David von Oheimb <dev@ddvo.net>
Wed, 14 Apr 2021 14:48:27 +0000 (16:48 +0200)
Reviewed-by: Paul Dale <pauli@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/14840)

28 files changed:
apps/ca.c
apps/cmp.c
apps/cms.c
apps/dgst.c
apps/dhparam.c
apps/dsaparam.c
apps/ecparam.c
apps/enc.c
apps/gendsa.c
apps/genrsa.c
apps/include/apps.h
apps/include/opt.h
apps/lib/app_rand.c
apps/openssl.c
apps/passwd.c
apps/pkcs12.c
apps/pkcs8.c
apps/pkeyutl.c
apps/rand.c
apps/req.c
apps/rsautl.c
apps/s_client.c
apps/s_server.c
apps/smime.c
apps/speed.c
apps/srp.c
apps/ts.c
apps/x509.c

index 268bd76912ac21ce660040c7fb712a6fd2045321..cec5c8f1ac4021872ca683317c1039d5c5f3a902 100755 (executable)
--- a/apps/ca.c
+++ b/apps/ca.c
@@ -521,7 +521,8 @@ end_of_options:
         goto end;
 
     app_RAND_load_conf(conf, BASE_SECTION);
-    app_RAND_load();
+    if (!app_RAND_load())
+        goto end;
 
     f = NCONF_get_string(conf, section, STRING_MASK);
     if (f == NULL)
index 8a996f6dce533182270d5c18a0d513141d273b24..135c5098311a0d8c9bf94a31a02d6f975ec125d3 100644 (file)
@@ -2603,6 +2603,8 @@ int cmp_main(int argc, char **argv)
     if (ret <= 0)
         goto err;
     ret = 0;
+    if (!app_RAND_load())
+        goto err;
 
     if (opt_batch)
         set_base_ui_method(UI_null());
index b03e981a56cdfb920c52f8be65429d51036a1e27..56f0b37bbf60fe30f2c5187209d1a12bda327b6e 100644 (file)
@@ -697,7 +697,9 @@ int cms_main(int argc, char **argv)
             break;
         }
     }
-    app_RAND_load();
+    if (!app_RAND_load())
+        goto end;
+
     if (digestname != NULL) {
         if (!opt_md(digestname, &sign_md))
             goto end;
index 891cf79279b01b1b7ad08ede681327b7faddda0b..20626c2b32b74fd59bdc2ae4919fe6a021978312 100644 (file)
@@ -225,7 +225,9 @@ int dgst_main(int argc, char **argv)
         BIO_printf(bio_err, "%s: Can only sign or verify one file.\n", prog);
         goto end;
     }
-    app_RAND_load();
+    if (!app_RAND_load())
+        goto end;
+
     if (digestname != NULL) {
         if (!opt_md(digestname, &md))
             goto opthelp;
index 136dbcff64f5d3d4a4899f5152a9ec3322ca833c..b43935eb7fa149400686c2f7d3bbc847ad6fca7d 100644 (file)
@@ -158,8 +158,8 @@ int dhparam_main(int argc, char **argv)
     } else if (argc != 0) {
         goto opthelp;
     }
-    app_RAND_load();
-
+    if (!app_RAND_load())
+        goto end;
 
     if (g && !num)
         num = DEFBITS;
index c83d1fff411a0723c421753e5852c684a636638b..a38dceb2555d388a15bdc1c03ba2b1a86858fe41 100644 (file)
@@ -135,7 +135,8 @@ int dsaparam_main(int argc, char **argv)
     } else if (argc != 0) {
         goto opthelp;
     }
-    app_RAND_load();
+    if (!app_RAND_load())
+        goto end;
 
     /* generate a key */
     numbits = num;
index fc19ab6bf9db77385dd726158bb27a6bb7ef1160..c99b8cc90935e81859dcc1ee13a78163e83c95ac 100644 (file)
@@ -190,7 +190,9 @@ int ecparam_main(int argc, char **argv)
     if (argc != 0)
         goto opthelp;
 
-    app_RAND_load();
+    if (!app_RAND_load())
+        goto end;
+
     private = genkey ? 1 : 0;
 
     in = bio_open_default(infile, 'r', informat);
index 498d0d500bc1d5f24ed105268dc239c4e7f237ee..3647a1ce61ccccbe47877e750a3df643a339d45a 100644 (file)
@@ -293,7 +293,8 @@ int enc_main(int argc, char **argv)
     argc = opt_num_rest();
     if (argc != 0)
         goto opthelp;
-    app_RAND_load();
+    if (!app_RAND_load())
+        goto end;
 
     /* Get the cipher name, either from progname (if set) or flag. */
     if (ciphername != NULL) {
index 482191d8bf34e94ebcca9a67647fb2826152a5c2..97904d2c829c10070d2acf04f7c923e6556f13f3 100644 (file)
@@ -107,7 +107,9 @@ int gendsa_main(int argc, char **argv)
         goto opthelp;
     dsaparams = argv[0];
 
-    app_RAND_load();
+    if (!app_RAND_load())
+        goto end;
+
     if (ciphername != NULL) {
         if (!opt_cipher(ciphername, &enc))
             goto end;
index 469b0a0b2fe3a1bd1512ed5f9f05da852c8cb8af..ab991d23853ec7b9ad06678be438095ca6e4b888 100644 (file)
@@ -163,7 +163,9 @@ opthelp:
         goto opthelp;
     }
 
-    app_RAND_load();
+    if (!app_RAND_load())
+        goto end;
+
     private = 1;
     if (ciphername != NULL) {
         if (!opt_cipher(ciphername, &enc))
index 96b37ef95cb190830e4a4a1f32f7d1b86fbea45c..2709b0ccafc16508905fa3745b3c4508a74019ef 100644 (file)
@@ -46,7 +46,7 @@
 # define _UC(c) ((unsigned char)(c))
 
 void app_RAND_load_conf(CONF *c, const char *section);
-void app_RAND_write(void);
+int app_RAND_write(void);
 int app_RAND_load(void);
 
 extern char *default_config_file; /* may be "" */
index d23bf262fc6bbe57079f3c22e6f40e94f96b8f72..3d79224d04382cf080640c90d4acb6204e1a5d23 100644 (file)
 
 # define OPT_R_OPTIONS \
     OPT_SECTION("Random state"), \
-    {"rand", OPT_R_RAND, 's', "Load the file(s) into the random number generator"}, \
+    {"rand", OPT_R_RAND, 's', "Load the given file(s) into the random number generator"}, \
     {"writerand", OPT_R_WRITERAND, '>', "Write random data to the specified file"}
 
 # define OPT_R_CASES \
index c5219795708603db40d282ef8624845ecd3a414b..e38d3a72fc7752e608ba213415683a800f27144e 100644 (file)
@@ -63,9 +63,6 @@ int app_RAND_load(void)
     char *p;
     int i, ret = 1;
 
-    if (randfiles == NULL)
-        return 1;
-
     for (i = 0; i < sk_OPENSSL_STRING_num(randfiles); i++) {
         p = sk_OPENSSL_STRING_value(randfiles, i);
         if (!loadfiles(p))
@@ -75,16 +72,20 @@ int app_RAND_load(void)
     return ret;
 }
 
-void app_RAND_write(void)
+int app_RAND_write(void)
 {
+    int ret = 1;
+
     if (save_rand_file == NULL)
-        return;
+        return 1;
     if (RAND_write_file(save_rand_file) == -1) {
         BIO_printf(bio_err, "Cannot write random bytes:\n");
         ERR_print_errors(bio_err);
+        ret = 0;
     }
     OPENSSL_free(save_rand_file);
     save_rand_file =  NULL;
+    return ret;
 }
 
 
index 6ef7d0ace1bcae046cb12413db786fbb99ccb0e0..30e1e755e0148ccefa557ea4abe1239c54589d77 100644 (file)
@@ -298,7 +298,8 @@ int main(int argc, char *argv[])
     OPENSSL_free(default_config_file);
     lh_FUNCTION_free(prog);
     OPENSSL_free(arg.argv);
-    app_RAND_write();
+    if (!app_RAND_write())
+        ret = EXIT_FAILURE;
 
     BIO_free(bio_in);
     BIO_free_all(bio_out);
index 6e5811236311b214b91407e7cdfcb055b129ece5..1203b7443e9cac64bf908d6b2223770959ba99b5 100644 (file)
@@ -195,7 +195,9 @@ int passwd_main(int argc, char **argv)
         passwds = argv;
     }
 
-    app_RAND_load();
+    if (!app_RAND_load())
+        goto end;
+
     if (mode == passwd_unset) {
         /* use default */
         mode = passwd_md5;
index 0a881408800f4d238811defe1d96e309b134acf2..8c515870dede5d3c044504783ad589d4954fa444 100644 (file)
@@ -345,7 +345,9 @@ int pkcs12_main(int argc, char **argv)
     if (argc != 0)
         goto opthelp;
 
-    app_RAND_load();
+    if (!app_RAND_load())
+        goto end;
+
     if (ciphername != NULL) {
         if (!opt_cipher(ciphername, &enc))
             goto opthelp;
index b8b56f1c8002b582f458e464e37624aae2369adc..cfcb341787bdac3c3b1bb0ffd32ac42d4a5d6f67 100644 (file)
@@ -199,7 +199,9 @@ int pkcs8_main(int argc, char **argv)
         goto opthelp;
 
     private = 1;
-    app_RAND_load();
+    if (!app_RAND_load())
+        goto end;
+
     if (ciphername != NULL) {
         if (!opt_cipher(ciphername, &cipher))
             goto opthelp;
index 27ade9f0792f27c6bea3392ce29901eb73b5f3e3..fd1cdf8f4b04fb5f05e7bf334b6eca6203c72405 100644 (file)
@@ -255,7 +255,8 @@ int pkeyutl_main(int argc, char **argv)
     if (argc != 0)
         goto opthelp;
 
-    app_RAND_load();
+    if (!app_RAND_load())
+        goto end;
 
     if (rawin && pkey_op != EVP_PKEY_OP_SIGN && pkey_op != EVP_PKEY_OP_VERIFY) {
         BIO_printf(bio_err,
index b4392168865c6c90307338e6044f482540fcde4f..24f8c64d435ab3af402f371e037491e56bd21936 100644 (file)
@@ -99,7 +99,9 @@ int rand_main(int argc, char **argv)
         goto opthelp;
     }
 
-    app_RAND_load();
+    if (!app_RAND_load())
+        goto end;
+
     out = bio_open_default(outfile, 'w', format);
     if (out == NULL)
         goto end;
index 04774db399f2eabb5236efd367cbb92ed6a57aee..1b17adb6f42cdd056d62c0d3f73bf2b431f92ac1 100644 (file)
@@ -478,7 +478,9 @@ int req_main(int argc, char **argv)
     if (argc != 0)
         goto opthelp;
 
-    app_RAND_load();
+    if (!app_RAND_load())
+        goto end;
+
     if (digestname != NULL) {
         if (!opt_md(digestname, &md_alg))
             goto opthelp;
index 05b2b189af30b354a87520a93a6e567ef1c74816..57a3f8b4fc9aaf5fab6bc6f5dd3edac9832fe389 100644 (file)
@@ -173,7 +173,9 @@ int rsautl_main(int argc, char **argv)
     if (argc != 0)
         goto opthelp;
 
-    app_RAND_load();
+    if (!app_RAND_load())
+        goto end;
+
     if (need_priv && (key_type != KEY_PRIVKEY)) {
         BIO_printf(bio_err, "A private key is needed for this operation\n");
         goto end;
index ac744ccbd5350a0128a8e0a6f78e2d283bca0766..7b3cf87620d3edcd85c419c41fae6f3b17e0145e 100644 (file)
@@ -1475,7 +1475,8 @@ int s_client_main(int argc, char **argv)
     } else if (argc != 0) {
         goto opthelp;
     }
-    app_RAND_load();
+    if (!app_RAND_load())
+        goto end;
 
     if (count4or6 >= 2) {
         BIO_printf(bio_err, "%s: Can't use both -4 and -6\n", prog);
index 34b28736a11d83eb6e941af8c0ed5ebdfb3b1700..b00303630f758f338d14747ad85c6302f5f0f9eb 100644 (file)
@@ -1610,7 +1610,9 @@ int s_server_main(int argc, char *argv[])
     if (argc != 0)
         goto opthelp;
 
-    app_RAND_load();
+    if (!app_RAND_load())
+        goto end;
+
 #ifndef OPENSSL_NO_NEXTPROTONEG
     if (min_version == TLS1_3_VERSION && next_proto_neg_in != NULL) {
         BIO_printf(bio_err, "Cannot supply -nextprotoneg with TLSv1.3\n");
index dcef6b3b4950c6bd9db77977e55ad72145558efd..98a2f32b4a12491dc9f7b291e21294fb2d774dec 100644 (file)
@@ -359,7 +359,9 @@ int smime_main(int argc, char **argv)
     argc = opt_num_rest();
     argv = opt_rest();
 
-    app_RAND_load();
+    if (!app_RAND_load())
+        goto end;
+
     if (digestname != NULL) {
         if (!opt_md(digestname, &sign_md))
             goto opthelp;
index 56c896c31e10371516ea881e9f45a8185aafd8e2..d867e87e45864b9dd0f0fbb47c92688f8dc6e534 100644 (file)
@@ -1648,7 +1648,9 @@ int speed_main(int argc, char **argv)
     argc = opt_num_rest();
     argv = opt_rest();
 
-    app_RAND_load();
+    if (!app_RAND_load())
+        goto end;
+
     for (; *argv; argv++) {
         const char *algo = *argv;
 
index 375ae1327c794d4b0eb635304795e191cbe2b4a9..af62e7e200272dbcfbc23b43c064906169f95627 100644 (file)
@@ -309,7 +309,9 @@ int srp_main(int argc, char **argv)
     argc = opt_num_rest();
     argv = opt_rest();
 
-    app_RAND_load();
+    if (!app_RAND_load())
+        goto end;
+
     if (srpvfile != NULL && configfile != NULL) {
         BIO_printf(bio_err,
                    "-srpvfile and -configfile cannot be specified together.\n");
index cc69a7de720523dd2c2f0c5dc3e281a449583989..b4a5e85feab92fce8afc65b1ad82a816458dd652 100644 (file)
--- a/apps/ts.c
+++ b/apps/ts.c
@@ -292,7 +292,9 @@ int ts_main(int argc, char **argv)
     if (argc != 0 || mode == OPT_ERR)
         goto opthelp;
 
-    app_RAND_load();
+    if (!app_RAND_load())
+        goto end;
+
     if (digestname != NULL) {
         if (!opt_md(digestname, &md))
             goto opthelp;
index abbffe37ab9e303b4a29df0b56fedaf86236c1b1..18c0ce90d8359c772cf063c2cb273c8b2cb9f7e5 100644 (file)
@@ -578,7 +578,9 @@ int x509_main(int argc, char **argv)
     if (argc != 0)
         goto opthelp;
 
-    app_RAND_load();
+    if (!app_RAND_load())
+        goto end;
+
     if (digestname != NULL) {
         if (!opt_md(digestname, &digest))
             goto opthelp;