1 /* Copyright (c) 2016, 2021, Oracle and/or its affiliates.
2 
3    This program is free software; you can redistribute it and/or modify
4    it under the terms of the GNU General Public License, version 2.0,
5    as published by the Free Software Foundation.
6 
7    This program is also distributed with certain software (including
8    but not limited to OpenSSL) that is licensed under separate terms,
9    as designated in a particular file or component or in included license
10    documentation.  The authors of MySQL hereby grant you an additional
11    permission to link the program and your derivative works with the
12    separately licensed software that they have included with MySQL.
13 
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License, version 2.0, for more details.
18 
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA */
22 
23 #ifndef KEYRING_KEY_INCLUDED
24 #define KEYRING_KEY_INCLUDED
25 
26 #include <my_global.h>
27 #include "i_keyring_key.h"
28 #include "keyring_memory.h"
29 #include <boost/move/unique_ptr.hpp>
30 
31 namespace keyring {
32 
33 struct Key : IKey
34 {
35   Key(const char *a_key_id, const char *a_key_type, const char *a_user_id,
36       const void *a_key, size_t a_key_len);
37   Key(const Key& other);
38   Key(IKey *other);
39   Key();
40 
41   ~Key();
42 
43   my_bool load_from_buffer(uchar* buffer, size_t *buffer_position,
44                            size_t input_buffer_size);
45   void store_in_buffer(uchar* buffer, size_t *buffer_position) const;
46   std::string* get_key_signature() const;
47   std::string* get_key_type();
48   std::string* get_key_id();
49   std::string* get_user_id();
50   uchar* get_key_data();
51   size_t get_key_data_size();
52   size_t get_key_pod_size() const;
53   uchar* release_key_data();
54   void xor_data();
55   void set_key_data(uchar *key_data, size_t key_data_size);
56   void set_key_type(const std::string *key_type);
57   my_bool is_key_type_valid();
58   my_bool is_key_id_valid();
59   my_bool is_key_valid();
60   my_bool is_key_length_valid();
61 
62 private:
63   void init(const char *a_key_id, const char *a_key_type, const char *a_user_id,
64             const void *a_key, size_t a_key_len);
65 
66   void clear_key_data();
67   void create_key_signature() const;
68   my_bool load_string_from_buffer(const uchar *buffer, size_t *buffer_position,
69                                   size_t key_pod_size, std::string *string,
70                                   size_t string_length);
71   inline void store_field_length(uchar *buffer, size_t *buffer_position,
72                                  size_t length) const;
73   inline void store_field(uchar *buffer, size_t *buffer_position,
74                           const char *field, size_t field_length) const;
75   my_bool load_field_size(const uchar *buffer, size_t *buffer_position,
76                           size_t key_pod_size, size_t *field_length);
77 protected:
78   std::string key_id;
79   std::string key_type;
80   std::string user_id;
81   boost::movelib::unique_ptr<uchar[]> key;
82   size_t key_len;
83   mutable std::string key_signature;
84 };
85 
86 } //namespace keyring
87 
88 #endif //KEYRING_KEY_INCLUDED
89