Coverage Report

Created: 2022-06-23 06:44

/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
#include <botan/data_src.h>
10
#include <botan/ber_dec.h>
11
#include <botan/pem.h>
12
#include <botan/asn1_obj.h>
13
#include <botan/pk_algs.h>
14
15
namespace Botan::X509 {
16
17
/*
18
* PEM encode a X.509 public key
19
*/
20
std::string PEM_encode(const Public_Key& key)
21
0
   {
22
0
   return PEM_Code::encode(key.subject_public_key(),
23
0
                           "PUBLIC KEY");
24
0
   }
25
26
/*
27
* Extract a public key and return it
28
*/
29
Public_Key* load_key(DataSource& source)
30
9.23k
   {
31
9.23k
   try {
32
9.23k
      AlgorithmIdentifier alg_id;
33
9.23k
      std::vector<uint8_t> key_bits;
34
35
9.23k
      if(ASN1::maybe_BER(source) && !PEM_Code::matches(source))
36
9.23k
         {
37
9.23k
         BER_Decoder(source)
38
9.23k
            .start_sequence()
39
9.23k
            .decode(alg_id)
40
9.23k
            .decode(key_bits, ASN1_Type::BitString)
41
9.23k
         .end_cons();
42
9.23k
         }
43
0
      else
44
0
         {
45
0
         DataSource_Memory ber(
46
0
            PEM_Code::decode_check_label(source, "PUBLIC KEY")
47
0
            );
48
49
0
         BER_Decoder(ber)
50
0
            .start_sequence()
51
0
            .decode(alg_id)
52
0
            .decode(key_bits, ASN1_Type::BitString)
53
0
         .end_cons();
54
0
         }
55
56
9.23k
      if(key_bits.empty())
57
4
         throw Decoding_Error("X.509 public key decoding");
58
59
9.22k
      return load_public_key(alg_id, key_bits).release();
60
9.23k
      }
61
9.23k
   catch(Decoding_Error& e)
62
9.23k
      {
63
1.90k
      throw Decoding_Error("X.509 public key decoding", e);
64
1.90k
      }
65
9.23k
   }
66
67
}