Skip to content

Commit 78e628a

Browse files
committed
Return nonzero from rsa-kg on any key generate or write failure
1 parent 3f97419 commit 78e628a

2 files changed

Lines changed: 14 additions & 8 deletions

File tree

pk/rsa-kg/Makefile

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ rsa-kg: rsa-kg.o
2020
.PHONY: clean check
2121

2222
clean:
23-
rm -f *.o rsa-kg-sv rsa-kg
23+
rm -f *.o rsa-kg-sv rsa-kg rsa-public.der rsa-private.der
2424

25-
check: SHELL := /bin/bash
26-
check: .SHELLFLAGS := -eo pipefail -c
25+
# Both binaries now return nonzero on any failure, so the exit code is the check
26+
# -- no grep of a line that is printed before the write can succeed or fail.
2727
check: rsa-kg-sv rsa-kg
2828
./rsa-kg-sv -load-key >/dev/null
29-
./rsa-kg -bits 2048 | grep -q 'writing public key to ./rsa-private.der'
29+
./rsa-kg -bits 2048 >/dev/null
3030
@echo "PASS: pk-rsa-kg checks"

pk/rsa-kg/rsa-kg.c

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ int main(int argc, char** argv)
6060
RsaKey* pRsaKey = NULL;
6161
WC_RNG rng;
6262
int ret = 0;
63+
int result = 0;
6364
int bits = DEF_RSA_KEY_SIZE;
6465
int sz;
6566
unsigned char derBuf[MAX_DER_SIZE];
@@ -184,19 +185,22 @@ int main(int argc, char** argv)
184185
/* Generate an RSA key pair. */
185186
if (wc_MakeRsaKey(pRsaKey, bits, WC_RSA_EXPONENT, &rng) != 0) {
186187
printf("failed to create rsa key\n");
188+
result = -1;
187189
}
188190
else {
189191
/* Open public key file. */
190192
f = fopen(pubKey, "wb");
191193
printf("writing public key to %s\n", pubKey);
192194
if (f == NULL) {
193195
printf("unable to write out public key\n");
196+
result = -1;
194197
}
195198
else {
196199
/* Encode public key to DER. */
197200
sz = wc_RsaKeyToPublicDer(pRsaKey, derBuf, sizeof(derBuf));
198201
if (sz <= 0) {
199202
printf("error with rsa to public der %d\n", sz);
203+
result = -1;
200204
}
201205
else {
202206
/* Write DER encoded public key to file. */
@@ -207,15 +211,17 @@ int main(int argc, char** argv)
207211

208212
/* Open private key file. */
209213
f = fopen(privKey, "wb");
210-
printf("writing public key to %s\n", privKey);
214+
printf("writing private key to %s\n", privKey);
211215
if (f == NULL) {
212-
printf("unable to write out public key\n");
216+
printf("unable to write out private key\n");
217+
result = -1;
213218
}
214219
else {
215220
/* Encode private key to DER. */
216221
sz = wc_RsaKeyToDer(pRsaKey, derBuf, sizeof(derBuf));
217222
if (sz <= 0) {
218-
printf("error with rsa to public der %d\n", sz);
223+
printf("error with rsa to private der %d\n", sz);
224+
result = -1;
219225
}
220226
else {
221227
/* Write DER encoded private key to file. */
@@ -231,7 +237,7 @@ int main(int argc, char** argv)
231237
free(pRsaKey);
232238
wolfSSL_Cleanup();
233239

234-
return 0;
240+
return result;
235241
#else
236242
(void)kRsaPubKey;
237243
(void)kRsaPrivKey;

0 commit comments

Comments
 (0)