COMP_METHOD *comp;
#endif
const EVP_MD *m;
- int n, i, j, k, cl;
+ int mdi;
+ size_t n, i, j, k, cl;
int reuse_dd = 0;
c = s->s3->tmp.new_sym_enc;
EVP_CIPHER_CTX_reset(dd);
p = s->s3->tmp.key_block;
- i = EVP_MD_size(m);
- if (i < 0)
+ mdi = EVP_MD_size(m);
+ if (mdi < 0)
goto err2;
+ i = mdi;
cl = EVP_CIPHER_key_length(c);
j = cl;
k = EVP_CIPHER_iv_length(c);
}
int ssl3_generate_master_secret(SSL *s, unsigned char *out, unsigned char *p,
- int len)
+ size_t len, size_t *secret_size)
{
static const unsigned char *salt[3] = {
#ifndef CHARSET_EBCDIC
};
unsigned char buf[EVP_MAX_MD_SIZE];
EVP_MD_CTX *ctx = EVP_MD_CTX_new();
- int i, ret = 0;
+ int i, ret = 1;
unsigned int n;
#ifdef OPENSSL_SSL_TRACE_CRYPTO
unsigned char *tmpout = out;
#endif
+ size_t ret_secret_size = 0;
if (ctx == NULL) {
SSLerr(SSL_F_SSL3_GENERATE_MASTER_SECRET, ERR_R_MALLOC_FAILURE);
SSL3_RANDOM_SIZE) <= 0
|| EVP_DigestUpdate(ctx, &(s->s3->server_random[0]),
SSL3_RANDOM_SIZE) <= 0
+ /* TODO(size_t) : convert me */
|| EVP_DigestFinal_ex(ctx, buf, &n) <= 0
|| EVP_DigestInit_ex(ctx, s->ctx->md5, NULL) <= 0
|| EVP_DigestUpdate(ctx, p, len) <= 0
break;
}
out += n;
- ret += n;
+ ret_secret_size += n;
}
EVP_MD_CTX_free(ctx);
#ifdef OPENSSL_SSL_TRACE_CRYPTO
- if (ret > 0 && s->msg_callback) {
+ if (ret && s->msg_callback) {
s->msg_callback(2, s->version, TLS1_RT_CRYPTO_PREMASTER,
p, len, s, s->msg_callback_arg);
s->msg_callback(2, s->version, TLS1_RT_CRYPTO_CLIENT_RANDOM,
}
#endif
OPENSSL_cleanse(buf, sizeof(buf));
- return (ret);
+ if (ret)
+ *secret_size = ret_secret_size;
+ return ret;
}
int ssl3_alert_code(int code)
int free_pms)
{
unsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
+ int ret = 0;
+
if (alg_k & SSL_PSK) {
#ifndef OPENSSL_NO_PSK
unsigned char *pskpms, *t;
pskpmslen = 4 + pmslen + psklen;
pskpms = OPENSSL_malloc(pskpmslen);
- if (pskpms == NULL) {
- s->session->master_key_length = 0;
+ if (pskpms == NULL)
goto err;
- }
t = pskpms;
s2n(pmslen, t);
if (alg_k & SSL_kPSK)
OPENSSL_clear_free(s->s3->tmp.psk, psklen);
s->s3->tmp.psk = NULL;
- s->session->master_key_length =
- s->method->ssl3_enc->generate_master_secret(s,
- s->session->master_key,
- pskpms, pskpmslen);
+ if (!s->method->ssl3_enc->generate_master_secret(s,
+ s->session->master_key,pskpms, pskpmslen,
+ &s->session->master_key_length))
+ goto err;
OPENSSL_clear_free(pskpms, pskpmslen);
#else
/* Should never happen */
- s->session->master_key_length = 0;
goto err;
#endif
} else {
- s->session->master_key_length =
- s->method->ssl3_enc->generate_master_secret(s,
- s->session->master_key,
- pms, pmslen);
+ if (!s->method->ssl3_enc->generate_master_secret(s,
+ s->session->master_key, pms, pmslen,
+ &s->session->master_key_length))
+ goto err;
}
+ ret = 1;
err:
if (pms) {
if (free_pms)
}
if (s->server == 0)
s->s3->tmp.pms = NULL;
- return s->session->master_key_length >= 0;
+ return ret;
}
/* Generate a private key from parameters */
EVP_PKEY_HMAC,
};
-static int ssl_mac_secret_size[SSL_MD_NUM_IDX];
+static size_t ssl_mac_secret_size[SSL_MD_NUM_IDX];
#define CIPHER_ADD 1
#define CIPHER_KILL 2
if (md == NULL) {
disabled_mac_mask |= t->mask;
} else {
- ssl_mac_secret_size[i] = EVP_MD_size(md);
- OPENSSL_assert(ssl_mac_secret_size[i] >= 0);
+ int tmpsize = EVP_MD_size(md);
+ OPENSSL_assert(tmpsize >= 0);
+ ssl_mac_secret_size[i] = tmpsize;
}
}
/* Make sure we can access MD5 and SHA1 */
int ssl_cipher_get_evp(const SSL_SESSION *s, const EVP_CIPHER **enc,
const EVP_MD **md, int *mac_pkey_type,
- int *mac_secret_size, SSL_COMP **comp, int use_etm)
+ size_t *mac_secret_size, SSL_COMP **comp, int use_etm)
{
int i;
const SSL_CIPHER *c;
(int (*)(SSL *, SSL3_RECORD *, size_t, int))ssl_undefined_function,
(int (*)(SSL *, SSL3_RECORD *, unsigned char *, int))ssl_undefined_function,
ssl_undefined_function,
- (int (*)(SSL *, unsigned char *, unsigned char *, int))
+ (int (*)(SSL *, unsigned char *, unsigned char *, size_t, size_t *))
ssl_undefined_function,
(int (*)(SSL *, int))ssl_undefined_function,
(int (*)(SSL *, const char *, int, unsigned char *))
size_t SSL_SESSION_get_master_key(const SSL_SESSION *session,
unsigned char *out, size_t outlen)
{
- if (session->master_key_length < 0) {
- /* Should never happen */
- return 0;
- }
if (outlen == 0)
return session->master_key_length;
- if (outlen > (size_t)session->master_key_length)
+ if (outlen > session->master_key_length)
outlen = session->master_key_length;
memcpy(out, session->master_key, outlen);
return outlen;
}
/* Retrieve handshake hashes */
-int ssl_handshake_hash(SSL *s, unsigned char *out, int outlen)
+int ssl_handshake_hash(SSL *s, unsigned char *out, size_t outlen,
+ size_t *hashlen)
{
EVP_MD_CTX *ctx = NULL;
EVP_MD_CTX *hdgst = s->s3->handshake_dgst;
- int ret = EVP_MD_CTX_size(hdgst);
- if (ret < 0 || ret > outlen) {
- ret = 0;
+ int hashleni = EVP_MD_CTX_size(hdgst);
+ int ret = 0;
+
+ if (hashleni < 0 || (size_t)hashleni > outlen)
goto err;
- }
+
ctx = EVP_MD_CTX_new();
- if (ctx == NULL) {
- ret = 0;
+ if (ctx == NULL)
goto err;
- }
+
if (!EVP_MD_CTX_copy_ex(ctx, hdgst)
|| EVP_DigestFinal_ex(ctx, out, NULL) <= 0)
- ret = 0;
+ goto err;
+
+ *hashlen = hashleni;
+
+ ret = 1;
err:
EVP_MD_CTX_free(ctx);
return ret;
struct ssl_session_st {
int ssl_version; /* what ssl version session info is being kept
* in here? */
- int master_key_length;
+ size_t master_key_length;
unsigned char master_key[SSL_MAX_MASTER_KEY_LENGTH];
/* session_id - valid? */
unsigned int session_id_length;
int ctype_num;
char ctype[SSL3_CT_NUMBER];
STACK_OF(X509_NAME) *ca_names;
- int key_block_length;
+ size_t key_block_length;
unsigned char *key_block;
const EVP_CIPHER *new_sym_enc;
const EVP_MD *new_hash;
int (*mac) (SSL *, SSL3_RECORD *, unsigned char *, int);
int (*setup_key_block) (SSL *);
int (*generate_master_secret) (SSL *, unsigned char *, unsigned char *,
- int);
+ size_t, size_t *);
int (*change_cipher_state) (SSL *, int);
int (*final_finish_mac) (SSL *, const char *, int, unsigned char *);
int finish_mac_length;
void ssl_update_cache(SSL *s, int mode);
__owur int ssl_cipher_get_evp(const SSL_SESSION *s, const EVP_CIPHER **enc,
const EVP_MD **md, int *mac_pkey_type,
- int *mac_secret_size, SSL_COMP **comp,
+ size_t *mac_secret_size, SSL_COMP **comp,
int use_etm);
__owur int ssl_cipher_get_overhead(const SSL_CIPHER *c, size_t *mac_overhead,
size_t *int_overhead, size_t *blocksize,
__owur int ssl3_do_write(SSL *s, int type);
int ssl3_send_alert(SSL *s, int level, int desc);
__owur int ssl3_generate_master_secret(SSL *s, unsigned char *out,
- unsigned char *p, int len);
+ unsigned char *p, size_t len,
+ size_t *secret_size);
__owur int ssl3_get_req_cert_type(SSL *s, WPACKET *pkt);
__owur int ssl3_num_ciphers(void);
__owur const SSL_CIPHER *ssl3_get_cipher(unsigned int u);
__owur int tls1_final_finish_mac(SSL *s,
const char *str, int slen, unsigned char *p);
__owur int tls1_generate_master_secret(SSL *s, unsigned char *out,
- unsigned char *p, int len);
+ unsigned char *p, size_t len,
+ size_t *secret_size);
__owur int tls1_export_keying_material(SSL *s, unsigned char *out, size_t olen,
const char *label, size_t llen,
const unsigned char *p, size_t plen,
__owur int ssl_parse_clienthello_use_srtp_ext(SSL *s, PACKET *pkt, int *al);
__owur int ssl_parse_serverhello_use_srtp_ext(SSL *s, PACKET *pkt, int *al);
-__owur int ssl_handshake_hash(SSL *s, unsigned char *out, int outlen);
+__owur int ssl_handshake_hash(SSL *s, unsigned char *out, size_t outlen,
+ size_t *hashlen);
__owur const EVP_MD *ssl_md(int idx);
__owur const EVP_MD *ssl_handshake_md(SSL *s);
__owur const EVP_MD *ssl_prf_md(SSL *s);
if (s->version >= TLS1_VERSION && s->tls_session_secret_cb &&
s->session->tlsext_tick) {
const SSL_CIPHER *pref_cipher = NULL;
- s->session->master_key_length = sizeof(s->session->master_key);
+ /*
+ * s->session->master_key_length is a size_t, but this is an int for
+ * backwards compat reasons
+ */
+ int master_key_length;
+ master_key_length = sizeof(s->session->master_key);
if (s->tls_session_secret_cb(s, s->session->master_key,
- &s->session->master_key_length,
+ &master_key_length,
NULL, &pref_cipher,
- s->tls_session_secret_cb_arg)) {
+ s->tls_session_secret_cb_arg)
+ && master_key_length > 0) {
+ s->session->master_key_length = master_key_length;
s->session->cipher = pref_cipher ?
pref_cipher : ssl_get_cipher_by_char(s, cipherchars);
} else {
if (!s->hit && s->version >= TLS1_VERSION && s->tls_session_secret_cb) {
const SSL_CIPHER *pref_cipher = NULL;
+ /*
+ * s->session->master_key_length is a size_t, but this is an int for
+ * backwards compat reasons
+ */
+ int master_key_length;
- s->session->master_key_length = sizeof(s->session->master_key);
+ master_key_length = sizeof(s->session->master_key);
if (s->tls_session_secret_cb(s, s->session->master_key,
- &s->session->master_key_length, ciphers,
+ &master_key_length, ciphers,
&pref_cipher,
- s->tls_session_secret_cb_arg)) {
+ s->tls_session_secret_cb_arg)
+ && master_key_length > 0) {
+ s->session->master_key_length = master_key_length;
s->hit = 1;
s->session->ciphers = ciphers;
s->session->verify_result = X509_V_OK;
return ret;
}
-static int tls1_generate_key_block(SSL *s, unsigned char *km, int num)
+/* TODO(size_t): convert me */
+static int tls1_generate_key_block(SSL *s, unsigned char *km, size_t num)
{
int ret;
ret = tls1_PRF(s,
unsigned char *p;
const EVP_CIPHER *c;
const EVP_MD *hash;
- int num;
SSL_COMP *comp;
- int mac_type = NID_undef, mac_secret_size = 0;
+ int mac_type = NID_undef;
+ size_t num, mac_secret_size = 0;
int ret = 0;
if (s->s3->tmp.key_block_length != 0)
}
printf("master key\n");
{
- int z;
+ size_t z;
for (z = 0; z < s->session->master_key_length; z++)
printf("%02X%c", s->session->master_key[z],
((z + 1) % 16) ? ' ' : '\n');
#ifdef SSL_DEBUG
printf("\nkey block\n");
{
- int z;
+ size_t z;
for (z = 0; z < num; z++)
printf("%02X%c", p[z], ((z + 1) % 16) ? ' ' : '\n');
}
int tls1_final_finish_mac(SSL *s, const char *str, int slen, unsigned char *out)
{
- int hashlen;
+ size_t hashlen;
unsigned char hash[EVP_MAX_MD_SIZE];
if (!ssl3_digest_cached_records(s, 0))
return 0;
- hashlen = ssl_handshake_hash(s, hash, sizeof(hash));
-
- if (hashlen == 0)
+ if (!ssl_handshake_hash(s, hash, sizeof(hash), &hashlen))
return 0;
if (!tls1_PRF(s, str, slen, hash, hashlen, NULL, 0, NULL, 0, NULL, 0,
}
int tls1_generate_master_secret(SSL *s, unsigned char *out, unsigned char *p,
- int len)
+ size_t len, size_t *secret_size)
{
if (s->session->flags & SSL_SESS_FLAG_EXTMS) {
unsigned char hash[EVP_MAX_MD_SIZE * 2];
- int hashlen;
+ size_t hashlen;
/*
* Digest cached records keeping record buffer (if present): this wont
* affect client auth because we're freezing the buffer at the same
* point (after client key exchange and before certificate verify)
*/
if (!ssl3_digest_cached_records(s, 1))
- return -1;
- hashlen = ssl_handshake_hash(s, hash, sizeof(hash));
+ return 0;
+ if(!ssl_handshake_hash(s, hash, sizeof(hash), &hashlen))
+ return 0;
#ifdef SSL_DEBUG
fprintf(stderr, "Handshake hashes:\n");
BIO_dump_fp(stderr, (char *)hash, hashlen);
}
#endif
- return (SSL3_MASTER_SECRET_SIZE);
+ *secret_size = SSL3_MASTER_SECRET_SIZE;
+ return 1;
}
int tls1_export_keying_material(SSL *s, unsigned char *out, size_t olen,