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: /* michael@0: * shlibsign creates the checksum (.chk) files for the NSS libraries, michael@0: * libsoftokn3/softokn3 and libfreebl/freebl (platforms can have michael@0: * multiple freebl variants), that contain the NSS cryptograhic boundary. michael@0: * michael@0: * The generated .chk files must be put in the same directory as michael@0: * the NSS libraries they were generated for. michael@0: * michael@0: * When in FIPS 140 mode, the NSS Internal FIPS PKCS #11 Module will michael@0: * compute the checksum for the NSS cryptographic boundary libraries michael@0: * and compare the checksum with the value in .chk file. michael@0: */ michael@0: michael@0: #ifdef XP_UNIX michael@0: #define USES_LINKS 1 michael@0: #endif michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: #ifdef USES_LINKS michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #endif michael@0: michael@0: /* nspr headers */ michael@0: #include "prlink.h" michael@0: #include "prprf.h" michael@0: #include "prenv.h" michael@0: #include "plgetopt.h" michael@0: #include "prinit.h" michael@0: #include "prmem.h" michael@0: #include "plstr.h" michael@0: #include "prerror.h" michael@0: michael@0: /* softoken headers */ michael@0: #include "pkcs11.h" michael@0: #include "pkcs11t.h" michael@0: michael@0: /* freebl headers */ michael@0: #include "shsign.h" michael@0: michael@0: #define NUM_ELEM(array) (sizeof(array)/sizeof(array[0])) michael@0: CK_BBOOL true = CK_TRUE; michael@0: CK_BBOOL false = CK_FALSE; michael@0: static PRBool verbose = PR_FALSE; michael@0: michael@0: static void michael@0: usage (const char *program_name) michael@0: { michael@0: PRFileDesc *debug_out = PR_GetSpecialFD(PR_StandardError); michael@0: PR_fprintf (debug_out, michael@0: "type %s -H for more detail information.\n", program_name); michael@0: PR_fprintf (debug_out, michael@0: "Usage: %s [-v] [-V] [-o outfile] [-d dbdir] [-f pwfile]\n" michael@0: " [-F] [-p pwd] -[P dbprefix ] " michael@0: "-i shared_library_name\n", michael@0: program_name); michael@0: exit(1); michael@0: } michael@0: michael@0: static void michael@0: long_usage(const char *program_name) michael@0: { michael@0: PRFileDesc *debug_out = PR_GetSpecialFD(PR_StandardError); michael@0: PR_fprintf(debug_out, "%s test program usage:\n", program_name); michael@0: PR_fprintf(debug_out, "\t-i shared_library_name to process\n"); michael@0: PR_fprintf(debug_out, "\t-o checksum outfile\n"); michael@0: PR_fprintf(debug_out, "\t-d database path location\n"); michael@0: PR_fprintf(debug_out, "\t-P database prefix\n"); michael@0: PR_fprintf(debug_out, "\t-f password File : echo pw > file \n"); michael@0: PR_fprintf(debug_out, "\t-F FIPS mode\n"); michael@0: PR_fprintf(debug_out, "\t-p password\n"); michael@0: PR_fprintf(debug_out, "\t-v verbose output\n"); michael@0: PR_fprintf(debug_out, "\t-V perform Verify operations\n"); michael@0: PR_fprintf(debug_out, "\t-? short help message\n"); michael@0: PR_fprintf(debug_out, "\t-h short help message\n"); michael@0: PR_fprintf(debug_out, "\t-H this help message\n"); michael@0: PR_fprintf(debug_out, "\n\n\tNote: Use of FIPS mode requires your "); michael@0: PR_fprintf(debug_out, "library path is using \n"); michael@0: PR_fprintf(debug_out, "\t pre-existing libraries with generated "); michael@0: PR_fprintf(debug_out, "checksum files\n"); michael@0: PR_fprintf(debug_out, "\t and database in FIPS mode \n"); michael@0: exit(1); michael@0: } michael@0: michael@0: static char * michael@0: mkoutput(const char *input) michael@0: { michael@0: int in_len = strlen(input); michael@0: char *output = PR_Malloc(in_len+sizeof(SGN_SUFFIX)); michael@0: int index = in_len + 1 - sizeof("."SHLIB_SUFFIX); michael@0: michael@0: if ((index > 0) && michael@0: (PL_strncmp(&input[index], michael@0: "."SHLIB_SUFFIX,sizeof("."SHLIB_SUFFIX)) == 0)) { michael@0: in_len = index; michael@0: } michael@0: memcpy(output,input,in_len); michael@0: memcpy(&output[in_len],SGN_SUFFIX,sizeof(SGN_SUFFIX)); michael@0: return output; michael@0: } michael@0: michael@0: static void michael@0: lperror(const char *string) { michael@0: PRErrorCode errorcode; michael@0: michael@0: errorcode = PR_GetError(); michael@0: PR_fprintf(PR_STDERR, "%s: %d: %s\n", string, errorcode, michael@0: PR_ErrorToString(errorcode, PR_LANGUAGE_I_DEFAULT)); michael@0: } michael@0: michael@0: static void michael@0: encodeInt(unsigned char *buf, int val) michael@0: { michael@0: buf[3] = (val >> 0) & 0xff; michael@0: buf[2] = (val >> 8) & 0xff; michael@0: buf[1] = (val >> 16) & 0xff; michael@0: buf[0] = (val >> 24) & 0xff; michael@0: return; michael@0: } michael@0: michael@0: static PRStatus michael@0: writeItem(PRFileDesc *fd, CK_VOID_PTR pValue, michael@0: CK_ULONG ulValueLen, char *file) michael@0: { michael@0: unsigned char buf[4]; michael@0: int bytesWritten; michael@0: if (ulValueLen == 0) { michael@0: PR_fprintf(PR_STDERR, "call to writeItem with 0 bytes of data.\n"); michael@0: return PR_FAILURE; michael@0: } michael@0: michael@0: encodeInt(buf,ulValueLen); michael@0: bytesWritten = PR_Write(fd,buf, 4); michael@0: if (bytesWritten != 4) { michael@0: lperror(file); michael@0: return PR_FAILURE; michael@0: } michael@0: bytesWritten = PR_Write(fd, pValue, ulValueLen); michael@0: if (bytesWritten != ulValueLen) { michael@0: lperror(file); michael@0: return PR_FAILURE; michael@0: } michael@0: return PR_SUCCESS; michael@0: } michael@0: michael@0: static const unsigned char prime[] = { 0x00, michael@0: 0x97, 0x44, 0x1d, 0xcc, 0x0d, 0x39, 0x0d, 0x8d, michael@0: 0xcb, 0x75, 0xdc, 0x24, 0x25, 0x6f, 0x01, 0x92, michael@0: 0xa1, 0x11, 0x07, 0x6b, 0x70, 0xac, 0x73, 0xd7, michael@0: 0x82, 0x28, 0xdf, 0xab, 0x82, 0x0c, 0x41, 0x0c, michael@0: 0x95, 0xb3, 0x3c, 0x3d, 0xea, 0x8a, 0xe6, 0x44, michael@0: 0x0a, 0xb8, 0xab, 0x90, 0x15, 0x41, 0x11, 0xe8, michael@0: 0x48, 0x7b, 0x8d, 0xb0, 0x9c, 0xd3, 0xf2, 0x69, michael@0: 0x66, 0xff, 0x66, 0x4b, 0x70, 0x2b, 0xbf, 0xfb, michael@0: 0xd6, 0x68, 0x85, 0x76, 0x1e, 0x34, 0xaa, 0xc5, michael@0: 0x57, 0x6e, 0x23, 0x02, 0x08, 0x60, 0x6e, 0xfd, michael@0: 0x67, 0x76, 0xe1, 0x7c, 0xc8, 0xcb, 0x51, 0x77, michael@0: 0xcf, 0xb1, 0x3b, 0x00, 0x2e, 0xfa, 0x21, 0xcd, michael@0: 0x34, 0x76, 0x75, 0x01, 0x19, 0xfe, 0xf8, 0x5d, michael@0: 0x43, 0xc5, 0x34, 0xf3, 0x7a, 0x95, 0xdc, 0xc2, michael@0: 0x58, 0x07, 0x19, 0x2f, 0x1d, 0x6f, 0x9a, 0x77, michael@0: 0x7e, 0x55, 0xaa, 0xe7, 0x5a, 0x50, 0x43, 0xd3 }; michael@0: michael@0: static const unsigned char subprime[] = { 0x0, michael@0: 0xd8, 0x16, 0x23, 0x34, 0x8a, 0x9e, 0x3a, 0xf5, michael@0: 0xd9, 0x10, 0x13, 0x35, 0xaa, 0xf3, 0xf3, 0x54, michael@0: 0x0b, 0x31, 0x24, 0xf1 }; michael@0: michael@0: static const unsigned char base[] = { michael@0: 0x03, 0x3a, 0xad, 0xfa, 0x3a, 0x0c, 0xea, 0x0a, michael@0: 0x4e, 0x43, 0x32, 0x92, 0xbb, 0x87, 0xf1, 0x11, michael@0: 0xc0, 0xad, 0x39, 0x38, 0x56, 0x1a, 0xdb, 0x23, michael@0: 0x66, 0xb1, 0x08, 0xda, 0xb6, 0x19, 0x51, 0x42, michael@0: 0x93, 0x4f, 0xc3, 0x44, 0x43, 0xa8, 0x05, 0xc1, michael@0: 0xf8, 0x71, 0x62, 0x6f, 0x3d, 0xe2, 0xab, 0x6f, michael@0: 0xd7, 0x80, 0x22, 0x6f, 0xca, 0x0d, 0xf6, 0x9f, michael@0: 0x45, 0x27, 0x83, 0xec, 0x86, 0x0c, 0xda, 0xaa, michael@0: 0xd6, 0xe0, 0xd0, 0x84, 0xfd, 0xb1, 0x4f, 0xdc, michael@0: 0x08, 0xcd, 0x68, 0x3a, 0x77, 0xc2, 0xc5, 0xf1, michael@0: 0x99, 0x0f, 0x15, 0x1b, 0x6a, 0x8c, 0x3d, 0x18, michael@0: 0x2b, 0x6f, 0xdc, 0x2b, 0xd8, 0xb5, 0x9b, 0xb8, michael@0: 0x2d, 0x57, 0x92, 0x1c, 0x46, 0x27, 0xaf, 0x6d, michael@0: 0xe1, 0x45, 0xcf, 0x0b, 0x3f, 0xfa, 0x07, 0xcc, michael@0: 0x14, 0x8e, 0xe7, 0xb8, 0xaa, 0xd5, 0xd1, 0x36, michael@0: 0x1d, 0x7e, 0x5e, 0x7d, 0xfa, 0x5b, 0x77, 0x1f }; michael@0: michael@0: static const unsigned char h[] = { michael@0: 0x41, 0x87, 0x47, 0x79, 0xd8, 0xba, 0x4e, 0xac, michael@0: 0x44, 0x4f, 0x6b, 0xd2, 0x16, 0x5e, 0x04, 0xc6, michael@0: 0xc2, 0x29, 0x93, 0x5e, 0xbd, 0xc7, 0xa9, 0x8f, michael@0: 0x23, 0xa1, 0xc8, 0xee, 0x80, 0x64, 0xd5, 0x67, michael@0: 0x3c, 0xba, 0x59, 0x9a, 0x06, 0x0c, 0xcc, 0x29, michael@0: 0x56, 0xc0, 0xb2, 0x21, 0xe0, 0x5b, 0x52, 0xcd, michael@0: 0x84, 0x73, 0x57, 0xfd, 0xd8, 0xc3, 0x5b, 0x13, michael@0: 0x54, 0xd7, 0x4a, 0x06, 0x86, 0x63, 0x09, 0xa5, michael@0: 0xb0, 0x59, 0xe2, 0x32, 0x9e, 0x09, 0xa3, 0x9f, michael@0: 0x49, 0x62, 0xcc, 0xa6, 0xf9, 0x54, 0xd5, 0xb2, michael@0: 0xc3, 0x08, 0x71, 0x7e, 0xe3, 0x37, 0x50, 0xd6, michael@0: 0x7b, 0xa7, 0xc2, 0x60, 0xc1, 0xeb, 0x51, 0x32, michael@0: 0xfa, 0xad, 0x35, 0x25, 0x17, 0xf0, 0x7f, 0x23, michael@0: 0xe5, 0xa8, 0x01, 0x52, 0xcf, 0x2f, 0xd9, 0xa9, michael@0: 0xf6, 0x00, 0x21, 0x15, 0xf1, 0xf7, 0x70, 0xb7, michael@0: 0x57, 0x8a, 0xd0, 0x59, 0x6a, 0x82, 0xdc, 0x9c }; michael@0: michael@0: static const unsigned char seed[] = { 0x00, michael@0: 0xcc, 0x4c, 0x69, 0x74, 0xf6, 0x72, 0x24, 0x68, michael@0: 0x24, 0x4f, 0xd7, 0x50, 0x11, 0x40, 0x81, 0xed, michael@0: 0x19, 0x3c, 0x8a, 0x25, 0xbc, 0x78, 0x0a, 0x85, michael@0: 0x82, 0x53, 0x70, 0x20, 0xf6, 0x54, 0xa5, 0x1b, michael@0: 0xf4, 0x15, 0xcd, 0xff, 0xc4, 0x88, 0xa7, 0x9d, michael@0: 0xf3, 0x47, 0x1c, 0x0a, 0xbe, 0x10, 0x29, 0x83, michael@0: 0xb9, 0x0f, 0x4c, 0xdf, 0x90, 0x16, 0x83, 0xa2, michael@0: 0xb3, 0xe3, 0x2e, 0xc1, 0xc2, 0x24, 0x6a, 0xc4, michael@0: 0x9d, 0x57, 0xba, 0xcb, 0x0f, 0x18, 0x75, 0x00, michael@0: 0x33, 0x46, 0x82, 0xec, 0xd6, 0x94, 0x77, 0xc3, michael@0: 0x4f, 0x4c, 0x58, 0x1c, 0x7f, 0x61, 0x3c, 0x36, michael@0: 0xd5, 0x2f, 0xa5, 0x66, 0xd8, 0x2f, 0xce, 0x6e, michael@0: 0x8e, 0x20, 0x48, 0x4a, 0xbb, 0xe3, 0xe0, 0xb2, michael@0: 0x50, 0x33, 0x63, 0x8a, 0x5b, 0x2d, 0x6a, 0xbe, michael@0: 0x4c, 0x28, 0x81, 0x53, 0x5b, 0xe4, 0xf6, 0xfc, michael@0: 0x64, 0x06, 0x13, 0x51, 0xeb, 0x4a, 0x91, 0x9c }; michael@0: michael@0: static const unsigned int counter=1496; michael@0: michael@0: static const unsigned char prime2[] = { 0x00, michael@0: 0xa4, 0xc2, 0x83, 0x4f, 0x36, 0xd3, 0x4f, 0xae, michael@0: 0xa0, 0xb1, 0x47, 0x43, 0xa8, 0x15, 0xee, 0xad, michael@0: 0xa3, 0x98, 0xa3, 0x29, 0x45, 0xae, 0x5c, 0xd9, michael@0: 0x12, 0x99, 0x09, 0xdc, 0xef, 0x05, 0xb4, 0x98, michael@0: 0x05, 0xaa, 0x07, 0xaa, 0x83, 0x89, 0xd7, 0xba, michael@0: 0xd1, 0x25, 0x56, 0x58, 0xd1, 0x73, 0x3c, 0xd0, michael@0: 0x91, 0x65, 0xbe, 0x27, 0x92, 0x94, 0x86, 0x95, michael@0: 0xdb, 0xcf, 0x07, 0x13, 0xa0, 0x85, 0xd6, 0xaa, michael@0: 0x6c, 0x1d, 0x63, 0xbf, 0xdd, 0xdf, 0xbc, 0x30, michael@0: 0xeb, 0x42, 0x2f, 0x52, 0x11, 0xec, 0x6e, 0x65, michael@0: 0xdf, 0x50, 0xbe, 0x28, 0x3d, 0xa4, 0xec, 0x45, michael@0: 0x19, 0x4c, 0x13, 0x0f, 0x59, 0x74, 0x57, 0x69, michael@0: 0x99, 0x4f, 0x4a, 0x74, 0x7f, 0x8c, 0x9e, 0xa2, michael@0: 0xe7, 0x94, 0xc9, 0x70, 0x70, 0xd0, 0xc4, 0xda, michael@0: 0x49, 0x5b, 0x7a, 0x7d, 0xd9, 0x71, 0x7c, 0x3b, michael@0: 0xdc, 0xd2, 0x8a, 0x74, 0x5f, 0xce, 0x09, 0xa2, michael@0: 0xdb, 0xec, 0xa4, 0xba, 0x75, 0xaa, 0x0a, 0x97, michael@0: 0xa6, 0x82, 0x25, 0x90, 0x90, 0x37, 0xe4, 0x40, michael@0: 0x05, 0x28, 0x8f, 0x98, 0x8e, 0x68, 0x01, 0xaf, michael@0: 0x9b, 0x08, 0x2a, 0x9b, 0xd5, 0xb9, 0x8c, 0x14, michael@0: 0xbf, 0xba, 0xcb, 0x5b, 0xda, 0x4c, 0x95, 0xb8, michael@0: 0xdf, 0x67, 0xa6, 0x6b, 0x76, 0x8c, 0xad, 0x4f, michael@0: 0xfd, 0x6a, 0xd6, 0xcc, 0x62, 0x71, 0x30, 0x30, michael@0: 0xc1, 0x29, 0x84, 0xe4, 0x8e, 0x32, 0x51, 0xb6, michael@0: 0xea, 0xfa, 0xba, 0x00, 0x99, 0x76, 0xea, 0x86, michael@0: 0x90, 0xab, 0x2d, 0xe9, 0xfd, 0x1e, 0x8c, 0xcc, michael@0: 0x3c, 0x2b, 0x5d, 0x13, 0x1b, 0x47, 0xb4, 0xf5, michael@0: 0x09, 0x74, 0x1d, 0xd4, 0x78, 0xb2, 0x42, 0x19, michael@0: 0xd6, 0x24, 0xd1, 0x68, 0xbf, 0x11, 0xf1, 0x38, michael@0: 0xa0, 0x44, 0x9c, 0xc6, 0x51, 0x33, 0xaa, 0x42, michael@0: 0x93, 0x9e, 0x30, 0x58, 0x9e, 0xc0, 0x70, 0xdf, michael@0: 0x7e, 0x64, 0xb1, 0xd8, 0x68, 0x75, 0x98, 0xa7 }; michael@0: michael@0: static const unsigned char subprime2[] = { 0x00, michael@0: 0x8e, 0xab, 0xf4, 0xbe, 0x45, 0xeb, 0xa3, 0x58, michael@0: 0x4e, 0x60, 0x15, 0x66, 0x5a, 0x4b, 0x25, 0xcf, michael@0: 0x45, 0x77, 0x89, 0x3f, 0x73, 0x34, 0x4a, 0xe0, michael@0: 0x9e, 0xac, 0xfd, 0xdc, 0xff, 0x9c, 0x8d, 0xe7 }; michael@0: michael@0: static const unsigned char base2[] = { 0x00, michael@0: 0x8d, 0x72, 0x32, 0x46, 0xa6, 0x5c, 0x80, 0xe3, michael@0: 0x43, 0x0a, 0x9e, 0x94, 0x35, 0x86, 0xd4, 0x58, michael@0: 0xa1, 0xca, 0x22, 0xb9, 0x73, 0x46, 0x0b, 0xfb, michael@0: 0x3e, 0x33, 0xf1, 0xd5, 0xd3, 0xb4, 0x26, 0xbf, michael@0: 0x50, 0xd7, 0xf2, 0x09, 0x33, 0x6e, 0xc0, 0x31, michael@0: 0x1b, 0x6d, 0x07, 0x70, 0x86, 0xca, 0x57, 0xf7, michael@0: 0x0b, 0x4a, 0x63, 0xf0, 0x6f, 0xc8, 0x8a, 0xed, michael@0: 0x50, 0x60, 0xf3, 0x11, 0xc7, 0x44, 0xf3, 0xce, michael@0: 0x4e, 0x50, 0x42, 0x2d, 0x85, 0x33, 0x54, 0x57, michael@0: 0x03, 0x8d, 0xdc, 0x66, 0x4d, 0x61, 0x83, 0x17, michael@0: 0x1c, 0x7b, 0x0d, 0x65, 0xbc, 0x8f, 0x2c, 0x19, michael@0: 0x86, 0xfc, 0xe2, 0x9f, 0x5d, 0x67, 0xfc, 0xd4, michael@0: 0xa5, 0xf8, 0x23, 0xa1, 0x1a, 0xa2, 0xe1, 0x11, michael@0: 0x15, 0x84, 0x32, 0x01, 0xee, 0x88, 0xf1, 0x55, michael@0: 0x30, 0xe9, 0x74, 0x3c, 0x1a, 0x2b, 0x54, 0x45, michael@0: 0x2e, 0x39, 0xb9, 0x77, 0xe1, 0x32, 0xaf, 0x2d, michael@0: 0x97, 0xe0, 0x21, 0xec, 0xf5, 0x58, 0xe1, 0xc7, michael@0: 0x2e, 0xe0, 0x71, 0x3d, 0x29, 0xa4, 0xd6, 0xe2, michael@0: 0x5f, 0x85, 0x9c, 0x05, 0x04, 0x46, 0x41, 0x89, michael@0: 0x03, 0x3c, 0xfa, 0xb2, 0xcf, 0xfa, 0xd5, 0x67, michael@0: 0xcc, 0xec, 0x68, 0xfc, 0x83, 0xd9, 0x1f, 0x2e, michael@0: 0x4e, 0x9a, 0x5e, 0x77, 0xa1, 0xff, 0xe6, 0x6f, michael@0: 0x04, 0x8b, 0xf9, 0x6b, 0x47, 0xc6, 0x49, 0xd2, michael@0: 0x88, 0x6e, 0x29, 0xa3, 0x1b, 0xae, 0xe0, 0x4f, michael@0: 0x72, 0x8a, 0x28, 0x94, 0x0c, 0x1d, 0x8c, 0x99, michael@0: 0xa2, 0x6f, 0xf8, 0xba, 0x99, 0x90, 0xc7, 0xe5, michael@0: 0xb1, 0x3c, 0x10, 0x34, 0x86, 0x6a, 0x6a, 0x1f, michael@0: 0x39, 0x63, 0x58, 0xe1, 0x5e, 0x97, 0x95, 0x45, michael@0: 0x40, 0x38, 0x45, 0x6f, 0x02, 0xb5, 0x86, 0x6e, michael@0: 0xae, 0x2f, 0x32, 0x7e, 0xa1, 0x3a, 0x34, 0x2c, michael@0: 0x1c, 0xd3, 0xff, 0x4e, 0x2c, 0x38, 0x1c, 0xaa, michael@0: 0x2e, 0x66, 0xbe, 0x32, 0x3e, 0x3c, 0x06, 0x5f }; michael@0: michael@0: static const unsigned char h2[] = { michael@0: 0x30, 0x91, 0xa1, 0x2e, 0x40, 0xa5, 0x7d, 0xf7, michael@0: 0xdc, 0xed, 0xee, 0x05, 0xc2, 0x31, 0x91, 0x37, michael@0: 0xda, 0xc5, 0xe3, 0x47, 0xb5, 0x35, 0x4b, 0xfd, michael@0: 0x18, 0xb2, 0x7e, 0x67, 0x1e, 0x92, 0x22, 0xe7, michael@0: 0xf5, 0x00, 0x71, 0xc0, 0x86, 0x8d, 0x90, 0x31, michael@0: 0x36, 0x3e, 0xd0, 0x94, 0x5d, 0x2f, 0x9a, 0x68, michael@0: 0xd2, 0xf8, 0x3d, 0x5e, 0x84, 0x42, 0x35, 0xda, michael@0: 0x75, 0xdd, 0x05, 0xf0, 0x03, 0x31, 0x39, 0xe5, michael@0: 0xfd, 0x2f, 0x5a, 0x7d, 0x56, 0xd8, 0x26, 0xa0, michael@0: 0x51, 0x5e, 0x32, 0xb4, 0xad, 0xee, 0xd4, 0x89, michael@0: 0xae, 0x01, 0x7f, 0xac, 0x86, 0x98, 0x77, 0x26, michael@0: 0x5c, 0x31, 0xd2, 0x5e, 0xbb, 0x7f, 0xf5, 0x4c, michael@0: 0x9b, 0xf0, 0xa6, 0x37, 0x34, 0x08, 0x86, 0x6b, michael@0: 0xce, 0xeb, 0x85, 0x66, 0x0a, 0x26, 0x8a, 0x14, michael@0: 0x92, 0x12, 0x74, 0xf4, 0xf0, 0xcb, 0xb5, 0xfc, michael@0: 0x38, 0xd5, 0x1e, 0xa1, 0x2f, 0x4a, 0x1a, 0xca, michael@0: 0x66, 0xde, 0x6e, 0xe6, 0x6e, 0x1c, 0xef, 0x50, michael@0: 0x41, 0x31, 0x09, 0xe7, 0x4a, 0xb8, 0xa3, 0xaa, michael@0: 0x5a, 0x22, 0xbd, 0x63, 0x0f, 0xe9, 0x0e, 0xdb, michael@0: 0xb3, 0xca, 0x7e, 0x8d, 0x40, 0xb3, 0x3e, 0x0b, michael@0: 0x12, 0x8b, 0xb0, 0x80, 0x4d, 0x6d, 0xb0, 0x54, michael@0: 0xbb, 0x4c, 0x1d, 0x6c, 0xa0, 0x5c, 0x9d, 0x91, michael@0: 0xb3, 0xbb, 0xd9, 0xfc, 0x60, 0xec, 0xc1, 0xbc, michael@0: 0xae, 0x72, 0x3f, 0xa5, 0x4f, 0x36, 0x2d, 0x2c, michael@0: 0x81, 0x03, 0x86, 0xa2, 0x03, 0x38, 0x36, 0x8e, michael@0: 0xad, 0x1d, 0x53, 0xc6, 0xc5, 0x9e, 0xda, 0x08, michael@0: 0x35, 0x4f, 0xb2, 0x78, 0xba, 0xd1, 0x22, 0xde, michael@0: 0xc4, 0x6b, 0xbe, 0x83, 0x71, 0x0f, 0xee, 0x38, michael@0: 0x4a, 0x9f, 0xda, 0x90, 0x93, 0x6b, 0x9a, 0xf2, michael@0: 0xeb, 0x23, 0xfe, 0x41, 0x3f, 0xf1, 0xfc, 0xee, michael@0: 0x7f, 0x67, 0xa7, 0xb8, 0xab, 0x29, 0xf4, 0x75, michael@0: 0x1c, 0xe9, 0xd1, 0x47, 0x7d, 0x86, 0x44, 0xe2 }; michael@0: michael@0: static const unsigned char seed2[] = { 0x00, michael@0: 0xbc, 0xae, 0xc4, 0xea, 0x4e, 0xd2, 0xed, 0x1c, michael@0: 0x8d, 0x48, 0xed, 0xf2, 0xa5, 0xb4, 0x18, 0xba, michael@0: 0x00, 0xcb, 0x9c, 0x75, 0x8a, 0x39, 0x94, 0x3b, michael@0: 0xd0, 0xd6, 0x01, 0xf7, 0xc1, 0xf5, 0x9d, 0xe5, michael@0: 0xe3, 0xb4, 0x1d, 0xf5, 0x30, 0xfe, 0x99, 0xe4, michael@0: 0x01, 0xab, 0xc0, 0x88, 0x4e, 0x67, 0x8f, 0xc6, michael@0: 0x72, 0x39, 0x2e, 0xac, 0x51, 0xec, 0x91, 0x41, michael@0: 0x47, 0x71, 0x14, 0x8a, 0x1d, 0xca, 0x88, 0x15, michael@0: 0xea, 0xc9, 0x48, 0x9a, 0x71, 0x50, 0x19, 0x38, michael@0: 0xdb, 0x4e, 0x65, 0xd5, 0x13, 0xd8, 0x2a, 0xc4, michael@0: 0xcd, 0xfd, 0x0c, 0xe3, 0xc3, 0x60, 0xae, 0x6d, michael@0: 0x88, 0xf2, 0x3a, 0xd0, 0x64, 0x73, 0x32, 0x89, michael@0: 0xcd, 0x0b, 0xb8, 0xc7, 0xa5, 0x27, 0x84, 0xd5, michael@0: 0x83, 0x3f, 0x0e, 0x10, 0x63, 0x10, 0x78, 0xac, michael@0: 0x6b, 0x56, 0xb2, 0x62, 0x3a, 0x44, 0x56, 0xc0, michael@0: 0xe4, 0x33, 0xd7, 0x63, 0x4c, 0xc9, 0x6b, 0xae, michael@0: 0xfb, 0xe2, 0x9b, 0xf4, 0x96, 0xc7, 0xf0, 0x2a, michael@0: 0x50, 0xde, 0x86, 0x69, 0x4f, 0x42, 0x4b, 0x1c, michael@0: 0x7c, 0xa8, 0x6a, 0xfb, 0x54, 0x47, 0x1b, 0x41, michael@0: 0x31, 0x9e, 0x0a, 0xc6, 0xc0, 0xbc, 0x88, 0x7f, michael@0: 0x5a, 0x42, 0xa9, 0x82, 0x58, 0x32, 0xb3, 0xeb, michael@0: 0x54, 0x83, 0x84, 0x26, 0x92, 0xa6, 0xc0, 0x6e, michael@0: 0x2b, 0xa6, 0x82, 0x82, 0x43, 0x58, 0x84, 0x53, michael@0: 0x31, 0xcf, 0xd0, 0x0a, 0x11, 0x09, 0x44, 0xc8, michael@0: 0x11, 0x36, 0xe0, 0x04, 0x85, 0x2e, 0xd1, 0x29, michael@0: 0x6b, 0x7b, 0x00, 0x71, 0x5f, 0xef, 0x7b, 0x7a, michael@0: 0x2d, 0x91, 0xf9, 0x84, 0x45, 0x4d, 0xc7, 0xe1, michael@0: 0xee, 0xd4, 0xb8, 0x61, 0x3b, 0x13, 0xb7, 0xba, michael@0: 0x95, 0x39, 0xf6, 0x3d, 0x89, 0xbd, 0xa5, 0x80, michael@0: 0x93, 0xf7, 0xe5, 0x17, 0x05, 0xc5, 0x65, 0xb7, michael@0: 0xde, 0xc9, 0x9f, 0x04, 0x87, 0xcf, 0x4f, 0x86, michael@0: 0xc3, 0x29, 0x7d, 0xb7, 0x89, 0xbf, 0xe3, 0xde }; michael@0: michael@0: static const unsigned int counter2=210; michael@0: michael@0: struct tuple_str { michael@0: CK_RV errNum; michael@0: const char * errString; michael@0: }; michael@0: michael@0: typedef struct tuple_str tuple_str; michael@0: michael@0: static const tuple_str errStrings[] = { michael@0: {CKR_OK , "CKR_OK "}, michael@0: {CKR_CANCEL , "CKR_CANCEL "}, michael@0: {CKR_HOST_MEMORY , "CKR_HOST_MEMORY "}, michael@0: {CKR_SLOT_ID_INVALID , "CKR_SLOT_ID_INVALID "}, michael@0: {CKR_GENERAL_ERROR , "CKR_GENERAL_ERROR "}, michael@0: {CKR_FUNCTION_FAILED , "CKR_FUNCTION_FAILED "}, michael@0: {CKR_ARGUMENTS_BAD , "CKR_ARGUMENTS_BAD "}, michael@0: {CKR_NO_EVENT , "CKR_NO_EVENT "}, michael@0: {CKR_NEED_TO_CREATE_THREADS , "CKR_NEED_TO_CREATE_THREADS "}, michael@0: {CKR_CANT_LOCK , "CKR_CANT_LOCK "}, michael@0: {CKR_ATTRIBUTE_READ_ONLY , "CKR_ATTRIBUTE_READ_ONLY "}, michael@0: {CKR_ATTRIBUTE_SENSITIVE , "CKR_ATTRIBUTE_SENSITIVE "}, michael@0: {CKR_ATTRIBUTE_TYPE_INVALID , "CKR_ATTRIBUTE_TYPE_INVALID "}, michael@0: {CKR_ATTRIBUTE_VALUE_INVALID , "CKR_ATTRIBUTE_VALUE_INVALID "}, michael@0: {CKR_DATA_INVALID , "CKR_DATA_INVALID "}, michael@0: {CKR_DATA_LEN_RANGE , "CKR_DATA_LEN_RANGE "}, michael@0: {CKR_DEVICE_ERROR , "CKR_DEVICE_ERROR "}, michael@0: {CKR_DEVICE_MEMORY , "CKR_DEVICE_MEMORY "}, michael@0: {CKR_DEVICE_REMOVED , "CKR_DEVICE_REMOVED "}, michael@0: {CKR_ENCRYPTED_DATA_INVALID , "CKR_ENCRYPTED_DATA_INVALID "}, michael@0: {CKR_ENCRYPTED_DATA_LEN_RANGE , "CKR_ENCRYPTED_DATA_LEN_RANGE "}, michael@0: {CKR_FUNCTION_CANCELED , "CKR_FUNCTION_CANCELED "}, michael@0: {CKR_FUNCTION_NOT_PARALLEL , "CKR_FUNCTION_NOT_PARALLEL "}, michael@0: {CKR_FUNCTION_NOT_SUPPORTED , "CKR_FUNCTION_NOT_SUPPORTED "}, michael@0: {CKR_KEY_HANDLE_INVALID , "CKR_KEY_HANDLE_INVALID "}, michael@0: {CKR_KEY_SIZE_RANGE , "CKR_KEY_SIZE_RANGE "}, michael@0: {CKR_KEY_TYPE_INCONSISTENT , "CKR_KEY_TYPE_INCONSISTENT "}, michael@0: {CKR_KEY_NOT_NEEDED , "CKR_KEY_NOT_NEEDED "}, michael@0: {CKR_KEY_CHANGED , "CKR_KEY_CHANGED "}, michael@0: {CKR_KEY_NEEDED , "CKR_KEY_NEEDED "}, michael@0: {CKR_KEY_INDIGESTIBLE , "CKR_KEY_INDIGESTIBLE "}, michael@0: {CKR_KEY_FUNCTION_NOT_PERMITTED , "CKR_KEY_FUNCTION_NOT_PERMITTED "}, michael@0: {CKR_KEY_NOT_WRAPPABLE , "CKR_KEY_NOT_WRAPPABLE "}, michael@0: {CKR_KEY_UNEXTRACTABLE , "CKR_KEY_UNEXTRACTABLE "}, michael@0: {CKR_MECHANISM_INVALID , "CKR_MECHANISM_INVALID "}, michael@0: {CKR_MECHANISM_PARAM_INVALID , "CKR_MECHANISM_PARAM_INVALID "}, michael@0: {CKR_OBJECT_HANDLE_INVALID , "CKR_OBJECT_HANDLE_INVALID "}, michael@0: {CKR_OPERATION_ACTIVE , "CKR_OPERATION_ACTIVE "}, michael@0: {CKR_OPERATION_NOT_INITIALIZED , "CKR_OPERATION_NOT_INITIALIZED "}, michael@0: {CKR_PIN_INCORRECT , "CKR_PIN_INCORRECT "}, michael@0: {CKR_PIN_INVALID , "CKR_PIN_INVALID "}, michael@0: {CKR_PIN_LEN_RANGE , "CKR_PIN_LEN_RANGE "}, michael@0: {CKR_PIN_EXPIRED , "CKR_PIN_EXPIRED "}, michael@0: {CKR_PIN_LOCKED , "CKR_PIN_LOCKED "}, michael@0: {CKR_SESSION_CLOSED , "CKR_SESSION_CLOSED "}, michael@0: {CKR_SESSION_COUNT , "CKR_SESSION_COUNT "}, michael@0: {CKR_SESSION_HANDLE_INVALID , "CKR_SESSION_HANDLE_INVALID "}, michael@0: {CKR_SESSION_PARALLEL_NOT_SUPPORTED , "CKR_SESSION_PARALLEL_NOT_SUPPORTED "}, michael@0: {CKR_SESSION_READ_ONLY , "CKR_SESSION_READ_ONLY "}, michael@0: {CKR_SESSION_EXISTS , "CKR_SESSION_EXISTS "}, michael@0: {CKR_SESSION_READ_ONLY_EXISTS , "CKR_SESSION_READ_ONLY_EXISTS "}, michael@0: {CKR_SESSION_READ_WRITE_SO_EXISTS , "CKR_SESSION_READ_WRITE_SO_EXISTS "}, michael@0: {CKR_SIGNATURE_INVALID , "CKR_SIGNATURE_INVALID "}, michael@0: {CKR_SIGNATURE_LEN_RANGE , "CKR_SIGNATURE_LEN_RANGE "}, michael@0: {CKR_TEMPLATE_INCOMPLETE , "CKR_TEMPLATE_INCOMPLETE "}, michael@0: {CKR_TEMPLATE_INCONSISTENT , "CKR_TEMPLATE_INCONSISTENT "}, michael@0: {CKR_TOKEN_NOT_PRESENT , "CKR_TOKEN_NOT_PRESENT "}, michael@0: {CKR_TOKEN_NOT_RECOGNIZED , "CKR_TOKEN_NOT_RECOGNIZED "}, michael@0: {CKR_TOKEN_WRITE_PROTECTED , "CKR_TOKEN_WRITE_PROTECTED "}, michael@0: {CKR_UNWRAPPING_KEY_HANDLE_INVALID , "CKR_UNWRAPPING_KEY_HANDLE_INVALID "}, michael@0: {CKR_UNWRAPPING_KEY_SIZE_RANGE , "CKR_UNWRAPPING_KEY_SIZE_RANGE "}, michael@0: {CKR_UNWRAPPING_KEY_TYPE_INCONSISTENT, "CKR_UNWRAPPING_KEY_TYPE_INCONSISTENT"}, michael@0: {CKR_USER_ALREADY_LOGGED_IN , "CKR_USER_ALREADY_LOGGED_IN "}, michael@0: {CKR_USER_NOT_LOGGED_IN , "CKR_USER_NOT_LOGGED_IN "}, michael@0: {CKR_USER_PIN_NOT_INITIALIZED , "CKR_USER_PIN_NOT_INITIALIZED "}, michael@0: {CKR_USER_TYPE_INVALID , "CKR_USER_TYPE_INVALID "}, michael@0: {CKR_USER_ANOTHER_ALREADY_LOGGED_IN , "CKR_USER_ANOTHER_ALREADY_LOGGED_IN "}, michael@0: {CKR_USER_TOO_MANY_TYPES , "CKR_USER_TOO_MANY_TYPES "}, michael@0: {CKR_WRAPPED_KEY_INVALID , "CKR_WRAPPED_KEY_INVALID "}, michael@0: {CKR_WRAPPED_KEY_LEN_RANGE , "CKR_WRAPPED_KEY_LEN_RANGE "}, michael@0: {CKR_WRAPPING_KEY_HANDLE_INVALID , "CKR_WRAPPING_KEY_HANDLE_INVALID "}, michael@0: {CKR_WRAPPING_KEY_SIZE_RANGE , "CKR_WRAPPING_KEY_SIZE_RANGE "}, michael@0: {CKR_WRAPPING_KEY_TYPE_INCONSISTENT , "CKR_WRAPPING_KEY_TYPE_INCONSISTENT "}, michael@0: {CKR_RANDOM_SEED_NOT_SUPPORTED , "CKR_RANDOM_SEED_NOT_SUPPORTED "}, michael@0: {CKR_RANDOM_NO_RNG , "CKR_RANDOM_NO_RNG "}, michael@0: {CKR_DOMAIN_PARAMS_INVALID , "CKR_DOMAIN_PARAMS_INVALID "}, michael@0: {CKR_BUFFER_TOO_SMALL , "CKR_BUFFER_TOO_SMALL "}, michael@0: {CKR_SAVED_STATE_INVALID , "CKR_SAVED_STATE_INVALID "}, michael@0: {CKR_INFORMATION_SENSITIVE , "CKR_INFORMATION_SENSITIVE "}, michael@0: {CKR_STATE_UNSAVEABLE , "CKR_STATE_UNSAVEABLE "}, michael@0: {CKR_CRYPTOKI_NOT_INITIALIZED , "CKR_CRYPTOKI_NOT_INITIALIZED "}, michael@0: {CKR_CRYPTOKI_ALREADY_INITIALIZED , "CKR_CRYPTOKI_ALREADY_INITIALIZED "}, michael@0: {CKR_MUTEX_BAD , "CKR_MUTEX_BAD "}, michael@0: {CKR_MUTEX_NOT_LOCKED , "CKR_MUTEX_NOT_LOCKED "}, michael@0: {CKR_FUNCTION_REJECTED , "CKR_FUNCTION_REJECTED "}, michael@0: {CKR_VENDOR_DEFINED , "CKR_VENDOR_DEFINED "}, michael@0: {0xCE534351 , "CKR_NETSCAPE_CERTDB_FAILED "}, michael@0: {0xCE534352 , "CKR_NETSCAPE_KEYDB_FAILED "} michael@0: michael@0: }; michael@0: michael@0: static const CK_ULONG numStrings = sizeof(errStrings) / sizeof(tuple_str); michael@0: michael@0: /* Returns constant error string for "CRV". michael@0: * Returns "unknown error" if errNum is unknown. michael@0: */ michael@0: static const char * michael@0: CK_RVtoStr(CK_RV errNum) { michael@0: CK_ULONG low = 1; michael@0: CK_ULONG high = numStrings - 1; michael@0: CK_ULONG i; michael@0: CK_RV num; michael@0: static int initDone; michael@0: michael@0: /* make sure table is in ascending order. michael@0: * binary search depends on it. michael@0: */ michael@0: if (!initDone) { michael@0: CK_RV lastNum = CKR_OK; michael@0: for (i = low; i <= high; ++i) { michael@0: num = errStrings[i].errNum; michael@0: if (num <= lastNum) { michael@0: PR_fprintf(PR_STDERR, michael@0: "sequence error in error strings at item %d\n" michael@0: "error %d (%s)\n" michael@0: "should come after \n" michael@0: "error %d (%s)\n", michael@0: (int) i, (int) lastNum, errStrings[i-1].errString, michael@0: (int) num, errStrings[i].errString); michael@0: } michael@0: lastNum = num; michael@0: } michael@0: initDone = 1; michael@0: } michael@0: michael@0: /* Do binary search of table. */ michael@0: while (low + 1 < high) { michael@0: i = (low + high) / 2; michael@0: num = errStrings[i].errNum; michael@0: if (errNum == num) michael@0: return errStrings[i].errString; michael@0: if (errNum < num) michael@0: high = i; michael@0: else michael@0: low = i; michael@0: } michael@0: if (errNum == errStrings[low].errNum) michael@0: return errStrings[low].errString; michael@0: if (errNum == errStrings[high].errNum) michael@0: return errStrings[high].errString; michael@0: return "unknown error"; michael@0: } michael@0: michael@0: static void michael@0: pk11error(const char *string, CK_RV crv) { michael@0: PRErrorCode errorcode; michael@0: michael@0: PR_fprintf(PR_STDERR, "%s: 0x%08lX, %-26s\n", string, crv, CK_RVtoStr(crv)); michael@0: michael@0: errorcode = PR_GetError(); michael@0: if (errorcode) { michael@0: PR_fprintf(PR_STDERR, "NSPR error code: %d: %s\n", errorcode, michael@0: PR_ErrorToString(errorcode, PR_LANGUAGE_I_DEFAULT)); michael@0: } michael@0: } michael@0: michael@0: static void michael@0: logIt(const char *fmt, ...) { michael@0: va_list args; michael@0: michael@0: if (verbose) { michael@0: va_start (args, fmt); michael@0: vprintf(fmt, args); michael@0: va_end(args); michael@0: } michael@0: } michael@0: michael@0: static CK_RV michael@0: softokn_Init(CK_FUNCTION_LIST_PTR pFunctionList, const char * configDir, michael@0: const char * dbPrefix) { michael@0: michael@0: CK_RV crv = CKR_OK; michael@0: CK_C_INITIALIZE_ARGS initArgs; michael@0: char *moduleSpec = NULL; michael@0: michael@0: initArgs.CreateMutex = NULL; michael@0: initArgs.DestroyMutex = NULL; michael@0: initArgs.LockMutex = NULL; michael@0: initArgs.UnlockMutex = NULL; michael@0: initArgs.flags = CKF_OS_LOCKING_OK; michael@0: if (configDir) { michael@0: moduleSpec = PR_smprintf("configdir='%s' certPrefix='%s' " michael@0: "keyPrefix='%s' secmod='secmod.db' flags=ReadOnly ", michael@0: configDir, dbPrefix, dbPrefix); michael@0: } else { michael@0: moduleSpec = PR_smprintf("configdir='' certPrefix='' keyPrefix='' " michael@0: "secmod='' flags=noCertDB, noModDB"); michael@0: } michael@0: if (!moduleSpec) { michael@0: PR_fprintf(PR_STDERR, "softokn_Init: out of memory error\n"); michael@0: return CKR_HOST_MEMORY; michael@0: } michael@0: logIt("moduleSpec %s\n", moduleSpec); michael@0: initArgs.LibraryParameters = (CK_CHAR_PTR *) moduleSpec; michael@0: initArgs.pReserved = NULL; michael@0: michael@0: crv = pFunctionList->C_Initialize(&initArgs); michael@0: if (crv != CKR_OK) { michael@0: pk11error("C_Initialize failed", crv); michael@0: goto cleanup; michael@0: } michael@0: michael@0: cleanup: michael@0: if (moduleSpec) { michael@0: PR_smprintf_free(moduleSpec); michael@0: } michael@0: michael@0: return crv; michael@0: } michael@0: michael@0: static char * michael@0: filePasswd(char *pwFile) michael@0: { michael@0: unsigned char phrase[200]; michael@0: PRFileDesc *fd; michael@0: PRInt32 nb; michael@0: int i; michael@0: michael@0: if (!pwFile) michael@0: return 0; michael@0: michael@0: fd = PR_Open(pwFile, PR_RDONLY, 0); michael@0: if (!fd) { michael@0: lperror(pwFile); michael@0: return NULL; michael@0: } michael@0: michael@0: nb = PR_Read(fd, phrase, sizeof(phrase)); michael@0: michael@0: PR_Close(fd); michael@0: /* handle the Windows EOL case */ michael@0: i = 0; michael@0: while (phrase[i] != '\r' && phrase[i] != '\n' && i < nb) i++; michael@0: phrase[i] = '\0'; michael@0: if (nb == 0) { michael@0: PR_fprintf(PR_STDERR,"password file contains no data\n"); michael@0: return NULL; michael@0: } michael@0: return (char*) PL_strdup((char*)phrase); michael@0: } michael@0: michael@0: static void michael@0: checkPath(char *string) michael@0: { michael@0: char *src; michael@0: char *dest; michael@0: michael@0: /* michael@0: * windows support convert any back slashes to michael@0: * forward slashes. michael@0: */ michael@0: for (src=string, dest=string; *src; src++,dest++) { michael@0: if (*src == '\\') { michael@0: *dest = '/'; michael@0: } michael@0: } michael@0: dest--; michael@0: /* if the last char is a / set it to 0 */ michael@0: if (*dest == '/') michael@0: *dest = 0; michael@0: michael@0: } michael@0: michael@0: static CK_SLOT_ID * michael@0: getSlotList(CK_FUNCTION_LIST_PTR pFunctionList, michael@0: CK_ULONG slotIndex) { michael@0: CK_RV crv = CKR_OK; michael@0: CK_SLOT_ID *pSlotList = NULL; michael@0: CK_ULONG slotCount; michael@0: michael@0: /* Get slot list */ michael@0: crv = pFunctionList->C_GetSlotList(CK_FALSE /* all slots */, michael@0: NULL, &slotCount); michael@0: if (crv != CKR_OK) { michael@0: pk11error( "C_GetSlotList failed", crv); michael@0: return NULL; michael@0: } michael@0: michael@0: if (slotIndex >= slotCount) { michael@0: PR_fprintf(PR_STDERR, "provided slotIndex is greater than the slot count."); michael@0: return NULL; michael@0: } michael@0: michael@0: pSlotList = (CK_SLOT_ID *)PR_Malloc(slotCount * sizeof(CK_SLOT_ID)); michael@0: if (!pSlotList) { michael@0: lperror("failed to allocate slot list"); michael@0: return NULL; michael@0: } michael@0: crv = pFunctionList->C_GetSlotList(CK_FALSE /* all slots */, michael@0: pSlotList, &slotCount); michael@0: if (crv != CKR_OK) { michael@0: pk11error( "C_GetSlotList failed", crv); michael@0: if (pSlotList) PR_Free(pSlotList); michael@0: return NULL; michael@0: } michael@0: return pSlotList; michael@0: } michael@0: michael@0: int main(int argc, char **argv) michael@0: { michael@0: PLOptState *optstate; michael@0: char *program_name; michael@0: char *libname = NULL; michael@0: PRLibrary *lib; michael@0: PRFileDesc *fd; michael@0: PRStatus rv = PR_SUCCESS; michael@0: const char *input_file = NULL; /* read/create encrypted data from here */ michael@0: char *output_file = NULL; /* write new encrypted data here */ michael@0: int bytesRead; michael@0: int bytesWritten; michael@0: unsigned char file_buf[512]; michael@0: int count=0; michael@0: int keySize = 0; michael@0: int i; michael@0: PRBool verify = PR_FALSE; michael@0: static PRBool FIPSMODE = PR_FALSE; michael@0: PRBool successful = PR_FALSE; michael@0: michael@0: #ifdef USES_LINKS michael@0: int ret; michael@0: struct stat stat_buf; michael@0: char link_buf[MAXPATHLEN+1]; michael@0: char *link_file = NULL; michael@0: #endif michael@0: michael@0: char *pwd = NULL; michael@0: char *configDir = NULL; michael@0: char *dbPrefix = NULL; michael@0: char *disableUnload = NULL; michael@0: michael@0: CK_C_GetFunctionList pC_GetFunctionList; michael@0: CK_TOKEN_INFO tokenInfo; michael@0: CK_FUNCTION_LIST_PTR pFunctionList = NULL; michael@0: CK_RV crv = CKR_OK; michael@0: CK_SESSION_HANDLE hRwSession; michael@0: CK_SLOT_ID *pSlotList = NULL; michael@0: CK_ULONG slotIndex = 0; michael@0: CK_MECHANISM digestmech; michael@0: CK_ULONG digestLen = 0; michael@0: CK_BYTE digest[32]; /* SHA256_LENGTH */ michael@0: CK_BYTE sign[64]; /* DSA SIGNATURE LENGTH */ michael@0: CK_ULONG signLen = 0 ; michael@0: CK_MECHANISM signMech = { michael@0: CKM_DSA, NULL, 0 michael@0: }; michael@0: michael@0: /*** DSA Key ***/ michael@0: michael@0: CK_MECHANISM dsaKeyPairGenMech; michael@0: CK_ATTRIBUTE dsaPubKeyTemplate[5]; michael@0: CK_ATTRIBUTE dsaPrivKeyTemplate[5]; michael@0: CK_OBJECT_HANDLE hDSApubKey = CK_INVALID_HANDLE; michael@0: CK_OBJECT_HANDLE hDSAprivKey = CK_INVALID_HANDLE; michael@0: michael@0: CK_BYTE dsaPubKey[384]; michael@0: CK_ATTRIBUTE dsaPubKeyValue; michael@0: michael@0: michael@0: program_name = strrchr(argv[0], '/'); michael@0: program_name = program_name ? (program_name + 1) : argv[0]; michael@0: optstate = PL_CreateOptState (argc, argv, "i:o:f:Fd:hH?k:p:P:vVs:"); michael@0: if (optstate == NULL) { michael@0: lperror("PL_CreateOptState failed"); michael@0: return 1; michael@0: } michael@0: michael@0: while (PL_GetNextOpt (optstate) == PL_OPT_OK) { michael@0: switch (optstate->option) { michael@0: michael@0: case 'd': michael@0: if (!optstate->value) { michael@0: PL_DestroyOptState(optstate); michael@0: usage(program_name); michael@0: } michael@0: configDir = PL_strdup(optstate->value); michael@0: checkPath(configDir); michael@0: break; michael@0: michael@0: case 'i': michael@0: if (!optstate->value) { michael@0: PL_DestroyOptState(optstate); michael@0: usage(program_name); michael@0: } michael@0: input_file = optstate->value; michael@0: break; michael@0: michael@0: case 'o': michael@0: if (!optstate->value) { michael@0: PL_DestroyOptState(optstate); michael@0: usage(program_name); michael@0: } michael@0: output_file = PL_strdup(optstate->value); michael@0: break; michael@0: michael@0: case 'k': michael@0: if (!optstate->value) { michael@0: PL_DestroyOptState(optstate); michael@0: usage(program_name); michael@0: } michael@0: keySize = atoi(optstate->value); michael@0: break; michael@0: michael@0: case 'f': michael@0: if (!optstate->value) { michael@0: PL_DestroyOptState(optstate); michael@0: usage(program_name); michael@0: } michael@0: pwd = filePasswd((char *)optstate->value); michael@0: if (!pwd) usage(program_name); michael@0: break; michael@0: michael@0: case 'F': michael@0: FIPSMODE = PR_TRUE; michael@0: break; michael@0: michael@0: case 'p': michael@0: if (!optstate->value) { michael@0: PL_DestroyOptState(optstate); michael@0: usage(program_name); michael@0: } michael@0: pwd = PL_strdup(optstate->value); michael@0: break; michael@0: michael@0: case 'P': michael@0: if (!optstate->value) { michael@0: PL_DestroyOptState(optstate); michael@0: usage(program_name); michael@0: } michael@0: dbPrefix = PL_strdup(optstate->value); michael@0: break; michael@0: michael@0: case 'v': michael@0: verbose = PR_TRUE; michael@0: break; michael@0: michael@0: case 'V': michael@0: verify = PR_TRUE; michael@0: break; michael@0: michael@0: case 'H': michael@0: PL_DestroyOptState(optstate); michael@0: long_usage (program_name); michael@0: return 1; michael@0: break; michael@0: michael@0: case 'h': michael@0: case '?': michael@0: default: michael@0: PL_DestroyOptState(optstate); michael@0: usage(program_name); michael@0: return 1; michael@0: break; michael@0: } michael@0: } michael@0: PL_DestroyOptState(optstate); michael@0: michael@0: if (!input_file) { michael@0: usage(program_name); michael@0: return 1; michael@0: } michael@0: michael@0: /* Get the platform-dependent library name of the michael@0: * NSS cryptographic module. michael@0: */ michael@0: libname = PR_GetLibraryName(NULL, "softokn3"); michael@0: assert(libname != NULL); michael@0: lib = PR_LoadLibrary(libname); michael@0: assert(lib != NULL); michael@0: PR_FreeLibraryName(libname); michael@0: michael@0: michael@0: if (FIPSMODE) { michael@0: /* FIPSMODE == FC_GetFunctionList */ michael@0: /* library path must be set to an already signed softokn3/freebl */ michael@0: pC_GetFunctionList = (CK_C_GetFunctionList) michael@0: PR_FindFunctionSymbol(lib, "FC_GetFunctionList"); michael@0: } else { michael@0: /* NON FIPS mode == C_GetFunctionList */ michael@0: pC_GetFunctionList = (CK_C_GetFunctionList) michael@0: PR_FindFunctionSymbol(lib, "C_GetFunctionList"); michael@0: } michael@0: assert(pC_GetFunctionList != NULL); michael@0: michael@0: crv = (*pC_GetFunctionList)(&pFunctionList); michael@0: assert(crv == CKR_OK); michael@0: michael@0: if (configDir) { michael@0: if (!dbPrefix) { michael@0: dbPrefix = PL_strdup(""); michael@0: } michael@0: crv = softokn_Init(pFunctionList, configDir, dbPrefix); michael@0: if (crv != CKR_OK) { michael@0: logIt("Failed to use provided database directory " michael@0: "will just initialize the volatile certdb.\n"); michael@0: crv = softokn_Init(pFunctionList, NULL, NULL); /* NoDB Init */ michael@0: } michael@0: } else { michael@0: crv = softokn_Init(pFunctionList, NULL, NULL); /* NoDB Init */ michael@0: } michael@0: michael@0: if (crv != CKR_OK) { michael@0: pk11error( "Initiailzing softoken failed", crv); michael@0: goto cleanup; michael@0: } michael@0: michael@0: pSlotList = getSlotList(pFunctionList, slotIndex); michael@0: if (pSlotList == NULL) { michael@0: PR_fprintf(PR_STDERR, "getSlotList failed"); michael@0: goto cleanup; michael@0: } michael@0: michael@0: if ((keySize == 0) || (keySize > 1024)) { michael@0: CK_MECHANISM_INFO mechInfo; michael@0: crv = pFunctionList->C_GetMechanismInfo(pSlotList[slotIndex], michael@0: CKM_DSA, &mechInfo); michael@0: if (crv != CKR_OK) { michael@0: pk11error( "Couldn't get mechanism info for DSA", crv); michael@0: goto cleanup; michael@0: } michael@0: michael@0: if (keySize && (mechInfo.ulMaxKeySize < keySize)) { michael@0: PR_fprintf(PR_STDERR, michael@0: "token doesn't support DSA2 (Max key size=%d)\n", michael@0: mechInfo.ulMaxKeySize); michael@0: goto cleanup; michael@0: } michael@0: michael@0: if ((keySize == 0) && mechInfo.ulMaxKeySize >=2048 ) { michael@0: keySize = 2048; michael@0: } else { michael@0: keySize = 1024; michael@0: } michael@0: } michael@0: michael@0: /* DSA key init */ michael@0: if (keySize == 1024) { michael@0: dsaPubKeyTemplate[0].type = CKA_PRIME; michael@0: dsaPubKeyTemplate[0].pValue = (CK_VOID_PTR) ′ michael@0: dsaPubKeyTemplate[0].ulValueLen = sizeof(prime); michael@0: dsaPubKeyTemplate[1].type = CKA_SUBPRIME; michael@0: dsaPubKeyTemplate[1].pValue = (CK_VOID_PTR) &subprime; michael@0: dsaPubKeyTemplate[1].ulValueLen = sizeof(subprime); michael@0: dsaPubKeyTemplate[2].type = CKA_BASE; michael@0: dsaPubKeyTemplate[2].pValue = (CK_VOID_PTR) &base; michael@0: dsaPubKeyTemplate[2].ulValueLen = sizeof(base); michael@0: digestmech.mechanism = CKM_SHA_1; michael@0: digestmech.pParameter = NULL; michael@0: digestmech.ulParameterLen = 0; michael@0: } else if (keySize == 2048) { michael@0: dsaPubKeyTemplate[0].type = CKA_PRIME; michael@0: dsaPubKeyTemplate[0].pValue = (CK_VOID_PTR) &prime2; michael@0: dsaPubKeyTemplate[0].ulValueLen = sizeof(prime2); michael@0: dsaPubKeyTemplate[1].type = CKA_SUBPRIME; michael@0: dsaPubKeyTemplate[1].pValue = (CK_VOID_PTR) &subprime2; michael@0: dsaPubKeyTemplate[1].ulValueLen = sizeof(subprime2); michael@0: dsaPubKeyTemplate[2].type = CKA_BASE; michael@0: dsaPubKeyTemplate[2].pValue = (CK_VOID_PTR) &base2; michael@0: dsaPubKeyTemplate[2].ulValueLen = sizeof(base2); michael@0: digestmech.mechanism = CKM_SHA256; michael@0: digestmech.pParameter = NULL; michael@0: digestmech.ulParameterLen = 0; michael@0: } else { michael@0: /* future - generate pqg */ michael@0: PR_fprintf(PR_STDERR, "Only keysizes 1024 and 2048 are supported"); michael@0: goto cleanup; michael@0: } michael@0: dsaPubKeyTemplate[3].type = CKA_TOKEN; michael@0: dsaPubKeyTemplate[3].pValue = &false; /* session object */ michael@0: dsaPubKeyTemplate[3].ulValueLen = sizeof(false); michael@0: dsaPubKeyTemplate[4].type = CKA_VERIFY; michael@0: dsaPubKeyTemplate[4].pValue = &true; michael@0: dsaPubKeyTemplate[4].ulValueLen = sizeof(true); michael@0: dsaKeyPairGenMech.mechanism = CKM_DSA_KEY_PAIR_GEN; michael@0: dsaKeyPairGenMech.pParameter = NULL; michael@0: dsaKeyPairGenMech.ulParameterLen = 0; michael@0: dsaPrivKeyTemplate[0].type = CKA_TOKEN; michael@0: dsaPrivKeyTemplate[0].pValue = &false; /* session object */ michael@0: dsaPrivKeyTemplate[0].ulValueLen = sizeof(false); michael@0: dsaPrivKeyTemplate[1].type = CKA_PRIVATE; michael@0: dsaPrivKeyTemplate[1].pValue = &true; michael@0: dsaPrivKeyTemplate[1].ulValueLen = sizeof(true); michael@0: dsaPrivKeyTemplate[2].type = CKA_SENSITIVE; michael@0: dsaPrivKeyTemplate[2].pValue = &true; michael@0: dsaPrivKeyTemplate[2].ulValueLen = sizeof(true); michael@0: dsaPrivKeyTemplate[3].type = CKA_SIGN, michael@0: dsaPrivKeyTemplate[3].pValue = &true; michael@0: dsaPrivKeyTemplate[3].ulValueLen = sizeof(true); michael@0: dsaPrivKeyTemplate[4].type = CKA_EXTRACTABLE; michael@0: dsaPrivKeyTemplate[4].pValue = &false; michael@0: dsaPrivKeyTemplate[4].ulValueLen = sizeof(false); michael@0: michael@0: crv = pFunctionList->C_OpenSession(pSlotList[slotIndex], michael@0: CKF_RW_SESSION | CKF_SERIAL_SESSION, michael@0: NULL, NULL, &hRwSession); michael@0: if (crv != CKR_OK) { michael@0: pk11error( "Opening a read/write session failed", crv); michael@0: goto cleanup; michael@0: } michael@0: michael@0: /* check if a password is needed */ michael@0: crv = pFunctionList->C_GetTokenInfo(pSlotList[slotIndex], &tokenInfo); michael@0: if (crv != CKR_OK) { michael@0: pk11error( "C_GetTokenInfo failed", crv); michael@0: goto cleanup; michael@0: } michael@0: if (tokenInfo.flags & CKF_LOGIN_REQUIRED) { michael@0: if (pwd) { michael@0: int pwdLen = strlen((const char*)pwd); michael@0: crv = pFunctionList->C_Login(hRwSession, CKU_USER, michael@0: (CK_UTF8CHAR_PTR) pwd, (CK_ULONG)pwdLen); michael@0: if (crv != CKR_OK) { michael@0: pk11error("C_Login failed", crv); michael@0: goto cleanup; michael@0: } michael@0: } else { michael@0: PR_fprintf(PR_STDERR, "Please provide the password for the token"); michael@0: goto cleanup; michael@0: } michael@0: } else if (pwd) { michael@0: logIt("A password was provided but the password was not used.\n"); michael@0: } michael@0: michael@0: /* Generate a DSA key pair */ michael@0: logIt("Generate a DSA key pair ... \n"); michael@0: crv = pFunctionList->C_GenerateKeyPair(hRwSession, &dsaKeyPairGenMech, michael@0: dsaPubKeyTemplate, michael@0: NUM_ELEM(dsaPubKeyTemplate), michael@0: dsaPrivKeyTemplate, michael@0: NUM_ELEM(dsaPrivKeyTemplate), michael@0: &hDSApubKey, &hDSAprivKey); michael@0: if (crv != CKR_OK) { michael@0: pk11error("DSA key pair generation failed", crv); michael@0: goto cleanup; michael@0: } michael@0: michael@0: /* open the shared library */ michael@0: fd = PR_OpenFile(input_file,PR_RDONLY,0); michael@0: if (fd == NULL ) { michael@0: lperror(input_file); michael@0: goto cleanup; michael@0: } michael@0: #ifdef USES_LINKS michael@0: ret = lstat(input_file, &stat_buf); michael@0: if (ret < 0) { michael@0: perror(input_file); michael@0: goto cleanup; michael@0: } michael@0: if (S_ISLNK(stat_buf.st_mode)) { michael@0: char *dirpath,*dirend; michael@0: ret = readlink(input_file, link_buf, sizeof(link_buf) - 1); michael@0: if (ret < 0) { michael@0: perror(input_file); michael@0: goto cleanup; michael@0: } michael@0: link_buf[ret] = 0; michael@0: link_file = mkoutput(input_file); michael@0: /* get the dirname of input_file */ michael@0: dirpath = PL_strdup(input_file); michael@0: dirend = strrchr(dirpath, '/'); michael@0: if (dirend) { michael@0: *dirend = '\0'; michael@0: ret = chdir(dirpath); michael@0: if (ret < 0) { michael@0: perror(dirpath); michael@0: goto cleanup; michael@0: } michael@0: } michael@0: PL_strfree(dirpath); michael@0: input_file = link_buf; michael@0: /* get the basename of link_file */ michael@0: dirend = strrchr(link_file, '/'); michael@0: if (dirend) { michael@0: char * tmp_file = NULL; michael@0: tmp_file = PL_strdup(dirend +1 ); michael@0: PL_strfree(link_file); michael@0: link_file = tmp_file; michael@0: } michael@0: } michael@0: #endif michael@0: if (output_file == NULL) { michael@0: output_file = mkoutput(input_file); michael@0: } michael@0: michael@0: /* compute the digest */ michael@0: memset(digest, 0, sizeof(digest)); michael@0: crv = pFunctionList->C_DigestInit(hRwSession, &digestmech); michael@0: if (crv != CKR_OK) { michael@0: pk11error("C_DigestInit failed", crv); michael@0: goto cleanup; michael@0: } michael@0: michael@0: /* Digest the file */ michael@0: while ((bytesRead = PR_Read(fd,file_buf,sizeof(file_buf))) > 0) { michael@0: crv = pFunctionList->C_DigestUpdate(hRwSession, (CK_BYTE_PTR)file_buf, michael@0: bytesRead); michael@0: if (crv != CKR_OK) { michael@0: pk11error("C_DigestUpdate failed", crv); michael@0: goto cleanup; michael@0: } michael@0: count += bytesRead; michael@0: } michael@0: michael@0: /* close the input_File */ michael@0: PR_Close(fd); michael@0: fd = NULL; michael@0: if (bytesRead < 0) { michael@0: lperror("0 bytes read from input file"); michael@0: goto cleanup; michael@0: } michael@0: michael@0: digestLen = sizeof(digest); michael@0: crv = pFunctionList->C_DigestFinal(hRwSession, (CK_BYTE_PTR)digest, michael@0: &digestLen); michael@0: if (crv != CKR_OK) { michael@0: pk11error("C_DigestFinal failed", crv); michael@0: goto cleanup; michael@0: } michael@0: michael@0: if (digestLen != sizeof(digest)) { michael@0: PR_fprintf(PR_STDERR, "digestLen has incorrect length %lu " michael@0: "it should be %lu \n",digestLen, sizeof(digest)); michael@0: goto cleanup; michael@0: } michael@0: michael@0: /* sign the hash */ michael@0: memset(sign, 0, sizeof(sign)); michael@0: /* SignUpdate */ michael@0: crv = pFunctionList->C_SignInit(hRwSession, &signMech, hDSAprivKey); michael@0: if (crv != CKR_OK) { michael@0: pk11error("C_SignInit failed", crv); michael@0: goto cleanup; michael@0: } michael@0: michael@0: signLen = sizeof(sign); michael@0: crv = pFunctionList->C_Sign(hRwSession, (CK_BYTE * ) digest, digestLen, michael@0: sign, &signLen); michael@0: if (crv != CKR_OK) { michael@0: pk11error("C_Sign failed", crv); michael@0: goto cleanup; michael@0: } michael@0: michael@0: if (signLen != sizeof(sign)) { michael@0: PR_fprintf(PR_STDERR, "signLen has incorrect length %lu " michael@0: "it should be %lu \n", signLen, sizeof(sign)); michael@0: goto cleanup; michael@0: } michael@0: michael@0: if (verify) { michael@0: crv = pFunctionList->C_VerifyInit(hRwSession, &signMech, hDSApubKey); michael@0: if (crv != CKR_OK) { michael@0: pk11error("C_VerifyInit failed", crv); michael@0: goto cleanup; michael@0: } michael@0: crv = pFunctionList->C_Verify(hRwSession, digest, digestLen, michael@0: sign, signLen); michael@0: if (crv != CKR_OK) { michael@0: pk11error("C_Verify failed", crv); michael@0: goto cleanup; michael@0: } michael@0: } michael@0: michael@0: if (verbose) { michael@0: int j; michael@0: PR_fprintf(PR_STDERR,"Library File: %s %d bytes\n",input_file, count); michael@0: PR_fprintf(PR_STDERR,"Check File: %s\n",output_file); michael@0: #ifdef USES_LINKS michael@0: if (link_file) { michael@0: PR_fprintf(PR_STDERR,"Link: %s\n",link_file); michael@0: } michael@0: #endif michael@0: PR_fprintf(PR_STDERR," hash: %lu bytes\n", digestLen); michael@0: #define STEP 10 michael@0: for (i=0; i < (int) digestLen; i += STEP) { michael@0: PR_fprintf(PR_STDERR," "); michael@0: for (j=0; j < STEP && (i+j) < (int) digestLen; j++) { michael@0: PR_fprintf(PR_STDERR," %02x", digest[i+j]); michael@0: } michael@0: PR_fprintf(PR_STDERR,"\n"); michael@0: } michael@0: PR_fprintf(PR_STDERR," signature: %lu bytes\n", signLen); michael@0: for (i=0; i < (int) signLen; i += STEP) { michael@0: PR_fprintf(PR_STDERR," "); michael@0: for (j=0; j < STEP && (i+j) < (int) signLen; j++) { michael@0: PR_fprintf(PR_STDERR," %02x", sign[i+j]); michael@0: } michael@0: PR_fprintf(PR_STDERR,"\n"); michael@0: } michael@0: } michael@0: michael@0: /* open the target signature file */ michael@0: fd = PR_Open(output_file,PR_WRONLY|PR_CREATE_FILE|PR_TRUNCATE,0666); michael@0: if (fd == NULL ) { michael@0: lperror(output_file); michael@0: goto cleanup; michael@0: } michael@0: michael@0: /* michael@0: * we write the key out in a straight binary format because very michael@0: * low level libraries need to read an parse this file. Ideally we should michael@0: * just derEncode the public key (which would be pretty simple, and be michael@0: * more general), but then we'd need to link the ASN.1 decoder with the michael@0: * freebl libraries. michael@0: */ michael@0: michael@0: file_buf[0] = NSS_SIGN_CHK_MAGIC1; michael@0: file_buf[1] = NSS_SIGN_CHK_MAGIC2; michael@0: file_buf[2] = NSS_SIGN_CHK_MAJOR_VERSION; michael@0: file_buf[3] = NSS_SIGN_CHK_MINOR_VERSION; michael@0: encodeInt(&file_buf[4],12); /* offset to data start */ michael@0: encodeInt(&file_buf[8],CKK_DSA); michael@0: bytesWritten = PR_Write(fd,file_buf, 12); michael@0: if (bytesWritten != 12) { michael@0: lperror(output_file); michael@0: goto cleanup; michael@0: } michael@0: michael@0: /* get DSA Public KeyValue */ michael@0: memset(dsaPubKey, 0, sizeof(dsaPubKey)); michael@0: dsaPubKeyValue.type =CKA_VALUE; michael@0: dsaPubKeyValue.pValue = (CK_VOID_PTR) &dsaPubKey; michael@0: dsaPubKeyValue.ulValueLen = sizeof(dsaPubKey); michael@0: michael@0: crv = pFunctionList->C_GetAttributeValue(hRwSession, hDSApubKey, michael@0: &dsaPubKeyValue, 1); michael@0: if (crv != CKR_OK && crv != CKR_ATTRIBUTE_TYPE_INVALID) { michael@0: pk11error("C_GetAttributeValue failed", crv); michael@0: goto cleanup; michael@0: } michael@0: michael@0: /* CKA_PRIME */ michael@0: rv = writeItem(fd,dsaPubKeyTemplate[0].pValue, michael@0: dsaPubKeyTemplate[0].ulValueLen, output_file); michael@0: if (rv != PR_SUCCESS) goto cleanup; michael@0: /* CKA_SUBPRIME */ michael@0: rv = writeItem(fd,dsaPubKeyTemplate[1].pValue, michael@0: dsaPubKeyTemplate[1].ulValueLen, output_file); michael@0: if (rv != PR_SUCCESS) goto cleanup; michael@0: /* CKA_BASE */ michael@0: rv = writeItem(fd,dsaPubKeyTemplate[2].pValue, michael@0: dsaPubKeyTemplate[2].ulValueLen, output_file); michael@0: if (rv != PR_SUCCESS) goto cleanup; michael@0: /* DSA Public Key value */ michael@0: rv = writeItem(fd,dsaPubKeyValue.pValue, michael@0: dsaPubKeyValue.ulValueLen, output_file); michael@0: if (rv != PR_SUCCESS) goto cleanup; michael@0: /* DSA SIGNATURE */ michael@0: rv = writeItem(fd,&sign, signLen, output_file); michael@0: if (rv != PR_SUCCESS) goto cleanup; michael@0: PR_Close(fd); michael@0: michael@0: #ifdef USES_LINKS michael@0: if (link_file) { michael@0: (void)unlink(link_file); michael@0: ret = symlink(output_file, link_file); michael@0: if (ret < 0) { michael@0: perror(link_file); michael@0: goto cleanup; michael@0: } michael@0: } michael@0: #endif michael@0: michael@0: successful = PR_TRUE; michael@0: michael@0: cleanup: michael@0: if (pFunctionList) { michael@0: /* C_Finalize will automatically logout, close session, */ michael@0: /* and delete the temp objects on the token */ michael@0: crv = pFunctionList->C_Finalize(NULL); michael@0: if (crv != CKR_OK) { michael@0: pk11error("C_Finalize failed", crv); michael@0: } michael@0: } michael@0: if (pSlotList) { michael@0: PR_Free(pSlotList); michael@0: } michael@0: if (pwd) { michael@0: PL_strfree(pwd); michael@0: } michael@0: if (configDir) { michael@0: PL_strfree(configDir); michael@0: } michael@0: if (dbPrefix) { michael@0: PL_strfree(dbPrefix); michael@0: } michael@0: if (output_file) { /* allocated by mkoutput function */ michael@0: PL_strfree(output_file); michael@0: } michael@0: #ifdef USES_LINKS michael@0: if (link_file) { /* allocated by mkoutput function */ michael@0: PL_strfree(link_file); michael@0: } michael@0: #endif michael@0: michael@0: disableUnload = PR_GetEnv("NSS_DISABLE_UNLOAD"); michael@0: if (!disableUnload) { michael@0: PR_UnloadLibrary(lib); michael@0: } michael@0: PR_Cleanup(); michael@0: michael@0: if (crv != CKR_OK) michael@0: return crv; michael@0: michael@0: return (successful) ? 0 : 1; michael@0: }