File: server_parsec.cc

package info (click to toggle)
mariadb 1%3A11.8.3-0%2Bdeb13u1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 772,508 kB
  • sloc: ansic: 2,414,714; cpp: 1,791,394; asm: 381,336; perl: 62,905; sh: 49,647; pascal: 40,897; java: 39,363; python: 20,791; yacc: 20,432; sql: 17,907; xml: 12,344; ruby: 8,544; cs: 6,542; makefile: 6,145; ada: 1,879; lex: 1,193; javascript: 996; objc: 80; tcl: 73; awk: 46; php: 22
file content (320 lines) | stat: -rw-r--r-- 9,156 bytes parent folder | download | duplicates (3)
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/*
  Copyright (c) 2024, MariaDB plc

  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 Street, Fifth Floor, Boston, MA  02110-1335  USA */

#include <openssl/evp.h>
#include <openssl/rand.h>
#include <openssl/err.h>
#ifdef HAVE_WOLFSSL
#include <openssl/ed25519.h>
#include <wolfcrypt/ed25519.h>
#endif

#include <mysql/plugin_auth.h>
#include <mysql/plugin.h>
#include <mysqld_error.h>
#include "scope.h"

#include <cstring>

typedef unsigned char uchar;
constexpr size_t CHALLENGE_SCRAMBLE_LENGTH= 32;
constexpr size_t CHALLENGE_SALT_LENGTH= 18;
constexpr size_t ED25519_SIG_LENGTH= 64;
constexpr size_t ED25519_KEY_LENGTH= 32;
constexpr size_t PBKDF2_HASH_LENGTH= ED25519_KEY_LENGTH;
constexpr size_t CLIENT_RESPONSE_LENGTH= CHALLENGE_SCRAMBLE_LENGTH
                                         + ED25519_SIG_LENGTH;

constexpr size_t base64_length(size_t input_length)
{
  return ((input_length + 2) / 3) * 4; // with padding
}

constexpr size_t base64_length_raw(size_t input_length)
{
  return (input_length * 4 + 2) / 3; // no padding
}

struct alignas(1) Client_signed_response
{
  union {
    struct {
      uchar client_scramble[CHALLENGE_SCRAMBLE_LENGTH];
      uchar signature[ED25519_SIG_LENGTH];
    };
    uchar start[1];
  };
};

static_assert(sizeof(Client_signed_response) == CLIENT_RESPONSE_LENGTH,
              "Client_signed_response is not aligned.");

struct alignas(1) Passwd_as_stored
{
  char algorithm;
  uchar iterations;
  char colon;
  char salt[base64_length_raw(CHALLENGE_SALT_LENGTH)];
  char colon2;
  char pub_key[base64_length_raw(ED25519_KEY_LENGTH)];
};

struct alignas(1) Passwd_in_memory
{
  char algorithm;
  uchar iterations;
  uchar salt[CHALLENGE_SALT_LENGTH];
  uchar pub_key[ED25519_KEY_LENGTH];
};

int print_ssl_error()
{
  char buf[512];
  unsigned long err= ERR_get_error();
  ERR_error_string_n(err, buf, sizeof buf);
  my_printf_error(err, "parsec: %s", ME_ERROR_LOG_ONLY, buf);
  return 1;
}

static
int compute_derived_key(const char* password, size_t pass_len,
                         const Passwd_in_memory *params, uchar *derived_key)
{
  assert(params->algorithm == 'P');
  int ret = PKCS5_PBKDF2_HMAC(password, (int)pass_len, params->salt,
                              sizeof(params->salt), 1024 << params->iterations,
                              EVP_sha512(), PBKDF2_HASH_LENGTH, derived_key);
  if(ret == 0)
    return print_ssl_error();

  return 0;
}


static
int verify_ed25519(const uchar *public_key, const uchar *signature,
                    const uchar *message, size_t message_len)
{
#ifdef HAVE_WOLFSSL
  int res= wolfSSL_ED25519_verify(message, (unsigned)message_len,
                                  public_key, ED25519_KEY_LENGTH,
                                  signature, ED25519_SIG_LENGTH);
  return res != WOLFSSL_SUCCESS;
#else
  int ret= 1;
  EVP_MD_CTX *mdctx= EVP_MD_CTX_new();
  EVP_PKEY *pkey= EVP_PKEY_new_raw_public_key(EVP_PKEY_ED25519, NULL,
                                              public_key, 32);

  if (pkey && mdctx && EVP_DigestVerifyInit(mdctx, NULL, NULL, NULL, pkey))
    ret= !EVP_DigestVerify(mdctx, signature, ED25519_SIG_LENGTH,
                           message, message_len);
  EVP_MD_CTX_free(mdctx);
  EVP_PKEY_free(pkey);
  return ret;
#endif
}

static
int ed25519_derive_public_key(const uchar *raw_private_key, uchar *pub_key)
{
#ifdef HAVE_WOLFSSL
  ed25519_key key;

  int ret = wc_ed25519_init(&key);
  if (ret != 0)
    return print_ssl_error();

  SCOPE_EXIT([&key](){ wc_ed25519_free(&key); });

  ret = wc_ed25519_import_private_only(raw_private_key, ED25519_KEY_LENGTH,
                                       &key);
  if (ret != 0)
    return print_ssl_error();

  ret = wc_ed25519_make_public(&key, pub_key, ED25519_KEY_LENGTH);
  if (ret != 0)
    return print_ssl_error();

  return false;
#else
  EVP_PKEY *pkey = EVP_PKEY_new_raw_private_key(EVP_PKEY_ED25519, NULL,
                                                raw_private_key,
                                                ED25519_KEY_LENGTH);
  bool res= pkey != NULL;
  size_t len= ED25519_KEY_LENGTH;
  if (pkey)
    res= EVP_PKEY_get_raw_public_key(pkey, pub_key, &len); // 1 == success

  if (!res)
    print_ssl_error();

  EVP_PKEY_free(pkey);
  return !res;
#endif
}

