summaryrefslogtreecommitdiff
path: root/src/usr.bin/openssl/prime.c
diff options
context:
space:
mode:
authorlteo <>2015-01-13 03:42:36 +0000
committerlteo <>2015-01-13 03:42:36 +0000
commit75435651a50b36d1e85f4eb4bd3df1f842912952 (patch)
tree6b562bb609f14f9bc9cc3957ac90c89fd7af742b /src/usr.bin/openssl/prime.c
parent707bceb580955f7866f1d89a76676269505ea501 (diff)
downloadopenbsd-75435651a50b36d1e85f4eb4bd3df1f842912952.tar.gz
openbsd-75435651a50b36d1e85f4eb4bd3df1f842912952.tar.bz2
openbsd-75435651a50b36d1e85f4eb4bd3df1f842912952.zip
Implement more thorough error checks:
- Check the return value of every relevant function call. - If BIO_new() returns NULL instead of a valid BIO, do not attempt to blindly use the NULL value as a BIO throughout the rest of the code. - Ensure that bio_out is freed by BIO_free_all() at the end of all error paths. ok doug@
Diffstat (limited to 'src/usr.bin/openssl/prime.c')
-rw-r--r--src/usr.bin/openssl/prime.c50
1 files changed, 38 insertions, 12 deletions
diff --git a/src/usr.bin/openssl/prime.c b/src/usr.bin/openssl/prime.c
index fca3701632..98fcca69c2 100644
--- a/src/usr.bin/openssl/prime.c
+++ b/src/usr.bin/openssl/prime.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: prime.c,v 1.4 2014/12/28 15:48:52 jsing Exp $ */ 1/* $OpenBSD: prime.c,v 1.5 2015/01/13 03:42:36 lteo Exp $ */
2/* ==================================================================== 2/* ====================================================================
3 * Copyright (c) 2004 The OpenSSL Project. All rights reserved. 3 * Copyright (c) 2004 The OpenSSL Project. All rights reserved.
4 * 4 *
@@ -117,6 +117,7 @@ prime_main(int argc, char **argv)
117 char *prime = NULL; 117 char *prime = NULL;
118 BIO *bio_out; 118 BIO *bio_out;
119 char *s; 119 char *s;
120 int ret = 1;
120 121
121 memset(&prime_config, 0, sizeof(prime_config)); 122 memset(&prime_config, 0, sizeof(prime_config));
122 123
@@ -134,26 +135,48 @@ prime_main(int argc, char **argv)
134 return (1); 135 return (1);
135 } 136 }
136 137
137 if ((bio_out = BIO_new(BIO_s_file())) != NULL) { 138 if ((bio_out = BIO_new(BIO_s_file())) == NULL) {
138 BIO_set_fp(bio_out, stdout, BIO_NOCLOSE); 139 ERR_print_errors(bio_err);
140 return (1);
139 } 141 }
142 BIO_set_fp(bio_out, stdout, BIO_NOCLOSE);
140 143
141 if (prime_config.generate != 0) { 144 if (prime_config.generate != 0) {
142 if (prime_config.bits == 0) { 145 if (prime_config.bits == 0) {
143 BIO_printf(bio_err, "Specify the number of bits.\n"); 146 BIO_printf(bio_err, "Specify the number of bits.\n");
144 return 1; 147 goto end;
148 }
149 bn = BN_new();
150 if (!bn) {
151 BIO_printf(bio_err, "Out of memory.\n");
152 goto end;
153 }
154 if (!BN_generate_prime_ex(bn, prime_config.bits,
155 prime_config.safe, NULL, NULL, NULL)) {
156 BIO_printf(bio_err, "Prime generation error.\n");
157 goto end;
145 } 158 }
146 bn = BN_new(); /* XXX - unchecked malloc. */
147 BN_generate_prime_ex(bn, prime_config.bits, prime_config.safe,
148 NULL, NULL, NULL);
149 s = prime_config.hex ? BN_bn2hex(bn) : BN_bn2dec(bn); 159 s = prime_config.hex ? BN_bn2hex(bn) : BN_bn2dec(bn);
160 if (s == NULL) {
161 BIO_printf(bio_err, "Out of memory.\n");
162 goto end;
163 }
150 BIO_printf(bio_out, "%s\n", s); 164 BIO_printf(bio_out, "%s\n", s);
151 free(s); 165 free(s);
152 } else { 166 } else {
153 if (prime_config.hex) 167 if (prime_config.hex) {
154 BN_hex2bn(&bn, prime); 168 if (!BN_hex2bn(&bn, prime)) {
155 else 169 BIO_printf(bio_err, "%s is an invalid hex "
156 BN_dec2bn(&bn, prime); 170 "value.\n", prime);
171 goto end;
172 }
173 } else {
174 if (!BN_dec2bn(&bn, prime)) {
175 BIO_printf(bio_err, "%s is an invalid decimal "
176 "value.\n", prime);
177 goto end;
178 }
179 }
157 180
158 BN_print(bio_out, bn); 181 BN_print(bio_out, bn);
159 BIO_printf(bio_out, " is %sprime\n", 182 BIO_printf(bio_out, " is %sprime\n",
@@ -161,8 +184,11 @@ prime_main(int argc, char **argv)
161 NULL, NULL) ? "" : "not "); 184 NULL, NULL) ? "" : "not ");
162 } 185 }
163 186
187 ret = 0;
188
189end:
164 BN_free(bn); 190 BN_free(bn);
165 BIO_free_all(bio_out); 191 BIO_free_all(bio_out);
166 192
167 return 0; 193 return (ret);
168} 194}