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