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

randombit / botan / 21753596263

06 Feb 2026 02:13PM UTC coverage: 90.063% (-0.01%) from 90.073%
21753596263

Pull #5289

github

web-flow
Merge 587099284 into 8ea0ca252
Pull Request #5289: Further misc header reductions, forward declarations, etc

102237 of 113517 relevant lines covered (90.06%)

11402137.11 hits per line

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

95.19
/src/tests/unit_tls_policy.cpp
1
/*
2
* TLS Policy tests
3
*
4
* (C) 2016 Juraj Somorovsky
5
*
6
* Botan is released under the Simplified BSD License (see license.txt)
7
*/
8

9
#include "tests.h"
10

11
#if defined(BOTAN_HAS_TLS)
12
   #include <botan/tls_exceptn.h>
13
   #include <botan/tls_policy.h>
14
#endif
15

16
#if defined(BOTAN_HAS_RSA)
17
   #include <botan/rsa.h>
18
#endif
19

20
#if defined(BOTAN_HAS_ECDH)
21
   #include <botan/ec_group.h>
22
   #include <botan/ecdh.h>
23
#endif
24

25
#if defined(BOTAN_HAS_ECDSA)
26
   #include <botan/ec_group.h>
27
   #include <botan/ecdsa.h>
28
#endif
29

30
#if defined(BOTAN_HAS_DIFFIE_HELLMAN)
31
   #include <botan/dh.h>
32
   #include <botan/dl_group.h>
33
#endif
34

