summaryrefslogtreecommitdiff
path: root/sql/encryption.cc
blob: 209b092b0a4799889e6f3b311c7e5d03ab809ba1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/* Copyright (C) 2015 MariaDB

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; version 2 of the License.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA */

#include <my_global.h>
#include <mysql/plugin_encryption.h>
#include "log.h"
#include "sql_plugin.h"
#include <my_crypt.h>

/* there can be only one encryption plugin enabled */
static plugin_ref encryption_manager= 0;
struct encryption_service_st encryption_handler;

uint no_key(uint)
{
  return ENCRYPTION_KEY_VERSION_INVALID;
}

static int ctx_init(void *ctx, const unsigned char* key, unsigned int klen,
                    const unsigned char* iv, unsigned int ivlen, int flags,
                    unsigned int key_id, unsigned int key_version)
{
  return my_aes_crypt_init(ctx, MY_AES_CBC, flags, key, klen, iv, ivlen);
}

static unsigned int get_length(unsigned int slen, unsigned int key_id,
                               unsigned int key_version)
{
  return my_aes_get_size(MY_AES_CBC, slen);
}

int initialize_encryption_plugin(st_plugin_int *plugin)
{
  if (encryption_manager)
    return 1;

  if (plugin->plugin->init && plugin->plugin->init(plugin))
  {
    sql_print_error("Plugin '%s' init function returned error.",
                    plugin->name.str);
    return 1;
  }

  encryption_manager= plugin_lock(NULL, plugin_int_to_ref(plugin));
  st_mariadb_encryption *handle=
    (struct st_mariadb_encryption*) plugin->plugin->info;

  encryption_handler.encryption_ctx_size_func=
    handle->crypt_ctx_size ? handle->crypt_ctx_size :
    (uint (*)(unsigned int, unsigned int))my_aes_ctx_size;

  encryption_handler.encryption_ctx_init_func=
    handle->crypt_ctx_init ? handle->crypt_ctx_init : ctx_init;

  encryption_handler.encryption_ctx_update_func=
    handle->crypt_ctx_update ? handle->crypt_ctx_update : my_aes_crypt_update;

  encryption_handler.encryption_ctx_finish_func=
    handle->crypt_ctx_finish ? handle->crypt_ctx_finish : my_aes_crypt_finish;

  encryption_handler.encryption_encrypted_length_func=
    handle->encrypted_length ? handle->encrypted_length : get_length;

  encryption_handler.encryption_key_get_func=
    handle->get_key;

  encryption_handler.encryption_key_get_latest_version_func=
    handle->get_latest_key_version; // must be the last

  return 0;
}

int finalize_encryption_plugin(st_plugin_int *plugin)
{
  encryption_handler.encryption_key_get_func=
      (uint (*)(uint, uint, uchar*, uint*))no_key;
  encryption_handler.encryption_key_get_latest_version_func= no_key;

  if (plugin && plugin->plugin->deinit && plugin->plugin->deinit(NULL))
  {
    DBUG_PRINT("warning", ("Plugin '%s' deinit function returned error.",
                           plugin->name.str));
  }
  if (encryption_manager)
    plugin_unlock(NULL, encryption_manager);
  encryption_manager= 0;
  return 0;
}

/******************************************************************
  Encryption Scheme service
******************************************************************/
static uint scheme_get_key(st_encryption_scheme *scheme,
                           st_encryption_scheme_key *key)
{
  if (scheme->locker)
    scheme->locker(scheme, 0);

  // Check if we already have key
  for (uint i = 0; i < array_elements(scheme->key); i++)
  {
    if (scheme->key[i].version == 0) // no more keys
      break;

    if (scheme->key[i].version == key->version)
    {
      *key= scheme->key[i];
      if (scheme->locker)
        scheme->locker(scheme, 1);
      return 0;
    }
  }

  // Not found!
  scheme->keyserver_requests++;

  uchar global_key[MY_AES_MAX_KEY_LENGTH];
  uint  global_key_len= sizeof(global_key), key_len;

  uint rc = encryption_key_get(scheme->key_id, key->version,
                              global_key, & global_key_len);
  if (rc)
    goto ret;

  /* Now generate the local key by encrypting IV using the global key */
  rc = my_aes_crypt(MY_AES_ECB, ENCRYPTION_FLAG_ENCRYPT | ENCRYPTION_FLAG_NOPAD,
                    scheme->iv, sizeof(scheme->iv), key->key, &key_len,
                    global_key, global_key_len, NULL, 0);

  DBUG_ASSERT(key_len == sizeof(key->key));

  if (rc)
    goto ret;

  // Rotate keys to make room for a new
  for (uint i = array_elements(scheme->key) - 1; i; i--)
    scheme->key[i] = scheme->key[i - 1];

  scheme->key[0]= *key;

ret:
  if (scheme->locker)
    scheme->locker(scheme, 1);
  return rc;
}

int do_crypt(const unsigned char* src, unsigned int slen,
             unsigned char* dst, unsigned int* dlen,
             struct st_encryption_scheme *scheme,
             unsigned int key_version, unsigned int i32_1,
             unsigned int i32_2, unsigned long long i64,
             int flag)
{
  compile_time_assert(ENCRYPTION_SCHEME_KEY_INVALID ==
                      (int)ENCRYPTION_KEY_VERSION_INVALID);

  // Maybe temporal solution for MDEV-8173
  // Rationale: scheme->type is currently global/object
  // and when used here might not represent actual state
  // of smaller granularity objects e.g. InnoDB page state
  // as type is stored to tablespace (FIL) and could represent
  // state where key rotation is trying to reach
  //DBUG_ASSERT(scheme->type == 1);

  if (key_version == ENCRYPTION_KEY_VERSION_INVALID ||
      key_version == ENCRYPTION_KEY_NOT_ENCRYPTED)
    return ENCRYPTION_SCHEME_KEY_INVALID;

  st_encryption_scheme_key key;
  key.version= key_version;
  uint rc= scheme_get_key(scheme, &key);
  if (rc)
    return (int)rc;

  unsigned char iv[4 + 4 + 8];
  int4store(iv + 0, i32_1);
  int4store(iv + 4, i32_2);
  int8store(iv + 8, i64);

  return encryption_crypt(src, slen, dst, dlen, key.key, sizeof(key.key),
                          iv, sizeof(iv), flag, scheme->key_id, key_version);
}

int encryption_scheme_encrypt(const unsigned char* src, unsigned int slen,
                              unsigned char* dst, unsigned int* dlen,
                              struct st_encryption_scheme *scheme,
                              unsigned int key_version, unsigned int i32_1,
                              unsigned int i32_2, unsigned long long i64)
{
  return do_crypt(src, slen, dst, dlen, scheme, key_version, i32_1,
                  i32_2, i64, ENCRYPTION_FLAG_NOPAD | ENCRYPTION_FLAG_ENCRYPT);
}


int encryption_scheme_decrypt(const unsigned char* src, unsigned int slen,
                              unsigned char* dst, unsigned int* dlen,
                              struct st_encryption_scheme *scheme,
                              unsigned int key_version, unsigned int i32_1,
                              unsigned int i32_2, unsigned long long i64)
{
  return do_crypt(src, slen, dst, dlen, scheme, key_version, i32_1,
                  i32_2, i64, ENCRYPTION_FLAG_NOPAD | ENCRYPTION_FLAG_DECRYPT);
}