Coverage Report

Created: 2025-04-11 06:34

/src/botan/src/lib/pubkey/x509_key.cpp
Line
Count
Source (jump to first uncovered line)
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
9.29k
std::unique_ptr<Public_Key> load_key(DataSource& source) {
29
9.29k
   try {
30
9.29k
      AlgorithmIdentifier alg_id;
31
9.29k
      std::vector<uint8_t> key_bits;
32
33
9.29k
      if(ASN1::maybe_BER(source) && !PEM_Code::matches(source)) {
34
8.89k
         BER_Decoder(source).start_sequence().decode(alg_id).decode(key_bits, ASN1_Type::BitString).end_cons();
35
8.89k
      } else {
36
405
         DataSource_Memory ber(PEM_Code::decode_check_label(source, "PUBLIC KEY"));
37
38
405
         BER_Decoder(ber).start_sequence().decode(alg_id).decode(key_bits, ASN1_Type::BitString).end_cons();
39
405
      }
40
41
9.29k
      if(key_bits.empty()) {
42
25
         throw Decoding_Error("X.509 public key decoding");
43
25
      }
44
45
9.27k
      return load_public_key(alg_id, key_bits);
46
9.29k
   } catch(Decoding_Error& e) {
47
4.42k
      throw Decoding_Error("X.509 public key decoding", e);
48
4.42k
   }
49
9.29k
}
50
51
}  // namespace Botan::X509