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 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507
|
/*
* Copyright (C) 2011-2012 Free Software Foundation, Inc.
* Copyright (C) 2017 Red Hat, Inc.
*
* Author: Nikos Mavrogiannopoulos
*
* This file is part of GnuTLS.
*
* The GnuTLS is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>
*
*/
/* This file contains common stuff in Ephemeral Diffie-Hellman (DHE)
* and Anonymous DH key exchange(DHA). These are used in the handshake
* procedure of the certificate and anonymous authentication.
*/
#include "gnutls_int.h"
#include "auth.h"
#include "errors.h"
#include "dh.h"
#include "num.h"
#include "tls-sig.h"
#include "state.h"
#include "datum.h"
#include "x509.h"
#include "auth/ecdhe.h"
#include "ecc.h"
#include "ext/supported_groups.h"
#include "algorithms.h"
#include "auth/psk.h"
#include "auth/cert.h"
#include "pk.h"
static int gen_ecdhe_server_kx(gnutls_session_t, gnutls_buffer_st *);
static int proc_ecdhe_server_kx(gnutls_session_t session, uint8_t *data,
size_t _data_size);
static int proc_ecdhe_client_kx(gnutls_session_t session, uint8_t *data,
size_t _data_size);
#if defined(ENABLE_ECDHE)
const mod_auth_st ecdhe_ecdsa_auth_struct = {
"ECDHE_ECDSA",
_gnutls_gen_cert_server_crt,
_gnutls_gen_cert_client_crt,
gen_ecdhe_server_kx,
_gnutls_gen_ecdh_common_client_kx, /* This is the only difference */
_gnutls_gen_cert_client_crt_vrfy,
_gnutls_gen_cert_server_cert_req,
_gnutls_proc_crt,
_gnutls_proc_crt,
proc_ecdhe_server_kx,
proc_ecdhe_client_kx,
_gnutls_proc_cert_client_crt_vrfy,
_gnutls_proc_cert_cert_req
};
const mod_auth_st ecdhe_rsa_auth_struct = {
"ECDHE_RSA",
_gnutls_gen_cert_server_crt,
_gnutls_gen_cert_client_crt,
gen_ecdhe_server_kx,
_gnutls_gen_ecdh_common_client_kx, /* This is the only difference */
_gnutls_gen_cert_client_crt_vrfy,
_gnutls_gen_cert_server_cert_req,
_gnutls_proc_crt,
_gnutls_proc_crt,
proc_ecdhe_server_kx,
proc_ecdhe_client_kx,
_gnutls_proc_cert_client_crt_vrfy,
_gnutls_proc_cert_cert_req
};
static int calc_ecdh_key(gnutls_session_t session, gnutls_datum_t *psk_key,
const gnutls_ecc_curve_entry_st *ecurve)
{
gnutls_pk_params_st pub;
int ret;
gnutls_datum_t tmp_dh_key;
gnutls_pk_params_init(&pub);
pub.params[ECC_X] = session->key.proto.tls12.ecdh.x;
pub.params[ECC_Y] = session->key.proto.tls12.ecdh.y;
pub.raw_pub.data = session->key.proto.tls12.ecdh.raw.data;
pub.raw_pub.size = session->key.proto.tls12.ecdh.raw.size;
pub.curve = ecurve->id;
ret = _gnutls_pk_derive(ecurve->pk, &tmp_dh_key,
&session->key.proto.tls12.ecdh.params, &pub);
if (ret < 0) {
ret = gnutls_assert_val(ret);
goto cleanup;
}
if (psk_key == NULL) {
memcpy(&session->key.key, &tmp_dh_key, sizeof(gnutls_datum_t));
tmp_dh_key.data = NULL; /* no longer needed */
} else {
ret = _gnutls_set_psk_session_key(session, psk_key,
&tmp_dh_key);
_gnutls_free_key_datum(&tmp_dh_key);
if (ret < 0) {
ret = gnutls_assert_val(ret);
goto cleanup;
}
}
ret = 0;
cleanup:
/* no longer needed */
_gnutls_mpi_release(&session->key.proto.tls12.ecdh.x);
_gnutls_mpi_release(&session->key.proto.tls12.ecdh.y);
_gnutls_free_datum(&session->key.proto.tls12.ecdh.raw);
gnutls_pk_params_release(&session->key.proto.tls12.ecdh.params);
return ret;
}
int _gnutls_proc_ecdh_common_client_kx(
gnutls_session_t session, uint8_t *data, size_t _data_size,
const struct gnutls_group_entry_st *group, gnutls_datum_t *psk_key)
{
ssize_t data_size = _data_size;
int ret, i = 0;
unsigned point_size;
const gnutls_ecc_curve_entry_st *ecurve;
if (group == NULL)
return gnutls_assert_val(GNUTLS_E_ECC_NO_SUPPORTED_CURVES);
ecurve = _gnutls_ecc_curve_get_params(group->curve);
if (ecurve == NULL)
return gnutls_assert_val(GNUTLS_E_ECC_NO_SUPPORTED_CURVES);
DECR_LEN(data_size, 1);
point_size = data[i];
i += 1;
if (point_size == 0) {
ret = gnutls_assert_val(GNUTLS_E_UNEXPECTED_PACKET_LENGTH);
goto cleanup;
}
DECR_LEN(data_size, point_size);
if (ecurve->pk == GNUTLS_PK_EC) {
ret = _gnutls_ecc_ansi_x962_import(
&data[i], point_size, &session->key.proto.tls12.ecdh.x,
&session->key.proto.tls12.ecdh.y);
if (ret < 0) {
gnutls_assert();
goto cleanup;
}
} else if (ecurve->pk == GNUTLS_PK_ECDH_X25519 ||
ecurve->pk == GNUTLS_PK_ECDH_X448) {
if (ecurve->size != point_size)
return gnutls_assert_val(
GNUTLS_E_RECEIVED_ILLEGAL_PARAMETER);
ret = _gnutls_set_datum(&session->key.proto.tls12.ecdh.raw,
&data[i], point_size);
if (ret < 0) {
gnutls_assert();
goto cleanup;
}
/* RFC7748 requires to mask the MSB in the final byte
* for X25519 (not X448) */
if (ecurve->id == GNUTLS_ECC_CURVE_X25519) {
session->key.proto.tls12.ecdh.raw.data[point_size - 1] &=
0x7f;
}
} else {
return gnutls_assert_val(GNUTLS_E_RECEIVED_ILLEGAL_PARAMETER);
}
if (data_size != 0) {
ret = gnutls_assert_val(GNUTLS_E_UNEXPECTED_PACKET_LENGTH);
goto cleanup;
}
/* generate pre-shared key */
ret = calc_ecdh_key(session, psk_key, ecurve);
if (ret < 0) {
gnutls_assert();
goto cleanup;
}
cleanup:
_gnutls_mpi_release(&session->key.proto.tls12.ecdh.x);
_gnutls_mpi_release(&session->key.proto.tls12.ecdh.y);
_gnutls_free_datum(&session->key.proto.tls12.ecdh.raw);
gnutls_pk_params_clear(&session->key.proto.tls12.ecdh.params);
return ret;
}
static int proc_ecdhe_client_kx(gnutls_session_t session, uint8_t *data,
size_t _data_size)
{
gnutls_certificate_credentials_t cred;
cred = (gnutls_certificate_credentials_t)_gnutls_get_cred(
session, GNUTLS_CRD_CERTIFICATE);
if (cred == NULL) {
gnutls_assert();
return GNUTLS_E_INSUFFICIENT_CREDENTIALS;
}
return _gnutls_proc_ecdh_common_client_kx(session, data, _data_size,
get_group(session), NULL);
}
int _gnutls_gen_ecdh_common_client_kx(gnutls_session_t session,
gnutls_buffer_st *data)
{
return _gnutls_gen_ecdh_common_client_kx_int(session, data, NULL);
}
int _gnutls_gen_ecdh_common_client_kx_int(gnutls_session_t session,
gnutls_buffer_st *data,
gnutls_datum_t *psk_key)
{
int ret;
gnutls_datum_t out;
const gnutls_group_entry_st *group = get_group(session);
const gnutls_ecc_curve_entry_st *ecurve;
int pk;
unsigned init_pos = data->length;
if (group == NULL)
return gnutls_assert_val(GNUTLS_E_ECC_NO_SUPPORTED_CURVES);
ecurve = _gnutls_ecc_curve_get_params(group->curve);
if (ecurve == NULL)
return gnutls_assert_val(GNUTLS_E_ECC_NO_SUPPORTED_CURVES);
pk = ecurve->pk;
/* generate temporal key */
ret = _gnutls_pk_generate_keys(
pk, ecurve->id, &session->key.proto.tls12.ecdh.params, 1);
if (ret < 0)
return gnutls_assert_val(ret);
if (pk == GNUTLS_PK_EC) {
ret = _gnutls_ecc_ansi_x962_export(
ecurve->id,
session->key.proto.tls12.ecdh.params
.params[ECC_X] /* x */,
session->key.proto.tls12.ecdh.params
.params[ECC_Y] /* y */,
&out);
if (ret < 0) {
gnutls_assert();
goto cleanup;
}
ret = _gnutls_buffer_append_data_prefix(data, 8, out.data,
out.size);
_gnutls_free_datum(&out);
if (ret < 0) {
gnutls_assert();
goto cleanup;
}
} else if (pk == GNUTLS_PK_ECDH_X25519 || pk == GNUTLS_PK_ECDH_X448) {
ret = _gnutls_buffer_append_data_prefix(
data, 8,
session->key.proto.tls12.ecdh.params.raw_pub.data,
session->key.proto.tls12.ecdh.params.raw_pub.size);
if (ret < 0) {
gnutls_assert();
goto cleanup;
}
}
/* generate pre-shared key */
ret = calc_ecdh_key(session, psk_key, ecurve);
if (ret < 0) {
gnutls_assert();
goto cleanup;
}
ret = data->length - init_pos;
cleanup:
gnutls_pk_params_clear(&session->key.proto.tls12.ecdh.params);
return ret;
}
static int proc_ecdhe_server_kx(gnutls_session_t session, uint8_t *data,
size_t _data_size)
{
int ret;
gnutls_datum_t vparams;
ret = _gnutls_proc_ecdh_common_server_kx(session, data, _data_size);
if (ret < 0)
return gnutls_assert_val(ret);
vparams.data = data;
vparams.size = ret;
return _gnutls_proc_dhe_signature(session, data + ret, _data_size - ret,
&vparams);
}
int _gnutls_proc_ecdh_common_server_kx(gnutls_session_t session, uint8_t *data,
size_t _data_size)
{
int i, ret;
unsigned point_size;
const gnutls_group_entry_st *group;
ssize_t data_size = _data_size;
const gnutls_ecc_curve_entry_st *ecurve;
/* just in case we are resuming a session */
gnutls_pk_params_release(&session->key.proto.tls12.ecdh.params);
gnutls_pk_params_init(&session->key.proto.tls12.ecdh.params);
i = 0;
DECR_LEN(data_size, 1);
if (data[i++] != 3)
return gnutls_assert_val(GNUTLS_E_ECC_NO_SUPPORTED_CURVES);
DECR_LEN(data_size, 2);
group = _gnutls_tls_id_to_group(_gnutls_read_uint16(&data[i]));
if (group == NULL || group->curve == 0) {
_gnutls_debug_log("received unknown curve %u.%u\n",
(unsigned)data[i], (unsigned)data[i + 1]);
return gnutls_assert_val(GNUTLS_E_RECEIVED_ILLEGAL_PARAMETER);
} else {
_gnutls_debug_log("received curve %s\n", group->name);
}
i += 2;
if (!_gnutls_session_supports_group(session, group->id))
return gnutls_assert_val(GNUTLS_E_ECC_UNSUPPORTED_CURVE);
ecurve = _gnutls_ecc_curve_get_params(group->curve);
if (ecurve == NULL) {
return gnutls_assert_val(GNUTLS_E_RECEIVED_ILLEGAL_PARAMETER);
}
_gnutls_session_group_set(session, group);
DECR_LEN(data_size, 1);
point_size = data[i];
i++;
DECR_LEN(data_size, point_size);
if (ecurve->pk == GNUTLS_PK_EC) {
ret = _gnutls_ecc_ansi_x962_import(
&data[i], point_size, &session->key.proto.tls12.ecdh.x,
&session->key.proto.tls12.ecdh.y);
if (ret < 0)
return gnutls_assert_val(ret);
} else if (ecurve->pk == GNUTLS_PK_ECDH_X25519 ||
ecurve->pk == GNUTLS_PK_ECDH_X448) {
if (ecurve->size != point_size)
return gnutls_assert_val(
GNUTLS_E_RECEIVED_ILLEGAL_PARAMETER);
ret = _gnutls_set_datum(&session->key.proto.tls12.ecdh.raw,
&data[i], point_size);
if (ret < 0)
return gnutls_assert_val(ret);
/* RFC7748 requires to mask the MSB in the final byte
* for X25519 (not X448) */
if (ecurve->id == GNUTLS_ECC_CURVE_X25519) {
session->key.proto.tls12.ecdh.raw.data[point_size - 1] &=
0x7f;
}
} else {
return gnutls_assert_val(GNUTLS_E_RECEIVED_ILLEGAL_PARAMETER);
}
i += point_size;
return i;
}
/* If the psk flag is set, then an empty psk_identity_hint will
* be inserted */
int _gnutls_ecdh_common_print_server_kx(gnutls_session_t session,
gnutls_buffer_st *data,
const gnutls_group_entry_st *group)
{
uint8_t p;
int ret;
gnutls_datum_t out;
unsigned init_pos = data->length;
if (group == NULL || group->curve == 0)
return gnutls_assert_val(GNUTLS_E_ECC_NO_SUPPORTED_CURVES);
/* just in case we are resuming a session */
gnutls_pk_params_release(&session->key.proto.tls12.ecdh.params);
gnutls_pk_params_init(&session->key.proto.tls12.ecdh.params);
/* curve type */
p = 3;
ret = _gnutls_buffer_append_data(data, &p, 1);
if (ret < 0)
return gnutls_assert_val(ret);
ret = _gnutls_buffer_append_prefix(data, 16, group->tls_id);
if (ret < 0)
return gnutls_assert_val(ret);
/* generate temporal key */
ret = _gnutls_pk_generate_keys(group->pk, group->curve,
&session->key.proto.tls12.ecdh.params,
1);
if (ret < 0)
return gnutls_assert_val(ret);
if (group->pk == GNUTLS_PK_EC) {
ret = _gnutls_ecc_ansi_x962_export(
group->curve,
session->key.proto.tls12.ecdh.params
.params[ECC_X] /* x */,
session->key.proto.tls12.ecdh.params
.params[ECC_Y] /* y */,
&out);
if (ret < 0)
return gnutls_assert_val(ret);
ret = _gnutls_buffer_append_data_prefix(data, 8, out.data,
out.size);
_gnutls_free_datum(&out);
if (ret < 0)
return gnutls_assert_val(ret);
} else if (group->pk == GNUTLS_PK_ECDH_X25519 ||
group->pk == GNUTLS_PK_ECDH_X448) {
ret = _gnutls_buffer_append_data_prefix(
data, 8,
session->key.proto.tls12.ecdh.params.raw_pub.data,
session->key.proto.tls12.ecdh.params.raw_pub.size);
if (ret < 0)
return gnutls_assert_val(ret);
} else {
return gnutls_assert_val(GNUTLS_E_ECC_NO_SUPPORTED_CURVES);
}
return data->length - init_pos;
}
static int gen_ecdhe_server_kx(gnutls_session_t session, gnutls_buffer_st *data)
{
int ret = 0;
gnutls_certificate_credentials_t cred;
unsigned sig_pos;
cred = (gnutls_certificate_credentials_t)_gnutls_get_cred(
session, GNUTLS_CRD_CERTIFICATE);
if (cred == NULL) {
gnutls_assert();
return GNUTLS_E_INSUFFICIENT_CREDENTIALS;
}
if ((ret = _gnutls_auth_info_init(session, GNUTLS_CRD_CERTIFICATE,
sizeof(cert_auth_info_st), 1)) < 0) {
gnutls_assert();
return ret;
}
sig_pos = data->length;
ret = _gnutls_ecdh_common_print_server_kx(session, data,
get_group(session));
if (ret < 0) {
gnutls_assert();
return ret;
}
/* Generate the signature. */
return _gnutls_gen_dhe_signature(session, data, &data->data[sig_pos],
data->length - sig_pos);
}
#endif
|