security/nss/cmd/ppcertdata/ppcertdata.c

Wed, 31 Dec 2014 06:55:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:55:50 +0100
changeset 2
7e26c7da4463
permissions
-rw-r--r--

Added tag UPSTREAM_283F7C6 for changeset ca08bd8f51b2

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 #include <stdio.h>
michael@0 6 #include <string.h>
michael@0 7 #include <ctype.h>
michael@0 8 #include <stdlib.h>
michael@0 9 #include "secutil.h"
michael@0 10 #include "nss.h"
michael@0 11
michael@0 12 unsigned char binary_line[64 * 1024];
michael@0 13
michael@0 14 int
michael@0 15 main(int argc, const char ** argv)
michael@0 16 {
michael@0 17 int skip_count = 0;
michael@0 18 int bytes_read;
michael@0 19 char line[133];
michael@0 20
michael@0 21 if (argc > 1) {
michael@0 22 skip_count = atoi(argv[1]);
michael@0 23 }
michael@0 24 if (argc > 2 || skip_count < 0) {
michael@0 25 printf("Usage: %s [ skip_columns ] \n", argv[0]);
michael@0 26 return 1;
michael@0 27 }
michael@0 28
michael@0 29 NSS_NoDB_Init(NULL);
michael@0 30
michael@0 31 while (fgets(line, 132, stdin) && (bytes_read = strlen(line)) > 0 ) {
michael@0 32 int bytes_written;
michael@0 33 char * found;
michael@0 34 char * in = line + skip_count;
michael@0 35 int left = bytes_read - skip_count;
michael@0 36 int is_cert;
michael@0 37 int is_serial;
michael@0 38 int is_name;
michael@0 39 int is_hash;
michael@0 40 int use_pp = 0;
michael@0 41 int out = 0;
michael@0 42 SECItem der = {siBuffer, NULL, 0 };
michael@0 43
michael@0 44 line[bytes_read] = 0;
michael@0 45 if (bytes_read <= skip_count)
michael@0 46 continue;
michael@0 47 fwrite(in, 1, left, stdout);
michael@0 48 found = strstr(in, "MULTILINE_OCTAL");
michael@0 49 if (!found)
michael@0 50 continue;
michael@0 51 fflush(stdout);
michael@0 52
michael@0 53 is_cert = (NULL != strstr(in, "CKA_VALUE"));
michael@0 54 is_serial = (NULL != strstr(in, "CKA_SERIAL_NUMBER"));
michael@0 55 is_name = (NULL != strstr(in, "CKA_ISSUER")) ||
michael@0 56 (NULL != strstr(in, "CKA_SUBJECT"));
michael@0 57 is_hash = (NULL != strstr(in, "_HASH"));
michael@0 58 while (fgets(line, 132, stdin) &&
michael@0 59 (bytes_read = strlen(line)) > 0 ) {
michael@0 60 in = line + skip_count;
michael@0 61 left = bytes_read - skip_count;
michael@0 62
michael@0 63 if ((left >= 3) && !strncmp(in, "END", 3))
michael@0 64 break;
michael@0 65 while (left >= 4) {
michael@0 66 if (in[0] == '\\' && isdigit(in[1]) &&
michael@0 67 isdigit(in[2]) && isdigit(in[3])) {
michael@0 68 left -= 4;
michael@0 69 binary_line[out++] = ((in[1] - '0') << 6) |
michael@0 70 ((in[2] - '0') << 3) |
michael@0 71 (in[3] - '0');
michael@0 72 in += 4;
michael@0 73 } else
michael@0 74 break;
michael@0 75 }
michael@0 76 }
michael@0 77 der.data = binary_line;
michael@0 78 der.len = out;
michael@0 79 if (is_cert)
michael@0 80 SECU_PrintSignedData(stdout, &der, "Certificate", 0,
michael@0 81 SECU_PrintCertificate);
michael@0 82 else if (is_name)
michael@0 83 SECU_PrintDERName(stdout, &der, "Name", 0);
michael@0 84 else if (is_serial) {
michael@0 85 if (out > 2 && binary_line[0] == 2 &&
michael@0 86 out == 2 + binary_line[1]) {
michael@0 87 der.data += 2;
michael@0 88 der.len -= 2;
michael@0 89 SECU_PrintInteger(stdout, &der, "DER Serial Number", 0);
michael@0 90 } else
michael@0 91 SECU_PrintInteger(stdout, &der, "Raw Serial Number", 0);
michael@0 92 } else if (is_hash)
michael@0 93 SECU_PrintAsHex(stdout, &der, "Hash", 0);
michael@0 94 else
michael@0 95 SECU_PrintBuf(stdout, "Other", binary_line, out);
michael@0 96 }
michael@0 97 NSS_Shutdown();
michael@0 98 return 0;
michael@0 99 }
michael@0 100

mercurial