Coverage Report

Created: 2021-05-04 09:02

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