Coverage Report

Created: 2026-02-26 06:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/Botan-3.4.0/src/lib/pubkey/x509_key.cpp
Line
Count
Source
1
/*
2
* X.509 Public Key
3
* (C) 1999-2010 Jack Lloyd
4
*
5
* Botan is released under the Simplified BSD License (see license.txt)
6
*/
7
8
#include <botan/x509_key.h>
9
10
#include <botan/asn1_obj.h>
11
#include <botan/ber_dec.h>
12
#include <botan/data_src.h>
13
#include <botan/pem.h>
14
#include <botan/pk_algs.h>
15
16
namespace Botan::X509 {
17
18
/*
19
* PEM encode a X.509 public key
20
*/
21
0
std::string PEM_encode(const Public_Key& key) {
22
0
   return PEM_Code::encode(key.subject_public_key(), "PUBLIC KEY");
23
0
}
24
25
/*
26
* Extract a public key and return it
27
*/
28
0
std::unique_ptr<Public_Key> load_key(DataSource& source) {
29
0
   try {
30
0
      AlgorithmIdentifier alg_id;
31
0
      std::vector<uint8_t> key_bits;
32
33
0
      if(ASN1::maybe_BER(source) && !PEM_Code::matches(source)) {
34
0
         BER_Decoder(source).start_sequence().decode(alg_id).decode(key_bits, ASN1_Type::BitString).end_cons();
35
0
      } else {
36
0
         DataSource_Memory ber(PEM_Code::decode_check_label(source, "PUBLIC KEY"));
37
38
0
         BER_Decoder(ber).start_sequence().decode(alg_id).decode(key_bits, ASN1_Type::BitString).end_cons();
39
0
      }
40
41
0
      if(key_bits.empty()) {
42
0
         throw Decoding_Error("X.509 public key decoding");
43
0
      }
44
45
0
      return load_public_key(alg_id, key_bits);
46
0
   } catch(Decoding_Error& e) {
47
0
      throw Decoding_Error("X.509 public key decoding", e);
48
0
   }
49
0
}
50
51
}  // namespace Botan::X509