security/nss/cmd/ppcertdata/ppcertdata.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/security/nss/cmd/ppcertdata/ppcertdata.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,100 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +#include <stdio.h>
     1.9 +#include <string.h>
    1.10 +#include <ctype.h>
    1.11 +#include <stdlib.h>
    1.12 +#include "secutil.h"
    1.13 +#include "nss.h"
    1.14 +
    1.15 +unsigned char  binary_line[64 * 1024];
    1.16 +
    1.17 +int
    1.18 +main(int argc, const char ** argv)
    1.19 +{
    1.20 +    int            skip_count = 0;
    1.21 +    int            bytes_read;
    1.22 +    char           line[133];
    1.23 +
    1.24 +    if (argc > 1) {
    1.25 +    	skip_count = atoi(argv[1]);
    1.26 +    }
    1.27 +    if (argc > 2 || skip_count < 0) {
    1.28 +        printf("Usage: %s [ skip_columns ] \n", argv[0]);
    1.29 +	return 1;
    1.30 +    }
    1.31 +
    1.32 +    NSS_NoDB_Init(NULL);
    1.33 +
    1.34 +    while (fgets(line, 132, stdin) && (bytes_read = strlen(line)) > 0 ) {
    1.35 +	int    bytes_written;
    1.36 +	char * found;
    1.37 +	char * in          = line       + skip_count; 
    1.38 +	int    left        = bytes_read - skip_count;
    1.39 +	int    is_cert;
    1.40 +	int    is_serial;
    1.41 +	int    is_name;
    1.42 +	int    is_hash;
    1.43 +	int    use_pp      = 0;
    1.44 +	int    out = 0;
    1.45 +	SECItem der = {siBuffer, NULL, 0 };
    1.46 +
    1.47 +	line[bytes_read] = 0;
    1.48 +	if (bytes_read <= skip_count) 
    1.49 +	    continue;
    1.50 +	fwrite(in, 1, left, stdout);
    1.51 +	found = strstr(in, "MULTILINE_OCTAL");
    1.52 +	if (!found) 
    1.53 +	    continue;
    1.54 +	fflush(stdout);
    1.55 +
    1.56 +	is_cert   = (NULL != strstr(in, "CKA_VALUE"));
    1.57 +	is_serial = (NULL != strstr(in, "CKA_SERIAL_NUMBER"));
    1.58 +	is_name   = (NULL != strstr(in, "CKA_ISSUER")) ||
    1.59 +		    (NULL != strstr(in, "CKA_SUBJECT"));
    1.60 +	is_hash   = (NULL != strstr(in, "_HASH"));
    1.61 +	while (fgets(line, 132, stdin) && 
    1.62 +	       (bytes_read = strlen(line)) > 0 ) {
    1.63 +	    in   = line       + skip_count; 
    1.64 +	    left = bytes_read - skip_count;
    1.65 +
    1.66 +	    if ((left >= 3) && !strncmp(in, "END", 3))
    1.67 +		break;
    1.68 +	    while (left >= 4) {
    1.69 +		if (in[0] == '\\'  && isdigit(in[1]) && 
    1.70 +		    isdigit(in[2]) && isdigit(in[3])) {
    1.71 +		    left -= 4;
    1.72 +		    binary_line[out++] = ((in[1] - '0') << 6) |
    1.73 +					 ((in[2] - '0') << 3) | 
    1.74 +					  (in[3] - '0');
    1.75 +		    in += 4;
    1.76 +		} else 
    1.77 +		    break;
    1.78 +	    }
    1.79 +	}
    1.80 +	der.data = binary_line;
    1.81 +	der.len  = out;
    1.82 +	if (is_cert)
    1.83 +	    SECU_PrintSignedData(stdout, &der, "Certificate", 0,
    1.84 +				 SECU_PrintCertificate);
    1.85 +	else if (is_name)
    1.86 +	    SECU_PrintDERName(stdout, &der, "Name", 0);
    1.87 +	else if (is_serial) {
    1.88 +	    if (out > 2 && binary_line[0] == 2 &&
    1.89 +	        out == 2 + binary_line[1]) {
    1.90 +		der.data += 2;
    1.91 +		der.len  -= 2;
    1.92 +		SECU_PrintInteger(stdout, &der, "DER Serial Number", 0);
    1.93 +	    } else
    1.94 +		SECU_PrintInteger(stdout, &der, "Raw Serial Number", 0);
    1.95 +	} else if (is_hash) 
    1.96 +	    SECU_PrintAsHex(stdout, &der, "Hash", 0);
    1.97 +	else 
    1.98 +	    SECU_PrintBuf(stdout, "Other", binary_line, out);
    1.99 +    }
   1.100 +    NSS_Shutdown();
   1.101 +    return 0;
   1.102 +}
   1.103 +

mercurial