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

randombit / botan / 25139258422

29 Apr 2026 08:02PM UTC coverage: 89.37% (-0.02%) from 89.385%
25139258422

push

github

web-flow
Merge pull request #5550 from randombit/jack/tls-misc

TLS conformance, hardening, and performance fixes

107055 of 119789 relevant lines covered (89.37%)

11415549.66 hits per line

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

95.76
/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_ECC_GROUP)
21
   #include <botan/ec_group.h>
22
#endif
23

24
#if defined(BOTAN_HAS_ECDH)
25
   #include <botan/ecdh.h>
26
#endif
27

28
#if defined(BOTAN_HAS_ECDSA)
29
   #include <botan/ecdsa.h>
30
#endif
31

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

37
namespace Botan_Tests {
38

39
namespace {
40

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

47
         results.push_back(test_peer_key_acceptable_rsa(this->rng()));
2✔
48
         results.push_back(test_peer_key_acceptable_ecdh(this->rng()));
2✔
49
         results.push_back(test_peer_key_acceptable_ecdsa(this->rng()));
2✔
50
         results.push_back(test_peer_key_acceptable_dh());
2✔
51
         results.push_back(test_key_exchange_groups_to_offer());
2✔
52
         results.push_back(test_require_extended_master_secret());
2✔
53

54
         return results;
1✔
55
      }
×
56

57
      static Test::Result test_require_extended_master_secret() {
1✔
58
         Test::Result result("TLS Policy require_extended_master_secret");
1✔
59

60
         const Botan::TLS::Policy default_policy;
1✔
61
         result.test_is_true("default Policy requires EMS", default_policy.require_extended_master_secret());
1✔
62

63
         using TP = Botan::TLS::Text_Policy;
1✔
64
         result.test_is_true("default text policy requires EMS", TP("").require_extended_master_secret());
1✔
65
         result.test_is_true("text policy override disables EMS requirement",
2✔
66
                             !TP("require_extended_master_secret = false").require_extended_master_secret());
1✔
67
         result.test_is_true("text policy override re-enables EMS requirement",
2✔
68
                             TP("require_extended_master_secret = true").require_extended_master_secret());
1✔
69
         return result;
1✔
70
      }
1✔
71

72
   private:
73
      static Test::Result test_peer_key_acceptable_rsa([[maybe_unused]] Botan::RandomNumberGenerator& rng) {
1✔
74
         Test::Result result("TLS Policy RSA key verification");
1✔
75
   #if defined(BOTAN_HAS_RSA)
76
         auto rsa_key_1024 = std::make_unique<Botan::RSA_PrivateKey>(rng, 1024);
1✔
77
         const Botan::TLS::Policy policy;
1✔
78

79
         try {
1✔
80
            policy.check_peer_key_acceptable(*rsa_key_1024);
1✔
81
            result.test_failure("Incorrectly accepting 1024 bit RSA keys");
×
82
         } catch(Botan::TLS::TLS_Exception&) {
1✔
83
            result.test_success("Correctly rejecting 1024 bit RSA keys");
1✔
84
         }
1✔
85

86
         auto rsa_key_2048 = std::make_unique<Botan::RSA_PrivateKey>(rng, 2048);
1✔
87
         policy.check_peer_key_acceptable(*rsa_key_2048);
1✔
88
         result.test_success("Correctly accepting 2048 bit RSA keys");
1✔
89
   #endif
90
         return result;
1✔
91
      }
1✔
92

93
      static Test::Result test_peer_key_acceptable_ecdh([[maybe_unused]] Botan::RandomNumberGenerator& rng) {
1✔
94
         Test::Result result("TLS Policy ECDH key verification");
1✔
95
   #if defined(BOTAN_HAS_ECDH)
96

97
         const Botan::TLS::Policy policy;
1✔
98

99
         if(Botan::EC_Group::supports_named_group("secp192r1")) {
1✔
100
            const auto group_192 = Botan::EC_Group::from_name("secp192r1");
1✔
101
            auto ecdh_192 = std::make_unique<Botan::ECDH_PrivateKey>(rng, group_192);
1✔
102

103
            try {
1✔
104
               policy.check_peer_key_acceptable(*ecdh_192);
1✔
105
               result.test_failure("Incorrectly accepting 192 bit EC keys");
×
106
            } catch(Botan::TLS::TLS_Exception&) {
1✔
107
               result.test_success("Correctly rejecting 192 bit EC keys");
1✔
108
            }
1✔
109
         }
1✔
110

111
         if(Botan::EC_Group::supports_named_group("secp256r1")) {
1✔
112
            const auto group_256 = Botan::EC_Group::from_name("secp256r1");
1✔
113
            auto ecdh_256 = std::make_unique<Botan::ECDH_PrivateKey>(rng, group_256);
1✔
114
            policy.check_peer_key_acceptable(*ecdh_256);
1✔
115
            result.test_success("Correctly accepting 256 bit EC keys");
1✔
116
         }
1✔
117
   #endif
118
         return result;
1✔
119
      }
1✔
120

121
      static Test::Result test_peer_key_acceptable_ecdsa([[maybe_unused]] Botan::RandomNumberGenerator& rng) {
1✔
122
         Test::Result result("TLS Policy ECDSA key verification");
1✔
123
   #if defined(BOTAN_HAS_ECDSA)
124
         const Botan::TLS::Policy policy;
1✔
125

126
         if(Botan::EC_Group::supports_named_group("secp192r1")) {
1✔
127
            const auto group_192 = Botan::EC_Group::from_name("secp192r1");
1✔
128
            auto ecdsa_192 = std::make_unique<Botan::ECDSA_PrivateKey>(rng, group_192);
1✔
129

130
            try {
1✔
131
               policy.check_peer_key_acceptable(*ecdsa_192);
1✔
132
               result.test_failure("Incorrectly accepting 192 bit EC keys");
×
133
            } catch(Botan::TLS::TLS_Exception&) {
1✔
134
               result.test_success("Correctly rejecting 192 bit EC keys");
1✔
135
            }
1✔
136
         }
1✔
137

138
         if(Botan::EC_Group::supports_named_group("secp256r1")) {
1✔
139
            const auto group_256 = Botan::EC_Group::from_name("secp256r1");
1✔
140
            auto ecdsa_256 = std::make_unique<Botan::ECDSA_PrivateKey>(rng, group_256);
1✔
141
            policy.check_peer_key_acceptable(*ecdsa_256);
1✔
142
            result.test_success("Correctly accepting 256 bit EC keys");
1✔
143
         }
1✔
144
   #endif
145
         return result;
1✔
146
      }
1✔
147

148
      static Test::Result test_peer_key_acceptable_dh() {
1✔
149
         Test::Result result("TLS Policy DH key verification");
1✔
150
   #if defined(BOTAN_HAS_DIFFIE_HELLMAN)
151
         const BigInt g("2");
1✔
152
         const BigInt p("58458002095536094658683755258523362961421200751439456159756164191494576279467");
1✔
153
         const Botan::DL_Group grp(p, g);
1✔
154
         const Botan::BigInt x("46205663093589612668746163860870963912226379131190812163519349848291472898748");
1✔
155
         auto dhkey = std::make_unique<Botan::DH_PrivateKey>(grp, x);
1✔
156

157
         const Botan::TLS::Policy policy;
1✔
158
         try {
1✔
159
            policy.check_peer_key_acceptable(*dhkey);
1✔
160
            result.test_failure("Incorrectly accepting short bit DH keys");
×
161
         } catch(Botan::TLS::TLS_Exception&) {
1✔
162
            result.test_success("Correctly rejecting short bit DH keys");
1✔
163
         }
1✔
164
   #endif
165
         return result;
2✔
166
      }
2✔
167

168
      static Test::Result test_key_exchange_groups_to_offer() {
1✔
169
         Test::Result result("TLS Policy key share offering");
1✔
170

171
         const Botan::TLS::Policy default_policy;
1✔
172
         result.test_sz_eq(
1✔
173
            "default TLS Policy offers exactly one", default_policy.key_exchange_groups_to_offer().size(), 1);
1✔
174
         result.test_is_true(
1✔
175
            "default TLS Policy offers preferred group",
176
            default_policy.key_exchange_groups().front() == default_policy.key_exchange_groups_to_offer().front());
2✔
177

178
         using TP = Botan::TLS::Text_Policy;
1✔
179

180
         result.test_sz_eq(
2✔
181
            "default behaviour from text policy (size)", TP("").key_exchange_groups_to_offer().size(), 1);
2✔
182
         result.test_is_true("default behaviour from text policy (preferred)",
2✔
183
                             TP("").key_exchange_groups().front() == TP("").key_exchange_groups_to_offer().front());
3✔
184

185
         result.test_is_true("no offerings",
2✔
186
                             TP("key_exchange_groups_to_offer = none").key_exchange_groups_to_offer().empty());
2✔
187

188
         const std::string two_groups = "key_exchange_groups_to_offer = secp256r1 ffdhe/ietf/4096";
1✔
189
         result.test_sz_eq("list of offerings (size)", TP(two_groups).key_exchange_groups_to_offer().size(), 2);
2✔
190
         result.test_is_true("list of offerings (0)",
2✔
191
                             TP(two_groups).key_exchange_groups_to_offer()[0] == Botan::TLS::Group_Params::SECP256R1);
2✔
192
         result.test_is_true("list of offerings (1)",
2✔
193
                             TP(two_groups).key_exchange_groups_to_offer()[1] == Botan::TLS::Group_Params::FFDHE_4096);
2✔
194

195
         return result;
1✔
196
      }
1✔
197
};
198

199
BOTAN_REGISTER_TEST("tls", "tls_policy", TLS_Policy_Unit_Tests);
200

201
#endif
202

203
}  // namespace
204

205
}  // 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