~ubuntu-branches/debian/sid/botan/sid

« back to all changes in this revision

Viewing changes to src/tests/unit_tls_policy.cpp

  • Committer: Package Import Robot
  • Author(s): Laszlo Boszormenyi (GCS)
  • Date: 2018-03-01 22:23:25 UTC
  • mfrom: (1.2.2)
  • Revision ID: package-import@ubuntu.com-20180301222325-7p7vc45gu3hta34d
Tags: 2.4.0-2
* Don't remove .doctrees from the manual if it doesn't exist.
* Don't specify parallel to debhelper.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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_policy.h>
 
13
   #include <botan/tls_exceptn.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/ecdh.h>
 
22
#endif
 
23
 
 
24
#if defined(BOTAN_HAS_ECDSA)
 
25
   #include <botan/ecdsa.h>
 
26
#endif
 
27
 
 
28
#if defined(BOTAN_HAS_DIFFIE_HELLMAN)
 
29
   #include <botan/dh.h>
 
30
#endif
 
31
 
 
32
#if defined(BOTAN_HAS_DSA)
 
33
   #include <botan/dsa.h>
 
34
#endif
 
35
 
 
36
namespace Botan_Tests {
 
37
 
 
38
namespace {
 
39
 
 
40
#if defined(BOTAN_HAS_TLS)
 
41
class TLS_Policy_Unit_Tests final : public Test
 
42
   {
 
43
   public:
 
44
      std::vector<Test::Result> run() override
 
45
         {
 
46
         std::vector<Test::Result> results;
 
47
 
 
48
         results.push_back(test_peer_key_acceptable_rsa());
 
49
         results.push_back(test_peer_key_acceptable_ecdh());
 
50
         results.push_back(test_peer_key_acceptable_ecdsa());
 
51
         results.push_back(test_peer_key_acceptable_dh());
 
52
         results.push_back(test_peer_key_acceptable_dsa());
 
53
 
 
54
         return results;
 
55
         }
 
56
   private:
 
57
      Test::Result test_peer_key_acceptable_rsa()
 
58
         {
 
59
         Test::Result result("TLS Policy RSA key verification");
 
60
#if defined(BOTAN_HAS_RSA)
 
61
         std::unique_ptr<Botan::Private_Key> rsa_key_1024(new Botan::RSA_PrivateKey(Test::rng(), 1024));
 
62
         Botan::TLS::Policy policy;
 
63
 
 
64
         try
 
65
            {
 
66
            policy.check_peer_key_acceptable(*rsa_key_1024);
 
67
            result.test_failure("Incorrectly accepting 1024 bit RSA keys");
 
68
            }
 
69
         catch(Botan::TLS::TLS_Exception&)
 
70
            {
 
71
            result.test_success("Correctly rejecting 1024 bit RSA keys");
 
72
            }
 
73
 
 
74
         std::unique_ptr<Botan::Private_Key> rsa_key_2048(new Botan::RSA_PrivateKey(Test::rng(), 2048));
 
75
         policy.check_peer_key_acceptable(*rsa_key_2048);
 
76
         result.test_success("Correctly accepting 2048 bit RSA keys");
 
77
#endif
 
78
         return result;
 
79
         }
 
80
 
 
81
      Test::Result test_peer_key_acceptable_ecdh()
 
82
         {
 
83
         Test::Result result("TLS Policy ECDH key verification");
 
84
#if defined(BOTAN_HAS_ECDH)
 
85
         Botan::EC_Group group_192("secp192r1");
 
86
         std::unique_ptr<Botan::Private_Key> ecdh_192(new Botan::ECDH_PrivateKey(Test::rng(), group_192));
 
87
 
 
88
         Botan::TLS::Policy policy;
 
89
         try
 
90
            {
 
91
            policy.check_peer_key_acceptable(*ecdh_192);
 
92
            result.test_failure("Incorrectly accepting 192 bit EC keys");
 
93
            }
 
94
         catch(Botan::TLS::TLS_Exception&)
 
95
            {
 
96
            result.test_success("Correctly rejecting 192 bit EC keys");
 
97
            }
 
98
 
 
99
         Botan::EC_Group group_256("secp256r1");
 
100
         std::unique_ptr<Botan::Private_Key> ecdh_256(new Botan::ECDH_PrivateKey(Test::rng(), group_256));
 
101
         policy.check_peer_key_acceptable(*ecdh_256);
 
102
         result.test_success("Correctly accepting 256 bit EC keys");
 
103
#endif
 
104
         return result;
 
105
         }
 
106
 
 
107
      Test::Result test_peer_key_acceptable_ecdsa()
 
108
         {
 
109
         Test::Result result("TLS Policy ECDSA key verification");
 
110
#if defined(BOTAN_HAS_ECDSA)
 
111
         Botan::EC_Group group_192("secp192r1");
 
112
         std::unique_ptr<Botan::Private_Key> ecdsa_192(new Botan::ECDSA_PrivateKey(Test::rng(), group_192));
 
113
 
 
114
         Botan::TLS::Policy policy;
 
115
         try
 
116
            {
 
117
            policy.check_peer_key_acceptable(*ecdsa_192);
 
118
            result.test_failure("Incorrectly accepting 192 bit EC keys");
 
119
            }
 
120
         catch(Botan::TLS::TLS_Exception&)
 
121
            {
 
122
            result.test_success("Correctly rejecting 192 bit EC keys");
 
123
            }
 
124
 
 
125
         Botan::EC_Group group_256("secp256r1");
 
126
         std::unique_ptr<Botan::Private_Key> ecdsa_256(new Botan::ECDSA_PrivateKey(Test::rng(), group_256));
 
127
         policy.check_peer_key_acceptable(*ecdsa_256);
 
128
         result.test_success("Correctly accepting 256 bit EC keys");
 
129
#endif
 
130
         return result;
 
131
         }
 
132
 
 
133
      Test::Result test_peer_key_acceptable_dh()
 
134
         {
 
135
         Test::Result result("TLS Policy DH key verification");
 
136
#if defined(BOTAN_HAS_DIFFIE_HELLMAN)
 
137
         const BigInt g("2");
 
138
         const BigInt p("58458002095536094658683755258523362961421200751439456159756164191494576279467");
 
139
         const Botan::DL_Group grp(p, g);
 
140
         const Botan::BigInt x("46205663093589612668746163860870963912226379131190812163519349848291472898748");
 
141
         std::unique_ptr<Botan::Private_Key> dhkey(new Botan::DH_PrivateKey(Test::rng(), grp, x));
 
142
 
 
143
         Botan::TLS::Policy policy;
 
144
         try
 
145
            {
 
146
            policy.check_peer_key_acceptable(*dhkey);
 
147
            result.test_failure("Incorrectly accepting short bit DH keys");
 
148
            }
 
149
         catch(Botan::TLS::TLS_Exception&)
 
150
            {
 
151
            result.test_success("Correctly rejecting short bit DH keys");
 
152
            }
 
153
#endif
 
154
         return result;
 
155
         }
 
156
 
 
157
      Test::Result test_peer_key_acceptable_dsa()
 
158
         {
 
159
         Test::Result result("TLS Policy DSA key verification");
 
160
#if defined(BOTAN_HAS_DSA)
 
161
         const Botan::DL_Group grp_1024("modp/ietf/1024");
 
162
         std::unique_ptr<Botan::Private_Key> dsa_1024(new Botan::DSA_PrivateKey(Test::rng(), grp_1024));
 
163
 
 
164
         Botan::TLS::Policy policy;
 
165
         try
 
166
            {
 
167
            policy.check_peer_key_acceptable(*dsa_1024);
 
168
            result.test_failure("Incorrectly accepting short bit DSA keys");
 
169
            }
 
170
         catch(Botan::TLS::TLS_Exception&)
 
171
            {
 
172
            result.test_success("Correctly rejecting short bit DSA keys");
 
173
            }
 
174
 
 
175
         const Botan::DL_Group grp_2048("modp/ietf/2048");
 
176
         std::unique_ptr<Botan::Private_Key> dsa_2048(new Botan::DSA_PrivateKey(Test::rng(), grp_2048));
 
177
         policy.check_peer_key_acceptable(*dsa_2048);
 
178
         result.test_success("Correctly accepting 2048 bit DSA keys");
 
179
#endif
 
180
         return result;
 
181
         }
 
182
 
 
183
 
 
184
   };
 
185
 
 
186
BOTAN_REGISTER_TEST("tls_policy", TLS_Policy_Unit_Tests);
 
187
 
 
188
#endif
 
189
 
 
190
}
 
191
 
 
192
}