1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 // This utility can dump the contents of CRL set, optionally augmented with a 6 // delta CRL set. 7 8 #include <errno.h> 9 #include <stdio.h> 10 #include <stdlib.h> 11 12 #include <string> 13 14 #include "base/at_exit.h" 15 #include "base/files/file_util.h" 16 #include "base/memory/ref_counted.h" 17 #include "base/strings/string_number_conversions.h" 18 #include "net/cert/crl_set.h" 19 #include "net/cert/crl_set_storage.h" 20 21 static int Usage(const char* argv0) { 22 fprintf(stderr, "Usage: %s <crl-set file> [<delta file>]" 23 " [<resulting output file>]\n", argv0); 24 return 1; 25 } 26 27 int main(int argc, char** argv) { 28 base::AtExitManager at_exit_manager; 29 30 base::FilePath crl_set_filename, delta_filename, output_filename; 31 32 if (argc < 2 || argc > 4) 33 return Usage(argv[0]); 34 35 crl_set_filename = base::FilePath::FromUTF8Unsafe(argv[1]); 36 if (argc >= 3) 37 delta_filename = base::FilePath::FromUTF8Unsafe(argv[2]); 38 if (argc >= 4) 39 output_filename = base::FilePath::FromUTF8Unsafe(argv[3]); 40 41 std::string crl_set_bytes, delta_bytes; 42 if (!base::ReadFileToString(crl_set_filename, &crl_set_bytes)) 43 return 1; 44 if (!delta_filename.empty() && 45 !base::ReadFileToString(delta_filename, &delta_bytes)) { 46 return 1; 47 } 48 49 scoped_refptr<net::CRLSet> crl_set, final_crl_set; 50 if (!net::CRLSetStorage::Parse(crl_set_bytes, &crl_set)) { 51 fprintf(stderr, "Failed to parse CRLSet\n"); 52 return 1; 53 } 54 55 if (!delta_bytes.empty()) { 56 if (!net::CRLSetStorage::ApplyDelta( 57 crl_set.get(), delta_bytes, &final_crl_set)) { 58 fprintf(stderr, "Failed to apply delta to CRLSet\n"); 59 return 1; 60 } 61 } else { 62 final_crl_set = crl_set; 63 } 64 65 if (!output_filename.empty()) { 66 const std::string out = net::CRLSetStorage::Serialize(final_crl_set.get()); 67 if (base::WriteFile(output_filename, out.data(), out.size()) == -1) { 68 fprintf(stderr, "Failed to write resulting CRL set\n"); 69 return 1; 70 } 71 } 72 73 const net::CRLSet::CRLList& crls = final_crl_set->crls(); 74 for (net::CRLSet::CRLList::const_iterator i = crls.begin(); i != crls.end(); 75 i++) { 76 printf("%s\n", base::HexEncode(i->first.data(), i->first.size()).c_str()); 77 for (std::vector<std::string>::const_iterator j = i->second.begin(); 78 j != i->second.end(); j++) { 79 printf(" %s\n", base::HexEncode(j->data(), j->size()).c_str()); 80 } 81 } 82 83 return 0; 84 } 85