2626#include <wolfssl/wolfcrypt/types.h>
2727#include <wolfssl/wolfcrypt/wc_port.h>
2828
29+ static void PrintBuffer (const byte * der , word32 derSz )
30+ {
31+ word32 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 */
3147int 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" ;
@@ -49,8 +64,6 @@ int main(int argc, char** argv)
4964 return -1 ;
5065 }
5166
52- printf ("extracting private key and certificate from PKCS12 (test-servercert.p12)\n" );
53-
5467 pkcs12 = wc_PKCS12_new ();
5568 if (pkcs12 == NULL ) {
5669 printf ("issue creating pkcs12 object\n" );
@@ -63,6 +76,7 @@ int main(int argc, char** argv)
6376 else {
6477 file = defaultFile ;
6578 }
79+ printf ("extracting private key and certificate from PKCS12 (%s)\n" , file );
6680
6781 /* open PKCS12 file */
6882 f = fopen (file , "rb" );
@@ -87,42 +101,40 @@ int main(int argc, char** argv)
87101 ret = wc_PKCS12_parse (pkcs12 , "wolfSSL test" , & keyDer , & keySz ,
88102 & certDer , & certSz , & list );
89103 printf ("return value of parsing pkcs12 = %d %s\n" , ret , (ret == 0 )? "SUCCESS" : "FAIL" );
90- if (ret != 0 || keyDer == NULL || certDer == NULL ) {
104+ if (ret != 0 ) {
91105 printf ("\t error parsing pkcs12\n" );
92106 wc_PKCS12_free (pkcs12 );
93107 return -1 ;
94108 }
95109
96110 /* 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-
111111 if (keyDer != NULL ) {
112+ printf ("HEX of Private Key Read (DER format) :\n" );
113+ PrintBuffer (keyDer , keySz );
112114 XFREE (keyDer , NULL , DYNAMIC_TYPE_PKCS );
113115 }
114116
115117 if (certDer != NULL ) {
118+ printf ("\nHEX of Certificate Read (DER format) :\n" );
119+ PrintBuffer (certDer , certSz );
116120 XFREE (certDer , NULL , DYNAMIC_TYPE_PKCS );
117121 }
118122
119- /* 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. */
120125 if (list != NULL ) {
121126 WC_DerCertList * current ;
127+ int certIdx = 0 ;
128+
129+ printf ("\nHEX of Certificate LIST (DER format) :\n" );
122130 current = list ;
123131 while (current != NULL ) {
124- WC_DerCertList * next = current -> next ;
132+ WC_DerCertList * next ;
133+
134+ next = current -> next ;
125135 if (current -> buffer != NULL ) {
136+ printf ("\n[CERT %d] :" , certIdx ++ );
137+ PrintBuffer (current -> buffer , current -> bufferSz );
126138 XFREE (current -> buffer , NULL , DYNAMIC_TYPE_PKCS );
127139 }
128140 XFREE (current , NULL , DYNAMIC_TYPE_PKCS );
0 commit comments