Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adjust pkcs12 example to print out list of certificates found #366

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 32 additions & 20 deletions crypto/pkcs12/pkcs12-example.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,22 @@
#include <wolfssl/wolfcrypt/types.h>
#include <wolfssl/wolfcrypt/wc_port.h>

static void PRINT_BUFFER(byte* der, int derSz)
{
int i;

if (der != NULL) {
for (i = 0; i < derSz; i++) {
if (i != 0 && !(i%16)) {
printf("\n");
}
printf("%02X", der[i]);
}
printf("\n");
}
}


/* This is an example with using wc_ function for PKCS12. To see an example of
* wolfSSL_PKCS12 functions look in tests/api.c */
int main(int argc, char** argv)
Expand All @@ -37,7 +53,6 @@ int main(int argc, char** argv)
byte* certDer = NULL;
word32 keySz;
word32 certSz;
word32 i;
byte buffer[5300];
char *file;
char defaultFile[] = "./test-servercert.p12";
Expand All @@ -49,8 +64,6 @@ int main(int argc, char** argv)
return -1;
}

printf("extracting private key and certificate from PKCS12 (test-servercert.p12)\n");

pkcs12 = wc_PKCS12_new();
if (pkcs12 == NULL) {
printf("issue creating pkcs12 object\n");
Expand All @@ -63,6 +76,7 @@ int main(int argc, char** argv)
else {
file = defaultFile;
}
printf("extracting private key and certificate from PKCS12 (%s)\n", file);

/* open PKCS12 file */
f = fopen(file, "rb");
Expand All @@ -87,42 +101,40 @@ int main(int argc, char** argv)
ret = wc_PKCS12_parse(pkcs12, "wolfSSL test", &keyDer, &keySz,
&certDer, &certSz, &list);
printf("return value of parsing pkcs12 = %d %s\n", ret, (ret == 0)? "SUCCESS": "FAIL");
if (ret != 0 || keyDer == NULL || certDer == NULL) {
if (ret != 0) {
printf("\t error parsing pkcs12\n");
wc_PKCS12_free(pkcs12);
return -1;
}

/* print out key and cert found */
printf("HEX of Private Key Read (DER format) :\n");
for (i = 0; i < keySz; i++) {
if (i != 0 && !(i%16)) printf("\n");
printf("%02X", keyDer[i]);
}
printf("\n");

printf("\nHEX of Certificate Read (DER format) :\n");
for (i = 0; i < certSz; i++) {
if (i != 0 && !(i%16)) printf("\n");
printf("%02X", certDer[i]);
}
printf("\n");

if (keyDer != NULL) {
printf("HEX of Private Key Read (DER format) :\n");
PRINT_BUFFER(keyDer, keySz);
XFREE(keyDer, NULL, DYNAMIC_TYPE_PKCS);
}

if (certDer != NULL) {
printf("\nHEX of Certificate Read (DER format) :\n");
PRINT_BUFFER(certDer, certSz);
XFREE(certDer, NULL, DYNAMIC_TYPE_PKCS);
}

/* itterate through list if was not passed as null and free each node */
/* Iterate through list of certificates and print each out if was not passed
* as null, and then free each node. */
if (list != NULL) {
WC_DerCertList* current;
int certIdx = 0;

printf("\nHEX of Certificate LIST (DER format) :\n");
current = list;
while (current != NULL) {
WC_DerCertList* next = current->next;
WC_DerCertList* next;

next = current->next;
if (current->buffer != NULL) {
printf("\n[CERT %d] :", certIdx++);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add newline after cert number: [CERT %d] :\n... Current output (without new-line) looks like this:

HEX of Certificate LIST (DER format) :

[CERT 0] :308204AA30820392A003020102020900
B7B69033661B6B23300D06092A864886

Adding new-line would align HEX output (looks a little cleaner!)

PRINT_BUFFER(current->buffer, current->bufferSz);
XFREE(current->buffer, NULL, DYNAMIC_TYPE_PKCS);
}
XFREE(current, NULL, DYNAMIC_TYPE_PKCS);
Expand Down