From ebe91731ad3930ec6e05f2b2d9194ecae5b21fba Mon Sep 17 00:00:00 2001 From: JacobBarthelmeh Date: Fri, 3 Feb 2023 13:36:15 -0800 Subject: [PATCH 1/2] adjust pkcs12 example to print out list of certificates found --- crypto/pkcs12/pkcs12-example.c | 43 ++++++++++++++++++++-------------- 1 file changed, 25 insertions(+), 18 deletions(-) diff --git a/crypto/pkcs12/pkcs12-example.c b/crypto/pkcs12/pkcs12-example.c index 1fbc072e..6c73291d 100644 --- a/crypto/pkcs12/pkcs12-example.c +++ b/crypto/pkcs12/pkcs12-example.c @@ -49,8 +49,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"); @@ -63,6 +61,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"); @@ -87,42 +86,50 @@ 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"); + for (i = 0; i < keySz; i++) { + if (i != 0 && !(i%16)) printf("\n"); + printf("%02X", keyDer[i]); + } + printf("\n"); XFREE(keyDer, NULL, DYNAMIC_TYPE_PKCS); } if (certDer != NULL) { + 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"); XFREE(certDer, NULL, DYNAMIC_TYPE_PKCS); } /* itterate through list if was not passed as null and 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("[CERT %d] :", certIdx++); + for (i = 0; i < current->bufferSz; i++) + printf("%02X", current->buffer[i]); + printf("\n"); + XFREE(current->buffer, NULL, DYNAMIC_TYPE_PKCS); } XFREE(current, NULL, DYNAMIC_TYPE_PKCS); From e1e32e5fe02f069dd3eff34e0c26deee00a5d894 Mon Sep 17 00:00:00 2001 From: Jacob Barthelmeh Date: Mon, 20 Feb 2023 15:17:55 -0700 Subject: [PATCH 2/2] improvements to example print out --- crypto/pkcs12/pkcs12-example.c | 41 +++++++++++++++++++--------------- 1 file changed, 23 insertions(+), 18 deletions(-) diff --git a/crypto/pkcs12/pkcs12-example.c b/crypto/pkcs12/pkcs12-example.c index 6c73291d..24afd056 100644 --- a/crypto/pkcs12/pkcs12-example.c +++ b/crypto/pkcs12/pkcs12-example.c @@ -26,6 +26,22 @@ #include #include +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) @@ -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"; @@ -95,25 +110,18 @@ int main(int argc, char** argv) /* print out key and cert found */ if (keyDer != NULL) { 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"); + PRINT_BUFFER(keyDer, keySz); XFREE(keyDer, NULL, DYNAMIC_TYPE_PKCS); } if (certDer != NULL) { 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"); + 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; @@ -123,13 +131,10 @@ int main(int argc, char** argv) while (current != NULL) { WC_DerCertList* next; - next = current->next; + next = current->next; if (current->buffer != NULL) { - printf("[CERT %d] :", certIdx++); - for (i = 0; i < current->bufferSz; i++) - printf("%02X", current->buffer[i]); - printf("\n"); - + printf("\n[CERT %d] :", certIdx++); + PRINT_BUFFER(current->buffer, current->bufferSz); XFREE(current->buffer, NULL, DYNAMIC_TYPE_PKCS); } XFREE(current, NULL, DYNAMIC_TYPE_PKCS);