Botan  2.1.0
Crypto and TLS for C++11
key_constraint.cpp
Go to the documentation of this file.
1 /*
2 * KeyUsage
3 * (C) 1999-2007,2016 Jack Lloyd
4 * (C) 2016 RenĂ© Korthaus, Rohde & Schwarz Cybersecurity
5 *
6 * Botan is released under the Simplified BSD License (see license.txt)
7 */
8 
9 #include <botan/key_constraint.h>
10 #include <botan/x509_key.h>
11 
12 namespace Botan {
13 
15  {
16  std::vector<std::string> str;
17 
18  if(constraints == NO_CONSTRAINTS)
19  return "no_constraints";
20 
21  if(constraints & DIGITAL_SIGNATURE)
22  str.push_back("digital_signature");
23 
24  if(constraints & NON_REPUDIATION)
25  str.push_back("non_repudiation");
26 
27  if(constraints & KEY_ENCIPHERMENT)
28  str.push_back("key_encipherment");
29 
30  if(constraints & DATA_ENCIPHERMENT)
31  str.push_back("data_encipherment");
32 
33  if(constraints & KEY_AGREEMENT)
34  str.push_back("key_agreement");
35 
36  if(constraints & KEY_CERT_SIGN)
37  str.push_back("key_cert_sign");
38 
39  if(constraints & CRL_SIGN)
40  str.push_back("crl_sign");
41 
42  if(constraints & ENCIPHER_ONLY)
43  str.push_back("encipher_only");
44 
45  if(constraints & DECIPHER_ONLY)
46  str.push_back("decipher_only");
47 
48  // Not 0 (checked at start) but nothing matched above!
49  if(str.empty())
50  return "other_unknown_constraints";
51 
52  if(str.size() == 1)
53  return str[0];
54 
55  std::string out;
56  for(size_t i = 0; i < str.size() - 1; ++i)
57  {
58  out += str[i];
59  out += ',';
60  }
61  out += str[str.size() - 1];
62 
63  return out;
64  }
65 
66 /*
67 * Make sure the given key constraints are permitted for the given key type
68 */
70  Key_Constraints constraints)
71  {
72  const std::string name = pub_key.algo_name();
73 
74  size_t permitted = 0;
75 
76  if(name == "DH" || name == "ECDH")
77  {
78  permitted |= KEY_AGREEMENT | ENCIPHER_ONLY | DECIPHER_ONLY;
79  }
80 
81  if(name == "RSA" || name == "ElGamal")
82  {
83  permitted |= KEY_ENCIPHERMENT | DATA_ENCIPHERMENT;
84  }
85 
86  if(name == "RSA" || name == "DSA" || name == "ECDSA" || name == "ECGDSA" || name == "ECKCDSA" || name == "GOST-34.10")
87  {
89  }
90 
91  if((constraints & permitted) != constraints)
92  {
93  throw Exception("Invalid " + name + " constraints " + key_constraints_to_string(constraints));
94  }
95  }
96 
97 }
virtual std::string algo_name() const =0
void verify_cert_constraints_valid_for_key_type(const Public_Key &pub_key, Key_Constraints constraints)
Definition: alg_id.cpp:13
std::string key_constraints_to_string(Key_Constraints constraints)