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: * dumpcrl.c michael@0: * michael@0: * dump CRL 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:\tdumpcrl \n"); michael@0: (void) printf("\tParses a CRL 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_CRL * michael@0: createCRL(char *inFileName) michael@0: { michael@0: PKIX_PL_ByteArray *byteArray = NULL; michael@0: PKIX_PL_CRL *crl = NULL; michael@0: PKIX_Error *error = NULL; michael@0: PRFileDesc *inFile = NULL; michael@0: SECItem crlDER; michael@0: void *buf = NULL; michael@0: PKIX_UInt32 len; michael@0: SECStatus rv; michael@0: michael@0: PKIX_TEST_STD_VARS(); michael@0: michael@0: crlDER.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 crl file"); michael@0: goto cleanup; michael@0: } else { michael@0: rv = SECU_ReadDERFromFile(&crlDER, inFile, PR_FALSE, PR_FALSE); michael@0: if (!rv){ michael@0: buf = (void *)crlDER.data; michael@0: len = crlDER.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_CRL_Create(byteArray, &crl, plContext); michael@0: if (error){ michael@0: printFailure("PKIX_PL_CRL_Create failed"); michael@0: goto cleanup; michael@0: } michael@0: michael@0: SECITEM_FreeItem(&crlDER, PR_FALSE); michael@0: } else { michael@0: printFailure("Unable to read DER from crl 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 (error){ michael@0: SECITEM_FreeItem(&crlDER, 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: PKIX_TEST_RETURN(); michael@0: michael@0: return (crl); michael@0: } michael@0: michael@0: int dumpcrl(int argc, char *argv[]) michael@0: { michael@0: michael@0: PKIX_PL_String *string = NULL; michael@0: PKIX_PL_CRL *crl = NULL; michael@0: PKIX_Error *error = NULL; michael@0: char *ascii = NULL; michael@0: PKIX_UInt32 length; michael@0: PKIX_UInt32 actualMinorVersion; michael@0: PKIX_UInt32 j = 0; michael@0: PKIX_Boolean useArenas = PKIX_FALSE; 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: crl = createCRL(argv[j+1]); michael@0: michael@0: if (crl){ michael@0: michael@0: error = PKIX_PL_Object_ToString michael@0: ((PKIX_PL_Object *)crl, &string, plContext); michael@0: michael@0: if (error){ michael@0: printFailure("Unable to get string representation " michael@0: "of crl"); 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: 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 CRL"); michael@0: goto cleanup; michael@0: } michael@0: michael@0: cleanup: michael@0: michael@0: if (crl){ michael@0: PKIX_PL_Object_DecRef((PKIX_PL_Object *)(crl), 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("DUMPCRL"); michael@0: michael@0: return (0); michael@0: }