• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

randombit / botan / 27806188297

18 Jun 2026 04:12PM UTC coverage: 89.37% (-0.03%) from 89.397%
27806188297

push

github

web-flow
Merge pull request #5677 from randombit/jack/oid-names

Add OID::registered_name

111637 of 124915 relevant lines covered (89.37%)

10895907.86 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

87.5
/src/cli/asn1.cpp
1
/*
2
* (C) 2014,2015 Jack Lloyd
3
*
4
* Botan is released under the Simplified BSD License (see license.txt)
5
*/
6

7
#include "cli.h"
8

9
#if defined(BOTAN_HAS_ASN1)
10

11
   #include <botan/asn1_print.h>
12
   #include <botan/data_src.h>
13

14
   #if defined(BOTAN_HAS_PEM_CODEC)
15
      #include <botan/pem.h>
16
   #endif
17

18
namespace Botan_CLI {
19

20
namespace {
21

22
class ASN1_Printer final : public Command {
23
   public:
24
      ASN1_Printer() :
2✔
25
            Command("asn1print --skip-context-specific --print-limit=4096 --bin-limit=2048 --max-depth=64 --pem file") {
2✔
26
      }
2✔
27

28
      std::string group() const override { return "asn1"; }
1✔
29

30
      std::string description() const override { return "Decode and print file with ASN.1 Basic Encoding Rules (BER)"; }
1✔
31

32
      static bool first_n(const std::vector<uint8_t>& data, size_t n, uint8_t b) {
33
         if(data.size() < n) {
34
            return false;
35
         }
36

37
         for(size_t i = 0; i != n; ++i) {
×
38
            if(data[i] != b) {
×
39
               return false;
40
            }
41
         }
42

43
         return true;
44
      }
45

46
      void go() override {
1✔
47
         const std::string input = get_arg("file");
1✔
48
         const size_t print_limit = get_arg_sz("print-limit");
1✔
49
         const size_t bin_limit = get_arg_sz("bin-limit");
1✔
50
         const bool print_context_specific = !flag_set("skip-context-specific");
1✔
51
         const size_t max_depth = get_arg_sz("max-depth");
1✔
52

53
         const size_t value_column = 60;
1✔
54
         const size_t initial_level = 0;
1✔
55

56
         std::vector<uint8_t> file_contents = slurp_file(input);
1✔
57
         std::vector<uint8_t> data;
1✔
58

59
         if(flag_set("pem") || (input.size() > 4 && input.substr(input.size() - 4) == ".pem") ||
1✔
60
            (file_contents.size() > 20 && first_n(file_contents, 5, '-'))) {
×
61
   #if defined(BOTAN_HAS_PEM_CODEC)
62
            std::string pem_label;
1✔
63
            Botan::DataSource_Memory src(file_contents);
1✔
64
            data = unlock(Botan::PEM_Code::decode(src, pem_label));
3✔
65
   #else
66
            throw CLI_Error_Unsupported("PEM decoding not available in this build");
67
   #endif
68
         } else {
1✔
69
            data.swap(file_contents);
×
70
         }
71

72
         const Botan::ASN1_Pretty_Printer printer(
1✔
73
            print_limit, bin_limit, print_context_specific, initial_level, value_column, max_depth);
1✔
74

75
         printer.print_to_stream(output(), data.data(), data.size());
1✔
76
      }
3✔
77
};
78

79
BOTAN_REGISTER_COMMAND("asn1print", ASN1_Printer);
2✔
80

81
class OID_Info final : public Command {
82
   public:
83
      OID_Info() : Command("oid_info oid") {}
8✔
84

85
      std::string group() const override { return "asn1"; }
1✔
86

87
      std::string description() const override { return "Provide information about an object identifier"; }
1✔
88

89
      void go() override {
3✔
90
         const std::string oid_str = get_arg("oid");
3✔
91

92
         if(oid_str.empty()) {
3✔
93
            throw CLI_Usage_Error("Must provide non-empty OID string");
×
94
         }
95

96
         try {
3✔
97
            const Botan::OID oid(oid_str);
3✔
98

99
            if(const auto name = oid.registered_name()) {
2✔
100
               output() << "OID " << oid_str << " is associated with " << *name << "\n";
1✔
101
            } else {
102
               output() << "OID " << oid_str << " is not recognized\n";
1✔
103
            }
×
104

105
            return;
2✔
106
         } catch(Botan::Exception&) {}
3✔
107

108
         // This throws if the string is not known
109
         const Botan::OID oid = Botan::OID::from_string(oid_str);
1✔
110
         output() << "The string '" << oid_str << "' is associated with OID " << oid.to_string() << "\n";
2✔
111
      }
3✔
112
};
113

114
BOTAN_REGISTER_COMMAND("oid_info", OID_Info);
4✔
115

116
}  // namespace
117

118
}  // namespace Botan_CLI
119

120
#endif  // BOTAN_HAS_ASN1
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc