Skip to content

Commit 43a3546

Browse files
adjust pkcs12 example to print out list of certificates found
1 parent 8e47edb commit 43a3546

1 file changed

Lines changed: 25 additions & 18 deletions

File tree

crypto/pkcs12/pkcs12-example.c

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,6 @@ int main(int argc, char** argv)
4949
return -1;
5050
}
5151

52-
printf("extracting private key and certificate from PKCS12 (test-servercert.p12)\n");
53-
5452
pkcs12 = wc_PKCS12_new();
5553
if (pkcs12 == NULL) {
5654
printf("issue creating pkcs12 object\n");
@@ -63,6 +61,7 @@ int main(int argc, char** argv)
6361
else {
6462
file = defaultFile;
6563
}
64+
printf("extracting private key and certificate from PKCS12 (%s)\n", file);
6665

6766
/* open PKCS12 file */
6867
f = fopen(file, "rb");
@@ -87,42 +86,50 @@ int main(int argc, char** argv)
8786
ret = wc_PKCS12_parse(pkcs12, "wolfSSL test", &keyDer, &keySz,
8887
&certDer, &certSz, &list);
8988
printf("return value of parsing pkcs12 = %d %s\n", ret, (ret == 0)? "SUCCESS": "FAIL");
90-
if (ret != 0 || keyDer == NULL || certDer == NULL) {
89+
if (ret != 0) {
9190
printf("\t error parsing pkcs12\n");
9291
wc_PKCS12_free(pkcs12);
9392
return -1;
9493
}
9594

9695
/* print out key and cert found */
97-
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");
103-
104-
printf("\nHEX of Certificate Read (DER format) :\n");
105-
for (i = 0; i < certSz; i++) {
106-
if (i != 0 && !(i%16)) printf("\n");
107-
printf("%02X", certDer[i]);
108-
}
109-
printf("\n");
110-
11196
if (keyDer != NULL) {
97+
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");
112103
XFREE(keyDer, NULL, DYNAMIC_TYPE_PKCS);
113104
}
114105

115106
if (certDer != NULL) {
107+
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");
116113
XFREE(certDer, NULL, DYNAMIC_TYPE_PKCS);
117114
}
118115

119116
/* itterate through list if was not passed as null and free each node */
120117
if (list != NULL) {
121118
WC_DerCertList* current;
119+
int certIdx = 0;
120+
121+
printf("\nHEX of Certificate LIST (DER format) :\n");
122122
current = list;
123123
while (current != NULL) {
124-
WC_DerCertList* next = current->next;
124+
WC_DerCertList* next;
125+
126+
next = current->next;
125127
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+
126133
XFREE(current->buffer, NULL, DYNAMIC_TYPE_PKCS);
127134
}
128135
XFREE(current, NULL, DYNAMIC_TYPE_PKCS);

0 commit comments

Comments
 (0)