michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: /* michael@0: * dumpcert.c michael@0: * michael@0: * dump certificate sample application michael@0: * michael@0: */ michael@0: michael@0: #include michael@0: michael@0: #include "pkix.h" michael@0: #include "testutil.h" michael@0: #include "prlong.h" michael@0: #include "plstr.h" michael@0: #include "prthread.h" michael@0: #include "plarena.h" michael@0: #include "seccomon.h" michael@0: #include "secdert.h" michael@0: #include "secasn1t.h" michael@0: #include "certt.h" michael@0: michael@0: static void *plContext = NULL; michael@0: michael@0: static michael@0: void printUsage(void){ michael@0: (void) printf("\nUSAGE:\tdumpcert \n"); michael@0: (void) printf("\tParses a certificate located at " michael@0: "and displays it.\n"); michael@0: } michael@0: michael@0: static michael@0: void printFailure(char *msg){ michael@0: (void) printf("FAILURE: %s\n", msg); michael@0: } michael@0: michael@0: static PKIX_PL_Cert * michael@0: createCert(char *inFileName) michael@0: { michael@0: PKIX_PL_ByteArray *byteArray = NULL; michael@0: PKIX_PL_Cert *cert = NULL; michael@0: PKIX_Error *error = NULL; michael@0: PRFileDesc *inFile = NULL; michael@0: SECItem certDER; michael@0: void *buf = NULL; michael@0: PKIX_UInt32 len; michael@0: SECStatus rv = SECFailure; michael@0: michael@0: certDER.data = NULL; michael@0: michael@0: inFile = PR_Open(inFileName, PR_RDONLY, 0); michael@0: michael@0: if (!inFile){ michael@0: printFailure("Unable to open cert file"); michael@0: goto cleanup; michael@0: } else { michael@0: rv = SECU_ReadDERFromFile(&certDER, inFile, PR_FALSE, PR_FALSE); michael@0: if (!rv){ michael@0: buf = (void *)certDER.data; michael@0: len = certDER.len; michael@0: michael@0: error = PKIX_PL_ByteArray_Create michael@0: (buf, len, &byteArray, plContext); michael@0: michael@0: if (error){ michael@0: printFailure("PKIX_PL_ByteArray_Create failed"); michael@0: goto cleanup; michael@0: } michael@0: michael@0: error = PKIX_PL_Cert_Create michael@0: (byteArray, &cert, plContext); michael@0: michael@0: if (error){ michael@0: printFailure("PKIX_PL_Cert_Create failed"); michael@0: goto cleanup; michael@0: } michael@0: } else { michael@0: printFailure("Unable to read DER from cert file"); michael@0: goto cleanup; michael@0: } michael@0: } michael@0: michael@0: cleanup: michael@0: michael@0: if (inFile){ michael@0: PR_Close(inFile); michael@0: } michael@0: michael@0: if (rv == SECSuccess){ michael@0: SECITEM_FreeItem(&certDER, PR_FALSE); michael@0: } michael@0: michael@0: if (byteArray){ michael@0: PKIX_PL_Object_DecRef((PKIX_PL_Object *)(byteArray), plContext); michael@0: } michael@0: michael@0: return (cert); michael@0: } michael@0: michael@0: int dumpcert(int argc, char *argv[]) michael@0: { michael@0: michael@0: PKIX_PL_String *string = NULL; michael@0: PKIX_PL_Cert *cert = NULL; michael@0: PKIX_Error *error = NULL; michael@0: char *ascii = NULL; michael@0: PKIX_UInt32 length = 0; michael@0: PKIX_UInt32 j = 0; michael@0: PKIX_Boolean useArenas = PKIX_FALSE; michael@0: PKIX_UInt32 actualMinorVersion; michael@0: michael@0: PKIX_TEST_STD_VARS(); michael@0: michael@0: if (argc == 1){ michael@0: printUsage(); michael@0: return (0); michael@0: } michael@0: michael@0: useArenas = PKIX_TEST_ARENAS_ARG(argv[1]); michael@0: michael@0: PKIX_Initialize michael@0: (PKIX_TRUE, /* nssInitNeeded */ michael@0: useArenas, michael@0: PKIX_MAJOR_VERSION, michael@0: PKIX_MINOR_VERSION, michael@0: PKIX_MINOR_VERSION, michael@0: &actualMinorVersion, michael@0: &plContext); michael@0: michael@0: cert = createCert(argv[1+j]); michael@0: michael@0: if (cert){ michael@0: michael@0: error = PKIX_PL_Object_ToString michael@0: ((PKIX_PL_Object *)cert, &string, plContext); michael@0: michael@0: if (error){ michael@0: printFailure("Unable to get string representation " michael@0: "of cert"); michael@0: goto cleanup; michael@0: } michael@0: michael@0: error = PKIX_PL_String_GetEncoded michael@0: (string, michael@0: PKIX_ESCASCII, michael@0: (void **)&ascii, michael@0: &length, michael@0: plContext); michael@0: michael@0: if (error || !ascii){ michael@0: printFailure("Unable to get ASCII encoding of string"); michael@0: goto cleanup; michael@0: } michael@0: michael@0: (void) printf("OUTPUT:\n%s\n", ascii); michael@0: michael@0: } else { michael@0: printFailure("Unable to create certificate"); michael@0: goto cleanup; michael@0: } michael@0: michael@0: cleanup: michael@0: michael@0: if (cert){ michael@0: PKIX_PL_Object_DecRef((PKIX_PL_Object *)(cert), plContext); michael@0: } michael@0: michael@0: if (string){ michael@0: PKIX_PL_Object_DecRef((PKIX_PL_Object *)(string), plContext); michael@0: } michael@0: michael@0: if (ascii){ michael@0: PKIX_PL_Free((PKIX_PL_Object *)(ascii), plContext); michael@0: } michael@0: michael@0: PKIX_Shutdown(plContext); michael@0: michael@0: PKIX_TEST_RETURN(); michael@0: michael@0: endTests("DUMPCERT"); michael@0: michael@0: return (0); michael@0: }