static
int hash_password(const char *password, size_t password_length,
                  char *hash, size_t *hash_length)
{
  auto stored= (Passwd_as_stored*)hash;
  assert(*hash_length >= sizeof(*stored) + 2); // it should fit the buffer

  Passwd_in_memory memory;
  memory.algorithm= 'P';
  memory.iterations= 0;
  my_random_bytes(memory.salt, sizeof(memory.salt));

  uchar derived_key[PBKDF2_HASH_LENGTH];
  if (compute_derived_key(password, password_length, &memory, derived_key))
    return 1;

  if (ed25519_derive_public_key(derived_key, memory.pub_key))
    return 1;

  stored->algorithm= memory.algorithm;
  stored->iterations= memory.iterations + '0';
  my_base64_encode(memory.salt, sizeof(memory.salt), stored->salt);
  my_base64_encode(memory.pub_key, sizeof(memory.pub_key), stored->pub_key);
  stored->colon= stored->colon2= ':';

  *hash_length = sizeof *stored;
  hash[*hash_length]= 0; // safety

  return 0;
}

static
int digest_to_binary(const char *hash, size_t hash_length,
                    unsigned char *out, size_t *out_length)
{
  auto stored= (Passwd_as_stored*)hash;
  auto memory= (Passwd_in_memory*)out;

  if (hash_length != sizeof (*stored) || *out_length < sizeof(*memory) ||
      stored->algorithm != 'P' ||
      stored->iterations < '0' || stored->iterations > '3' ||
      stored->colon != ':' || stored->colon2 != ':')
  {
    my_printf_error(ER_PASSWD_LENGTH, "Wrong ext-salt format", 0);
    return 1;
  }

  *out_length = sizeof(*memory);
  memory->algorithm= stored->algorithm;
  memory->iterations= stored->iterations - '0';

  static_assert(base64_length(CHALLENGE_SALT_LENGTH) == base64_length_raw(CHALLENGE_SALT_LENGTH),
                "Salt is base64-aligned");
  if (my_base64_decode(stored->salt, base64_length(CHALLENGE_SALT_LENGTH),
                       memory->salt, NULL, 0) < 0)
  {
    my_printf_error(ER_PASSWD_LENGTH,
                    "Password salt should be base64 encoded", 0);
    return 1;
  }

  char buf[base64_length(ED25519_KEY_LENGTH)+1];
  constexpr int pad= (int)base64_length(ED25519_KEY_LENGTH)
                     - (int)base64_length_raw(ED25519_KEY_LENGTH);
  static_assert(pad > 0, "base64 length calculation check");
  memcpy(buf, stored->pub_key, base64_length_raw(ED25519_KEY_LENGTH));
  memset(buf + base64_length_raw(ED25519_KEY_LENGTH), '=', pad);
  if (my_base64_decode(buf, base64_length(ED25519_KEY_LENGTH),
                       memory->pub_key, NULL, 0) < 0)
  {
    my_printf_error(ER_PASSWD_LENGTH,
                    "Password-derived key should be base64 encoded", 0);
    return 1;
  }

  return 0;
}

static
int auth(MYSQL_PLUGIN_VIO *vio, MYSQL_SERVER_AUTH_INFO *info)
{
  union
  {
    struct
    {
      uchar server[CHALLENGE_SCRAMBLE_LENGTH];
      uchar client[CHALLENGE_SCRAMBLE_LENGTH];
    };
    uchar start[1];
  } scramble_pair;

  my_random_bytes(scramble_pair.server, CHALLENGE_SCRAMBLE_LENGTH);

  if (vio->write_packet(vio, scramble_pair.server, sizeof(scramble_pair.server)))
    return CR_ERROR;

  // Begin with reading the handshake packet. It should be empty (for now).
  uchar *dummy;
  int bytes_read= vio->read_packet(vio, &dummy);
  if (bytes_read != 0)
    return CR_ERROR;

  auto passwd= (Passwd_in_memory*)info->auth_string;

  if (vio->write_packet(vio, (uchar*)info->auth_string, 2 + CHALLENGE_SALT_LENGTH))
    return CR_ERROR;

  Client_signed_response *client_response;
  bytes_read= vio->read_packet(vio, (uchar**)&client_response);
  if (bytes_read < 0)
    return CR_ERROR;
  if (bytes_read != sizeof *client_response)
    return CR_AUTH_HANDSHAKE;

  memcpy(scramble_pair.client, client_response->client_scramble,
         CHALLENGE_SCRAMBLE_LENGTH);

  if (verify_ed25519(passwd->pub_key, client_response->signature,
                     scramble_pair.start, sizeof(scramble_pair)))
    return CR_AUTH_HANDSHAKE;

  return CR_OK;
}

static struct st_mysql_auth info =
{
  MYSQL_AUTHENTICATION_INTERFACE_VERSION,
  "parsec",
  auth,
  hash_password,
  digest_to_binary
};


maria_declare_plugin(auth_parsec)
{
  MYSQL_AUTHENTICATION_PLUGIN,
  &info,
  "parsec",
  "Nikita Maliavin",
  "Password Authentication using Response Signed with Elliptic Curve",
  PLUGIN_LICENSE_GPL,
  NULL,
  NULL,
  0x0100,
  NULL,
  NULL,
  "1.0",
  MariaDB_PLUGIN_MATURITY_GAMMA
}
maria_declare_plugin_end;