35
namespace Botan_Tests {
36

37
namespace {
38

39
#if defined(BOTAN_HAS_TLS)
40
class TLS_Policy_Unit_Tests final : public Test {
1✔
41
   public:
42
      std::vector<Test::Result> run() override {
1✔
43
         std::vector<Test::Result> results;
1✔
44

45
         results.push_back(test_peer_key_acceptable_rsa(this->rng()));
2✔
46
         results.push_back(test_peer_key_acceptable_ecdh(this->rng()));
2✔
47
         results.push_back(test_peer_key_acceptable_ecdsa(this->rng()));
2✔
48
         results.push_back(test_peer_key_acceptable_dh());
2✔
49
         results.push_back(test_key_exchange_groups_to_offer());
2✔
50

51
         return results;
1✔
52
      }
×
53

54
   private:
55
      static Test::Result test_peer_key_acceptable_rsa(Botan::RandomNumberGenerator& rng) {
1✔
56
         Test::Result result("TLS Policy RSA key verification");
1✔
57
   #if defined(BOTAN_HAS_RSA)
58
         auto rsa_key_1024 = std::make_unique<Botan::RSA_PrivateKey>(rng, 1024);
1✔
59
         const Botan::TLS::Policy policy;
1✔
60

61
         try {
1✔
62
            policy.check_peer_key_acceptable(*rsa_key_1024);
1✔
63
            result.test_failure("Incorrectly accepting 1024 bit RSA keys");
×
64
         } catch(Botan::TLS::TLS_Exception&) {
1✔
65
            result.test_success("Correctly rejecting 1024 bit RSA keys");
1✔
66
         }
1✔
67

68
         auto rsa_key_2048 = std::make_unique<Botan::RSA_PrivateKey>(rng, 2048);
1✔
69
         policy.check_peer_key_acceptable(*rsa_key_2048);
1✔
70
         result.test_success("Correctly accepting 2048 bit RSA keys");
1✔
71
   #endif
72
         return result;
1✔
73
      }
1✔
74

75
      static Test::Result test_peer_key_acceptable_ecdh(Botan::RandomNumberGenerator& rng) {
1✔
76
         Test::Result result("TLS Policy ECDH key verification");
1✔
77
   #if defined(BOTAN_HAS_ECDH)
78

79
         const Botan::TLS::Policy policy;
1✔
80

81
         if(Botan::EC_Group::supports_named_group("secp192r1")) {
1✔
82
            const auto group_192 = Botan::EC_Group::from_name("secp192r1");
1✔
83
            auto ecdh_192 = std::make_unique<Botan::ECDH_PrivateKey>(rng, group_192);
1✔
84

85
            try {
1✔
86
               policy.check_peer_key_acceptable(*ecdh_192);
1✔
87
               result.test_failure("Incorrectly accepting 192 bit EC keys");
×
88
            } catch(Botan::TLS::TLS_Exception&) {
1✔
89
               result.test_success("Correctly rejecting 192 bit EC keys");
1✔
90
            }
1✔
91
         }
1✔
92

93
         if(Botan::EC_Group::supports_named_group("secp256r1")) {
1✔
94
            const auto group_256 = Botan::EC_Group::from_name("secp256r1");
1✔
95
            auto ecdh_256 = std::make_unique<Botan::ECDH_PrivateKey>(rng, group_256);
1✔
96
            policy.check_peer_key_acceptable(*ecdh_256);
1✔
97
            result.test_success("Correctly accepting 256 bit EC keys");
1✔
98
         }
1✔
99
   #endif
100
         return result;
1✔
101
      }
1✔
102

103
      static Test::Result test_peer_key_acceptable_ecdsa(Botan::RandomNumberGenerator& rng) {
1✔
104
         Test::Result result("TLS Policy ECDSA key verification");
1✔
105
   #if defined(BOTAN_HAS_ECDSA)
106
         const Botan::TLS::Policy policy;
1✔
107

108
         if(Botan::EC_Group::supports_named_group("secp192r1")) {
1✔
109
            const auto group_192 = Botan::EC_Group::from_name("secp192r1");
1✔
110
            auto ecdsa_192 = std::make_unique<Botan::ECDSA_PrivateKey>(rng, group_192);
1✔
111

112
            try {
1✔
113
               policy.check_peer_key_acceptable(*ecdsa_192);
1✔
114
               result.test_failure("Incorrectly accepting 192 bit EC keys");
×
115
            } catch(Botan::TLS::TLS_Exception&) {
1✔
116
               result.test_success("Correctly rejecting 192 bit EC keys");
1✔
117
            }
1✔
118
         }
1✔
119

120
         if(Botan::EC_Group::supports_named_group("secp256r1")) {
1✔
121
            const auto group_256 = Botan::EC_Group::from_name("secp256r1");
1✔
122
            auto ecdsa_256 = std::make_unique<Botan::ECDSA_PrivateKey>(rng, group_256);
1✔
123
            policy.check_peer_key_acceptable(*ecdsa_256);
1✔
124
            result.test_success("Correctly accepting 256 bit EC keys");
1✔
125
         }
1✔
126
   #endif
127
         return result;
1✔
128
      }
1✔
129

130
      static Test::Result test_peer_key_acceptable_dh() {
1✔
131
         Test::Result result("TLS Policy DH key verification");
1✔
132
   #if defined(BOTAN_HAS_DIFFIE_HELLMAN)
133
         const BigInt g("2");
1✔
134
         const BigInt p("58458002095536094658683755258523362961421200751439456159756164191494576279467");
1✔
135
         const Botan::DL_Group grp(p, g);
1✔
136
         const Botan::BigInt x("46205663093589612668746163860870963912226379131190812163519349848291472898748");
1✔
137
         auto dhkey = std::make_unique<Botan::DH_PrivateKey>(grp, x);
1✔
138

139
         const Botan::TLS::Policy policy;
1✔
140
         try {
1✔
141
            policy.check_peer_key_acceptable(*dhkey);
1✔
142
            result.test_failure("Incorrectly accepting short bit DH keys");
×
143
         } catch(Botan::TLS::TLS_Exception&) {
1✔
144
            result.test_success("Correctly rejecting short bit DH keys");
1✔
145
         }
1✔
146
   #endif
147
         return result;
2✔
148
      }
2✔
149

150
      static Test::Result test_key_exchange_groups_to_offer() {
1✔
151
         Test::Result result("TLS Policy key share offering");
1✔
152

153
         const Botan::TLS::Policy default_policy;
1✔
154
         result.test_eq(
2✔
155
            "default TLS Policy offers exactly one", default_policy.key_exchange_groups_to_offer().size(), 1);
1✔
156
         result.confirm(
1✔
157
            "default TLS Policy offers preferred group",
158
            default_policy.key_exchange_groups().front() == default_policy.key_exchange_groups_to_offer().front());
2✔
159

160
         using TP = Botan::TLS::Text_Policy;
1✔
161

162
         result.test_eq("default behaviour from text policy (size)", TP("").key_exchange_groups_to_offer().size(), 1);
3✔
163
         result.confirm("default behaviour from text policy (preferred)",
4✔
164
                        TP("").key_exchange_groups().front() == TP("").key_exchange_groups_to_offer().front());
3✔
165

166
         result.confirm("no offerings",
2✔
167
                        TP("key_exchange_groups_to_offer = none").key_exchange_groups_to_offer().empty());
2✔
168

169
         const std::string two_groups = "key_exchange_groups_to_offer = secp256r1 ffdhe/ietf/4096";
1✔
170
         result.test_eq("list of offerings (size)", TP(two_groups).key_exchange_groups_to_offer().size(), 2);
3✔
171
         result.confirm("list of offerings (0)",
3✔
172
                        TP(two_groups).key_exchange_groups_to_offer()[0] == Botan::TLS::Group_Params::SECP256R1);
2✔
173
         result.confirm("list of offerings (1)",
3✔
174
                        TP(two_groups).key_exchange_groups_to_offer()[1] == Botan::TLS::Group_Params::FFDHE_4096);
2✔
175

176
         return result;
1✔
177
      }
1✔
178
};
179

180
BOTAN_REGISTER_TEST("tls", "tls_policy", TLS_Policy_Unit_Tests);
181

182
#endif
183

184
}  // namespace
185

186
}  // namespace Botan_Tests
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