368
371
s->s3->tmp.use_rsa_tmp=0;
370
/* only send if a DH key exchange, fortezza or
373
/* only send if a DH key exchange or
371
374
* RSA but we have a sign only certificate */
372
375
if (s->s3->tmp.use_rsa_tmp
373
|| (l & (SSL_DH|SSL_kFZA))
376
/* PSK: send ServerKeyExchange if PSK identity
377
* hint if provided */
378
#ifndef OPENSSL_NO_PSK
379
|| ((alg_k & SSL_kPSK) && s->ctx->psk_identity_hint)
381
|| (alg_k & (SSL_kEDH|SSL_kDHr|SSL_kDHd))
382
|| (alg_k & SSL_kEECDH)
383
|| ((alg_k & SSL_kRSA)
375
384
&& (s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey == NULL
376
385
|| (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher)
377
386
&& EVP_PKEY_size(s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey)*8 > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)
402
411
/* never request cert in anonymous ciphersuites
403
412
* (see section "Certificate request" in SSL 3 drafts
404
413
* and in RFC 2246): */
405
((s->s3->tmp.new_cipher->algorithms & SSL_aNULL) &&
414
((s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL) &&
406
415
/* ... except when the application insists on verification
407
416
* (against the specs, but s3_clnt.c accepts this for SSL 3) */
408
417
!(s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) ||
409
/* never request cert in Kerberos ciphersuites */
410
(s->s3->tmp.new_cipher->algorithms & SSL_aKRB5))
418
/* never request cert in Kerberos ciphersuites */
419
(s->s3->tmp.new_cipher->algorithm_auth & SSL_aKRB5)
420
/* With normal PSK Certificates and
421
* Certificate Requests are omitted */
422
|| (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK))
412
424
/* no cert request */
479
491
s->state=SSL3_ST_SR_CERT_VRFY_A;
482
/* We need to get hashes here so if there is
483
* a client cert, it can be verified */
484
s->method->ssl3_enc->cert_verify_mac(s,
485
&(s->s3->finish_dgst1),
486
&(s->s3->tmp.cert_verify_md[0]));
487
s->method->ssl3_enc->cert_verify_mac(s,
488
&(s->s3->finish_dgst2),
489
&(s->s3->tmp.cert_verify_md[MD5_DIGEST_LENGTH]));
496
/* For the ECDH ciphersuites when
497
* the client sends its ECDH pub key in
498
* a certificate, the CertificateVerify
499
* message is not sent.
501
s->state=SSL3_ST_SR_FINISHED_A;
506
s->state=SSL3_ST_SR_CERT_VRFY_A;
509
/* We need to get hashes here so if there is
510
* a client cert, it can be verified */
511
s->method->ssl3_enc->cert_verify_mac(s,
513
&(s->s3->tmp.cert_verify_md[0]));
514
s->method->ssl3_enc->cert_verify_mac(s,
516
&(s->s3->tmp.cert_verify_md[MD5_DIGEST_LENGTH]));
493
520
case SSL3_ST_SR_CERT_VRFY_A:
686
713
buf = (unsigned char *)s->init_buf->data;
688
715
msg = p = &(buf[DTLS1_HM_HEADER_LENGTH]);
689
if (s->client_version == DTLS1_BAD_VER)
690
*(p++) = DTLS1_BAD_VER>>8,
691
*(p++) = DTLS1_BAD_VER&0xff;
693
*(p++) = s->version >> 8,
694
*(p++) = s->version & 0xFF;
716
*(p++) = s->version >> 8;
717
*(p++) = s->version & 0xFF;
696
719
if (s->ctx->app_gen_cookie_cb == NULL ||
697
720
s->ctx->app_gen_cookie_cb(s, s->d1->cookie,
988
#ifndef OPENSSL_NO_ECDH
989
if (type & SSL_kEECDH)
991
const EC_GROUP *group;
993
ecdhp=cert->ecdh_tmp;
994
if ((ecdhp == NULL) && (s->cert->ecdh_tmp_cb != NULL))
996
ecdhp=s->cert->ecdh_tmp_cb(s,
997
SSL_C_IS_EXPORT(s->s3->tmp.new_cipher),
998
SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher));
1002
al=SSL_AD_HANDSHAKE_FAILURE;
1003
SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,SSL_R_MISSING_TMP_ECDH_KEY);
1007
if (s->s3->tmp.ecdh != NULL)
1009
EC_KEY_free(s->s3->tmp.ecdh);
1010
SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
1014
/* Duplicate the ECDH structure. */
1017
SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,ERR_R_ECDH_LIB);
1020
if (!EC_KEY_up_ref(ecdhp))
1022
SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,ERR_R_ECDH_LIB);
1027
s->s3->tmp.ecdh=ecdh;
1028
if ((EC_KEY_get0_public_key(ecdh) == NULL) ||
1029
(EC_KEY_get0_private_key(ecdh) == NULL) ||
1030
(s->options & SSL_OP_SINGLE_ECDH_USE))
1032
if(!EC_KEY_generate_key(ecdh))
1034
SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,ERR_R_ECDH_LIB);
1039
if (((group = EC_KEY_get0_group(ecdh)) == NULL) ||
1040
(EC_KEY_get0_public_key(ecdh) == NULL) ||
1041
(EC_KEY_get0_private_key(ecdh) == NULL))
1043
SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,ERR_R_ECDH_LIB);
1047
if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) &&
1048
(EC_GROUP_get_degree(group) > 163))
1050
SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,SSL_R_ECGROUP_TOO_LARGE_FOR_CIPHER);
1054
/* XXX: For now, we only support ephemeral ECDH
1055
* keys over named (not generic) curves. For
1056
* supported named curves, curve_id is non-zero.
1059
tls1_ec_nid2curve_id(EC_GROUP_get_curve_name(group)))
1062
SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,SSL_R_UNSUPPORTED_ELLIPTIC_CURVE);
1066
/* Encode the public key.
1067
* First check the size of encoding and
1068
* allocate memory accordingly.
1070
encodedlen = EC_POINT_point2oct(group,
1071
EC_KEY_get0_public_key(ecdh),
1072
POINT_CONVERSION_UNCOMPRESSED,
1075
encodedPoint = (unsigned char *)
1076
OPENSSL_malloc(encodedlen*sizeof(unsigned char));
1077
bn_ctx = BN_CTX_new();
1078
if ((encodedPoint == NULL) || (bn_ctx == NULL))
1080
SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE);
1085
encodedlen = EC_POINT_point2oct(group,
1086
EC_KEY_get0_public_key(ecdh),
1087
POINT_CONVERSION_UNCOMPRESSED,
1088
encodedPoint, encodedlen, bn_ctx);
1090
if (encodedlen == 0)
1092
SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,ERR_R_ECDH_LIB);
1096
BN_CTX_free(bn_ctx); bn_ctx=NULL;
1098
/* XXX: For now, we only support named (not
1099
* generic) curves in ECDH ephemeral key exchanges.
1100
* In this situation, we need four additional bytes
1101
* to encode the entire ServerECDHParams
1106
/* We'll generate the serverKeyExchange message
1107
* explicitly so we can set these to NULLs
1115
#endif /* !OPENSSL_NO_ECDH */
1116
#ifndef OPENSSL_NO_PSK
1117
if (type & SSL_kPSK)
1119
/* reserve size for record length and PSK identity hint*/
1120
n+=2+strlen(s->ctx->psk_identity_hint);
1123
#endif /* !OPENSSL_NO_PSK */
963
1125
al=SSL_AD_HANDSHAKE_FAILURE;
964
1126
SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);
1167
#ifndef OPENSSL_NO_ECDH
1168
if (type & SSL_kEECDH)
1170
/* XXX: For now, we only support named (not generic) curves.
1171
* In this situation, the serverKeyExchange message has:
1172
* [1 byte CurveType], [2 byte CurveName]
1173
* [1 byte length of encoded point], followed by
1174
* the actual encoded point itself
1176
*p = NAMED_CURVE_TYPE;
1184
memcpy((unsigned char*)p,
1185
(unsigned char *)encodedPoint,
1187
OPENSSL_free(encodedPoint);
1192
#ifndef OPENSSL_NO_PSK
1193
if (type & SSL_kPSK)
1195
/* copy PSK identity hint */
1196
s2n(strlen(s->ctx->psk_identity_hint), p);
1197
strncpy((char *)p, s->ctx->psk_identity_hint, strlen(s->ctx->psk_identity_hint));
1198
p+=strlen(s->ctx->psk_identity_hint);
1004
1202
/* not anonymous */
1005
1203
if (pkey != NULL)
1255
#if !defined(OPENSSL_NO_ECDSA)
1256
if (pkey->type == EVP_PKEY_EC)
1258
/* let's do ECDSA */
1259
EVP_SignInit_ex(&md_ctx,EVP_ecdsa(), NULL);
1260
EVP_SignUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
1261
EVP_SignUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
1262
EVP_SignUpdate(&md_ctx,&(d[4]),n);
1263
if (!EVP_SignFinal(&md_ctx,&(p[2]),
1264
(unsigned int *)&i,pkey))
1266
SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,ERR_LIB_ECDSA);
1058
1275
/* Is this error check actually needed? */
1059
1276
al=SSL_AD_HANDSHAKE_FAILURE;
1193
1414
if (s->state == SSL3_ST_SW_CERT_A)
1195
1416
x=ssl_get_server_send_cert(s);
1197
/* VRS: allow null cert if auth == KRB5 */
1198
(s->s3->tmp.new_cipher->algorithms
1199
& (SSL_MKEY_MASK|SSL_AUTH_MASK))
1200
!= (SSL_aKRB5|SSL_kKRB5))
1202
SSLerr(SSL_F_DTLS1_SEND_SERVER_CERTIFICATE,ERR_R_INTERNAL_ERROR);
1419
/* VRS: allow null cert if auth == KRB5 */
1420
if ((s->s3->tmp.new_cipher->algorithm_mkey != SSL_kKRB5) ||
1421
(s->s3->tmp.new_cipher->algorithm_auth != SSL_aKRB5))
1423
SSLerr(SSL_F_DTLS1_SEND_SERVER_CERTIFICATE,ERR_R_INTERNAL_ERROR);
1206
1428
l=dtls1_output_cert_chain(s,x);