2018-05-04 16:08:28 +02:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2015-10-21 05:03:22 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Pretty-print some well-known BER or DER encoded data (e.g. certificates,
|
|
|
|
* keys, pkcs7)
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "secutil.h"
|
|
|
|
|
|
|
|
#if defined(__sun) && !defined(SVR4)
|
|
|
|
extern int fprintf(FILE *, char *, ...);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "plgetopt.h"
|
|
|
|
|
|
|
|
#include "pk11func.h"
|
|
|
|
#include "nspr.h"
|
|
|
|
#include "nss.h"
|
|
|
|
|
|
|
|
static void Usage(char *progName)
|
|
|
|
{
|
|
|
|
fprintf(stderr,
|
cherry-picked mozilla NSS upstream changes (to rev b07697c94038, which is on par with 3.16.2):
bug753136, bug999893, bug1011090, bug1009785, bug1009794, bug421391, bug1011229, bug1013088, bug996237, bug970539, bug1016567, bug485732, bug334013, bug959864, bug1016836, bug1016811, bug1018536, bug996250, bug1009227, bug963150, bug1007126, bug952572, bug1021102, bug1020395, bug902171
2018-07-11 14:39:02 +02:00
|
|
|
"Usage: %s [-t type] [-a] [-i input] [-o output] [-w] [-u]\n",
|
2015-10-21 05:03:22 +02:00
|
|
|
progName);
|
cherry-picked mozilla NSS upstream changes (to rev b07697c94038, which is on par with 3.16.2):
bug753136, bug999893, bug1011090, bug1009785, bug1009794, bug421391, bug1011229, bug1013088, bug996237, bug970539, bug1016567, bug485732, bug334013, bug959864, bug1016836, bug1016811, bug1018536, bug996250, bug1009227, bug963150, bug1007126, bug952572, bug1021102, bug1020395, bug902171
2018-07-11 14:39:02 +02:00
|
|
|
fprintf(stderr, "Pretty prints a file containing ASN.1 data in DER or ascii format.\n");
|
cherry-picked mozilla NSS upstream changes (to rev bad5fd065fa1, which is on par with 3.20):
bug1001332, 56b691c003ad, bug1086145, bug1054069, bug1155922, bug991783, bug1125025, bug1162521, bug1162644, bug1132941, bug1164364, bug1166205, bug1166163, bug1166515, bug1138554, bug1167046, bug1167043, bug1169451, bug1172128, bug1170322, bug102794, bug1128184, bug557830, bug1174648, bug1180244, bug1177784, bug1173413, bug1169174, bug1084669, bug951455, bug1183395, bug1177430, bug1183827, bug1160139, bug1154106, bug1142209, bug1185033, bug1193467, bug1182667(with sha512 changes backed out, which breaks VC6 compilation), bug1158489, bug337796
2018-07-12 15:44:51 +02:00
|
|
|
fprintf(stderr, "%-14s Specify input and display type:", "-t type");
|
|
|
|
#ifdef HAVE_EPV_TEMPLATE
|
|
|
|
fprintf(stderr, " %s (sk),", SEC_CT_PRIVATE_KEY);
|
|
|
|
#endif
|
|
|
|
fprintf(stderr, "\n");
|
cherry-picked mozilla NSS upstream changes (to rev b07697c94038, which is on par with 3.16.2):
bug753136, bug999893, bug1011090, bug1009785, bug1009794, bug421391, bug1011229, bug1013088, bug996237, bug970539, bug1016567, bug485732, bug334013, bug959864, bug1016836, bug1016811, bug1018536, bug996250, bug1009227, bug963150, bug1007126, bug952572, bug1021102, bug1020395, bug902171
2018-07-11 14:39:02 +02:00
|
|
|
fprintf(stderr, "%-14s %s (pk), %s (c), %s (cr),\n", "", SEC_CT_PUBLIC_KEY,
|
2015-10-21 05:03:22 +02:00
|
|
|
SEC_CT_CERTIFICATE, SEC_CT_CERTIFICATE_REQUEST);
|
cherry-picked mozilla NSS upstream changes (to rev b07697c94038, which is on par with 3.16.2):
bug753136, bug999893, bug1011090, bug1009785, bug1009794, bug421391, bug1011229, bug1013088, bug996237, bug970539, bug1016567, bug485732, bug334013, bug959864, bug1016836, bug1016811, bug1018536, bug996250, bug1009227, bug963150, bug1007126, bug952572, bug1021102, bug1020395, bug902171
2018-07-11 14:39:02 +02:00
|
|
|
fprintf(stderr, "%-14s %s (ci), %s (p7), %s or %s (n).\n", "", SEC_CT_CERTIFICATE_ID,
|
2018-05-04 16:08:28 +02:00
|
|
|
SEC_CT_PKCS7, SEC_CT_CRL, SEC_CT_NAME);
|
cherry-picked mozilla NSS upstream changes (to rev 82de44ead36f, which is on par with 3.18):
bug1095307, bug1073330(backout), bug1084986, bug1050069, bug942172, bug1054547, bug532081, bug1096348, bug1058870, bug1093940, bug1102985, bug1112461, bug1094492, bug112029, bug1119983, bug1120685, bug1120691, bug1113632, bug863076, bug1082973, bug1124539, bug1117617, bug1117621, bug1121273, bug753136, bug921684, bug1132818, bug1125375, bug647690, bug1055441, bug1134455, bug975010, bug950369, bug1128367, bug1129573, bug1136095, bug1117897, bug1113453, bug1061725, bug1073330, bug1111901, bug1083900, bug1136095, bug1138820, bug1096741, bug1134548, bug345725, bug950348, bug950344, bug1151037, bug991783, bug1153994
2018-07-11 16:42:30 +02:00
|
|
|
fprintf(stderr, "%-14s (Use either the long type name or the shortcut.)\n", "");
|
cherry-picked mozilla NSS upstream changes (to rev b07697c94038, which is on par with 3.16.2):
bug753136, bug999893, bug1011090, bug1009785, bug1009794, bug421391, bug1011229, bug1013088, bug996237, bug970539, bug1016567, bug485732, bug334013, bug959864, bug1016836, bug1016811, bug1018536, bug996250, bug1009227, bug963150, bug1007126, bug952572, bug1021102, bug1020395, bug902171
2018-07-11 14:39:02 +02:00
|
|
|
fprintf(stderr, "%-14s Input is in ascii encoded form (RFC1113)\n",
|
2015-10-21 05:03:22 +02:00
|
|
|
"-a");
|
cherry-picked mozilla NSS upstream changes (to rev b07697c94038, which is on par with 3.16.2):
bug753136, bug999893, bug1011090, bug1009785, bug1009794, bug421391, bug1011229, bug1013088, bug996237, bug970539, bug1016567, bug485732, bug334013, bug959864, bug1016836, bug1016811, bug1018536, bug996250, bug1009227, bug963150, bug1007126, bug952572, bug1021102, bug1020395, bug902171
2018-07-11 14:39:02 +02:00
|
|
|
fprintf(stderr, "%-14s Define an input file to use (default is stdin)\n",
|
2015-10-21 05:03:22 +02:00
|
|
|
"-i input");
|
cherry-picked mozilla NSS upstream changes (to rev b07697c94038, which is on par with 3.16.2):
bug753136, bug999893, bug1011090, bug1009785, bug1009794, bug421391, bug1011229, bug1013088, bug996237, bug970539, bug1016567, bug485732, bug334013, bug959864, bug1016836, bug1016811, bug1018536, bug996250, bug1009227, bug963150, bug1007126, bug952572, bug1021102, bug1020395, bug902171
2018-07-11 14:39:02 +02:00
|
|
|
fprintf(stderr, "%-14s Define an output file to use (default is stdout)\n",
|
2015-10-21 05:03:22 +02:00
|
|
|
"-o output");
|
cherry-picked mozilla NSS upstream changes (to rev b07697c94038, which is on par with 3.16.2):
bug753136, bug999893, bug1011090, bug1009785, bug1009794, bug421391, bug1011229, bug1013088, bug996237, bug970539, bug1016567, bug485732, bug334013, bug959864, bug1016836, bug1016811, bug1018536, bug996250, bug1009227, bug963150, bug1007126, bug952572, bug1021102, bug1020395, bug902171
2018-07-11 14:39:02 +02:00
|
|
|
fprintf(stderr, "%-14s Don't wrap long output lines\n",
|
2018-05-04 16:08:28 +02:00
|
|
|
"-w");
|
cherry-picked mozilla NSS upstream changes (to rev b07697c94038, which is on par with 3.16.2):
bug753136, bug999893, bug1011090, bug1009785, bug1009794, bug421391, bug1011229, bug1013088, bug996237, bug970539, bug1016567, bug485732, bug334013, bug959864, bug1016836, bug1016811, bug1018536, bug996250, bug1009227, bug963150, bug1007126, bug952572, bug1021102, bug1020395, bug902171
2018-07-11 14:39:02 +02:00
|
|
|
fprintf(stderr, "%-14s Use UTF-8 (default is to show non-ascii as .)\n",
|
|
|
|
"-u");
|
2015-10-21 05:03:22 +02:00
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
int rv, ascii;
|
|
|
|
char *progName;
|
|
|
|
FILE *outFile;
|
|
|
|
PRFileDesc *inFile;
|
|
|
|
SECItem der, data;
|
|
|
|
char *typeTag;
|
|
|
|
PLOptState *optstate;
|
2018-05-04 16:08:28 +02:00
|
|
|
PRBool wrap = PR_TRUE;
|
2015-10-21 05:03:22 +02:00
|
|
|
|
|
|
|
progName = strrchr(argv[0], '/');
|
|
|
|
progName = progName ? progName+1 : argv[0];
|
|
|
|
|
|
|
|
ascii = 0;
|
|
|
|
inFile = 0;
|
|
|
|
outFile = 0;
|
|
|
|
typeTag = 0;
|
cherry-picked mozilla NSS upstream changes (to rev b07697c94038, which is on par with 3.16.2):
bug753136, bug999893, bug1011090, bug1009785, bug1009794, bug421391, bug1011229, bug1013088, bug996237, bug970539, bug1016567, bug485732, bug334013, bug959864, bug1016836, bug1016811, bug1018536, bug996250, bug1009227, bug963150, bug1007126, bug952572, bug1021102, bug1020395, bug902171
2018-07-11 14:39:02 +02:00
|
|
|
optstate = PL_CreateOptState(argc, argv, "at:i:o:uw");
|
2015-10-21 05:03:22 +02:00
|
|
|
while ( PL_GetNextOpt(optstate) == PL_OPT_OK ) {
|
|
|
|
switch (optstate->option) {
|
|
|
|
case '?':
|
|
|
|
Usage(progName);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'a':
|
|
|
|
ascii = 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'i':
|
|
|
|
inFile = PR_Open(optstate->value, PR_RDONLY, 0);
|
|
|
|
if (!inFile) {
|
|
|
|
fprintf(stderr, "%s: unable to open \"%s\" for reading\n",
|
|
|
|
progName, optstate->value);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'o':
|
|
|
|
outFile = fopen(optstate->value, "w");
|
|
|
|
if (!outFile) {
|
|
|
|
fprintf(stderr, "%s: unable to open \"%s\" for writing\n",
|
|
|
|
progName, optstate->value);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 't':
|
|
|
|
typeTag = strdup(optstate->value);
|
|
|
|
break;
|
2018-05-04 16:08:28 +02:00
|
|
|
|
cherry-picked mozilla NSS upstream changes (to rev b07697c94038, which is on par with 3.16.2):
bug753136, bug999893, bug1011090, bug1009785, bug1009794, bug421391, bug1011229, bug1013088, bug996237, bug970539, bug1016567, bug485732, bug334013, bug959864, bug1016836, bug1016811, bug1018536, bug996250, bug1009227, bug963150, bug1007126, bug952572, bug1021102, bug1020395, bug902171
2018-07-11 14:39:02 +02:00
|
|
|
case 'u':
|
|
|
|
SECU_EnableUtf8Display(PR_TRUE);
|
|
|
|
break;
|
|
|
|
|
2018-05-04 16:08:28 +02:00
|
|
|
case 'w':
|
|
|
|
wrap = PR_FALSE;
|
|
|
|
break;
|
2015-10-21 05:03:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
PL_DestroyOptState(optstate);
|
|
|
|
if (!typeTag) Usage(progName);
|
|
|
|
|
|
|
|
if (!inFile) inFile = PR_STDIN;
|
|
|
|
if (!outFile) outFile = stdout;
|
|
|
|
|
|
|
|
PR_Init(PR_SYSTEM_THREAD, PR_PRIORITY_NORMAL, 1);
|
|
|
|
rv = NSS_NoDB_Init(NULL);
|
|
|
|
if (rv != SECSuccess) {
|
|
|
|
fprintf(stderr, "%s: NSS_NoDB_Init failed (%s)\n",
|
|
|
|
progName, SECU_Strerror(PORT_GetError()));
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
SECU_RegisterDynamicOids();
|
|
|
|
|
2018-05-04 16:08:28 +02:00
|
|
|
rv = SECU_ReadDERFromFile(&der, inFile, ascii, PR_FALSE);
|
2015-10-21 05:03:22 +02:00
|
|
|
if (rv != SECSuccess) {
|
|
|
|
fprintf(stderr, "%s: SECU_ReadDERFromFile failed\n", progName);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Data is untyped, using the specified type */
|
|
|
|
data.data = der.data;
|
|
|
|
data.len = der.len;
|
|
|
|
|
2018-05-04 16:08:28 +02:00
|
|
|
SECU_EnableWrap(wrap);
|
|
|
|
|
2015-10-21 05:03:22 +02:00
|
|
|
/* Pretty print it */
|
cherry-picked mozilla NSS upstream changes (to rev b07697c94038, which is on par with 3.16.2):
bug753136, bug999893, bug1011090, bug1009785, bug1009794, bug421391, bug1011229, bug1013088, bug996237, bug970539, bug1016567, bug485732, bug334013, bug959864, bug1016836, bug1016811, bug1018536, bug996250, bug1009227, bug963150, bug1007126, bug952572, bug1021102, bug1020395, bug902171
2018-07-11 14:39:02 +02:00
|
|
|
if (PORT_Strcmp(typeTag, SEC_CT_CERTIFICATE) == 0 ||
|
|
|
|
PORT_Strcmp(typeTag, "c") == 0) {
|
2015-10-21 05:03:22 +02:00
|
|
|
rv = SECU_PrintSignedData(outFile, &data, "Certificate", 0,
|
cherry-picked mozilla NSS upstream changes (to rev bad5fd065fa1, which is on par with 3.20):
bug1001332, 56b691c003ad, bug1086145, bug1054069, bug1155922, bug991783, bug1125025, bug1162521, bug1162644, bug1132941, bug1164364, bug1166205, bug1166163, bug1166515, bug1138554, bug1167046, bug1167043, bug1169451, bug1172128, bug1170322, bug102794, bug1128184, bug557830, bug1174648, bug1180244, bug1177784, bug1173413, bug1169174, bug1084669, bug951455, bug1183395, bug1177430, bug1183827, bug1160139, bug1154106, bug1142209, bug1185033, bug1193467, bug1182667(with sha512 changes backed out, which breaks VC6 compilation), bug1158489, bug337796
2018-07-12 15:44:51 +02:00
|
|
|
(SECU_PPFunc)SECU_PrintCertificate);
|
cherry-picked mozilla NSS upstream changes (to rev b07697c94038, which is on par with 3.16.2):
bug753136, bug999893, bug1011090, bug1009785, bug1009794, bug421391, bug1011229, bug1013088, bug996237, bug970539, bug1016567, bug485732, bug334013, bug959864, bug1016836, bug1016811, bug1018536, bug996250, bug1009227, bug963150, bug1007126, bug952572, bug1021102, bug1020395, bug902171
2018-07-11 14:39:02 +02:00
|
|
|
} else if (PORT_Strcmp(typeTag, SEC_CT_CERTIFICATE_ID) == 0 ||
|
|
|
|
PORT_Strcmp(typeTag, "ci") == 0) {
|
2018-05-04 16:08:28 +02:00
|
|
|
rv = SECU_PrintSignedContent(outFile, &data, 0, 0,
|
|
|
|
SECU_PrintDumpDerIssuerAndSerial);
|
cherry-picked mozilla NSS upstream changes (to rev b07697c94038, which is on par with 3.16.2):
bug753136, bug999893, bug1011090, bug1009785, bug1009794, bug421391, bug1011229, bug1013088, bug996237, bug970539, bug1016567, bug485732, bug334013, bug959864, bug1016836, bug1016811, bug1018536, bug996250, bug1009227, bug963150, bug1007126, bug952572, bug1021102, bug1020395, bug902171
2018-07-11 14:39:02 +02:00
|
|
|
} else if (PORT_Strcmp(typeTag, SEC_CT_CERTIFICATE_REQUEST) == 0 ||
|
|
|
|
PORT_Strcmp(typeTag, "cr") == 0) {
|
2015-10-21 05:03:22 +02:00
|
|
|
rv = SECU_PrintSignedData(outFile, &data, "Certificate Request", 0,
|
|
|
|
SECU_PrintCertificateRequest);
|
cherry-picked mozilla NSS upstream changes (to rev b07697c94038, which is on par with 3.16.2):
bug753136, bug999893, bug1011090, bug1009785, bug1009794, bug421391, bug1011229, bug1013088, bug996237, bug970539, bug1016567, bug485732, bug334013, bug959864, bug1016836, bug1016811, bug1018536, bug996250, bug1009227, bug963150, bug1007126, bug952572, bug1021102, bug1020395, bug902171
2018-07-11 14:39:02 +02:00
|
|
|
} else if (PORT_Strcmp(typeTag, SEC_CT_CRL) == 0) {
|
2015-10-21 05:03:22 +02:00
|
|
|
rv = SECU_PrintSignedData (outFile, &data, "CRL", 0, SECU_PrintCrl);
|
|
|
|
#ifdef HAVE_EPV_TEMPLATE
|
cherry-picked mozilla NSS upstream changes (to rev b07697c94038, which is on par with 3.16.2):
bug753136, bug999893, bug1011090, bug1009785, bug1009794, bug421391, bug1011229, bug1013088, bug996237, bug970539, bug1016567, bug485732, bug334013, bug959864, bug1016836, bug1016811, bug1018536, bug996250, bug1009227, bug963150, bug1007126, bug952572, bug1021102, bug1020395, bug902171
2018-07-11 14:39:02 +02:00
|
|
|
} else if (PORT_Strcmp(typeTag, SEC_CT_PRIVATE_KEY) == 0 ||
|
|
|
|
PORT_Strcmp(typeTag, "sk") == 0) {
|
2015-10-21 05:03:22 +02:00
|
|
|
rv = SECU_PrintPrivateKey(outFile, &data, "Private Key", 0);
|
|
|
|
#endif
|
cherry-picked mozilla NSS upstream changes (to rev b07697c94038, which is on par with 3.16.2):
bug753136, bug999893, bug1011090, bug1009785, bug1009794, bug421391, bug1011229, bug1013088, bug996237, bug970539, bug1016567, bug485732, bug334013, bug959864, bug1016836, bug1016811, bug1018536, bug996250, bug1009227, bug963150, bug1007126, bug952572, bug1021102, bug1020395, bug902171
2018-07-11 14:39:02 +02:00
|
|
|
} else if (PORT_Strcmp(typeTag, SEC_CT_PUBLIC_KEY) == 0 ||
|
|
|
|
PORT_Strcmp (typeTag, "pk") == 0) {
|
2015-10-21 05:03:22 +02:00
|
|
|
rv = SECU_PrintSubjectPublicKeyInfo(outFile, &data, "Public Key", 0);
|
cherry-picked mozilla NSS upstream changes (to rev b07697c94038, which is on par with 3.16.2):
bug753136, bug999893, bug1011090, bug1009785, bug1009794, bug421391, bug1011229, bug1013088, bug996237, bug970539, bug1016567, bug485732, bug334013, bug959864, bug1016836, bug1016811, bug1018536, bug996250, bug1009227, bug963150, bug1007126, bug952572, bug1021102, bug1020395, bug902171
2018-07-11 14:39:02 +02:00
|
|
|
} else if (PORT_Strcmp(typeTag, SEC_CT_PKCS7) == 0 ||
|
|
|
|
PORT_Strcmp (typeTag, "p7") == 0) {
|
2015-10-21 05:03:22 +02:00
|
|
|
rv = SECU_PrintPKCS7ContentInfo(outFile, &data,
|
|
|
|
"PKCS #7 Content Info", 0);
|
cherry-picked mozilla NSS upstream changes (to rev b07697c94038, which is on par with 3.16.2):
bug753136, bug999893, bug1011090, bug1009785, bug1009794, bug421391, bug1011229, bug1013088, bug996237, bug970539, bug1016567, bug485732, bug334013, bug959864, bug1016836, bug1016811, bug1018536, bug996250, bug1009227, bug963150, bug1007126, bug952572, bug1021102, bug1020395, bug902171
2018-07-11 14:39:02 +02:00
|
|
|
} else if (PORT_Strcmp(typeTag, SEC_CT_NAME) == 0 ||
|
|
|
|
PORT_Strcmp (typeTag, "n") == 0) {
|
2018-05-04 16:08:28 +02:00
|
|
|
rv = SECU_PrintDERName(outFile, &data, "Name", 0);
|
2015-10-21 05:03:22 +02:00
|
|
|
} else {
|
|
|
|
fprintf(stderr, "%s: don't know how to print out '%s' files\n",
|
|
|
|
progName, typeTag);
|
|
|
|
SECU_PrintAny(outFile, &data, "File contains", 0);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (inFile != PR_STDIN)
|
|
|
|
PR_Close(inFile);
|
|
|
|
PORT_Free(der.data);
|
|
|
|
if (rv) {
|
|
|
|
fprintf(stderr, "%s: problem converting data (%s)\n",
|
|
|
|
progName, SECU_Strerror(PORT_GetError()));
|
|
|
|
}
|
|
|
|
if (NSS_Shutdown() != SECSuccess) {
|
|
|
|
fprintf(stderr, "%s: NSS_Shutdown failed (%s)\n",
|
|
|
|
progName, SECU_Strerror(PORT_GetError()));
|
|
|
|
rv = SECFailure;
|
|
|
|
}
|
|
|
|
PR_Cleanup();
|
|
|
|
return rv;
|
|
|
|
}
|