Skip to content

Commit e601e04

Browse files
committed
fix examples/pem/ and scripts/pem.test:
examples/pem/pem.c: * improve error messages, * add wc_SetSeed_Cb() if WC_RNG_SEED_CB, and * add wolfCrypt_Init() and wolfCrypt_Cleanup(). scripts/pem.test: * fix exit code to unmask script failure, * add configured feature detection, * improve error messages and handling, * add configuration gating around subtests, and * comment out currently failing subtests.
1 parent 8c3d471 commit e601e04

2 files changed

Lines changed: 205 additions & 102 deletions

File tree

examples/pem/pem.c

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@
3939

4040
#if defined(WOLFSSL_PEM_TO_DER) && !defined(NO_FILESYSTEM)
4141

42+
static const char *progname;
43+
4244
/* Increment allocated data by this much. */
4345
#define DATA_INC_LEN 256
4446
/* Maximum block size of a cipher. */
@@ -554,15 +556,20 @@ static int EncryptDer(unsigned char* in, word32 in_len, char* password,
554556
if (ret == WC_NO_ERR_TRACE(LENGTH_ONLY_E)) {
555557
ret = 0;
556558
}
557-
else if (ret == 0) {
558-
ret = 1;
559+
else {
560+
fprintf(stderr,
561+
"%s: wc_CreateEncryptedPKCS8Key() with enc_alg_id %d: "
562+
"unexpected retval: %s.\n",
563+
progname, enc_alg_id, wc_GetErrorString(ret));
564+
if (ret == 0)
565+
ret = 1;
559566
}
560567
}
561568
if (ret == 0) {
562569
/* Allocate memory for encrypted DER data. */
563570
*enc = (unsigned char*)XMALLOC(*enc_len, NULL, DYNAMIC_TYPE_TMP_BUFFER);
564571
if (*enc == NULL) {
565-
ret = 1;
572+
ret = MEMORY_E;
566573
}
567574
}
568575
if (ret == 0) {
@@ -749,6 +756,12 @@ int main(int argc, char* argv[])
749756
int log = 0;
750757
#endif
751758

759+
progname = strrchr(argv[0], '/');
760+
if (progname)
761+
++progname;
762+
else
763+
progname = argv[0];
764+
752765
memset(&info, 0, sizeof(info));
753766

754767
/* Skip over program name. */
@@ -951,6 +964,22 @@ int main(int argc, char* argv[])
951964
}
952965
#endif
953966

967+
#ifdef WC_RNG_SEED_CB
968+
ret = wc_SetSeed_Cb(WC_GENERATE_SEED_DEFAULT);
969+
if (ret != 0) {
970+
fprintf(stderr, "%s: wc_SetSeed_Cb() failed: %s.\n",
971+
progname, wc_GetErrorString(ret));
972+
exit(1);
973+
}
974+
#endif
975+
976+
ret = wolfCrypt_Init();
977+
if (ret != 0) {
978+
fprintf(stderr, "%s: wolfCrypt_Init() failed: %s.\n",
979+
progname, wc_GetErrorString(ret));
980+
exit(1);
981+
}
982+
954983
/* Convert PEM type string to value. */
955984
if (type_str != NULL) {
956985
ret = StringToType(type_str, &type);
@@ -1037,7 +1066,7 @@ int main(int argc, char* argv[])
10371066
XFREE(in, NULL, DYNAMIC_TYPE_TMP_BUFFER);
10381067
}
10391068
if (ret < 0) {
1040-
fprintf(stderr, "%s\n", wc_GetErrorString(ret));
1069+
fprintf(stderr, "%s: %s\n", progname, wc_GetErrorString(ret));
10411070
}
10421071

10431072
if ((in_file != stdin) && (in_file != NULL))
@@ -1046,6 +1075,12 @@ int main(int argc, char* argv[])
10461075
if ((out_file != stdout) && (out_file != NULL))
10471076
(void)fclose(out_file);
10481077

1078+
ret = wolfCrypt_Cleanup();
1079+
if (ret != 0) {
1080+
fprintf(stderr, "%s: wolfCrypt_Cleanup() failed: %s.\n",
1081+
progname, wc_GetErrorString(ret));
1082+
}
1083+
10491084
return (ret == 0) ? 0 : 1;
10501085
}
10511086

0 commit comments

Comments
 (0)