Skip to content

Commit 2aa1f85

Browse files
improvements to example print out
1 parent 43a3546 commit 2aa1f85

File tree

1 file changed

+23
-18
lines changed

1 file changed

+23
-18
lines changed

crypto/pkcs12/pkcs12-example.c

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,22 @@
2626
#include <wolfssl/wolfcrypt/types.h>
2727
#include <wolfssl/wolfcrypt/wc_port.h>
2828

29+
static void PRINT_BUFFER(byte* der, int derSz)
30+
{
31+
int i;
32+
33+
if (der != NULL) {
34+
for (i = 0; i < derSz; i++) {
35+
if (i != 0 && !(i%16)) {
36+
printf("\n");
37+
}
38+
printf("%02X", der[i]);
39+
}
40+
printf("\n");
41+
}
42+
}
43+
44+
2945
/* This is an example with using wc_ function for PKCS12. To see an example of
3046
* wolfSSL_PKCS12 functions look in tests/api.c */
3147
int main(int argc, char** argv)
@@ -37,7 +53,6 @@ int main(int argc, char** argv)
3753
byte* certDer = NULL;
3854
word32 keySz;
3955
word32 certSz;
40-
word32 i;
4156
byte buffer[5300];
4257
char *file;
4358
char defaultFile[] = "./test-servercert.p12";
@@ -95,25 +110,18 @@ int main(int argc, char** argv)
95110
/* print out key and cert found */
96111
if (keyDer != NULL) {
97112
printf("HEX of Private Key Read (DER format) :\n");
98-
for (i = 0; i < keySz; i++) {
99-
if (i != 0 && !(i%16)) printf("\n");
100-
printf("%02X", keyDer[i]);
101-
}
102-
printf("\n");
113+
PRINT_BUFFER(keyDer, keySz);
103114
XFREE(keyDer, NULL, DYNAMIC_TYPE_PKCS);
104115
}
105116

106117
if (certDer != NULL) {
107118
printf("\nHEX of Certificate Read (DER format) :\n");
108-
for (i = 0; i < certSz; i++) {
109-
if (i != 0 && !(i%16)) printf("\n");
110-
printf("%02X", certDer[i]);
111-
}
112-
printf("\n");
119+
PRINT_BUFFER(certDer, certSz);
113120
XFREE(certDer, NULL, DYNAMIC_TYPE_PKCS);
114121
}
115122

116-
/* itterate through list if was not passed as null and free each node */
123+
/* Iterate through list of certificates and print each out if was not passed
124+
* as null, and then free each node. */
117125
if (list != NULL) {
118126
WC_DerCertList* current;
119127
int certIdx = 0;
@@ -123,13 +131,10 @@ int main(int argc, char** argv)
123131
while (current != NULL) {
124132
WC_DerCertList* next;
125133

126-
next = current->next;
134+
next = current->next;
127135
if (current->buffer != NULL) {
128-
printf("[CERT %d] :", certIdx++);
129-
for (i = 0; i < current->bufferSz; i++)
130-
printf("%02X", current->buffer[i]);
131-
printf("\n");
132-
136+
printf("\n[CERT %d] :", certIdx++);
137+
PRINT_BUFFER(current->buffer, current->bufferSz);
133138
XFREE(current->buffer, NULL, DYNAMIC_TYPE_PKCS);
134139
}
135140
XFREE(current, NULL, DYNAMIC_TYPE_PKCS);

0 commit comments

Comments
 (0)