ppcertdata.c (3180B)
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 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((unsigned char)in[1]) && 67 isdigit((unsigned char)in[2]) && 68 isdigit((unsigned char)in[3])) { 69 left -= 4; 70 binary_line[out++] = ((in[1] - '0') << 6) | 71 ((in[2] - '0') << 3) | 72 (in[3] - '0'); 73 in += 4; 74 } else 75 break; 76 } 77 } 78 der.data = binary_line; 79 der.len = out; 80 if (is_cert) 81 SECU_PrintSignedData(stdout, &der, "Certificate", 0, 82 SECU_PrintCertificate); 83 else if (is_name) 84 SECU_PrintDERName(stdout, &der, "Name", 0); 85 else if (is_serial) { 86 if (out > 2 && binary_line[0] == 2 && 87 out == 2 + binary_line[1]) { 88 der.data += 2; 89 der.len -= 2; 90 SECU_PrintInteger(stdout, &der, "DER Serial Number", 0); 91 } else 92 SECU_PrintInteger(stdout, &der, "Raw Serial Number", 0); 93 } else if (is_hash) 94 SECU_PrintAsHex(stdout, &der, "Hash", 0); 95 else 96 SECU_PrintBuf(stdout, "Other", binary_line, out); 97 } 98 NSS_Shutdown(); 99 return 0; 100 }