project
stringclasses
633 values
commit_id
stringlengths
7
81
target
int64
0
1
func
stringlengths
5
484k
cwe
stringclasses
131 values
big_vul_idx
float64
0
189k
idx
int64
0
522k
hash
stringlengths
34
39
size
float64
1
24k
message
stringlengths
0
11.5k
dataset
stringclasses
1 value
openssl
77c77f0a1b9f15b869ca3342186dfbedd1119d0e
0
static int do_ssl3_write(SSL *s, int type, const unsigned char *buf, unsigned int len, int create_empty_fragment) { unsigned char *p, *plen; int i, mac_size, clear = 0; int prefix_len = 0; int eivlen; long align = 0; SSL3_RECORD *wr; SSL3_BUFFER *wb = &(s->s3->wbuf); SSL_SESSION *sess; /* * first check if there is a SSL3_BUFFER still being written out. This * will happen with non blocking IO */ if (wb->left != 0) return (ssl3_write_pending(s, type, buf, len)); /* If we have an alert to send, lets send it */ if (s->s3->alert_dispatch) { i = s->method->ssl_dispatch_alert(s); if (i <= 0) return (i); /* if it went, fall through and send more stuff */ } if (wb->buf == NULL) if (!ssl3_setup_write_buffer(s)) return -1; if (len == 0 && !create_empty_fragment) return 0; wr = &(s->s3->wrec); sess = s->session; if ((sess == NULL) || (s->enc_write_ctx == NULL) || (EVP_MD_CTX_md(s->write_hash) == NULL)) { #if 1 clear = s->enc_write_ctx ? 0 : 1; /* must be AEAD cipher */ #else clear = 1; #endif mac_size = 0; } else { mac_size = EVP_MD_CTX_size(s->write_hash); if (mac_size < 0) goto err; } /* * 'create_empty_fragment' is true only when this function calls itself */ if (!clear && !create_empty_fragment && !s->s3->empty_fragment_done) { /* * countermeasure against known-IV weakness in CBC ciphersuites (see * http://www.openssl.org/~bodo/tls-cbc.txt) */ if (s->s3->need_empty_fragments && type == SSL3_RT_APPLICATION_DATA) { /* * recursive function call with 'create_empty_fragment' set; this * prepares and buffers the data for an empty fragment (these * 'prefix_len' bytes are sent out later together with the actual * payload) */ prefix_len = do_ssl3_write(s, type, buf, 0, 1); if (prefix_len <= 0) goto err; if (prefix_len > (SSL3_RT_HEADER_LENGTH + SSL3_RT_SEND_MAX_ENCRYPTED_OVERHEAD)) { /* insufficient space */ SSLerr(SSL_F_DO_SSL3_WRITE, ERR_R_INTERNAL_ERROR); goto err; } } s->s3->empty_fragment_done = 1; } if (create_empty_fragment) { #if defined(SSL3_ALIGN_PAYLOAD) && SSL3_ALIGN_PAYLOAD!=0 /* * extra fragment would be couple of cipher blocks, which would be * multiple of SSL3_ALIGN_PAYLOAD, so if we want to align the real * payload, then we can just pretent we simply have two headers. */ align = (long)wb->buf + 2 * SSL3_RT_HEADER_LENGTH; align = (-align) & (SSL3_ALIGN_PAYLOAD - 1); #endif p = wb->buf + align; wb->offset = align; } else if (prefix_len) { p = wb->buf + wb->offset + prefix_len; } else { #if defined(SSL3_ALIGN_PAYLOAD) && SSL3_ALIGN_PAYLOAD!=0 align = (long)wb->buf + SSL3_RT_HEADER_LENGTH; align = (-align) & (SSL3_ALIGN_PAYLOAD - 1); #endif p = wb->buf + align; wb->offset = align; } /* write the header */ *(p++) = type & 0xff; wr->type = type; *(p++) = (s->version >> 8); /* * Some servers hang if iniatial client hello is larger than 256 bytes * and record version number > TLS 1.0 */ if (s->state == SSL3_ST_CW_CLNT_HELLO_B && !s->renegotiate && TLS1_get_version(s) > TLS1_VERSION) *(p++) = 0x1; else *(p++) = s->version & 0xff; /* field where we are to write out packet length */ plen = p; p += 2; /* Explicit IV length, block ciphers appropriate version flag */ if (s->enc_write_ctx && SSL_USE_EXPLICIT_IV(s)) { int mode = EVP_CIPHER_CTX_mode(s->enc_write_ctx); if (mode == EVP_CIPH_CBC_MODE) { eivlen = EVP_CIPHER_CTX_iv_length(s->enc_write_ctx); if (eivlen <= 1) eivlen = 0; } /* Need explicit part of IV for GCM mode */ else if (mode == EVP_CIPH_GCM_MODE) eivlen = EVP_GCM_TLS_EXPLICIT_IV_LEN; else eivlen = 0; } else eivlen = 0; /* lets setup the record stuff. */ wr->data = p + eivlen; wr->length = (int)len; wr->input = (unsigned char *)buf; /* * we now 'read' from wr->input, wr->length bytes into wr->data */ /* first we compress */ if (s->compress != NULL) { if (!ssl3_do_compress(s)) { SSLerr(SSL_F_DO_SSL3_WRITE, SSL_R_COMPRESSION_FAILURE); goto err; } } else { memcpy(wr->data, wr->input, wr->length); wr->input = wr->data; } /* * we should still have the output to wr->data and the input from * wr->input. Length should be wr->length. wr->data still points in the * wb->buf */ if (mac_size != 0) { if (s->method->ssl3_enc->mac(s, &(p[wr->length + eivlen]), 1) < 0) goto err; wr->length += mac_size; } wr->input = p; wr->data = p; if (eivlen) { /* * if (RAND_pseudo_bytes(p, eivlen) <= 0) goto err; */ wr->length += eivlen; } if (s->method->ssl3_enc->enc(s, 1) < 1) goto err; /* record length after mac and block padding */ s2n(wr->length, plen); if (s->msg_callback) s->msg_callback(1, 0, SSL3_RT_HEADER, plen - 5, 5, s, s->msg_callback_arg); /* * we should now have wr->data pointing to the encrypted data, which is * wr->length long */ wr->type = type; /* not needed but helps for debugging */ wr->length += SSL3_RT_HEADER_LENGTH; if (create_empty_fragment) { /* * we are in a recursive call; just return the length, don't write * out anything here */ return wr->length; } /* now let's set up wb */ wb->left = prefix_len + wr->length; /* * memorize arguments so that ssl3_write_pending can detect bad write * retries later */ s->s3->wpend_tot = len; s->s3->wpend_buf = buf; s->s3->wpend_type = type; s->s3->wpend_ret = len; /* we now just need to write the buffer */ return ssl3_write_pending(s, type, buf, len); err: return -1; }
CWE-17
6,182
14,750
100255526887249787179031522391302107362
null
null
null
openssl
77c77f0a1b9f15b869ca3342186dfbedd1119d0e
0
int ssl3_dispatch_alert(SSL *s) { int i, j; void (*cb) (const SSL *ssl, int type, int val) = NULL; s->s3->alert_dispatch = 0; i = do_ssl3_write(s, SSL3_RT_ALERT, &s->s3->send_alert[0], 2, 0); if (i <= 0) { s->s3->alert_dispatch = 1; } else { /* * Alert sent to BIO. If it is important, flush it now. If the * message does not get sent due to non-blocking IO, we will not * worry too much. */ if (s->s3->send_alert[0] == SSL3_AL_FATAL) (void)BIO_flush(s->wbio); if (s->msg_callback) s->msg_callback(1, s->version, SSL3_RT_ALERT, s->s3->send_alert, 2, s, s->msg_callback_arg); if (s->info_callback != NULL) cb = s->info_callback; else if (s->ctx->info_callback != NULL) cb = s->ctx->info_callback; if (cb != NULL) { j = (s->s3->send_alert[0] << 8) | s->s3->send_alert[1]; cb(s, SSL_CB_WRITE_ALERT, j); } } return (i); }
CWE-17
6,183
14,751
63267667001341509194080052019211616406
null
null
null
openssl
77c77f0a1b9f15b869ca3342186dfbedd1119d0e
0
int ssl3_do_change_cipher_spec(SSL *s) { int i; const char *sender; int slen; if (s->state & SSL_ST_ACCEPT) i = SSL3_CHANGE_CIPHER_SERVER_READ; else i = SSL3_CHANGE_CIPHER_CLIENT_READ; if (s->s3->tmp.key_block == NULL) { if (s->session == NULL || s->session->master_key_length == 0) { /* might happen if dtls1_read_bytes() calls this */ SSLerr(SSL_F_SSL3_DO_CHANGE_CIPHER_SPEC, SSL_R_CCS_RECEIVED_EARLY); return (0); } s->session->cipher = s->s3->tmp.new_cipher; if (!s->method->ssl3_enc->setup_key_block(s)) return (0); } if (!s->method->ssl3_enc->change_cipher_state(s, i)) return (0); /* * we have to record the message digest at this point so we can get it * before we read the finished message */ if (s->state & SSL_ST_CONNECT) { sender = s->method->ssl3_enc->server_finished_label; slen = s->method->ssl3_enc->server_finished_label_len; } else { sender = s->method->ssl3_enc->client_finished_label; slen = s->method->ssl3_enc->client_finished_label_len; } i = s->method->ssl3_enc->final_finish_mac(s, sender, slen, s->s3->tmp.peer_finish_md); if (i == 0) { SSLerr(SSL_F_SSL3_DO_CHANGE_CIPHER_SPEC, ERR_R_INTERNAL_ERROR); return 0; } s->s3->tmp.peer_finish_md_len = i; return (1); }
CWE-17
6,184
14,752
163250121607684853032200146149354167019
null
null
null
openssl
77c77f0a1b9f15b869ca3342186dfbedd1119d0e
0
int ssl3_do_compress(SSL *ssl) { #ifndef OPENSSL_NO_COMP int i; SSL3_RECORD *wr; wr = &(ssl->s3->wrec); i = COMP_compress_block(ssl->compress, wr->data, SSL3_RT_MAX_COMPRESSED_LENGTH, wr->input, (int)wr->length); if (i < 0) return (0); else wr->length = i; wr->input = wr->data; #endif return (1); }
CWE-17
6,185
14,753
40050387906104112352656517676354654877
null
null
null
openssl
77c77f0a1b9f15b869ca3342186dfbedd1119d0e
0
int ssl3_do_uncompress(SSL *ssl) { #ifndef OPENSSL_NO_COMP int i; SSL3_RECORD *rr; rr = &(ssl->s3->rrec); i = COMP_expand_block(ssl->expand, rr->comp, SSL3_RT_MAX_PLAIN_LENGTH, rr->data, (int)rr->length); if (i < 0) return (0); else rr->length = i; rr->data = rr->comp; #endif return (1); }
CWE-17
6,186
14,754
236781945036889274740418993480314083927
null
null
null
openssl
77c77f0a1b9f15b869ca3342186dfbedd1119d0e
0
int ssl3_read_bytes(SSL *s, int type, unsigned char *buf, int len, int peek) { int al, i, j, ret; unsigned int n; SSL3_RECORD *rr; void (*cb) (const SSL *ssl, int type2, int val) = NULL; if (s->s3->rbuf.buf == NULL) /* Not initialized yet */ if (!ssl3_setup_read_buffer(s)) return (-1); if ((type && (type != SSL3_RT_APPLICATION_DATA) && (type != SSL3_RT_HANDSHAKE)) || (peek && (type != SSL3_RT_APPLICATION_DATA))) { SSLerr(SSL_F_SSL3_READ_BYTES, ERR_R_INTERNAL_ERROR); return -1; } if ((type == SSL3_RT_HANDSHAKE) && (s->s3->handshake_fragment_len > 0)) /* (partially) satisfy request from storage */ { unsigned char *src = s->s3->handshake_fragment; unsigned char *dst = buf; unsigned int k; /* peek == 0 */ n = 0; while ((len > 0) && (s->s3->handshake_fragment_len > 0)) { *dst++ = *src++; len--; s->s3->handshake_fragment_len--; n++; } /* move any remaining fragment bytes: */ for (k = 0; k < s->s3->handshake_fragment_len; k++) s->s3->handshake_fragment[k] = *src++; return n; } /* * Now s->s3->handshake_fragment_len == 0 if type == SSL3_RT_HANDSHAKE. */ if (!s->in_handshake && SSL_in_init(s)) { /* type == SSL3_RT_APPLICATION_DATA */ i = s->handshake_func(s); if (i < 0) return (i); if (i == 0) { SSLerr(SSL_F_SSL3_READ_BYTES, SSL_R_SSL_HANDSHAKE_FAILURE); return (-1); } } start: s->rwstate = SSL_NOTHING; /*- * s->s3->rrec.type - is the type of record * s->s3->rrec.data, - data * s->s3->rrec.off, - offset into 'data' for next read * s->s3->rrec.length, - number of bytes. */ rr = &(s->s3->rrec); /* get new packet if necessary */ if ((rr->length == 0) || (s->rstate == SSL_ST_READ_BODY)) { ret = ssl3_get_record(s); if (ret <= 0) return (ret); } /* we now have a packet which can be read and processed */ if (s->s3->change_cipher_spec /* set when we receive ChangeCipherSpec, * reset by ssl3_get_finished */ && (rr->type != SSL3_RT_HANDSHAKE)) { al = SSL_AD_UNEXPECTED_MESSAGE; SSLerr(SSL_F_SSL3_READ_BYTES, SSL_R_DATA_BETWEEN_CCS_AND_FINISHED); goto f_err; } /* * If the other end has shut down, throw anything we read away (even in * 'peek' mode) */ if (s->shutdown & SSL_RECEIVED_SHUTDOWN) { rr->length = 0; s->rwstate = SSL_NOTHING; return (0); } if (type == rr->type) { /* SSL3_RT_APPLICATION_DATA or * SSL3_RT_HANDSHAKE */ /* * make sure that we are not getting application data when we are * doing a handshake for the first time */ if (SSL_in_init(s) && (type == SSL3_RT_APPLICATION_DATA) && (s->enc_read_ctx == NULL)) { al = SSL_AD_UNEXPECTED_MESSAGE; SSLerr(SSL_F_SSL3_READ_BYTES, SSL_R_APP_DATA_IN_HANDSHAKE); goto f_err; } if (len <= 0) return (len); if ((unsigned int)len > rr->length) n = rr->length; else n = (unsigned int)len; memcpy(buf, &(rr->data[rr->off]), n); if (!peek) { rr->length -= n; rr->off += n; if (rr->length == 0) { s->rstate = SSL_ST_READ_HEADER; rr->off = 0; if (s->mode & SSL_MODE_RELEASE_BUFFERS && s->s3->rbuf.left == 0) ssl3_release_read_buffer(s); } } return (n); } /* * If we get here, then type != rr->type; if we have a handshake message, * then it was unexpected (Hello Request or Client Hello). */ /* * In case of record types for which we have 'fragment' storage, fill * that so that we can process the data at a fixed place. */ { unsigned int dest_maxlen = 0; unsigned char *dest = NULL; unsigned int *dest_len = NULL; if (rr->type == SSL3_RT_HANDSHAKE) { dest_maxlen = sizeof s->s3->handshake_fragment; dest = s->s3->handshake_fragment; dest_len = &s->s3->handshake_fragment_len; } else if (rr->type == SSL3_RT_ALERT) { dest_maxlen = sizeof s->s3->alert_fragment; dest = s->s3->alert_fragment; dest_len = &s->s3->alert_fragment_len; } #ifndef OPENSSL_NO_HEARTBEATS else if (rr->type == TLS1_RT_HEARTBEAT) { tls1_process_heartbeat(s); /* Exit and notify application to read again */ rr->length = 0; s->rwstate = SSL_READING; BIO_clear_retry_flags(SSL_get_rbio(s)); BIO_set_retry_read(SSL_get_rbio(s)); return (-1); } #endif if (dest_maxlen > 0) { n = dest_maxlen - *dest_len; /* available space in 'dest' */ if (rr->length < n) n = rr->length; /* available bytes */ /* now move 'n' bytes: */ while (n-- > 0) { dest[(*dest_len)++] = rr->data[rr->off++]; rr->length--; } if (*dest_len < dest_maxlen) goto start; /* fragment was too small */ } } /*- * s->s3->handshake_fragment_len == 4 iff rr->type == SSL3_RT_HANDSHAKE; * s->s3->alert_fragment_len == 2 iff rr->type == SSL3_RT_ALERT. * (Possibly rr is 'empty' now, i.e. rr->length may be 0.) */ /* If we are a client, check for an incoming 'Hello Request': */ if ((!s->server) && (s->s3->handshake_fragment_len >= 4) && (s->s3->handshake_fragment[0] == SSL3_MT_HELLO_REQUEST) && (s->session != NULL) && (s->session->cipher != NULL)) { s->s3->handshake_fragment_len = 0; if ((s->s3->handshake_fragment[1] != 0) || (s->s3->handshake_fragment[2] != 0) || (s->s3->handshake_fragment[3] != 0)) { al = SSL_AD_DECODE_ERROR; SSLerr(SSL_F_SSL3_READ_BYTES, SSL_R_BAD_HELLO_REQUEST); goto f_err; } if (s->msg_callback) s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE, s->s3->handshake_fragment, 4, s, s->msg_callback_arg); if (SSL_is_init_finished(s) && !(s->s3->flags & SSL3_FLAGS_NO_RENEGOTIATE_CIPHERS) && !s->s3->renegotiate) { ssl3_renegotiate(s); if (ssl3_renegotiate_check(s)) { i = s->handshake_func(s); if (i < 0) return (i); if (i == 0) { SSLerr(SSL_F_SSL3_READ_BYTES, SSL_R_SSL_HANDSHAKE_FAILURE); return (-1); } if (!(s->mode & SSL_MODE_AUTO_RETRY)) { if (s->s3->rbuf.left == 0) { /* no read-ahead left? */ BIO *bio; /* * In the case where we try to read application data, * but we trigger an SSL handshake, we return -1 with * the retry option set. Otherwise renegotiation may * cause nasty problems in the blocking world */ s->rwstate = SSL_READING; bio = SSL_get_rbio(s); BIO_clear_retry_flags(bio); BIO_set_retry_read(bio); return (-1); } } } } /* * we either finished a handshake or ignored the request, now try * again to obtain the (application) data we were asked for */ goto start; } /* * If we are a server and get a client hello when renegotiation isn't * allowed send back a no renegotiation alert and carry on. WARNING: * experimental code, needs reviewing (steve) */ if (s->server && SSL_is_init_finished(s) && !s->s3->send_connection_binding && (s->version > SSL3_VERSION) && (s->s3->handshake_fragment_len >= 4) && (s->s3->handshake_fragment[0] == SSL3_MT_CLIENT_HELLO) && (s->session != NULL) && (s->session->cipher != NULL) && !(s->ctx->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION)) { /* * s->s3->handshake_fragment_len = 0; */ rr->length = 0; ssl3_send_alert(s, SSL3_AL_WARNING, SSL_AD_NO_RENEGOTIATION); goto start; } if (s->s3->alert_fragment_len >= 2) { int alert_level = s->s3->alert_fragment[0]; int alert_descr = s->s3->alert_fragment[1]; s->s3->alert_fragment_len = 0; if (s->msg_callback) s->msg_callback(0, s->version, SSL3_RT_ALERT, s->s3->alert_fragment, 2, s, s->msg_callback_arg); if (s->info_callback != NULL) cb = s->info_callback; else if (s->ctx->info_callback != NULL) cb = s->ctx->info_callback; if (cb != NULL) { j = (alert_level << 8) | alert_descr; cb(s, SSL_CB_READ_ALERT, j); } if (alert_level == SSL3_AL_WARNING) { s->s3->warn_alert = alert_descr; if (alert_descr == SSL_AD_CLOSE_NOTIFY) { s->shutdown |= SSL_RECEIVED_SHUTDOWN; return (0); } /* * This is a warning but we receive it if we requested * renegotiation and the peer denied it. Terminate with a fatal * alert because if application tried to renegotiatie it * presumably had a good reason and expects it to succeed. In * future we might have a renegotiation where we don't care if * the peer refused it where we carry on. */ else if (alert_descr == SSL_AD_NO_RENEGOTIATION) { al = SSL_AD_HANDSHAKE_FAILURE; SSLerr(SSL_F_SSL3_READ_BYTES, SSL_R_NO_RENEGOTIATION); goto f_err; } #ifdef SSL_AD_MISSING_SRP_USERNAME else if (alert_descr == SSL_AD_MISSING_SRP_USERNAME) return (0); #endif } else if (alert_level == SSL3_AL_FATAL) { char tmp[16]; s->rwstate = SSL_NOTHING; s->s3->fatal_alert = alert_descr; SSLerr(SSL_F_SSL3_READ_BYTES, SSL_AD_REASON_OFFSET + alert_descr); BIO_snprintf(tmp, sizeof tmp, "%d", alert_descr); ERR_add_error_data(2, "SSL alert number ", tmp); s->shutdown |= SSL_RECEIVED_SHUTDOWN; SSL_CTX_remove_session(s->ctx, s->session); return (0); } else { al = SSL_AD_ILLEGAL_PARAMETER; SSLerr(SSL_F_SSL3_READ_BYTES, SSL_R_UNKNOWN_ALERT_TYPE); goto f_err; } goto start; } if (s->shutdown & SSL_SENT_SHUTDOWN) { /* but we have not received a * shutdown */ s->rwstate = SSL_NOTHING; rr->length = 0; return (0); } if (rr->type == SSL3_RT_CHANGE_CIPHER_SPEC) { /* * 'Change Cipher Spec' is just a single byte, so we know exactly * what the record payload has to look like */ if ((rr->length != 1) || (rr->off != 0) || (rr->data[0] != SSL3_MT_CCS)) { al = SSL_AD_ILLEGAL_PARAMETER; SSLerr(SSL_F_SSL3_READ_BYTES, SSL_R_BAD_CHANGE_CIPHER_SPEC); goto f_err; } /* Check we have a cipher to change to */ if (s->s3->tmp.new_cipher == NULL) { al = SSL_AD_UNEXPECTED_MESSAGE; SSLerr(SSL_F_SSL3_READ_BYTES, SSL_R_CCS_RECEIVED_EARLY); goto f_err; } if (!(s->s3->flags & SSL3_FLAGS_CCS_OK)) { al = SSL_AD_UNEXPECTED_MESSAGE; SSLerr(SSL_F_SSL3_READ_BYTES, SSL_R_CCS_RECEIVED_EARLY); goto f_err; } s->s3->flags &= ~SSL3_FLAGS_CCS_OK; rr->length = 0; if (s->msg_callback) s->msg_callback(0, s->version, SSL3_RT_CHANGE_CIPHER_SPEC, rr->data, 1, s, s->msg_callback_arg); s->s3->change_cipher_spec = 1; if (!ssl3_do_change_cipher_spec(s)) goto err; else goto start; } /* * Unexpected handshake message (Client Hello, or protocol violation) */ if ((s->s3->handshake_fragment_len >= 4) && !s->in_handshake) { if (((s->state & SSL_ST_MASK) == SSL_ST_OK) && !(s->s3->flags & SSL3_FLAGS_NO_RENEGOTIATE_CIPHERS)) { #if 0 /* worked only because C operator preferences * are not as expected (and because this is * not really needed for clients except for * detecting protocol violations): */ s->state = SSL_ST_BEFORE | (s->server) ? SSL_ST_ACCEPT : SSL_ST_CONNECT; #else s->state = s->server ? SSL_ST_ACCEPT : SSL_ST_CONNECT; #endif s->renegotiate = 1; s->new_session = 1; } i = s->handshake_func(s); if (i < 0) return (i); if (i == 0) { SSLerr(SSL_F_SSL3_READ_BYTES, SSL_R_SSL_HANDSHAKE_FAILURE); return (-1); } if (!(s->mode & SSL_MODE_AUTO_RETRY)) { if (s->s3->rbuf.left == 0) { /* no read-ahead left? */ BIO *bio; /* * In the case where we try to read application data, but we * trigger an SSL handshake, we return -1 with the retry * option set. Otherwise renegotiation may cause nasty * problems in the blocking world */ s->rwstate = SSL_READING; bio = SSL_get_rbio(s); BIO_clear_retry_flags(bio); BIO_set_retry_read(bio); return (-1); } } goto start; } switch (rr->type) { default: #ifndef OPENSSL_NO_TLS /* * TLS up to v1.1 just ignores unknown message types: TLS v1.2 give * an unexpected message alert. */ if (s->version >= TLS1_VERSION && s->version <= TLS1_1_VERSION) { rr->length = 0; goto start; } #endif al = SSL_AD_UNEXPECTED_MESSAGE; SSLerr(SSL_F_SSL3_READ_BYTES, SSL_R_UNEXPECTED_RECORD); goto f_err; case SSL3_RT_CHANGE_CIPHER_SPEC: case SSL3_RT_ALERT: case SSL3_RT_HANDSHAKE: /* * we already handled all of these, with the possible exception of * SSL3_RT_HANDSHAKE when s->in_handshake is set, but that should not * happen when type != rr->type */ al = SSL_AD_UNEXPECTED_MESSAGE; SSLerr(SSL_F_SSL3_READ_BYTES, ERR_R_INTERNAL_ERROR); goto f_err; case SSL3_RT_APPLICATION_DATA: /* * At this point, we were expecting handshake data, but have * application data. If the library was running inside ssl3_read() * (i.e. in_read_app_data is set) and it makes sense to read * application data at this point (session renegotiation not yet * started), we will indulge it. */ if (s->s3->in_read_app_data && (s->s3->total_renegotiations != 0) && (((s->state & SSL_ST_CONNECT) && (s->state >= SSL3_ST_CW_CLNT_HELLO_A) && (s->state <= SSL3_ST_CR_SRVR_HELLO_A) ) || ((s->state & SSL_ST_ACCEPT) && (s->state <= SSL3_ST_SW_HELLO_REQ_A) && (s->state >= SSL3_ST_SR_CLNT_HELLO_A) ) )) { s->s3->in_read_app_data = 2; return (-1); } else { al = SSL_AD_UNEXPECTED_MESSAGE; SSLerr(SSL_F_SSL3_READ_BYTES, SSL_R_UNEXPECTED_RECORD); goto f_err; } } /* not reached */ f_err: ssl3_send_alert(s, SSL3_AL_FATAL, al); err: return (-1); }
CWE-17
6,188
14,755
213523268971293285190280400519702691666
null
null
null
openssl
77c77f0a1b9f15b869ca3342186dfbedd1119d0e
0
int ssl3_read_n(SSL *s, int n, int max, int extend) { /* * If extend == 0, obtain new n-byte packet; if extend == 1, increase * packet by another n bytes. The packet will be in the sub-array of * s->s3->rbuf.buf specified by s->packet and s->packet_length. (If * s->read_ahead is set, 'max' bytes may be stored in rbuf [plus * s->packet_length bytes if extend == 1].) */ int i, len, left; long align = 0; unsigned char *pkt; SSL3_BUFFER *rb; if (n <= 0) return n; rb = &(s->s3->rbuf); if (rb->buf == NULL) if (!ssl3_setup_read_buffer(s)) return -1; left = rb->left; #if defined(SSL3_ALIGN_PAYLOAD) && SSL3_ALIGN_PAYLOAD!=0 align = (long)rb->buf + SSL3_RT_HEADER_LENGTH; align = (-align) & (SSL3_ALIGN_PAYLOAD - 1); #endif if (!extend) { /* start with empty packet ... */ if (left == 0) rb->offset = align; else if (align != 0 && left >= SSL3_RT_HEADER_LENGTH) { /* * check if next packet length is large enough to justify payload * alignment... */ pkt = rb->buf + rb->offset; if (pkt[0] == SSL3_RT_APPLICATION_DATA && (pkt[3] << 8 | pkt[4]) >= 128) { /* * Note that even if packet is corrupted and its length field * is insane, we can only be led to wrong decision about * whether memmove will occur or not. Header values has no * effect on memmove arguments and therefore no buffer * overrun can be triggered. */ memmove(rb->buf + align, pkt, left); rb->offset = align; } } s->packet = rb->buf + rb->offset; s->packet_length = 0; /* ... now we can act as if 'extend' was set */ } /* * For DTLS/UDP reads should not span multiple packets because the read * operation returns the whole packet at once (as long as it fits into * the buffer). */ if (SSL_IS_DTLS(s)) { if (left == 0 && extend) return 0; if (left > 0 && n > left) n = left; } /* if there is enough in the buffer from a previous read, take some */ if (left >= n) { s->packet_length += n; rb->left = left - n; rb->offset += n; return (n); } /* else we need to read more data */ len = s->packet_length; pkt = rb->buf + align; /* * Move any available bytes to front of buffer: 'len' bytes already * pointed to by 'packet', 'left' extra ones at the end */ if (s->packet != pkt) { /* len > 0 */ memmove(pkt, s->packet, len + left); s->packet = pkt; rb->offset = len + align; } if (n > (int)(rb->len - rb->offset)) { /* does not happen */ SSLerr(SSL_F_SSL3_READ_N, ERR_R_INTERNAL_ERROR); return -1; } /* We always act like read_ahead is set for DTLS */ if (!s->read_ahead && !SSL_IS_DTLS(s)) /* ignore max parameter */ max = n; else { if (max < n) max = n; if (max > (int)(rb->len - rb->offset)) max = rb->len - rb->offset; } while (left < n) { /* * Now we have len+left bytes at the front of s->s3->rbuf.buf and * need to read in more until we have len+n (up to len+max if * possible) */ clear_sys_error(); if (s->rbio != NULL) { s->rwstate = SSL_READING; i = BIO_read(s->rbio, pkt + len + left, max - left); } else { SSLerr(SSL_F_SSL3_READ_N, SSL_R_READ_BIO_NOT_SET); i = -1; } if (i <= 0) { rb->left = left; if (s->mode & SSL_MODE_RELEASE_BUFFERS && !SSL_IS_DTLS(s)) if (len + left == 0) ssl3_release_read_buffer(s); return (i); } left += i; /* * reads should *never* span multiple packets for DTLS because the * underlying transport protocol is message oriented as opposed to * byte oriented as in the TLS case. */ if (SSL_IS_DTLS(s)) { if (n > left) n = left; /* makes the while condition false */ } } /* done reading, now the book-keeping */ rb->offset += n; rb->left = left - n; s->packet_length += n; s->rwstate = SSL_NOTHING; return (n); }
CWE-17
6,189
14,756
78283209699039679525599624319471723191
null
null
null
openssl
77c77f0a1b9f15b869ca3342186dfbedd1119d0e
0
int ssl3_send_alert(SSL *s, int level, int desc) { /* Map tls/ssl alert value to correct one */ desc = s->method->ssl3_enc->alert_value(desc); if (s->version == SSL3_VERSION && desc == SSL_AD_PROTOCOL_VERSION) desc = SSL_AD_HANDSHAKE_FAILURE; /* SSL 3.0 does not have * protocol_version alerts */ if (desc < 0) return -1; /* If a fatal one, remove from cache */ if ((level == 2) && (s->session != NULL)) SSL_CTX_remove_session(s->ctx, s->session); s->s3->alert_dispatch = 1; s->s3->send_alert[0] = level; s->s3->send_alert[1] = desc; if (s->s3->wbuf.left == 0) /* data still being written out? */ return s->method->ssl_dispatch_alert(s); /* * else data is still being written out, we will get written some time in * the future */ return -1; }
CWE-17
6,190
14,757
75952729699479371292912026081495960413
null
null
null
openssl
77c77f0a1b9f15b869ca3342186dfbedd1119d0e
0
int ssl3_write_pending(SSL *s, int type, const unsigned char *buf, unsigned int len) { int i; SSL3_BUFFER *wb = &(s->s3->wbuf); /* XXXX */ if ((s->s3->wpend_tot > (int)len) || ((s->s3->wpend_buf != buf) && !(s->mode & SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER)) || (s->s3->wpend_type != type)) { SSLerr(SSL_F_SSL3_WRITE_PENDING, SSL_R_BAD_WRITE_RETRY); return (-1); } for (;;) { clear_sys_error(); if (s->wbio != NULL) { s->rwstate = SSL_WRITING; i = BIO_write(s->wbio, (char *)&(wb->buf[wb->offset]), (unsigned int)wb->left); } else { SSLerr(SSL_F_SSL3_WRITE_PENDING, SSL_R_BIO_NOT_SET); i = -1; } if (i == wb->left) { wb->left = 0; wb->offset += i; s->rwstate = SSL_NOTHING; return (s->s3->wpend_ret); } else if (i <= 0) { if (s->version == DTLS1_VERSION || s->version == DTLS1_BAD_VER) { /* * For DTLS, just drop it. That's kind of the whole point in * using a datagram service */ wb->left = 0; } return (i); } wb->offset += i; wb->left -= i; } }
CWE-17
6,191
14,758
145890566282550731597615174755212541410
null
null
null
openssl
28a00bcd8e318da18031b2ac8778c64147cd54f9
0
int X509_REQ_add1_attr(X509_REQ *req, X509_ATTRIBUTE *attr) { if (X509at_add1_attr(&req->req_info->attributes, attr)) return 1; return 0; }
6,195
14,762
312747069449741658623703698902856756207
null
null
null
openssl
28a00bcd8e318da18031b2ac8778c64147cd54f9
0
int X509_REQ_add1_attr_by_NID(X509_REQ *req, int nid, int type, const unsigned char *bytes, int len) { if (X509at_add1_attr_by_NID(&req->req_info->attributes, nid, type, bytes, len)) return 1; return 0; }
6,196
14,763
229233162085290528269363622701106706305
null
null
null
openssl
28a00bcd8e318da18031b2ac8778c64147cd54f9
0
int X509_REQ_add1_attr_by_OBJ(X509_REQ *req, const ASN1_OBJECT *obj, int type, const unsigned char *bytes, int len) { if (X509at_add1_attr_by_OBJ(&req->req_info->attributes, obj, type, bytes, len)) return 1; return 0; }
6,197
14,764
265289798109913445567266848517714341614
null
null
null
openssl
28a00bcd8e318da18031b2ac8778c64147cd54f9
0
int X509_REQ_add1_attr_by_txt(X509_REQ *req, const char *attrname, int type, const unsigned char *bytes, int len) { if (X509at_add1_attr_by_txt(&req->req_info->attributes, attrname, type, bytes, len)) return 1; return 0; }
6,198
14,765
252598778374344236721967776508238224679
null
null
null
openssl
28a00bcd8e318da18031b2ac8778c64147cd54f9
0
int X509_REQ_add_extensions(X509_REQ *req, STACK_OF(X509_EXTENSION) *exts) { return X509_REQ_add_extensions_nid(req, exts, NID_ext_req); }
6,199
14,766
35605886833053477126787528201098386445
null
null
null
openssl
28a00bcd8e318da18031b2ac8778c64147cd54f9
0
int X509_REQ_add_extensions_nid(X509_REQ *req, STACK_OF(X509_EXTENSION) *exts, int nid) { ASN1_TYPE *at = NULL; X509_ATTRIBUTE *attr = NULL; if (!(at = ASN1_TYPE_new()) || !(at->value.sequence = ASN1_STRING_new())) goto err; at->type = V_ASN1_SEQUENCE; /* Generate encoding of extensions */ at->value.sequence->length = ASN1_item_i2d((ASN1_VALUE *)exts, &at->value.sequence->data, ASN1_ITEM_rptr(X509_EXTENSIONS)); if (!(attr = X509_ATTRIBUTE_new())) goto err; if (!(attr->value.set = sk_ASN1_TYPE_new_null())) goto err; if (!sk_ASN1_TYPE_push(attr->value.set, at)) goto err; at = NULL; attr->single = 0; attr->object = OBJ_nid2obj(nid); if (!req->req_info->attributes) { if (!(req->req_info->attributes = sk_X509_ATTRIBUTE_new_null())) goto err; } if (!sk_X509_ATTRIBUTE_push(req->req_info->attributes, attr)) goto err; return 1; err: X509_ATTRIBUTE_free(attr); ASN1_TYPE_free(at); return 0; }
6,200
14,767
316725098228597746442538173334218476896
null
null
null
openssl
28a00bcd8e318da18031b2ac8778c64147cd54f9
0
int X509_REQ_check_private_key(X509_REQ *x, EVP_PKEY *k) { EVP_PKEY *xk = NULL; int ok = 0; xk = X509_REQ_get_pubkey(x); switch (EVP_PKEY_cmp(xk, k)) { case 1: ok = 1; break; case 0: X509err(X509_F_X509_REQ_CHECK_PRIVATE_KEY, X509_R_KEY_VALUES_MISMATCH); break; case -1: X509err(X509_F_X509_REQ_CHECK_PRIVATE_KEY, X509_R_KEY_TYPE_MISMATCH); break; case -2: #ifndef OPENSSL_NO_EC if (k->type == EVP_PKEY_EC) { X509err(X509_F_X509_REQ_CHECK_PRIVATE_KEY, ERR_R_EC_LIB); break; } #endif #ifndef OPENSSL_NO_DH if (k->type == EVP_PKEY_DH) { /* No idea */ X509err(X509_F_X509_REQ_CHECK_PRIVATE_KEY, X509_R_CANT_CHECK_DH_KEY); break; } #endif X509err(X509_F_X509_REQ_CHECK_PRIVATE_KEY, X509_R_UNKNOWN_KEY_TYPE); } EVP_PKEY_free(xk); return (ok); }
6,201
14,768
134204667084059130409120752919450013419
null
null
null
openssl
28a00bcd8e318da18031b2ac8778c64147cd54f9
0
X509_ATTRIBUTE *X509_REQ_delete_attr(X509_REQ *req, int loc) { return X509at_delete_attr(req->req_info->attributes, loc); }
6,202
14,769
6225972701344170882044157331348245592
null
null
null
openssl
28a00bcd8e318da18031b2ac8778c64147cd54f9
0
X509_ATTRIBUTE *X509_REQ_get_attr(const X509_REQ *req, int loc) { return X509at_get_attr(req->req_info->attributes, loc); }
6,204
14,770
83816343317532344543941464904117236575
null
null
null
openssl
28a00bcd8e318da18031b2ac8778c64147cd54f9
0
int X509_REQ_get_attr_by_OBJ(const X509_REQ *req, ASN1_OBJECT *obj, int lastpos) { return X509at_get_attr_by_OBJ(req->req_info->attributes, obj, lastpos); }
6,206
14,771
318017072641461932569855694161894621777
null
null
null
openssl
28a00bcd8e318da18031b2ac8778c64147cd54f9
0
int X509_REQ_get_attr_count(const X509_REQ *req) { return X509at_get_attr_count(req->req_info->attributes); }
6,207
14,772
185128773155022937341527306029820202980
null
null
null
openssl
28a00bcd8e318da18031b2ac8778c64147cd54f9
0
int *X509_REQ_get_extension_nids(void) { return ext_nids; }
6,208
14,773
203581727626340801281483780153058888915
null
null
null
openssl
28a00bcd8e318da18031b2ac8778c64147cd54f9
0
STACK_OF(X509_EXTENSION) *X509_REQ_get_extensions(X509_REQ *req) { X509_ATTRIBUTE *attr; ASN1_TYPE *ext = NULL; int idx, *pnid; const unsigned char *p; if ((req == NULL) || (req->req_info == NULL) || !ext_nids) return (NULL); for (pnid = ext_nids; *pnid != NID_undef; pnid++) { idx = X509_REQ_get_attr_by_NID(req, *pnid, -1); if (idx == -1) continue; attr = X509_REQ_get_attr(req, idx); if (attr->single) ext = attr->value.single; else if (sk_ASN1_TYPE_num(attr->value.set)) ext = sk_ASN1_TYPE_value(attr->value.set, 0); break; } if (!ext || (ext->type != V_ASN1_SEQUENCE)) return NULL; p = ext->value.sequence->data; return (STACK_OF(X509_EXTENSION) *) ASN1_item_d2i(NULL, &p, ext->value.sequence->length, ASN1_ITEM_rptr(X509_EXTENSIONS)); }
6,209
14,774
93646061746983407788895751037510083609
null
null
null
openssl
28a00bcd8e318da18031b2ac8778c64147cd54f9
0
EVP_PKEY *X509_REQ_get_pubkey(X509_REQ *req) { if ((req == NULL) || (req->req_info == NULL)) return (NULL); return (X509_PUBKEY_get(req->req_info->pubkey)); }
6,210
14,775
165082473358545178280240973103756912414
null
null
null
openssl
c3c7fb07dc975dc3c9de0eddb7d8fd79fc9c67c1
0
int ASN1_TYPE_get(ASN1_TYPE *a) { if ((a->value.ptr != NULL) || (a->type == V_ASN1_NULL)) return (a->type); else return (0); }
CWE-17
6,211
14,776
282895983723246162136580684211834310604
null
null
null
openssl
c3c7fb07dc975dc3c9de0eddb7d8fd79fc9c67c1
0
void ASN1_TYPE_set(ASN1_TYPE *a, int type, void *value) { if (a->value.ptr != NULL) { ASN1_TYPE **tmp_a = &a; ASN1_primitive_free((ASN1_VALUE **)tmp_a, NULL); } a->type = type; if (type == V_ASN1_BOOLEAN) a->value.boolean = value ? 0xff : 0; else a->value.ptr = value; }
CWE-17
6,212
14,777
313407598033732866413179780018615653356
null
null
null
openssl
c3c7fb07dc975dc3c9de0eddb7d8fd79fc9c67c1
0
int ASN1_TYPE_set1(ASN1_TYPE *a, int type, const void *value) { if (!value || (type == V_ASN1_BOOLEAN)) { void *p = (void *)value; ASN1_TYPE_set(a, type, p); } else if (type == V_ASN1_OBJECT) { ASN1_OBJECT *odup; odup = OBJ_dup(value); if (!odup) return 0; ASN1_TYPE_set(a, type, odup); } else { ASN1_STRING *sdup; sdup = ASN1_STRING_dup(value); if (!sdup) return 0; ASN1_TYPE_set(a, type, sdup); } return 1; }
CWE-17
6,213
14,778
172074231297241043780798979014520423680
null
null
null
openssl
e1b568dd2462f7cacf98f3d117936c34e2849a6b
0
int ssl3_connect(SSL *s) { BUF_MEM *buf = NULL; unsigned long Time = (unsigned long)time(NULL); void (*cb) (const SSL *ssl, int type, int val) = NULL; int ret = -1; int new_state, state, skip = 0; RAND_add(&Time, sizeof(Time), 0); ERR_clear_error(); clear_sys_error(); if (s->info_callback != NULL) cb = s->info_callback; else if (s->ctx->info_callback != NULL) cb = s->ctx->info_callback; s->in_handshake++; if (!SSL_in_init(s) || SSL_in_before(s)) SSL_clear(s); #ifndef OPENSSL_NO_HEARTBEATS /* * If we're awaiting a HeartbeatResponse, pretend we already got and * don't await it anymore, because Heartbeats don't make sense during * handshakes anyway. */ if (s->tlsext_hb_pending) { s->tlsext_hb_pending = 0; s->tlsext_hb_seq++; } #endif for (;;) { state = s->state; switch (s->state) { case SSL_ST_RENEGOTIATE: s->renegotiate = 1; s->state = SSL_ST_CONNECT; s->ctx->stats.sess_connect_renegotiate++; /* break */ case SSL_ST_BEFORE: case SSL_ST_CONNECT: case SSL_ST_BEFORE | SSL_ST_CONNECT: case SSL_ST_OK | SSL_ST_CONNECT: s->server = 0; if (cb != NULL) cb(s, SSL_CB_HANDSHAKE_START, 1); if ((s->version & 0xff00) != 0x0300) { SSLerr(SSL_F_SSL3_CONNECT, ERR_R_INTERNAL_ERROR); ret = -1; goto end; } if (!ssl_security(s, SSL_SECOP_VERSION, 0, s->version, NULL)) { SSLerr(SSL_F_SSL3_CONNECT, SSL_R_VERSION_TOO_LOW); return -1; } /* s->version=SSL3_VERSION; */ s->type = SSL_ST_CONNECT; if (s->init_buf == NULL) { if ((buf = BUF_MEM_new()) == NULL) { ret = -1; goto end; } if (!BUF_MEM_grow(buf, SSL3_RT_MAX_PLAIN_LENGTH)) { ret = -1; goto end; } s->init_buf = buf; buf = NULL; } if (!ssl3_setup_buffers(s)) { ret = -1; goto end; } /* setup buffing BIO */ if (!ssl_init_wbio_buffer(s, 0)) { ret = -1; goto end; } /* don't push the buffering BIO quite yet */ ssl3_init_finished_mac(s); s->state = SSL3_ST_CW_CLNT_HELLO_A; s->ctx->stats.sess_connect++; s->init_num = 0; s->s3->flags &= ~SSL3_FLAGS_CCS_OK; /* * Should have been reset by ssl3_get_finished, too. */ s->s3->change_cipher_spec = 0; break; case SSL3_ST_CW_CLNT_HELLO_A: case SSL3_ST_CW_CLNT_HELLO_B: s->shutdown = 0; ret = ssl3_client_hello(s); if (ret <= 0) goto end; s->state = SSL3_ST_CR_SRVR_HELLO_A; s->init_num = 0; /* turn on buffering for the next lot of output */ if (s->bbio != s->wbio) s->wbio = BIO_push(s->bbio, s->wbio); break; case SSL3_ST_CR_SRVR_HELLO_A: case SSL3_ST_CR_SRVR_HELLO_B: ret = ssl3_get_server_hello(s); if (ret <= 0) goto end; if (s->hit) { s->state = SSL3_ST_CR_FINISHED_A; #ifndef OPENSSL_NO_TLSEXT if (s->tlsext_ticket_expected) { /* receive renewed session ticket */ s->state = SSL3_ST_CR_SESSION_TICKET_A; } #endif } else { s->state = SSL3_ST_CR_CERT_A; } s->init_num = 0; break; case SSL3_ST_CR_CERT_A: case SSL3_ST_CR_CERT_B: /* Check if it is anon DH/ECDH, SRP auth */ /* or PSK */ if (! (s->s3->tmp. new_cipher->algorithm_auth & (SSL_aNULL | SSL_aSRP)) && !(s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK)) { ret = ssl3_get_server_certificate(s); if (ret <= 0) goto end; #ifndef OPENSSL_NO_TLSEXT if (s->tlsext_status_expected) s->state = SSL3_ST_CR_CERT_STATUS_A; else s->state = SSL3_ST_CR_KEY_EXCH_A; } else { skip = 1; s->state = SSL3_ST_CR_KEY_EXCH_A; } #else } else skip = 1; s->state = SSL3_ST_CR_KEY_EXCH_A; #endif s->init_num = 0; break; case SSL3_ST_CR_KEY_EXCH_A: case SSL3_ST_CR_KEY_EXCH_B: ret = ssl3_get_key_exchange(s); if (ret <= 0) goto end; s->state = SSL3_ST_CR_CERT_REQ_A; s->init_num = 0; /* * at this point we check that we have the required stuff from * the server */ if (!ssl3_check_cert_and_algorithm(s)) { ret = -1; goto end; } break; case SSL3_ST_CR_CERT_REQ_A: case SSL3_ST_CR_CERT_REQ_B: ret = ssl3_get_certificate_request(s); if (ret <= 0) goto end; s->state = SSL3_ST_CR_SRVR_DONE_A; s->init_num = 0; break; case SSL3_ST_CR_SRVR_DONE_A: case SSL3_ST_CR_SRVR_DONE_B: ret = ssl3_get_server_done(s); if (ret <= 0) goto end; #ifndef OPENSSL_NO_SRP if (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kSRP) { if ((ret = SRP_Calc_A_param(s)) <= 0) { SSLerr(SSL_F_SSL3_CONNECT, SSL_R_SRP_A_CALC); ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); goto end; } } #endif if (s->s3->tmp.cert_req) s->state = SSL3_ST_CW_CERT_A; else s->state = SSL3_ST_CW_KEY_EXCH_A; s->init_num = 0; break; case SSL3_ST_CW_CERT_A: case SSL3_ST_CW_CERT_B: case SSL3_ST_CW_CERT_C: case SSL3_ST_CW_CERT_D: ret = ssl3_send_client_certificate(s); if (ret <= 0) goto end; s->state = SSL3_ST_CW_KEY_EXCH_A; s->init_num = 0; break; case SSL3_ST_CW_KEY_EXCH_A: case SSL3_ST_CW_KEY_EXCH_B: ret = ssl3_send_client_key_exchange(s); if (ret <= 0) goto end; /* * EAY EAY EAY need to check for DH fix cert sent back */ /* * For TLS, cert_req is set to 2, so a cert chain of nothing is * sent, but no verify packet is sent */ /* * XXX: For now, we do not support client authentication in ECDH * cipher suites with ECDH (rather than ECDSA) certificates. We * need to skip the certificate verify message when client's * ECDH public key is sent inside the client certificate. */ if (s->s3->tmp.cert_req == 1) { s->state = SSL3_ST_CW_CERT_VRFY_A; } else { s->state = SSL3_ST_CW_CHANGE_A; } if (s->s3->flags & TLS1_FLAGS_SKIP_CERT_VERIFY) { s->state = SSL3_ST_CW_CHANGE_A; } s->init_num = 0; break; case SSL3_ST_CW_CERT_VRFY_A: case SSL3_ST_CW_CERT_VRFY_B: ret = ssl3_send_client_verify(s); if (ret <= 0) goto end; s->state = SSL3_ST_CW_CHANGE_A; s->init_num = 0; break; case SSL3_ST_CW_CHANGE_A: case SSL3_ST_CW_CHANGE_B: ret = ssl3_send_change_cipher_spec(s, SSL3_ST_CW_CHANGE_A, SSL3_ST_CW_CHANGE_B); if (ret <= 0) goto end; #if defined(OPENSSL_NO_TLSEXT) || defined(OPENSSL_NO_NEXTPROTONEG) s->state = SSL3_ST_CW_FINISHED_A; #else if (s->s3->next_proto_neg_seen) s->state = SSL3_ST_CW_NEXT_PROTO_A; else s->state = SSL3_ST_CW_FINISHED_A; #endif s->init_num = 0; s->session->cipher = s->s3->tmp.new_cipher; #ifdef OPENSSL_NO_COMP s->session->compress_meth = 0; #else if (s->s3->tmp.new_compression == NULL) s->session->compress_meth = 0; else s->session->compress_meth = s->s3->tmp.new_compression->id; #endif if (!s->method->ssl3_enc->setup_key_block(s)) { ret = -1; goto end; } if (!s->method->ssl3_enc->change_cipher_state(s, SSL3_CHANGE_CIPHER_CLIENT_WRITE)) { ret = -1; goto end; } break; #if !defined(OPENSSL_NO_TLSEXT) && !defined(OPENSSL_NO_NEXTPROTONEG) case SSL3_ST_CW_NEXT_PROTO_A: case SSL3_ST_CW_NEXT_PROTO_B: ret = ssl3_send_next_proto(s); if (ret <= 0) goto end; s->state = SSL3_ST_CW_FINISHED_A; break; #endif case SSL3_ST_CW_FINISHED_A: case SSL3_ST_CW_FINISHED_B: ret = ssl3_send_finished(s, SSL3_ST_CW_FINISHED_A, SSL3_ST_CW_FINISHED_B, s->method-> ssl3_enc->client_finished_label, s->method-> ssl3_enc->client_finished_label_len); if (ret <= 0) goto end; s->state = SSL3_ST_CW_FLUSH; /* clear flags */ s->s3->flags &= ~SSL3_FLAGS_POP_BUFFER; if (s->hit) { s->s3->tmp.next_state = SSL_ST_OK; if (s->s3->flags & SSL3_FLAGS_DELAY_CLIENT_FINISHED) { s->state = SSL_ST_OK; s->s3->flags |= SSL3_FLAGS_POP_BUFFER; s->s3->delay_buf_pop_ret = 0; } } else { #ifndef OPENSSL_NO_TLSEXT /* * Allow NewSessionTicket if ticket expected */ if (s->tlsext_ticket_expected) s->s3->tmp.next_state = SSL3_ST_CR_SESSION_TICKET_A; else #endif s->s3->tmp.next_state = SSL3_ST_CR_FINISHED_A; } s->init_num = 0; break; #ifndef OPENSSL_NO_TLSEXT case SSL3_ST_CR_SESSION_TICKET_A: case SSL3_ST_CR_SESSION_TICKET_B: ret = ssl3_get_new_session_ticket(s); if (ret <= 0) goto end; s->state = SSL3_ST_CR_FINISHED_A; s->init_num = 0; break; case SSL3_ST_CR_CERT_STATUS_A: case SSL3_ST_CR_CERT_STATUS_B: ret = ssl3_get_cert_status(s); if (ret <= 0) goto end; s->state = SSL3_ST_CR_KEY_EXCH_A; s->init_num = 0; break; #endif case SSL3_ST_CR_FINISHED_A: case SSL3_ST_CR_FINISHED_B: s->s3->flags |= SSL3_FLAGS_CCS_OK; ret = ssl3_get_finished(s, SSL3_ST_CR_FINISHED_A, SSL3_ST_CR_FINISHED_B); if (ret <= 0) goto end; if (s->hit) s->state = SSL3_ST_CW_CHANGE_A; else s->state = SSL_ST_OK; s->init_num = 0; break; case SSL3_ST_CW_FLUSH: s->rwstate = SSL_WRITING; if (BIO_flush(s->wbio) <= 0) { ret = -1; goto end; } s->rwstate = SSL_NOTHING; s->state = s->s3->tmp.next_state; break; case SSL_ST_OK: /* clean a few things up */ ssl3_cleanup_key_block(s); if (s->init_buf != NULL) { BUF_MEM_free(s->init_buf); s->init_buf = NULL; } /* * If we are not 'joining' the last two packets, remove the * buffering now */ if (!(s->s3->flags & SSL3_FLAGS_POP_BUFFER)) ssl_free_wbio_buffer(s); /* else do it later in ssl3_write */ s->init_num = 0; s->renegotiate = 0; s->new_session = 0; ssl_update_cache(s, SSL_SESS_CACHE_CLIENT); if (s->hit) s->ctx->stats.sess_hit++; ret = 1; /* s->server=0; */ s->handshake_func = ssl3_connect; s->ctx->stats.sess_connect_good++; if (cb != NULL) cb(s, SSL_CB_HANDSHAKE_DONE, 1); goto end; /* break; */ default: SSLerr(SSL_F_SSL3_CONNECT, SSL_R_UNKNOWN_STATE); ret = -1; goto end; /* break; */ } /* did we do anything */ if (!s->s3->tmp.reuse_message && !skip) { if (s->debug) { if ((ret = BIO_flush(s->wbio)) <= 0) goto end; } if ((cb != NULL) && (s->state != state)) { new_state = s->state; s->state = state; cb(s, SSL_CB_CONNECT_LOOP, 1); s->state = new_state; } } skip = 0; }
CWE-310
6,214
14,779
232538743527319518166200654495816011766
null
null
null
openssl
e1b568dd2462f7cacf98f3d117936c34e2849a6b
0
int ssl3_get_cert_status(SSL *s) { int ok, al; unsigned long resplen, n; const unsigned char *p; n = s->method->ssl_get_message(s, SSL3_ST_CR_CERT_STATUS_A, SSL3_ST_CR_CERT_STATUS_B, SSL3_MT_CERTIFICATE_STATUS, 16384, &ok); if (!ok) return ((int)n); if (n < 4) { /* need at least status type + length */ al = SSL_AD_DECODE_ERROR; SSLerr(SSL_F_SSL3_GET_CERT_STATUS, SSL_R_LENGTH_MISMATCH); goto f_err; } p = (unsigned char *)s->init_msg; if (*p++ != TLSEXT_STATUSTYPE_ocsp) { al = SSL_AD_DECODE_ERROR; SSLerr(SSL_F_SSL3_GET_CERT_STATUS, SSL_R_UNSUPPORTED_STATUS_TYPE); goto f_err; } n2l3(p, resplen); if (resplen + 4 != n) { al = SSL_AD_DECODE_ERROR; SSLerr(SSL_F_SSL3_GET_CERT_STATUS, SSL_R_LENGTH_MISMATCH); goto f_err; } if (s->tlsext_ocsp_resp) OPENSSL_free(s->tlsext_ocsp_resp); s->tlsext_ocsp_resp = BUF_memdup(p, resplen); if (!s->tlsext_ocsp_resp) { al = SSL_AD_INTERNAL_ERROR; SSLerr(SSL_F_SSL3_GET_CERT_STATUS, ERR_R_MALLOC_FAILURE); goto f_err; } s->tlsext_ocsp_resplen = resplen; if (s->ctx->tlsext_status_cb) { int ret; ret = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg); if (ret == 0) { al = SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE; SSLerr(SSL_F_SSL3_GET_CERT_STATUS, SSL_R_INVALID_STATUS_RESPONSE); goto f_err; } if (ret < 0) { al = SSL_AD_INTERNAL_ERROR; SSLerr(SSL_F_SSL3_GET_CERT_STATUS, ERR_R_MALLOC_FAILURE); goto f_err; } } return 1; f_err: ssl3_send_alert(s, SSL3_AL_FATAL, al); return (-1); }
CWE-310
6,215
14,780
251147787553393221843235950974422651361
null
null
null
openssl
e1b568dd2462f7cacf98f3d117936c34e2849a6b
0
int ssl3_get_certificate_request(SSL *s) { int ok, ret = 0; unsigned long n, nc, l; unsigned int llen, ctype_num, i; X509_NAME *xn = NULL; const unsigned char *p, *q; unsigned char *d; STACK_OF(X509_NAME) *ca_sk = NULL; n = s->method->ssl_get_message(s, SSL3_ST_CR_CERT_REQ_A, SSL3_ST_CR_CERT_REQ_B, -1, s->max_cert_list, &ok); if (!ok) return ((int)n); s->s3->tmp.cert_req = 0; if (s->s3->tmp.message_type == SSL3_MT_SERVER_DONE) { s->s3->tmp.reuse_message = 1; /* * If we get here we don't need any cached handshake records as we * wont be doing client auth. */ if (s->s3->handshake_buffer) { if (!ssl3_digest_cached_records(s)) goto err; } return (1); } if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_REQUEST) { ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE); SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, SSL_R_WRONG_MESSAGE_TYPE); goto err; } /* TLS does not like anon-DH with client cert */ if (s->version > SSL3_VERSION) { if (s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL) { ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE); SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, SSL_R_TLS_CLIENT_CERT_REQ_WITH_ANON_CIPHER); goto err; } } p = d = (unsigned char *)s->init_msg; if ((ca_sk = sk_X509_NAME_new(ca_dn_cmp)) == NULL) { SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE); goto err; } /* get the certificate types */ ctype_num = *(p++); if (s->cert->ctypes) { OPENSSL_free(s->cert->ctypes); s->cert->ctypes = NULL; } if (ctype_num > SSL3_CT_NUMBER) { /* If we exceed static buffer copy all to cert structure */ s->cert->ctypes = OPENSSL_malloc(ctype_num); if (s->cert->ctypes == NULL) { SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE); goto err; } memcpy(s->cert->ctypes, p, ctype_num); s->cert->ctype_num = (size_t)ctype_num; ctype_num = SSL3_CT_NUMBER; } for (i = 0; i < ctype_num; i++) s->s3->tmp.ctype[i] = p[i]; p += p[-1]; if (SSL_USE_SIGALGS(s)) { n2s(p, llen); /* * Check we have enough room for signature algorithms and following * length value. */ if ((unsigned long)(p - d + llen + 2) > n) { ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, SSL_R_DATA_LENGTH_TOO_LONG); goto err; } /* Clear certificate digests and validity flags */ for (i = 0; i < SSL_PKEY_NUM; i++) { s->cert->pkeys[i].digest = NULL; s->cert->pkeys[i].valid_flags = 0; } if ((llen & 1) || !tls1_save_sigalgs(s, p, llen)) { ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, SSL_R_SIGNATURE_ALGORITHMS_ERROR); goto err; } if (!tls1_process_sigalgs(s)) { ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE); goto err; } p += llen; } /* get the CA RDNs */ n2s(p, llen); if ((unsigned long)(p - d + llen) != n) { ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, SSL_R_LENGTH_MISMATCH); goto err; } for (nc = 0; nc < llen;) { n2s(p, l); if ((l + nc + 2) > llen) { ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, SSL_R_CA_DN_TOO_LONG); goto err; } q = p; if ((xn = d2i_X509_NAME(NULL, &q, l)) == NULL) { ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, ERR_R_ASN1_LIB); goto err; } if (q != (p + l)) { ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, SSL_R_CA_DN_LENGTH_MISMATCH); goto err; } if (!sk_X509_NAME_push(ca_sk, xn)) { SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE); goto err; } p += l; nc += l + 2; } /* we should setup a certificate to return.... */ s->s3->tmp.cert_req = 1; s->s3->tmp.ctype_num = ctype_num; if (s->s3->tmp.ca_names != NULL) sk_X509_NAME_pop_free(s->s3->tmp.ca_names, X509_NAME_free); s->s3->tmp.ca_names = ca_sk; ca_sk = NULL; ret = 1; err: if (ca_sk != NULL) sk_X509_NAME_pop_free(ca_sk, X509_NAME_free); return (ret); }
CWE-310
6,216
14,781
203679086778302129574131313356178075882
null
null
null
openssl
e1b568dd2462f7cacf98f3d117936c34e2849a6b
0
int ssl3_get_key_exchange(SSL *s) { #ifndef OPENSSL_NO_RSA unsigned char *q, md_buf[EVP_MAX_MD_SIZE * 2]; #endif EVP_MD_CTX md_ctx; unsigned char *param, *p; int al, j, ok; long i, param_len, n, alg_k, alg_a; EVP_PKEY *pkey = NULL; const EVP_MD *md = NULL; #ifndef OPENSSL_NO_RSA RSA *rsa = NULL; #endif #ifndef OPENSSL_NO_DH DH *dh = NULL; #endif #ifndef OPENSSL_NO_ECDH EC_KEY *ecdh = NULL; BN_CTX *bn_ctx = NULL; EC_POINT *srvr_ecpoint = NULL; int curve_nid = 0; int encoded_pt_len = 0; #endif EVP_MD_CTX_init(&md_ctx); /* * use same message size as in ssl3_get_certificate_request() as * ServerKeyExchange message may be skipped */ n = s->method->ssl_get_message(s, SSL3_ST_CR_KEY_EXCH_A, SSL3_ST_CR_KEY_EXCH_B, -1, s->max_cert_list, &ok); if (!ok) return ((int)n); alg_k = s->s3->tmp.new_cipher->algorithm_mkey; if (s->s3->tmp.message_type != SSL3_MT_SERVER_KEY_EXCHANGE) { /* * Can't skip server key exchange if this is an ephemeral * ciphersuite. */ if (alg_k & (SSL_kDHE | SSL_kECDHE)) { SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_UNEXPECTED_MESSAGE); al = SSL_AD_UNEXPECTED_MESSAGE; goto f_err; } #ifndef OPENSSL_NO_PSK /* * In plain PSK ciphersuite, ServerKeyExchange can be omitted if no * identity hint is sent. Set session->sess_cert anyway to avoid * problems later. */ if (alg_k & SSL_kPSK) { s->session->sess_cert = ssl_sess_cert_new(); if (s->ctx->psk_identity_hint) OPENSSL_free(s->ctx->psk_identity_hint); s->ctx->psk_identity_hint = NULL; } #endif s->s3->tmp.reuse_message = 1; return (1); } param = p = (unsigned char *)s->init_msg; if (s->session->sess_cert != NULL) { #ifndef OPENSSL_NO_RSA if (s->session->sess_cert->peer_rsa_tmp != NULL) { RSA_free(s->session->sess_cert->peer_rsa_tmp); s->session->sess_cert->peer_rsa_tmp = NULL; } #endif #ifndef OPENSSL_NO_DH if (s->session->sess_cert->peer_dh_tmp) { DH_free(s->session->sess_cert->peer_dh_tmp); s->session->sess_cert->peer_dh_tmp = NULL; } #endif #ifndef OPENSSL_NO_ECDH if (s->session->sess_cert->peer_ecdh_tmp) { EC_KEY_free(s->session->sess_cert->peer_ecdh_tmp); s->session->sess_cert->peer_ecdh_tmp = NULL; } #endif } else { s->session->sess_cert = ssl_sess_cert_new(); } /* Total length of the parameters including the length prefix */ param_len = 0; alg_a = s->s3->tmp.new_cipher->algorithm_auth; al = SSL_AD_DECODE_ERROR; #ifndef OPENSSL_NO_PSK if (alg_k & SSL_kPSK) { char tmp_id_hint[PSK_MAX_IDENTITY_LEN + 1]; param_len = 2; if (param_len > n) { SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT); goto f_err; } n2s(p, i); /* * Store PSK identity hint for later use, hint is used in * ssl3_send_client_key_exchange. Assume that the maximum length of * a PSK identity hint can be as long as the maximum length of a PSK * identity. */ if (i > PSK_MAX_IDENTITY_LEN) { al = SSL_AD_HANDSHAKE_FAILURE; SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_DATA_LENGTH_TOO_LONG); goto f_err; } if (i > n - param_len) { SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_PSK_IDENTITY_HINT_LENGTH); goto f_err; } param_len += i; /* * If received PSK identity hint contains NULL characters, the hint * is truncated from the first NULL. p may not be ending with NULL, * so create a NULL-terminated string. */ memcpy(tmp_id_hint, p, i); memset(tmp_id_hint + i, 0, PSK_MAX_IDENTITY_LEN + 1 - i); if (s->ctx->psk_identity_hint != NULL) OPENSSL_free(s->ctx->psk_identity_hint); s->ctx->psk_identity_hint = BUF_strdup(tmp_id_hint); if (s->ctx->psk_identity_hint == NULL) { al = SSL_AD_HANDSHAKE_FAILURE; SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE); goto f_err; } p += i; n -= param_len; } else #endif /* !OPENSSL_NO_PSK */ #ifndef OPENSSL_NO_SRP if (alg_k & SSL_kSRP) { param_len = 2; if (param_len > n) { SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT); goto f_err; } n2s(p, i); if (i > n - param_len) { SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SRP_N_LENGTH); goto f_err; } param_len += i; if (!(s->srp_ctx.N = BN_bin2bn(p, i, NULL))) { SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB); goto err; } p += i; if (2 > n - param_len) { SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT); goto f_err; } param_len += 2; n2s(p, i); if (i > n - param_len) { SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SRP_G_LENGTH); goto f_err; } param_len += i; if (!(s->srp_ctx.g = BN_bin2bn(p, i, NULL))) { SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB); goto err; } p += i; if (1 > n - param_len) { SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT); goto f_err; } param_len += 1; i = (unsigned int)(p[0]); p++; if (i > n - param_len) { SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SRP_S_LENGTH); goto f_err; } param_len += i; if (!(s->srp_ctx.s = BN_bin2bn(p, i, NULL))) { SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB); goto err; } p += i; if (2 > n - param_len) { SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT); goto f_err; } param_len += 2; n2s(p, i); if (i > n - param_len) { SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SRP_B_LENGTH); goto f_err; } param_len += i; if (!(s->srp_ctx.B = BN_bin2bn(p, i, NULL))) { SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB); goto err; } p += i; n -= param_len; if (!srp_verify_server_param(s, &al)) { SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SRP_PARAMETERS); goto f_err; } /* We must check if there is a certificate */ # ifndef OPENSSL_NO_RSA if (alg_a & SSL_aRSA) pkey = X509_get_pubkey(s->session-> sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509); # else if (0) ; # endif # ifndef OPENSSL_NO_DSA else if (alg_a & SSL_aDSS) pkey = X509_get_pubkey(s->session-> sess_cert->peer_pkeys[SSL_PKEY_DSA_SIGN]. x509); # endif } else #endif /* !OPENSSL_NO_SRP */ #ifndef OPENSSL_NO_RSA if (alg_k & SSL_kRSA) { /* Temporary RSA keys only allowed in export ciphersuites */ if (!SSL_C_IS_EXPORT(s->s3->tmp.new_cipher)) { al = SSL_AD_UNEXPECTED_MESSAGE; SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_UNEXPECTED_MESSAGE); goto f_err; } if ((rsa = RSA_new()) == NULL) { SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE); goto err; } param_len = 2; if (param_len > n) { SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT); goto f_err; } n2s(p, i); if (i > n - param_len) { SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_RSA_MODULUS_LENGTH); goto f_err; } param_len += i; if (!(rsa->n = BN_bin2bn(p, i, rsa->n))) { SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB); goto err; } p += i; if (2 > n - param_len) { SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT); goto f_err; } param_len += 2; n2s(p, i); if (i > n - param_len) { SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_RSA_E_LENGTH); goto f_err; } param_len += i; if (!(rsa->e = BN_bin2bn(p, i, rsa->e))) { SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB); goto err; } p += i; n -= param_len; /* this should be because we are using an export cipher */ if (alg_a & SSL_aRSA) pkey = X509_get_pubkey(s->session-> sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509); else { SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR); goto err; } s->session->sess_cert->peer_rsa_tmp = rsa; rsa = NULL; } #else /* OPENSSL_NO_RSA */ if (0) ; #endif #ifndef OPENSSL_NO_DH else if (alg_k & SSL_kDHE) { if ((dh = DH_new()) == NULL) { SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_DH_LIB); goto err; } param_len = 2; if (param_len > n) { SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT); goto f_err; } n2s(p, i); if (i > n - param_len) { SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_DH_P_LENGTH); goto f_err; } param_len += i; if (!(dh->p = BN_bin2bn(p, i, NULL))) { SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB); goto err; } p += i; if (2 > n - param_len) { SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT); goto f_err; } param_len += 2; n2s(p, i); if (i > n - param_len) { SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_DH_G_LENGTH); goto f_err; } param_len += i; if (!(dh->g = BN_bin2bn(p, i, NULL))) { SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB); goto err; } p += i; if (2 > n - param_len) { SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT); goto f_err; } param_len += 2; n2s(p, i); if (i > n - param_len) { SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_DH_PUB_KEY_LENGTH); goto f_err; } param_len += i; if (!(dh->pub_key = BN_bin2bn(p, i, NULL))) { SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB); goto err; } p += i; n -= param_len; if (!ssl_security(s, SSL_SECOP_TMP_DH, DH_security_bits(dh), 0, dh)) { al = SSL_AD_HANDSHAKE_FAILURE; SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_DH_KEY_TOO_SMALL); goto f_err; } # ifndef OPENSSL_NO_RSA if (alg_a & SSL_aRSA) pkey = X509_get_pubkey(s->session-> sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509); # else if (0) ; # endif # ifndef OPENSSL_NO_DSA else if (alg_a & SSL_aDSS) pkey = X509_get_pubkey(s->session-> sess_cert->peer_pkeys[SSL_PKEY_DSA_SIGN]. x509); # endif /* else anonymous DH, so no certificate or pkey. */ s->session->sess_cert->peer_dh_tmp = dh; dh = NULL; } else if ((alg_k & SSL_kDHr) || (alg_k & SSL_kDHd)) { al = SSL_AD_ILLEGAL_PARAMETER; SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_TRIED_TO_USE_UNSUPPORTED_CIPHER); goto f_err; } #endif /* !OPENSSL_NO_DH */ #ifndef OPENSSL_NO_ECDH else if (alg_k & SSL_kECDHE) { EC_GROUP *ngroup; const EC_GROUP *group; if ((ecdh = EC_KEY_new()) == NULL) { SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE); goto err; } /* * Extract elliptic curve parameters and the server's ephemeral ECDH * public key. Keep accumulating lengths of various components in * param_len and make sure it never exceeds n. */ /* * XXX: For now we only support named (not generic) curves and the * ECParameters in this case is just three bytes. We also need one * byte for the length of the encoded point */ param_len = 4; if (param_len > n) { SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT); goto f_err; } /* * Check curve is one of our preferences, if not server has sent an * invalid curve. ECParameters is 3 bytes. */ if (!tls1_check_curve(s, p, 3)) { SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_WRONG_CURVE); goto f_err; } if ((curve_nid = tls1_ec_curve_id2nid(*(p + 2))) == 0) { al = SSL_AD_INTERNAL_ERROR; SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_UNABLE_TO_FIND_ECDH_PARAMETERS); goto f_err; } ngroup = EC_GROUP_new_by_curve_name(curve_nid); if (ngroup == NULL) { SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_EC_LIB); goto err; } if (EC_KEY_set_group(ecdh, ngroup) == 0) { SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_EC_LIB); goto err; } EC_GROUP_free(ngroup); group = EC_KEY_get0_group(ecdh); if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) && (EC_GROUP_get_degree(group) > 163)) { al = SSL_AD_EXPORT_RESTRICTION; SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_ECGROUP_TOO_LARGE_FOR_CIPHER); goto f_err; } p += 3; /* Next, get the encoded ECPoint */ if (((srvr_ecpoint = EC_POINT_new(group)) == NULL) || ((bn_ctx = BN_CTX_new()) == NULL)) { SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE); goto err; } encoded_pt_len = *p; /* length of encoded point */ p += 1; if ((encoded_pt_len > n - param_len) || (EC_POINT_oct2point(group, srvr_ecpoint, p, encoded_pt_len, bn_ctx) == 0)) { SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_ECPOINT); goto f_err; } param_len += encoded_pt_len; n -= param_len; p += encoded_pt_len; /* * The ECC/TLS specification does not mention the use of DSA to sign * ECParameters in the server key exchange message. We do support RSA * and ECDSA. */ if (0) ; # ifndef OPENSSL_NO_RSA else if (alg_a & SSL_aRSA) pkey = X509_get_pubkey(s->session-> sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509); # endif # ifndef OPENSSL_NO_ECDSA else if (alg_a & SSL_aECDSA) pkey = X509_get_pubkey(s->session-> sess_cert->peer_pkeys[SSL_PKEY_ECC].x509); # endif /* else anonymous ECDH, so no certificate or pkey. */ EC_KEY_set_public_key(ecdh, srvr_ecpoint); s->session->sess_cert->peer_ecdh_tmp = ecdh; ecdh = NULL; BN_CTX_free(bn_ctx); bn_ctx = NULL; EC_POINT_free(srvr_ecpoint); srvr_ecpoint = NULL; } else if (alg_k) { al = SSL_AD_UNEXPECTED_MESSAGE; SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_UNEXPECTED_MESSAGE); goto f_err; } #endif /* !OPENSSL_NO_ECDH */ /* p points to the next byte, there are 'n' bytes left */ /* if it was signed, check the signature */ if (pkey != NULL) { if (SSL_USE_SIGALGS(s)) { int rv; if (2 > n) { SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT); goto f_err; } rv = tls12_check_peer_sigalg(&md, s, p, pkey); if (rv == -1) goto err; else if (rv == 0) { goto f_err; } #ifdef SSL_DEBUG fprintf(stderr, "USING TLSv1.2 HASH %s\n", EVP_MD_name(md)); #endif p += 2; n -= 2; } else md = EVP_sha1(); if (2 > n) { SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT); goto f_err; } n2s(p, i); n -= 2; j = EVP_PKEY_size(pkey); /* * Check signature length. If n is 0 then signature is empty */ if ((i != n) || (n > j) || (n <= 0)) { /* wrong packet length */ SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_WRONG_SIGNATURE_LENGTH); goto f_err; } #ifndef OPENSSL_NO_RSA if (pkey->type == EVP_PKEY_RSA && !SSL_USE_SIGALGS(s)) { int num; unsigned int size; j = 0; q = md_buf; for (num = 2; num > 0; num--) { EVP_MD_CTX_set_flags(&md_ctx, EVP_MD_CTX_FLAG_NON_FIPS_ALLOW); EVP_DigestInit_ex(&md_ctx, (num == 2) ? s->ctx->md5 : s->ctx->sha1, NULL); EVP_DigestUpdate(&md_ctx, &(s->s3->client_random[0]), SSL3_RANDOM_SIZE); EVP_DigestUpdate(&md_ctx, &(s->s3->server_random[0]), SSL3_RANDOM_SIZE); EVP_DigestUpdate(&md_ctx, param, param_len); EVP_DigestFinal_ex(&md_ctx, q, &size); q += size; j += size; } i = RSA_verify(NID_md5_sha1, md_buf, j, p, n, pkey->pkey.rsa); if (i < 0) { al = SSL_AD_DECRYPT_ERROR; SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_RSA_DECRYPT); goto f_err; } if (i == 0) { /* bad signature */ al = SSL_AD_DECRYPT_ERROR; SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SIGNATURE); goto f_err; } } else #endif { EVP_VerifyInit_ex(&md_ctx, md, NULL); EVP_VerifyUpdate(&md_ctx, &(s->s3->client_random[0]), SSL3_RANDOM_SIZE); EVP_VerifyUpdate(&md_ctx, &(s->s3->server_random[0]), SSL3_RANDOM_SIZE); EVP_VerifyUpdate(&md_ctx, param, param_len); if (EVP_VerifyFinal(&md_ctx, p, (int)n, pkey) <= 0) { /* bad signature */ al = SSL_AD_DECRYPT_ERROR; SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SIGNATURE); goto f_err; } } } else { /* aNULL, aSRP or kPSK do not need public keys */ if (!(alg_a & (SSL_aNULL | SSL_aSRP)) && !(alg_k & SSL_kPSK)) { /* Might be wrong key type, check it */ if (ssl3_check_cert_and_algorithm(s)) /* Otherwise this shouldn't happen */ SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR); goto err; } /* still data left over */ if (n != 0) { SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_EXTRA_DATA_IN_MESSAGE); goto f_err; } } EVP_PKEY_free(pkey); EVP_MD_CTX_cleanup(&md_ctx); return (1); f_err: ssl3_send_alert(s, SSL3_AL_FATAL, al); err: EVP_PKEY_free(pkey); #ifndef OPENSSL_NO_RSA if (rsa != NULL) RSA_free(rsa); #endif #ifndef OPENSSL_NO_DH if (dh != NULL) DH_free(dh); #endif #ifndef OPENSSL_NO_ECDH BN_CTX_free(bn_ctx); EC_POINT_free(srvr_ecpoint); if (ecdh != NULL) EC_KEY_free(ecdh); #endif EVP_MD_CTX_cleanup(&md_ctx); return (-1); }
CWE-310
6,217
14,782
34046711783484045285315583200942828793
null
null
null
openssl
e1b568dd2462f7cacf98f3d117936c34e2849a6b
0
int ssl3_get_new_session_ticket(SSL *s) { int ok, al, ret = 0, ticklen; long n; const unsigned char *p; unsigned char *d; n = s->method->ssl_get_message(s, SSL3_ST_CR_SESSION_TICKET_A, SSL3_ST_CR_SESSION_TICKET_B, SSL3_MT_NEWSESSION_TICKET, 16384, &ok); if (!ok) return ((int)n); if (n < 6) { /* need at least ticket_lifetime_hint + ticket length */ al = SSL_AD_DECODE_ERROR; SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET, SSL_R_LENGTH_MISMATCH); goto f_err; } p = d = (unsigned char *)s->init_msg; n2l(p, s->session->tlsext_tick_lifetime_hint); n2s(p, ticklen); /* ticket_lifetime_hint + ticket_length + ticket */ if (ticklen + 6 != n) { al = SSL_AD_DECODE_ERROR; SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET, SSL_R_LENGTH_MISMATCH); goto f_err; } if (s->session->tlsext_tick) { OPENSSL_free(s->session->tlsext_tick); s->session->tlsext_ticklen = 0; } s->session->tlsext_tick = OPENSSL_malloc(ticklen); if (!s->session->tlsext_tick) { SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET, ERR_R_MALLOC_FAILURE); goto err; } memcpy(s->session->tlsext_tick, p, ticklen); s->session->tlsext_ticklen = ticklen; /* * There are two ways to detect a resumed ticket session. One is to set * an appropriate session ID and then the server must return a match in * ServerHello. This allows the normal client session ID matching to work * and we know much earlier that the ticket has been accepted. The * other way is to set zero length session ID when the ticket is * presented and rely on the handshake to determine session resumption. * We choose the former approach because this fits in with assumptions * elsewhere in OpenSSL. The session ID is set to the SHA256 (or SHA1 is * SHA256 is disabled) hash of the ticket. */ EVP_Digest(p, ticklen, s->session->session_id, &s->session->session_id_length, EVP_sha256(), NULL); ret = 1; return (ret); f_err: ssl3_send_alert(s, SSL3_AL_FATAL, al); err: return (-1); }
CWE-310
6,218
14,783
185820752659011329351519997352833626679
null
null
null
openssl
e1b568dd2462f7cacf98f3d117936c34e2849a6b
0
int ssl3_get_server_certificate(SSL *s) { int al, i, ok, ret = -1; unsigned long n, nc, llen, l; X509 *x = NULL; const unsigned char *q, *p; unsigned char *d; STACK_OF(X509) *sk = NULL; SESS_CERT *sc; EVP_PKEY *pkey = NULL; int need_cert = 1; /* VRS: 0=> will allow null cert if auth == * KRB5 */ n = s->method->ssl_get_message(s, SSL3_ST_CR_CERT_A, SSL3_ST_CR_CERT_B, -1, s->max_cert_list, &ok); if (!ok) return ((int)n); if ((s->s3->tmp.message_type == SSL3_MT_SERVER_KEY_EXCHANGE) || ((s->s3->tmp.new_cipher->algorithm_auth & SSL_aKRB5) && (s->s3->tmp.message_type == SSL3_MT_SERVER_DONE))) { s->s3->tmp.reuse_message = 1; return (1); } if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE) { al = SSL_AD_UNEXPECTED_MESSAGE; SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, SSL_R_BAD_MESSAGE_TYPE); goto f_err; } p = d = (unsigned char *)s->init_msg; if ((sk = sk_X509_new_null()) == NULL) { SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, ERR_R_MALLOC_FAILURE); goto err; } n2l3(p, llen); if (llen + 3 != n) { al = SSL_AD_DECODE_ERROR; SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, SSL_R_LENGTH_MISMATCH); goto f_err; } for (nc = 0; nc < llen;) { n2l3(p, l); if ((l + nc + 3) > llen) { al = SSL_AD_DECODE_ERROR; SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, SSL_R_CERT_LENGTH_MISMATCH); goto f_err; } q = p; x = d2i_X509(NULL, &q, l); if (x == NULL) { al = SSL_AD_BAD_CERTIFICATE; SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, ERR_R_ASN1_LIB); goto f_err; } if (q != (p + l)) { al = SSL_AD_DECODE_ERROR; SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, SSL_R_CERT_LENGTH_MISMATCH); goto f_err; } if (!sk_X509_push(sk, x)) { SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, ERR_R_MALLOC_FAILURE); goto err; } x = NULL; nc += l + 3; p = q; } i = ssl_verify_cert_chain(s, sk); if ((s->verify_mode != SSL_VERIFY_NONE) && (i <= 0) #ifndef OPENSSL_NO_KRB5 && !((s->s3->tmp.new_cipher->algorithm_mkey & SSL_kKRB5) && (s->s3->tmp.new_cipher->algorithm_auth & SSL_aKRB5)) #endif /* OPENSSL_NO_KRB5 */ ) { al = ssl_verify_alarm_type(s->verify_result); SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, SSL_R_CERTIFICATE_VERIFY_FAILED); goto f_err; } ERR_clear_error(); /* but we keep s->verify_result */ if (i > 1) { SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, i); al = SSL_AD_HANDSHAKE_FAILURE; goto f_err; } sc = ssl_sess_cert_new(); if (sc == NULL) goto err; if (s->session->sess_cert) ssl_sess_cert_free(s->session->sess_cert); s->session->sess_cert = sc; sc->cert_chain = sk; /* * Inconsistency alert: cert_chain does include the peer's certificate, * which we don't include in s3_srvr.c */ x = sk_X509_value(sk, 0); sk = NULL; /* * VRS 19990621: possible memory leak; sk=null ==> !sk_pop_free() @end */ pkey = X509_get_pubkey(x); /* VRS: allow null cert if auth == KRB5 */ need_cert = ((s->s3->tmp.new_cipher->algorithm_mkey & SSL_kKRB5) && (s->s3->tmp.new_cipher->algorithm_auth & SSL_aKRB5)) ? 0 : 1; #ifdef KSSL_DEBUG fprintf(stderr, "pkey,x = %p, %p\n", pkey, x); fprintf(stderr, "ssl_cert_type(x,pkey) = %d\n", ssl_cert_type(x, pkey)); fprintf(stderr, "cipher, alg, nc = %s, %lx, %lx, %d\n", s->s3->tmp.new_cipher->name, s->s3->tmp.new_cipher->algorithm_mkey, s->s3->tmp.new_cipher->algorithm_auth, need_cert); #endif /* KSSL_DEBUG */ if (need_cert && ((pkey == NULL) || EVP_PKEY_missing_parameters(pkey))) { x = NULL; al = SSL3_AL_FATAL; SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, SSL_R_UNABLE_TO_FIND_PUBLIC_KEY_PARAMETERS); goto f_err; } i = ssl_cert_type(x, pkey); if (need_cert && i < 0) { x = NULL; al = SSL3_AL_FATAL; SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, SSL_R_UNKNOWN_CERTIFICATE_TYPE); goto f_err; } if (need_cert) { int exp_idx = ssl_cipher_get_cert_index(s->s3->tmp.new_cipher); if (exp_idx >= 0 && i != exp_idx) { x = NULL; al = SSL_AD_ILLEGAL_PARAMETER; SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, SSL_R_WRONG_CERTIFICATE_TYPE); goto f_err; } sc->peer_cert_type = i; CRYPTO_add(&x->references, 1, CRYPTO_LOCK_X509); /* * Why would the following ever happen? We just created sc a couple * of lines ago. */ if (sc->peer_pkeys[i].x509 != NULL) X509_free(sc->peer_pkeys[i].x509); sc->peer_pkeys[i].x509 = x; sc->peer_key = &(sc->peer_pkeys[i]); if (s->session->peer != NULL) X509_free(s->session->peer); CRYPTO_add(&x->references, 1, CRYPTO_LOCK_X509); s->session->peer = x; } else { sc->peer_cert_type = i; sc->peer_key = NULL; if (s->session->peer != NULL) X509_free(s->session->peer); s->session->peer = NULL; } s->session->verify_result = s->verify_result; x = NULL; ret = 1; if (0) { f_err: ssl3_send_alert(s, SSL3_AL_FATAL, al); } err: EVP_PKEY_free(pkey); X509_free(x); sk_X509_pop_free(sk, X509_free); return (ret); }
CWE-310
6,219
14,784
204393519069312511802129716380426281399
null
null
null
openssl
e1b568dd2462f7cacf98f3d117936c34e2849a6b
0
int ssl3_get_server_hello(SSL *s) { STACK_OF(SSL_CIPHER) *sk; const SSL_CIPHER *c; CERT *ct = s->cert; unsigned char *p, *d; int i, al = SSL_AD_INTERNAL_ERROR, ok; unsigned int j; long n; #ifndef OPENSSL_NO_COMP SSL_COMP *comp; #endif /* * Hello verify request and/or server hello version may not match so set * first packet if we're negotiating version. */ if (SSL_IS_DTLS(s)) s->first_packet = 1; n = s->method->ssl_get_message(s, SSL3_ST_CR_SRVR_HELLO_A, SSL3_ST_CR_SRVR_HELLO_B, -1, 20000, &ok); if (!ok) return ((int)n); if (SSL_IS_DTLS(s)) { s->first_packet = 0; if (s->s3->tmp.message_type == DTLS1_MT_HELLO_VERIFY_REQUEST) { if (s->d1->send_cookie == 0) { s->s3->tmp.reuse_message = 1; return 1; } else { /* already sent a cookie */ al = SSL_AD_UNEXPECTED_MESSAGE; SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_BAD_MESSAGE_TYPE); goto f_err; } } } if (s->s3->tmp.message_type != SSL3_MT_SERVER_HELLO) { al = SSL_AD_UNEXPECTED_MESSAGE; SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_BAD_MESSAGE_TYPE); goto f_err; } d = p = (unsigned char *)s->init_msg; if (s->method->version == DTLS_ANY_VERSION) { /* Work out correct protocol version to use */ int hversion = (p[0] << 8) | p[1]; int options = s->options; if (hversion == DTLS1_2_VERSION && !(options & SSL_OP_NO_DTLSv1_2)) s->method = DTLSv1_2_client_method(); else if (tls1_suiteb(s)) { SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_ONLY_DTLS_1_2_ALLOWED_IN_SUITEB_MODE); s->version = hversion; al = SSL_AD_PROTOCOL_VERSION; goto f_err; } else if (hversion == DTLS1_VERSION && !(options & SSL_OP_NO_DTLSv1)) s->method = DTLSv1_client_method(); else { SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_WRONG_SSL_VERSION); s->version = hversion; al = SSL_AD_PROTOCOL_VERSION; goto f_err; } s->version = s->method->version; } if ((p[0] != (s->version >> 8)) || (p[1] != (s->version & 0xff))) { SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_WRONG_SSL_VERSION); s->version = (s->version & 0xff00) | p[1]; al = SSL_AD_PROTOCOL_VERSION; goto f_err; } p += 2; /* load the server hello data */ /* load the server random */ memcpy(s->s3->server_random, p, SSL3_RANDOM_SIZE); p += SSL3_RANDOM_SIZE; s->hit = 0; /* get the session-id */ j = *(p++); if ((j > sizeof s->session->session_id) || (j > SSL3_SESSION_ID_SIZE)) { al = SSL_AD_ILLEGAL_PARAMETER; SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_SSL3_SESSION_ID_TOO_LONG); goto f_err; } #ifndef OPENSSL_NO_TLSEXT /* * check if we want to resume the session based on external pre-shared * secret */ if (s->version >= TLS1_VERSION && s->tls_session_secret_cb) { SSL_CIPHER *pref_cipher = NULL; s->session->master_key_length = sizeof(s->session->master_key); if (s->tls_session_secret_cb(s, s->session->master_key, &s->session->master_key_length, NULL, &pref_cipher, s->tls_session_secret_cb_arg)) { s->session->cipher = pref_cipher ? pref_cipher : ssl_get_cipher_by_char(s, p + j); s->hit = 1; } } #endif /* OPENSSL_NO_TLSEXT */ if (!s->hit && j != 0 && j == s->session->session_id_length && memcmp(p, s->session->session_id, j) == 0) { if (s->sid_ctx_length != s->session->sid_ctx_length || memcmp(s->session->sid_ctx, s->sid_ctx, s->sid_ctx_length)) { /* actually a client application bug */ al = SSL_AD_ILLEGAL_PARAMETER; SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT); goto f_err; } s->hit = 1; } /* a miss or crap from the other end */ if (!s->hit) { /* * If we were trying for session-id reuse, make a new SSL_SESSION so * we don't stuff up other people */ if (s->session->session_id_length > 0) { if (!ssl_get_new_session(s, 0)) { goto f_err; } } s->session->session_id_length = j; memcpy(s->session->session_id, p, j); /* j could be 0 */ } p += j; c = ssl_get_cipher_by_char(s, p); if (c == NULL) { /* unknown cipher */ al = SSL_AD_ILLEGAL_PARAMETER; SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_UNKNOWN_CIPHER_RETURNED); goto f_err; } /* Set version disabled mask now we know version */ if (!SSL_USE_TLS1_2_CIPHERS(s)) ct->mask_ssl = SSL_TLSV1_2; else ct->mask_ssl = 0; /* * If it is a disabled cipher we didn't send it in client hello, so * return an error. */ if (ssl_cipher_disabled(s, c, SSL_SECOP_CIPHER_CHECK)) { al = SSL_AD_ILLEGAL_PARAMETER; SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_WRONG_CIPHER_RETURNED); goto f_err; } p += ssl_put_cipher_by_char(s, NULL, NULL); sk = ssl_get_ciphers_by_id(s); i = sk_SSL_CIPHER_find(sk, c); if (i < 0) { /* we did not say we would use this cipher */ al = SSL_AD_ILLEGAL_PARAMETER; SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_WRONG_CIPHER_RETURNED); goto f_err; } /* * Depending on the session caching (internal/external), the cipher * and/or cipher_id values may not be set. Make sure that cipher_id is * set and use it for comparison. */ if (s->session->cipher) s->session->cipher_id = s->session->cipher->id; if (s->hit && (s->session->cipher_id != c->id)) { al = SSL_AD_ILLEGAL_PARAMETER; SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED); goto f_err; } s->s3->tmp.new_cipher = c; /* * Don't digest cached records if no sigalgs: we may need them for client * authentication. */ if (!SSL_USE_SIGALGS(s) && !ssl3_digest_cached_records(s)) goto f_err; /* lets get the compression algorithm */ /* COMPRESSION */ #ifdef OPENSSL_NO_COMP if (*(p++) != 0) { al = SSL_AD_ILLEGAL_PARAMETER; SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM); goto f_err; } /* * If compression is disabled we'd better not try to resume a session * using compression. */ if (s->session->compress_meth != 0) { SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_INCONSISTENT_COMPRESSION); goto f_err; } #else j = *(p++); if (s->hit && j != s->session->compress_meth) { al = SSL_AD_ILLEGAL_PARAMETER; SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_OLD_SESSION_COMPRESSION_ALGORITHM_NOT_RETURNED); goto f_err; } if (j == 0) comp = NULL; else if (!ssl_allow_compression(s)) { al = SSL_AD_ILLEGAL_PARAMETER; SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_COMPRESSION_DISABLED); goto f_err; } else comp = ssl3_comp_find(s->ctx->comp_methods, j); if ((j != 0) && (comp == NULL)) { al = SSL_AD_ILLEGAL_PARAMETER; SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM); goto f_err; } else { s->s3->tmp.new_compression = comp; } #endif #ifndef OPENSSL_NO_TLSEXT /* TLS extensions */ if (!ssl_parse_serverhello_tlsext(s, &p, d, n)) { SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_PARSE_TLSEXT); goto err; } #endif if (p != (d + n)) { /* wrong packet length */ al = SSL_AD_DECODE_ERROR; SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_BAD_PACKET_LENGTH); goto f_err; } return (1); f_err: ssl3_send_alert(s, SSL3_AL_FATAL, al); err: return (-1); }
CWE-310
6,221
14,785
8400954362206205500414663019047798793
null
null
null
openssl
e1b568dd2462f7cacf98f3d117936c34e2849a6b
0
int ssl3_send_client_certificate(SSL *s) { X509 *x509 = NULL; EVP_PKEY *pkey = NULL; int i; if (s->state == SSL3_ST_CW_CERT_A) { /* Let cert callback update client certificates if required */ if (s->cert->cert_cb) { i = s->cert->cert_cb(s, s->cert->cert_cb_arg); if (i < 0) { s->rwstate = SSL_X509_LOOKUP; return -1; } if (i == 0) { ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); return 0; } s->rwstate = SSL_NOTHING; } if (ssl3_check_client_certificate(s)) s->state = SSL3_ST_CW_CERT_C; else s->state = SSL3_ST_CW_CERT_B; } /* We need to get a client cert */ if (s->state == SSL3_ST_CW_CERT_B) { /* * If we get an error, we need to ssl->rwstate=SSL_X509_LOOKUP; * return(-1); We then get retied later */ i = 0; i = ssl_do_client_cert_cb(s, &x509, &pkey); if (i < 0) { s->rwstate = SSL_X509_LOOKUP; return (-1); } s->rwstate = SSL_NOTHING; if ((i == 1) && (pkey != NULL) && (x509 != NULL)) { s->state = SSL3_ST_CW_CERT_B; if (!SSL_use_certificate(s, x509) || !SSL_use_PrivateKey(s, pkey)) i = 0; } else if (i == 1) { i = 0; SSLerr(SSL_F_SSL3_SEND_CLIENT_CERTIFICATE, SSL_R_BAD_DATA_RETURNED_BY_CALLBACK); } if (x509 != NULL) X509_free(x509); if (pkey != NULL) EVP_PKEY_free(pkey); if (i && !ssl3_check_client_certificate(s)) i = 0; if (i == 0) { if (s->version == SSL3_VERSION) { s->s3->tmp.cert_req = 0; ssl3_send_alert(s, SSL3_AL_WARNING, SSL_AD_NO_CERTIFICATE); return (1); } else { s->s3->tmp.cert_req = 2; } } /* Ok, we have a cert */ s->state = SSL3_ST_CW_CERT_C; } if (s->state == SSL3_ST_CW_CERT_C) { s->state = SSL3_ST_CW_CERT_D; if (!ssl3_output_cert_chain(s, (s->s3->tmp.cert_req == 2) ? NULL : s->cert->key)) { SSLerr(SSL_F_SSL3_SEND_CLIENT_CERTIFICATE, ERR_R_INTERNAL_ERROR); ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); return 0; } } /* SSL3_ST_CW_CERT_D */ return ssl_do_write(s); }
CWE-310
6,222
14,786
142625245249782763251152359019392404816
null
null
null
openssl
e1b568dd2462f7cacf98f3d117936c34e2849a6b
0
int ssl3_send_client_key_exchange(SSL *s) { unsigned char *p; int n; unsigned long alg_k; #ifndef OPENSSL_NO_RSA unsigned char *q; EVP_PKEY *pkey = NULL; #endif #ifndef OPENSSL_NO_KRB5 KSSL_ERR kssl_err; #endif /* OPENSSL_NO_KRB5 */ #ifndef OPENSSL_NO_ECDH EC_KEY *clnt_ecdh = NULL; const EC_POINT *srvr_ecpoint = NULL; EVP_PKEY *srvr_pub_pkey = NULL; unsigned char *encodedPoint = NULL; int encoded_pt_len = 0; BN_CTX *bn_ctx = NULL; #endif unsigned char *pms = NULL; size_t pmslen = 0; if (s->state == SSL3_ST_CW_KEY_EXCH_A) { p = ssl_handshake_start(s); alg_k = s->s3->tmp.new_cipher->algorithm_mkey; /* Fool emacs indentation */ if (0) { } #ifndef OPENSSL_NO_RSA else if (alg_k & SSL_kRSA) { RSA *rsa; pmslen = SSL_MAX_MASTER_KEY_LENGTH; pms = OPENSSL_malloc(pmslen); if (!pms) goto memerr; if (s->session->sess_cert == NULL) { /* * We should always have a server certificate with SSL_kRSA. */ SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR); goto err; } if (s->session->sess_cert->peer_rsa_tmp != NULL) rsa = s->session->sess_cert->peer_rsa_tmp; else { pkey = X509_get_pubkey(s->session-> sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC]. x509); if ((pkey == NULL) || (pkey->type != EVP_PKEY_RSA) || (pkey->pkey.rsa == NULL)) { SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR); goto err; } rsa = pkey->pkey.rsa; EVP_PKEY_free(pkey); } pms[0] = s->client_version >> 8; pms[1] = s->client_version & 0xff; if (RAND_bytes(pms + 2, pmslen - 2) <= 0) goto err; q = p; /* Fix buf for TLS and beyond */ if (s->version > SSL3_VERSION) p += 2; n = RSA_public_encrypt(pmslen, pms, p, rsa, RSA_PKCS1_PADDING); # ifdef PKCS1_CHECK if (s->options & SSL_OP_PKCS1_CHECK_1) p[1]++; if (s->options & SSL_OP_PKCS1_CHECK_2) tmp_buf[0] = 0x70; # endif if (n <= 0) { SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, SSL_R_BAD_RSA_ENCRYPT); goto err; } /* Fix buf for TLS and beyond */ if (s->version > SSL3_VERSION) { s2n(n, q); n += 2; } } #endif #ifndef OPENSSL_NO_KRB5 else if (alg_k & SSL_kKRB5) { krb5_error_code krb5rc; KSSL_CTX *kssl_ctx = s->kssl_ctx; /* krb5_data krb5_ap_req; */ krb5_data *enc_ticket; krb5_data authenticator, *authp = NULL; EVP_CIPHER_CTX ciph_ctx; const EVP_CIPHER *enc = NULL; unsigned char iv[EVP_MAX_IV_LENGTH]; unsigned char tmp_buf[SSL_MAX_MASTER_KEY_LENGTH]; unsigned char epms[SSL_MAX_MASTER_KEY_LENGTH + EVP_MAX_IV_LENGTH]; int padl, outl = sizeof(epms); EVP_CIPHER_CTX_init(&ciph_ctx); # ifdef KSSL_DEBUG fprintf(stderr, "ssl3_send_client_key_exchange(%lx & %lx)\n", alg_k, SSL_kKRB5); # endif /* KSSL_DEBUG */ authp = NULL; # ifdef KRB5SENDAUTH if (KRB5SENDAUTH) authp = &authenticator; # endif /* KRB5SENDAUTH */ krb5rc = kssl_cget_tkt(kssl_ctx, &enc_ticket, authp, &kssl_err); enc = kssl_map_enc(kssl_ctx->enctype); if (enc == NULL) goto err; # ifdef KSSL_DEBUG { fprintf(stderr, "kssl_cget_tkt rtn %d\n", krb5rc); if (krb5rc && kssl_err.text) fprintf(stderr, "kssl_cget_tkt kssl_err=%s\n", kssl_err.text); } # endif /* KSSL_DEBUG */ if (krb5rc) { ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, kssl_err.reason); goto err; } /*- * 20010406 VRS - Earlier versions used KRB5 AP_REQ * in place of RFC 2712 KerberosWrapper, as in: * * Send ticket (copy to *p, set n = length) * n = krb5_ap_req.length; * memcpy(p, krb5_ap_req.data, krb5_ap_req.length); * if (krb5_ap_req.data) * kssl_krb5_free_data_contents(NULL,&krb5_ap_req); * * Now using real RFC 2712 KerberosWrapper * (Thanks to Simon Wilkinson <sxw@sxw.org.uk>) * Note: 2712 "opaque" types are here replaced * with a 2-byte length followed by the value. * Example: * KerberosWrapper= xx xx asn1ticket 0 0 xx xx encpms * Where "xx xx" = length bytes. Shown here with * optional authenticator omitted. */ /* KerberosWrapper.Ticket */ s2n(enc_ticket->length, p); memcpy(p, enc_ticket->data, enc_ticket->length); p += enc_ticket->length; n = enc_ticket->length + 2; /* KerberosWrapper.Authenticator */ if (authp && authp->length) { s2n(authp->length, p); memcpy(p, authp->data, authp->length); p += authp->length; n += authp->length + 2; free(authp->data); authp->data = NULL; authp->length = 0; } else { s2n(0, p); /* null authenticator length */ n += 2; } pmslen = SSL_MAX_MASTER_KEY_LENGTH; pms = OPENSSL_malloc(pmslen); if (!pms) goto memerr; pms[0] = s->client_version >> 8; pms[1] = s->client_version & 0xff; if (RAND_bytes(pms + 2, pmslen - 2) <= 0) goto err; /*- * 20010420 VRS. Tried it this way; failed. * EVP_EncryptInit_ex(&ciph_ctx,enc, NULL,NULL); * EVP_CIPHER_CTX_set_key_length(&ciph_ctx, * kssl_ctx->length); * EVP_EncryptInit_ex(&ciph_ctx,NULL, key,iv); */ memset(iv, 0, sizeof iv); /* per RFC 1510 */ EVP_EncryptInit_ex(&ciph_ctx, enc, NULL, kssl_ctx->key, iv); EVP_EncryptUpdate(&ciph_ctx, epms, &outl, pms, pmslen); EVP_EncryptFinal_ex(&ciph_ctx, &(epms[outl]), &padl); outl += padl; if (outl > (int)sizeof epms) { SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR); goto err; } EVP_CIPHER_CTX_cleanup(&ciph_ctx); /* KerberosWrapper.EncryptedPreMasterSecret */ s2n(outl, p); memcpy(p, epms, outl); p += outl; n += outl + 2; OPENSSL_cleanse(epms, outl); } #endif #ifndef OPENSSL_NO_DH else if (alg_k & (SSL_kDHE | SSL_kDHr | SSL_kDHd)) { DH *dh_srvr, *dh_clnt; SESS_CERT *scert = s->session->sess_cert; if (scert == NULL) { ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE); SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, SSL_R_UNEXPECTED_MESSAGE); goto err; } if (scert->peer_dh_tmp != NULL) dh_srvr = scert->peer_dh_tmp; else { /* we get them from the cert */ int idx = scert->peer_cert_type; EVP_PKEY *spkey = NULL; dh_srvr = NULL; if (idx >= 0) spkey = X509_get_pubkey(scert->peer_pkeys[idx].x509); if (spkey) { dh_srvr = EVP_PKEY_get1_DH(spkey); EVP_PKEY_free(spkey); } if (dh_srvr == NULL) { SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR); goto err; } } if (s->s3->flags & TLS1_FLAGS_SKIP_CERT_VERIFY) { /* Use client certificate key */ EVP_PKEY *clkey = s->cert->key->privatekey; dh_clnt = NULL; if (clkey) dh_clnt = EVP_PKEY_get1_DH(clkey); if (dh_clnt == NULL) { SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR); goto err; } } else { /* generate a new random key */ if ((dh_clnt = DHparams_dup(dh_srvr)) == NULL) { SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_DH_LIB); goto err; } if (!DH_generate_key(dh_clnt)) { SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_DH_LIB); DH_free(dh_clnt); goto err; } } pmslen = DH_size(dh_clnt); pms = OPENSSL_malloc(pmslen); if (!pms) goto memerr; /* * use the 'p' output buffer for the DH key, but make sure to * clear it out afterwards */ n = DH_compute_key(pms, dh_srvr->pub_key, dh_clnt); if (scert->peer_dh_tmp == NULL) DH_free(dh_srvr); if (n <= 0) { SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_DH_LIB); DH_free(dh_clnt); goto err; } if (s->s3->flags & TLS1_FLAGS_SKIP_CERT_VERIFY) n = 0; else { /* send off the data */ n = BN_num_bytes(dh_clnt->pub_key); s2n(n, p); BN_bn2bin(dh_clnt->pub_key, p); n += 2; } DH_free(dh_clnt); /* perhaps clean things up a bit EAY EAY EAY EAY */ } #endif #ifndef OPENSSL_NO_ECDH else if (alg_k & (SSL_kECDHE | SSL_kECDHr | SSL_kECDHe)) { const EC_GROUP *srvr_group = NULL; EC_KEY *tkey; int ecdh_clnt_cert = 0; int field_size = 0; if (s->session->sess_cert == NULL) { ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE); SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, SSL_R_UNEXPECTED_MESSAGE); goto err; } /* * Did we send out the client's ECDH share for use in premaster * computation as part of client certificate? If so, set * ecdh_clnt_cert to 1. */ if ((alg_k & (SSL_kECDHr | SSL_kECDHe)) && (s->cert != NULL)) { /*- * XXX: For now, we do not support client * authentication using ECDH certificates. * To add such support, one needs to add * code that checks for appropriate * conditions and sets ecdh_clnt_cert to 1. * For example, the cert have an ECC * key on the same curve as the server's * and the key should be authorized for * key agreement. * * One also needs to add code in ssl3_connect * to skip sending the certificate verify * message. * * if ((s->cert->key->privatekey != NULL) && * (s->cert->key->privatekey->type == * EVP_PKEY_EC) && ...) * ecdh_clnt_cert = 1; */ } if (s->session->sess_cert->peer_ecdh_tmp != NULL) { tkey = s->session->sess_cert->peer_ecdh_tmp; } else { /* Get the Server Public Key from Cert */ srvr_pub_pkey = X509_get_pubkey(s->session-> sess_cert->peer_pkeys[SSL_PKEY_ECC].x509); if ((srvr_pub_pkey == NULL) || (srvr_pub_pkey->type != EVP_PKEY_EC) || (srvr_pub_pkey->pkey.ec == NULL)) { SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR); goto err; } tkey = srvr_pub_pkey->pkey.ec; } srvr_group = EC_KEY_get0_group(tkey); srvr_ecpoint = EC_KEY_get0_public_key(tkey); if ((srvr_group == NULL) || (srvr_ecpoint == NULL)) { SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR); goto err; } if ((clnt_ecdh = EC_KEY_new()) == NULL) { SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE); goto err; } if (!EC_KEY_set_group(clnt_ecdh, srvr_group)) { SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_EC_LIB); goto err; } if (ecdh_clnt_cert) { /* * Reuse key info from our certificate We only need our * private key to perform the ECDH computation. */ const BIGNUM *priv_key; tkey = s->cert->key->privatekey->pkey.ec; priv_key = EC_KEY_get0_private_key(tkey); if (priv_key == NULL) { SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE); goto err; } if (!EC_KEY_set_private_key(clnt_ecdh, priv_key)) { SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_EC_LIB); goto err; } } else { /* Generate a new ECDH key pair */ if (!(EC_KEY_generate_key(clnt_ecdh))) { SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_ECDH_LIB); goto err; } } /* * use the 'p' output buffer for the ECDH key, but make sure to * clear it out afterwards */ field_size = EC_GROUP_get_degree(srvr_group); if (field_size <= 0) { SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_ECDH_LIB); goto err; } pmslen = (field_size + 7) / 8; pms = OPENSSL_malloc(pmslen); if (!pms) goto memerr; n = ECDH_compute_key(pms, pmslen, srvr_ecpoint, clnt_ecdh, NULL); if (n <= 0 || pmslen != (size_t)n) { SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_ECDH_LIB); goto err; } if (ecdh_clnt_cert) { /* Send empty client key exch message */ n = 0; } else { /* * First check the size of encoding and allocate memory * accordingly. */ encoded_pt_len = EC_POINT_point2oct(srvr_group, EC_KEY_get0_public_key(clnt_ecdh), POINT_CONVERSION_UNCOMPRESSED, NULL, 0, NULL); encodedPoint = (unsigned char *) OPENSSL_malloc(encoded_pt_len * sizeof(unsigned char)); bn_ctx = BN_CTX_new(); if ((encodedPoint == NULL) || (bn_ctx == NULL)) { SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE); goto err; } /* Encode the public key */ n = EC_POINT_point2oct(srvr_group, EC_KEY_get0_public_key(clnt_ecdh), POINT_CONVERSION_UNCOMPRESSED, encodedPoint, encoded_pt_len, bn_ctx); *p = n; /* length of encoded point */ /* Encoded point will be copied here */ p += 1; /* copy the point */ memcpy((unsigned char *)p, encodedPoint, n); /* increment n to account for length field */ n += 1; } /* Free allocated memory */ BN_CTX_free(bn_ctx); if (encodedPoint != NULL) OPENSSL_free(encodedPoint); if (clnt_ecdh != NULL) EC_KEY_free(clnt_ecdh); EVP_PKEY_free(srvr_pub_pkey); } #endif /* !OPENSSL_NO_ECDH */ else if (alg_k & SSL_kGOST) { /* GOST key exchange message creation */ EVP_PKEY_CTX *pkey_ctx; X509 *peer_cert; size_t msglen; unsigned int md_len; int keytype; unsigned char shared_ukm[32], tmp[256]; EVP_MD_CTX *ukm_hash; EVP_PKEY *pub_key; pmslen = 32; pms = OPENSSL_malloc(pmslen); if (!pms) goto memerr; /* * Get server sertificate PKEY and create ctx from it */ peer_cert = s->session-> sess_cert->peer_pkeys[(keytype = SSL_PKEY_GOST01)].x509; if (!peer_cert) peer_cert = s->session-> sess_cert->peer_pkeys[(keytype = SSL_PKEY_GOST94)].x509; if (!peer_cert) { SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, SSL_R_NO_GOST_CERTIFICATE_SENT_BY_PEER); goto err; } pkey_ctx = EVP_PKEY_CTX_new(pub_key = X509_get_pubkey(peer_cert), NULL); /* * If we have send a certificate, and certificate key * * * parameters match those of server certificate, use * certificate key for key exchange */ /* Otherwise, generate ephemeral key pair */ EVP_PKEY_encrypt_init(pkey_ctx); /* Generate session key */ RAND_bytes(pms, pmslen); /* * If we have client certificate, use its secret as peer key */ if (s->s3->tmp.cert_req && s->cert->key->privatekey) { if (EVP_PKEY_derive_set_peer (pkey_ctx, s->cert->key->privatekey) <= 0) { /* * If there was an error - just ignore it. Ephemeral key * * would be used */ ERR_clear_error(); } } /* * Compute shared IV and store it in algorithm-specific context * data */ ukm_hash = EVP_MD_CTX_create(); EVP_DigestInit(ukm_hash, EVP_get_digestbynid(NID_id_GostR3411_94)); EVP_DigestUpdate(ukm_hash, s->s3->client_random, SSL3_RANDOM_SIZE); EVP_DigestUpdate(ukm_hash, s->s3->server_random, SSL3_RANDOM_SIZE); EVP_DigestFinal_ex(ukm_hash, shared_ukm, &md_len); EVP_MD_CTX_destroy(ukm_hash); if (EVP_PKEY_CTX_ctrl (pkey_ctx, -1, EVP_PKEY_OP_ENCRYPT, EVP_PKEY_CTRL_SET_IV, 8, shared_ukm) < 0) { SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, SSL_R_LIBRARY_BUG); goto err; } /* Make GOST keytransport blob message */ /* * Encapsulate it into sequence */ *(p++) = V_ASN1_SEQUENCE | V_ASN1_CONSTRUCTED; msglen = 255; if (EVP_PKEY_encrypt(pkey_ctx, tmp, &msglen, pms, pmslen) < 0) { SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, SSL_R_LIBRARY_BUG); goto err; } if (msglen >= 0x80) { *(p++) = 0x81; *(p++) = msglen & 0xff; n = msglen + 3; } else { *(p++) = msglen & 0xff; n = msglen + 2; } memcpy(p, tmp, msglen); /* Check if pubkey from client certificate was used */ if (EVP_PKEY_CTX_ctrl (pkey_ctx, -1, -1, EVP_PKEY_CTRL_PEER_KEY, 2, NULL) > 0) { /* Set flag "skip certificate verify" */ s->s3->flags |= TLS1_FLAGS_SKIP_CERT_VERIFY; } EVP_PKEY_CTX_free(pkey_ctx); EVP_PKEY_free(pub_key); } #ifndef OPENSSL_NO_SRP else if (alg_k & SSL_kSRP) { if (s->srp_ctx.A != NULL) { /* send off the data */ n = BN_num_bytes(s->srp_ctx.A); s2n(n, p); BN_bn2bin(s->srp_ctx.A, p); n += 2; } else { SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR); goto err; } if (s->session->srp_username != NULL) OPENSSL_free(s->session->srp_username); s->session->srp_username = BUF_strdup(s->srp_ctx.login); if (s->session->srp_username == NULL) { SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE); goto err; } } #endif #ifndef OPENSSL_NO_PSK else if (alg_k & SSL_kPSK) { /* * The callback needs PSK_MAX_IDENTITY_LEN + 1 bytes to return a * \0-terminated identity. The last byte is for us for simulating * strnlen. */ char identity[PSK_MAX_IDENTITY_LEN + 2]; size_t identity_len; unsigned char *t = NULL; unsigned int psk_len = 0; int psk_err = 1; n = 0; if (s->psk_client_callback == NULL) { SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, SSL_R_PSK_NO_CLIENT_CB); goto err; } memset(identity, 0, sizeof(identity)); /* Allocate maximum size buffer */ pmslen = PSK_MAX_PSK_LEN * 2 + 4; pms = OPENSSL_malloc(pmslen); if (!pms) goto memerr; psk_len = s->psk_client_callback(s, s->ctx->psk_identity_hint, identity, sizeof(identity) - 1, pms, pmslen); if (psk_len > PSK_MAX_PSK_LEN) { SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR); goto psk_err; } else if (psk_len == 0) { SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, SSL_R_PSK_IDENTITY_NOT_FOUND); goto psk_err; } /* Change pmslen to real length */ pmslen = 2 + psk_len + 2 + psk_len; identity[PSK_MAX_IDENTITY_LEN + 1] = '\0'; identity_len = strlen(identity); if (identity_len > PSK_MAX_IDENTITY_LEN) { SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR); goto psk_err; } /* create PSK pre_master_secret */ t = pms; memmove(pms + psk_len + 4, pms, psk_len); s2n(psk_len, t); memset(t, 0, psk_len); t += psk_len; s2n(psk_len, t); if (s->session->psk_identity_hint != NULL) OPENSSL_free(s->session->psk_identity_hint); s->session->psk_identity_hint = BUF_strdup(s->ctx->psk_identity_hint); if (s->ctx->psk_identity_hint != NULL && s->session->psk_identity_hint == NULL) { SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE); goto psk_err; } if (s->session->psk_identity != NULL) OPENSSL_free(s->session->psk_identity); s->session->psk_identity = BUF_strdup(identity); if (s->session->psk_identity == NULL) { SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE); goto psk_err; } s2n(identity_len, p); memcpy(p, identity, identity_len); n = 2 + identity_len; psk_err = 0; psk_err: OPENSSL_cleanse(identity, sizeof(identity)); if (psk_err != 0) { ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); goto err; } } #endif else { ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR); goto err; } ssl_set_handshake_header(s, SSL3_MT_CLIENT_KEY_EXCHANGE, n); s->state = SSL3_ST_CW_KEY_EXCH_B; } /* SSL3_ST_CW_KEY_EXCH_B */ n = ssl_do_write(s); #ifndef OPENSSL_NO_SRP /* Check for SRP */ if (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kSRP) { /* * If everything written generate master key: no need to save PMS as * SRP_generate_client_master_secret generates it internally. */ if (n > 0) { if ((s->session->master_key_length = SRP_generate_client_master_secret(s, s->session->master_key)) < 0) { SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR); goto err; } } } else #endif /* If we haven't written everything save PMS */ if (n <= 0) { s->cert->pms = pms; s->cert->pmslen = pmslen; } else { /* If we don't have a PMS restore */ if (pms == NULL) { pms = s->cert->pms; pmslen = s->cert->pmslen; } if (pms == NULL) { ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE); goto err; } s->session->master_key_length = s->method->ssl3_enc->generate_master_secret(s, s-> session->master_key, pms, pmslen); OPENSSL_cleanse(pms, pmslen); OPENSSL_free(pms); s->cert->pms = NULL; } return n; memerr: ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE); err: if (pms) { OPENSSL_cleanse(pms, pmslen); OPENSSL_free(pms); s->cert->pms = NULL; } #ifndef OPENSSL_NO_ECDH BN_CTX_free(bn_ctx); if (encodedPoint != NULL) OPENSSL_free(encodedPoint); if (clnt_ecdh != NULL) EC_KEY_free(clnt_ecdh); EVP_PKEY_free(srvr_pub_pkey); #endif return (-1); }
CWE-310
6,223
14,787
274506422514579084305030588976838874009
null
null
null
openssl
e1b568dd2462f7cacf98f3d117936c34e2849a6b
0
int ssl3_send_client_verify(SSL *s) { unsigned char *p; unsigned char data[MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH]; EVP_PKEY *pkey; EVP_PKEY_CTX *pctx = NULL; EVP_MD_CTX mctx; unsigned u = 0; unsigned long n; int j; EVP_MD_CTX_init(&mctx); if (s->state == SSL3_ST_CW_CERT_VRFY_A) { p = ssl_handshake_start(s); pkey = s->cert->key->privatekey; /* Create context from key and test if sha1 is allowed as digest */ pctx = EVP_PKEY_CTX_new(pkey, NULL); EVP_PKEY_sign_init(pctx); if (EVP_PKEY_CTX_set_signature_md(pctx, EVP_sha1()) > 0) { if (!SSL_USE_SIGALGS(s)) s->method->ssl3_enc->cert_verify_mac(s, NID_sha1, &(data [MD5_DIGEST_LENGTH])); } else { ERR_clear_error(); } /* * For TLS v1.2 send signature algorithm and signature using agreed * digest and cached handshake records. */ if (SSL_USE_SIGALGS(s)) { long hdatalen = 0; void *hdata; const EVP_MD *md = s->cert->key->digest; hdatalen = BIO_get_mem_data(s->s3->handshake_buffer, &hdata); if (hdatalen <= 0 || !tls12_get_sigandhash(p, pkey, md)) { SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR); goto err; } p += 2; #ifdef SSL_DEBUG fprintf(stderr, "Using TLS 1.2 with client alg %s\n", EVP_MD_name(md)); #endif if (!EVP_SignInit_ex(&mctx, md, NULL) || !EVP_SignUpdate(&mctx, hdata, hdatalen) || !EVP_SignFinal(&mctx, p + 2, &u, pkey)) { SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_EVP_LIB); goto err; } s2n(u, p); n = u + 4; /* * For extended master secret we've already digested cached * records. */ if (s->session->flags & SSL_SESS_FLAG_EXTMS) { BIO_free(s->s3->handshake_buffer); s->s3->handshake_buffer = NULL; s->s3->flags &= ~TLS1_FLAGS_KEEP_HANDSHAKE; } else if (!ssl3_digest_cached_records(s)) goto err; } else #ifndef OPENSSL_NO_RSA if (pkey->type == EVP_PKEY_RSA) { s->method->ssl3_enc->cert_verify_mac(s, NID_md5, &(data[0])); if (RSA_sign(NID_md5_sha1, data, MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH, &(p[2]), &u, pkey->pkey.rsa) <= 0) { SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_RSA_LIB); goto err; } s2n(u, p); n = u + 2; } else #endif #ifndef OPENSSL_NO_DSA if (pkey->type == EVP_PKEY_DSA) { if (!DSA_sign(pkey->save_type, &(data[MD5_DIGEST_LENGTH]), SHA_DIGEST_LENGTH, &(p[2]), (unsigned int *)&j, pkey->pkey.dsa)) { SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_DSA_LIB); goto err; } s2n(j, p); n = j + 2; } else #endif #ifndef OPENSSL_NO_ECDSA if (pkey->type == EVP_PKEY_EC) { if (!ECDSA_sign(pkey->save_type, &(data[MD5_DIGEST_LENGTH]), SHA_DIGEST_LENGTH, &(p[2]), (unsigned int *)&j, pkey->pkey.ec)) { SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_ECDSA_LIB); goto err; } s2n(j, p); n = j + 2; } else #endif if (pkey->type == NID_id_GostR3410_94 || pkey->type == NID_id_GostR3410_2001) { unsigned char signbuf[64]; int i; size_t sigsize = 64; s->method->ssl3_enc->cert_verify_mac(s, NID_id_GostR3411_94, data); if (EVP_PKEY_sign(pctx, signbuf, &sigsize, data, 32) <= 0) { SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR); goto err; } for (i = 63, j = 0; i >= 0; j++, i--) { p[2 + j] = signbuf[i]; } s2n(j, p); n = j + 2; } else { SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR); goto err; } ssl_set_handshake_header(s, SSL3_MT_CERTIFICATE_VERIFY, n); s->state = SSL3_ST_CW_CERT_VRFY_B; } EVP_MD_CTX_cleanup(&mctx); EVP_PKEY_CTX_free(pctx); return ssl_do_write(s); err: EVP_MD_CTX_cleanup(&mctx); EVP_PKEY_CTX_free(pctx); return (-1); }
CWE-310
6,224
14,788
234616646063999842385034839164054602436
null
null
null
openssl
e1b568dd2462f7cacf98f3d117936c34e2849a6b
0
int ssl3_send_next_proto(SSL *s) { unsigned int len, padding_len; unsigned char *d; if (s->state == SSL3_ST_CW_NEXT_PROTO_A) { len = s->next_proto_negotiated_len; padding_len = 32 - ((len + 2) % 32); d = (unsigned char *)s->init_buf->data; d[4] = len; memcpy(d + 5, s->next_proto_negotiated, len); d[5 + len] = padding_len; memset(d + 6 + len, 0, padding_len); *(d++) = SSL3_MT_NEXT_PROTO; l2n3(2 + len + padding_len, d); s->state = SSL3_ST_CW_NEXT_PROTO_B; s->init_num = 4 + 2 + len + padding_len; s->init_off = 0; } return ssl3_do_write(s, SSL3_RT_HANDSHAKE); }
CWE-310
6,225
14,789
13599264731039185044698200998535110569
null
null
null
openssl
e1b568dd2462f7cacf98f3d117936c34e2849a6b
0
int ssl_do_client_cert_cb(SSL *s, X509 **px509, EVP_PKEY **ppkey) { int i = 0; #ifndef OPENSSL_NO_ENGINE if (s->ctx->client_cert_engine) { i = ENGINE_load_ssl_client_cert(s->ctx->client_cert_engine, s, SSL_get_client_CA_list(s), px509, ppkey, NULL, NULL, NULL); if (i != 0) return i; } #endif if (s->ctx->client_cert_cb) i = s->ctx->client_cert_cb(s, px509, ppkey); return i; }
CWE-310
6,226
14,790
109091403475711085668937708973487991814
null
null
null
lxde
bc8c3d871e9ecc67c47ff002b68cf049793faf08
0
static gboolean on_client_socket_event(GIOChannel* ioc, GIOCondition cond, gpointer user_data) { SingleInstClient* client = (SingleInstClient*)user_data; if ( cond & (G_IO_IN|G_IO_PRI) ) { GString *str = g_string_sized_new(1024); gsize got; gchar ch; GIOStatus status; while((status = g_io_channel_read_chars(ioc, &ch, 1, &got, NULL)) == G_IO_STATUS_NORMAL) { if(ch != '\n') { if(ch < 0x20) /* zero or control char */ { g_error("client connection: invalid char %#x", (int)ch); break; } g_string_append_c(str, ch); continue; } if(str->len) { char *line = g_strndup(str->str, str->len); g_string_truncate(str, 0); g_debug("line = %s", line); if(!client->cwd) client->cwd = g_strcompress(line); else if(client->screen_num == -1) { client->screen_num = atoi(line); if(client->screen_num < 0) client->screen_num = 0; } else { char* str = g_strcompress(line); g_ptr_array_add(client->argv, str); } g_free(line); } } g_string_free(str, TRUE); switch(status) { case G_IO_STATUS_ERROR: cond |= G_IO_ERR; break; case G_IO_STATUS_EOF: cond |= G_IO_HUP; default: break; } } if(cond & (G_IO_ERR|G_IO_HUP)) { if(! (cond & G_IO_ERR) ) /* if there is no error */ { /* try to parse argv */ parse_args(client); } clients = g_list_remove(clients, client); single_inst_client_free(client); return FALSE; } return TRUE; }
CWE-20
6,406
14,807
64012546290956174782752189827646687010
null
null
null
lxde
bc8c3d871e9ecc67c47ff002b68cf049793faf08
0
static gboolean on_server_socket_event(GIOChannel* ioc, GIOCondition cond, gpointer user_data) { SingleInstData* data = user_data; if ( cond & (G_IO_IN|G_IO_PRI) ) { int client_sock = accept(g_io_channel_unix_get_fd(ioc), NULL, 0); if(client_sock != -1) { SingleInstClient* client = g_slice_new0(SingleInstClient); client->channel = g_io_channel_unix_new(client_sock); g_io_channel_set_encoding(client->channel, NULL, NULL); client->screen_num = -1; client->argv = g_ptr_array_new(); client->callback = data->cb; client->opt_entries = data->opt_entries; g_ptr_array_add(client->argv, g_strdup(g_get_prgname())); client->watch = g_io_add_watch(client->channel, G_IO_IN|G_IO_PRI|G_IO_ERR|G_IO_HUP, on_client_socket_event, client); clients = g_list_prepend(clients, client); /* g_debug("accept new client"); */ } else g_debug("accept() failed!\n%s", g_strerror(errno)); } if(cond & (G_IO_ERR|G_IO_HUP)) { single_inst_finalize(data); single_inst_init(data); return FALSE; } return TRUE; }
CWE-20
6,407
14,808
196822668399419206921681838621552095068
null
null
null
lxde
bc8c3d871e9ecc67c47ff002b68cf049793faf08
0
static void pass_args_to_existing_instance(const GOptionEntry* opt_entries, int screen_num, int sock) { const GOptionEntry* ent; FILE* f = fdopen(sock, "w"); char* escaped; /* pass cwd */ char* cwd = g_get_current_dir(); escaped = g_strescape(cwd, NULL); fprintf(f, "%s\n", escaped); g_free(cwd); cwd = escaped; /* pass screen number */ fprintf(f, "%d\n", screen_num); for(ent = opt_entries; ent->long_name; ++ent) { switch(ent->arg) { case G_OPTION_ARG_NONE: if(*(gboolean*)ent->arg_data) fprintf(f, "--%s\n", ent->long_name); break; case G_OPTION_ARG_STRING: case G_OPTION_ARG_FILENAME: { char* str = *(char**)ent->arg_data; if(str && *str) { fprintf(f, "--%s\n", ent->long_name); if(g_str_has_prefix(str, "--")) /* strings begining with -- */ fprintf(f, "--\n"); /* prepend a -- to it */ escaped = g_strescape(str, NULL); fprintf(f, "%s\n", escaped); g_free(escaped); } break; } case G_OPTION_ARG_INT: { gint value = *(gint*)ent->arg_data; if(value >= 0) { fprintf(f, "--%s\n%d\n", ent->long_name, value); } break; } case G_OPTION_ARG_STRING_ARRAY: case G_OPTION_ARG_FILENAME_ARRAY: { char** strv = *(char***)ent->arg_data; if(strv && *strv) { if(*ent->long_name) /* G_OPTION_REMAINING = "" */ fprintf(f, "--%s\n", ent->long_name); for(; *strv; ++strv) { char* str = *strv; /* if not absolute path and not URI then prepend cwd or $HOME */ if(str[0] == '~' && str[1] == '\0') ; /* pass "~" as is */ else if(str[0] == '~' && str[1] == '/') { const char *envvar = g_getenv("HOME"); if(envvar) { escaped = g_strescape(envvar, NULL); fprintf(f, "%s", escaped); g_free(escaped); str++; } } else if ((escaped = g_uri_parse_scheme(str))) /* a valid URI */ g_free(escaped); else if(str[0] != '/') fprintf(f, "%s/", cwd); escaped = g_strescape(str, NULL); fprintf(f, "%s\n", escaped); g_free(escaped); } } break; } case G_OPTION_ARG_DOUBLE: fprintf(f, "--%s\n%lf\n", ent->long_name, *(gdouble*)ent->arg_data); break; case G_OPTION_ARG_INT64: fprintf(f, "--%s\n%lld\n", ent->long_name, (long long int)*(gint64*)ent->arg_data); break; case G_OPTION_ARG_CALLBACK: /* Not supported */ break; } } fclose(f); g_free(cwd); }
CWE-20
6,408
14,809
255382239193922591691839691660267237806
null
null
null
lxde
bc8c3d871e9ecc67c47ff002b68cf049793faf08
0
static void single_inst_client_free(SingleInstClient* client) { g_io_channel_shutdown(client->channel, FALSE, NULL); g_io_channel_unref(client->channel); g_source_remove(client->watch); g_free(client->cwd); g_ptr_array_foreach(client->argv, (GFunc)g_free, NULL); g_ptr_array_free(client->argv, TRUE); g_slice_free(SingleInstClient, client); /* g_debug("free client"); */ }
CWE-20
6,409
14,810
141288432259010628203162162928214590345
null
null
null
lxde
bc8c3d871e9ecc67c47ff002b68cf049793faf08
0
void single_inst_finalize(SingleInstData* data) { if(data->sock >=0) { close(data->sock); data->sock = -1; if(data->io_channel) { char sock_path[256]; /* disconnect all clients */ if(clients) { g_list_foreach(clients, (GFunc)single_inst_client_free, NULL); g_list_free(clients); clients = NULL; } if(data->io_watch) { g_source_remove(data->io_watch); data->io_watch = 0; } g_io_channel_unref(data->io_channel); data->io_channel = NULL; /* remove the file */ get_socket_name(data, sock_path, 256); unlink(sock_path); } } }
CWE-20
6,410
14,811
278685857376848716863841903227306684225
null
null
null
lxde
bc8c3d871e9ecc67c47ff002b68cf049793faf08
0
SingleInstResult single_inst_init(SingleInstData* data) { struct sockaddr_un addr; int addr_len; int ret; int reuse; data->io_channel = NULL; data->io_watch = 0; if((data->sock = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) return SINGLE_INST_ERROR; /* FIXME: use abstract socket? */ addr.sun_family = AF_UNIX; get_socket_name(data, addr.sun_path, sizeof(addr.sun_path)); #ifdef SUN_LEN addr_len = SUN_LEN(&addr); #else addr_len = strlen(addr.sun_path) + sizeof(addr.sun_family); #endif /* try to connect to existing instance */ if(connect(data->sock, (struct sockaddr*)&addr, addr_len) == 0) { /* connected successfully, pass args in opt_entries to server process as argv and exit. */ pass_args_to_existing_instance(data->opt_entries, data->screen_num, data->sock); return SINGLE_INST_CLIENT; } /* There is no existing server, and we are in the first instance. */ unlink(addr.sun_path); /* delete old socket file if it exists. */ reuse = 1; ret = setsockopt( data->sock, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse) ); if(ret || bind(data->sock, (struct sockaddr*)&addr, addr_len) == -1) return SINGLE_INST_ERROR; data->io_channel = g_io_channel_unix_new(data->sock); if(data->io_channel == NULL) return SINGLE_INST_ERROR; g_io_channel_set_encoding(data->io_channel, NULL, NULL); g_io_channel_set_buffered(data->io_channel, FALSE); if(listen(data->sock, 5) == -1) return SINGLE_INST_ERROR; data->io_watch = g_io_add_watch(data->io_channel, G_IO_IN|G_IO_ERR|G_IO_PRI|G_IO_HUP, (GIOFunc)on_server_socket_event, data); return SINGLE_INST_SERVER; }
CWE-20
6,411
14,812
177824976987188303941345834131774146464
null
null
null
openssl
1b4a8df38fc9ab3c089ca5765075ee53ec5bd66a
0
int EC_GROUP_get_basis_type(const EC_GROUP *group) { int i = 0; if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) != NID_X9_62_characteristic_two_field) /* everything else is currently not supported */ return 0; while (group->poly[i] != 0) i++; if (i == 4) return NID_X9_62_ppBasis; else if (i == 2) return NID_X9_62_tpBasis; else /* everything else is currently not supported */ return 0; }
6,468
14,864
282311698750076911893709392314970568952
null
null
null
openssl
1b4a8df38fc9ab3c089ca5765075ee53ec5bd66a
0
int EC_GROUP_get_trinomial_basis(const EC_GROUP *group, unsigned int *k) { if (group == NULL) return 0; if (EC_GROUP_method_of(group)->group_set_curve != ec_GF2m_simple_group_set_curve || !((group->poly[0] != 0) && (group->poly[1] != 0) && (group->poly[2] == 0))) { ECerr(EC_F_EC_GROUP_GET_TRINOMIAL_BASIS, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); return 0; } if (k) *k = group->poly[1]; return 1; }
6,470
14,865
210990581744749265095609950378560237356
null
null
null
openssl
1b4a8df38fc9ab3c089ca5765075ee53ec5bd66a
0
EC_GROUP *d2i_ECPKParameters(EC_GROUP **a, const unsigned char **in, long len) { EC_GROUP *group = NULL; ECPKPARAMETERS *params = NULL; if ((params = d2i_ECPKPARAMETERS(NULL, in, len)) == NULL) { ECerr(EC_F_D2I_ECPKPARAMETERS, EC_R_D2I_ECPKPARAMETERS_FAILURE); ECPKPARAMETERS_free(params); return NULL; } if ((group = ec_asn1_pkparameters2group(params)) == NULL) { ECerr(EC_F_D2I_ECPKPARAMETERS, EC_R_PKPARAMETERS2GROUP_FAILURE); return NULL; } if (a && *a) EC_GROUP_clear_free(*a); if (a) *a = group; ECPKPARAMETERS_free(params); return (group); }
6,471
14,866
275610106841419742807522424863006803593
null
null
null
openssl
1b4a8df38fc9ab3c089ca5765075ee53ec5bd66a
0
EC_KEY *d2i_ECParameters(EC_KEY **a, const unsigned char **in, long len) { EC_KEY *ret; if (in == NULL || *in == NULL) { ECerr(EC_F_D2I_ECPARAMETERS, ERR_R_PASSED_NULL_PARAMETER); return NULL; } if (a == NULL || *a == NULL) { if ((ret = EC_KEY_new()) == NULL) { ECerr(EC_F_D2I_ECPARAMETERS, ERR_R_MALLOC_FAILURE); return NULL; } if (a) *a = ret; } else ret = *a; if (!d2i_ECPKParameters(&ret->group, in, len)) { ECerr(EC_F_D2I_ECPARAMETERS, ERR_R_EC_LIB); return NULL; } return ret; }
6,472
14,867
339900378710448476748399870798473235081
null
null
null
openssl
1b4a8df38fc9ab3c089ca5765075ee53ec5bd66a
0
static int ec_asn1_group2curve(const EC_GROUP *group, X9_62_CURVE *curve) { int ok = 0, nid; BIGNUM *tmp_1 = NULL, *tmp_2 = NULL; unsigned char *buffer_1 = NULL, *buffer_2 = NULL, *a_buf = NULL, *b_buf = NULL; size_t len_1, len_2; unsigned char char_zero = 0; if (!group || !curve || !curve->a || !curve->b) return 0; if ((tmp_1 = BN_new()) == NULL || (tmp_2 = BN_new()) == NULL) { ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_MALLOC_FAILURE); goto err; } nid = EC_METHOD_get_field_type(EC_GROUP_method_of(group)); /* get a and b */ if (nid == NID_X9_62_prime_field) { if (!EC_GROUP_get_curve_GFp(group, NULL, tmp_1, tmp_2, NULL)) { ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_EC_LIB); goto err; } } else { /* nid == NID_X9_62_characteristic_two_field */ if (!EC_GROUP_get_curve_GF2m(group, NULL, tmp_1, tmp_2, NULL)) { ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_EC_LIB); goto err; } } len_1 = (size_t)BN_num_bytes(tmp_1); len_2 = (size_t)BN_num_bytes(tmp_2); if (len_1 == 0) { /* len_1 == 0 => a == 0 */ a_buf = &char_zero; len_1 = 1; } else { if ((buffer_1 = OPENSSL_malloc(len_1)) == NULL) { ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_MALLOC_FAILURE); goto err; } if ((len_1 = BN_bn2bin(tmp_1, buffer_1)) == 0) { ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_BN_LIB); goto err; } a_buf = buffer_1; } if (len_2 == 0) { /* len_2 == 0 => b == 0 */ b_buf = &char_zero; len_2 = 1; } else { if ((buffer_2 = OPENSSL_malloc(len_2)) == NULL) { ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_MALLOC_FAILURE); goto err; } if ((len_2 = BN_bn2bin(tmp_2, buffer_2)) == 0) { ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_BN_LIB); goto err; } b_buf = buffer_2; } /* set a and b */ if (!M_ASN1_OCTET_STRING_set(curve->a, a_buf, len_1) || !M_ASN1_OCTET_STRING_set(curve->b, b_buf, len_2)) { ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_ASN1_LIB); goto err; } /* set the seed (optional) */ if (group->seed) { if (!curve->seed) if ((curve->seed = ASN1_BIT_STRING_new()) == NULL) { ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_MALLOC_FAILURE); goto err; } curve->seed->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07); curve->seed->flags |= ASN1_STRING_FLAG_BITS_LEFT; if (!ASN1_BIT_STRING_set(curve->seed, group->seed, (int)group->seed_len)) { ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_ASN1_LIB); goto err; } } else { if (curve->seed) { ASN1_BIT_STRING_free(curve->seed); curve->seed = NULL; } } ok = 1; err:if (buffer_1) OPENSSL_free(buffer_1); if (buffer_2) OPENSSL_free(buffer_2); if (tmp_1) BN_free(tmp_1); if (tmp_2) BN_free(tmp_2); return (ok); }
6,473
14,868
293598655947998722006864827653736145625
null
null
null
openssl
1b4a8df38fc9ab3c089ca5765075ee53ec5bd66a
0
static int ec_asn1_group2fieldid(const EC_GROUP *group, X9_62_FIELDID *field) { int ok = 0, nid; BIGNUM *tmp = NULL; if (group == NULL || field == NULL) return 0; /* clear the old values (if necessary) */ if (field->fieldType != NULL) ASN1_OBJECT_free(field->fieldType); if (field->p.other != NULL) ASN1_TYPE_free(field->p.other); nid = EC_METHOD_get_field_type(EC_GROUP_method_of(group)); /* set OID for the field */ if ((field->fieldType = OBJ_nid2obj(nid)) == NULL) { ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_OBJ_LIB); goto err; } if (nid == NID_X9_62_prime_field) { if ((tmp = BN_new()) == NULL) { ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_MALLOC_FAILURE); goto err; } /* the parameters are specified by the prime number p */ if (!EC_GROUP_get_curve_GFp(group, tmp, NULL, NULL, NULL)) { ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_EC_LIB); goto err; } /* set the prime number */ field->p.prime = BN_to_ASN1_INTEGER(tmp, NULL); if (field->p.prime == NULL) { ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_ASN1_LIB); goto err; } } else { /* nid == NID_X9_62_characteristic_two_field */ int field_type; X9_62_CHARACTERISTIC_TWO *char_two; field->p.char_two = X9_62_CHARACTERISTIC_TWO_new(); char_two = field->p.char_two; if (char_two == NULL) { ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_MALLOC_FAILURE); goto err; } char_two->m = (long)EC_GROUP_get_degree(group); field_type = EC_GROUP_get_basis_type(group); if (field_type == 0) { ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_EC_LIB); goto err; } /* set base type OID */ if ((char_two->type = OBJ_nid2obj(field_type)) == NULL) { ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_OBJ_LIB); goto err; } if (field_type == NID_X9_62_tpBasis) { unsigned int k; if (!EC_GROUP_get_trinomial_basis(group, &k)) goto err; char_two->p.tpBasis = ASN1_INTEGER_new(); if (!char_two->p.tpBasis) { ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_MALLOC_FAILURE); goto err; } if (!ASN1_INTEGER_set(char_two->p.tpBasis, (long)k)) { ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_ASN1_LIB); goto err; } } else if (field_type == NID_X9_62_ppBasis) { unsigned int k1, k2, k3; if (!EC_GROUP_get_pentanomial_basis(group, &k1, &k2, &k3)) goto err; char_two->p.ppBasis = X9_62_PENTANOMIAL_new(); if (!char_two->p.ppBasis) { ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_MALLOC_FAILURE); goto err; } /* set k? values */ char_two->p.ppBasis->k1 = (long)k1; char_two->p.ppBasis->k2 = (long)k2; char_two->p.ppBasis->k3 = (long)k3; } else { /* field_type == NID_X9_62_onBasis */ /* for ONB the parameters are (asn1) NULL */ char_two->p.onBasis = ASN1_NULL_new(); if (!char_two->p.onBasis) { ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_MALLOC_FAILURE); goto err; } } } ok = 1; err:if (tmp) BN_free(tmp); return (ok); }
6,474
14,869
124515869647927414801437169302999544429
null
null
null
openssl
1b4a8df38fc9ab3c089ca5765075ee53ec5bd66a
0
static ECPARAMETERS *ec_asn1_group2parameters(const EC_GROUP *group, ECPARAMETERS *param) { int ok = 0; size_t len = 0; ECPARAMETERS *ret = NULL; BIGNUM *tmp = NULL; unsigned char *buffer = NULL; const EC_POINT *point = NULL; point_conversion_form_t form; if ((tmp = BN_new()) == NULL) { ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_MALLOC_FAILURE); goto err; } if (param == NULL) { if ((ret = ECPARAMETERS_new()) == NULL) { ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_MALLOC_FAILURE); goto err; } } else ret = param; /* set the version (always one) */ ret->version = (long)0x1; /* set the fieldID */ if (!ec_asn1_group2fieldid(group, ret->fieldID)) { ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_EC_LIB); goto err; } /* set the curve */ if (!ec_asn1_group2curve(group, ret->curve)) { ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_EC_LIB); goto err; } /* set the base point */ if ((point = EC_GROUP_get0_generator(group)) == NULL) { ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, EC_R_UNDEFINED_GENERATOR); goto err; } form = EC_GROUP_get_point_conversion_form(group); len = EC_POINT_point2oct(group, point, form, NULL, len, NULL); if (len == 0) { ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_EC_LIB); goto err; } if ((buffer = OPENSSL_malloc(len)) == NULL) { ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_MALLOC_FAILURE); goto err; } if (!EC_POINT_point2oct(group, point, form, buffer, len, NULL)) { ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_EC_LIB); goto err; } if (ret->base == NULL && (ret->base = ASN1_OCTET_STRING_new()) == NULL) { ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_MALLOC_FAILURE); goto err; } if (!ASN1_OCTET_STRING_set(ret->base, buffer, len)) { ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_ASN1_LIB); goto err; } /* set the order */ if (!EC_GROUP_get_order(group, tmp, NULL)) { ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_EC_LIB); goto err; } ret->order = BN_to_ASN1_INTEGER(tmp, ret->order); if (ret->order == NULL) { ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_ASN1_LIB); goto err; } /* set the cofactor (optional) */ if (EC_GROUP_get_cofactor(group, tmp, NULL)) { ret->cofactor = BN_to_ASN1_INTEGER(tmp, ret->cofactor); if (ret->cofactor == NULL) { ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_ASN1_LIB); goto err; } } ok = 1; err:if (!ok) { if (ret && !param) ECPARAMETERS_free(ret); ret = NULL; } if (tmp) BN_free(tmp); if (buffer) OPENSSL_free(buffer); return (ret); }
6,475
14,870
158352603147586531851285496485671040593
null
null
null
openssl
1b4a8df38fc9ab3c089ca5765075ee53ec5bd66a
0
ECPKPARAMETERS *ec_asn1_group2pkparameters(const EC_GROUP *group, ECPKPARAMETERS *params) { int ok = 1, tmp; ECPKPARAMETERS *ret = params; if (ret == NULL) { if ((ret = ECPKPARAMETERS_new()) == NULL) { ECerr(EC_F_EC_ASN1_GROUP2PKPARAMETERS, ERR_R_MALLOC_FAILURE); return NULL; } } else { if (ret->type == 0 && ret->value.named_curve) ASN1_OBJECT_free(ret->value.named_curve); else if (ret->type == 1 && ret->value.parameters) ECPARAMETERS_free(ret->value.parameters); } if (EC_GROUP_get_asn1_flag(group)) { /* * use the asn1 OID to describe the the elliptic curve parameters */ tmp = EC_GROUP_get_curve_name(group); if (tmp) { ret->type = 0; if ((ret->value.named_curve = OBJ_nid2obj(tmp)) == NULL) ok = 0; } else /* we don't kmow the nid => ERROR */ ok = 0; } else { /* use the ECPARAMETERS structure */ ret->type = 1; if ((ret->value.parameters = ec_asn1_group2parameters(group, NULL)) == NULL) ok = 0; } if (!ok) { ECPKPARAMETERS_free(ret); return NULL; } return ret; }
6,476
14,871
217978674185281830802377100500646909913
null
null
null
openssl
1b4a8df38fc9ab3c089ca5765075ee53ec5bd66a
0
static EC_GROUP *ec_asn1_parameters2group(const ECPARAMETERS *params) { int ok = 0, tmp; EC_GROUP *ret = NULL; BIGNUM *p = NULL, *a = NULL, *b = NULL; EC_POINT *point = NULL; long field_bits; if (!params->fieldID || !params->fieldID->fieldType || !params->fieldID->p.ptr) { ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR); goto err; } /* now extract the curve parameters a and b */ if (!params->curve || !params->curve->a || !params->curve->a->data || !params->curve->b || !params->curve->b->data) { ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR); goto err; } a = BN_bin2bn(params->curve->a->data, params->curve->a->length, NULL); if (a == NULL) { ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_BN_LIB); goto err; } b = BN_bin2bn(params->curve->b->data, params->curve->b->length, NULL); if (b == NULL) { ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_BN_LIB); goto err; } /* get the field parameters */ tmp = OBJ_obj2nid(params->fieldID->fieldType); if (tmp == NID_X9_62_characteristic_two_field) { X9_62_CHARACTERISTIC_TWO *char_two; char_two = params->fieldID->p.char_two; field_bits = char_two->m; if (field_bits > OPENSSL_ECC_MAX_FIELD_BITS) { ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_FIELD_TOO_LARGE); goto err; } if ((p = BN_new()) == NULL) { ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_MALLOC_FAILURE); goto err; } /* get the base type */ tmp = OBJ_obj2nid(char_two->type); if (tmp == NID_X9_62_tpBasis) { long tmp_long; if (!char_two->p.tpBasis) { ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR); goto err; } tmp_long = ASN1_INTEGER_get(char_two->p.tpBasis); if (!(char_two->m > tmp_long && tmp_long > 0)) { ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_INVALID_TRINOMIAL_BASIS); goto err; } /* create the polynomial */ if (!BN_set_bit(p, (int)char_two->m)) goto err; if (!BN_set_bit(p, (int)tmp_long)) goto err; if (!BN_set_bit(p, 0)) goto err; } else if (tmp == NID_X9_62_ppBasis) { X9_62_PENTANOMIAL *penta; penta = char_two->p.ppBasis; if (!penta) { ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR); goto err; } if (! (char_two->m > penta->k3 && penta->k3 > penta->k2 && penta->k2 > penta->k1 && penta->k1 > 0)) { ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_INVALID_PENTANOMIAL_BASIS); goto err; } /* create the polynomial */ if (!BN_set_bit(p, (int)char_two->m)) goto err; if (!BN_set_bit(p, (int)penta->k1)) goto err; if (!BN_set_bit(p, (int)penta->k2)) goto err; if (!BN_set_bit(p, (int)penta->k3)) goto err; if (!BN_set_bit(p, 0)) goto err; } else if (tmp == NID_X9_62_onBasis) { ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_NOT_IMPLEMENTED); goto err; } else { /* error */ ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR); goto err; } /* create the EC_GROUP structure */ ret = EC_GROUP_new_curve_GF2m(p, a, b, NULL); } else if (tmp == NID_X9_62_prime_field) { /* we have a curve over a prime field */ /* extract the prime number */ if (!params->fieldID->p.prime) { ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR); goto err; } p = ASN1_INTEGER_to_BN(params->fieldID->p.prime, NULL); if (p == NULL) { ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_ASN1_LIB); goto err; } if (BN_is_negative(p) || BN_is_zero(p)) { ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_INVALID_FIELD); goto err; } field_bits = BN_num_bits(p); if (field_bits > OPENSSL_ECC_MAX_FIELD_BITS) { ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_FIELD_TOO_LARGE); goto err; } /* create the EC_GROUP structure */ ret = EC_GROUP_new_curve_GFp(p, a, b, NULL); } else { ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_INVALID_FIELD); goto err; } if (ret == NULL) { ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_EC_LIB); goto err; } /* extract seed (optional) */ if (params->curve->seed != NULL) { if (ret->seed != NULL) OPENSSL_free(ret->seed); if (!(ret->seed = OPENSSL_malloc(params->curve->seed->length))) { ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_MALLOC_FAILURE); goto err; } memcpy(ret->seed, params->curve->seed->data, params->curve->seed->length); ret->seed_len = params->curve->seed->length; } if (!params->order || !params->base || !params->base->data) { ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR); goto err; } if ((point = EC_POINT_new(ret)) == NULL) goto err; /* set the point conversion form */ EC_GROUP_set_point_conversion_form(ret, (point_conversion_form_t) (params->base->data[0] & ~0x01)); /* extract the ec point */ if (!EC_POINT_oct2point(ret, point, params->base->data, params->base->length, NULL)) { ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_EC_LIB); goto err; } /* extract the order */ if ((a = ASN1_INTEGER_to_BN(params->order, a)) == NULL) { ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_ASN1_LIB); goto err; } if (BN_is_negative(a) || BN_is_zero(a)) { ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_INVALID_GROUP_ORDER); goto err; } if (BN_num_bits(a) > (int)field_bits + 1) { /* Hasse bound */ ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_INVALID_GROUP_ORDER); goto err; } /* extract the cofactor (optional) */ if (params->cofactor == NULL) { if (b) { BN_free(b); b = NULL; } } else if ((b = ASN1_INTEGER_to_BN(params->cofactor, b)) == NULL) { ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_ASN1_LIB); goto err; } /* set the generator, order and cofactor (if present) */ if (!EC_GROUP_set_generator(ret, point, a, b)) { ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_EC_LIB); goto err; } ok = 1; err:if (!ok) { if (ret) EC_GROUP_clear_free(ret); ret = NULL; } if (p) BN_free(p); if (a) BN_free(a); if (b) BN_free(b); if (point) EC_POINT_free(point); return (ret); }
6,477
14,872
226523163801730094839198708762094380705
null
null
null
openssl
1b4a8df38fc9ab3c089ca5765075ee53ec5bd66a
0
EC_GROUP *ec_asn1_pkparameters2group(const ECPKPARAMETERS *params) { EC_GROUP *ret = NULL; int tmp = 0; if (params == NULL) { ECerr(EC_F_EC_ASN1_PKPARAMETERS2GROUP, EC_R_MISSING_PARAMETERS); return NULL; } if (params->type == 0) { /* the curve is given by an OID */ tmp = OBJ_obj2nid(params->value.named_curve); if ((ret = EC_GROUP_new_by_curve_name(tmp)) == NULL) { ECerr(EC_F_EC_ASN1_PKPARAMETERS2GROUP, EC_R_EC_GROUP_NEW_BY_NAME_FAILURE); return NULL; } EC_GROUP_set_asn1_flag(ret, OPENSSL_EC_NAMED_CURVE); } else if (params->type == 1) { /* the parameters are given by a * ECPARAMETERS structure */ ret = ec_asn1_parameters2group(params->value.parameters); if (!ret) { ECerr(EC_F_EC_ASN1_PKPARAMETERS2GROUP, ERR_R_EC_LIB); return NULL; } EC_GROUP_set_asn1_flag(ret, 0x0); } else if (params->type == 2) { /* implicitlyCA */ return NULL; } else { ECerr(EC_F_EC_ASN1_PKPARAMETERS2GROUP, EC_R_ASN1_ERROR); return NULL; } return ret; }
6,478
14,873
60029622412890932053515421356037840878
null
null
null
openssl
1b4a8df38fc9ab3c089ca5765075ee53ec5bd66a
0
int i2d_ECPKParameters(const EC_GROUP *a, unsigned char **out) { int ret = 0; ECPKPARAMETERS *tmp = ec_asn1_group2pkparameters(a, NULL); if (tmp == NULL) { ECerr(EC_F_I2D_ECPKPARAMETERS, EC_R_GROUP2PKPARAMETERS_FAILURE); return 0; } if ((ret = i2d_ECPKPARAMETERS(tmp, out)) == 0) { ECerr(EC_F_I2D_ECPKPARAMETERS, EC_R_I2D_ECPKPARAMETERS_FAILURE); ECPKPARAMETERS_free(tmp); return 0; } ECPKPARAMETERS_free(tmp); return (ret); }
6,479
14,874
313361807886069988973564272348538818067
null
null
null
openssl
1b4a8df38fc9ab3c089ca5765075ee53ec5bd66a
0
int i2d_ECParameters(EC_KEY *a, unsigned char **out) { if (a == NULL) { ECerr(EC_F_I2D_ECPARAMETERS, ERR_R_PASSED_NULL_PARAMETER); return 0; } return i2d_ECPKParameters(a->group, out); }
6,480
14,875
317077796147366466210944182659020665386
null
null
null
openssl
1b4a8df38fc9ab3c089ca5765075ee53ec5bd66a
0
int i2d_ECPrivateKey(EC_KEY *a, unsigned char **out) { int ret = 0, ok = 0; unsigned char *buffer = NULL; size_t buf_len = 0, tmp_len; EC_PRIVATEKEY *priv_key = NULL; if (a == NULL || a->group == NULL || a->priv_key == NULL) { ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_PASSED_NULL_PARAMETER); goto err; } if ((priv_key = EC_PRIVATEKEY_new()) == NULL) { ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_MALLOC_FAILURE); goto err; } priv_key->version = a->version; buf_len = (size_t)BN_num_bytes(a->priv_key); buffer = OPENSSL_malloc(buf_len); if (buffer == NULL) { ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_MALLOC_FAILURE); goto err; } if (!BN_bn2bin(a->priv_key, buffer)) { ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_BN_LIB); goto err; } if (!M_ASN1_OCTET_STRING_set(priv_key->privateKey, buffer, buf_len)) { ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_ASN1_LIB); goto err; } if (!(a->enc_flag & EC_PKEY_NO_PARAMETERS)) { if ((priv_key->parameters = ec_asn1_group2pkparameters(a->group, priv_key->parameters)) == NULL) { ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_EC_LIB); goto err; } } if (!(a->enc_flag & EC_PKEY_NO_PUBKEY)) { priv_key->publicKey = M_ASN1_BIT_STRING_new(); if (priv_key->publicKey == NULL) { ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_MALLOC_FAILURE); goto err; } tmp_len = EC_POINT_point2oct(a->group, a->pub_key, a->conv_form, NULL, 0, NULL); if (tmp_len > buf_len) { unsigned char *tmp_buffer = OPENSSL_realloc(buffer, tmp_len); if (!tmp_buffer) { ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_MALLOC_FAILURE); goto err; } buffer = tmp_buffer; buf_len = tmp_len; } if (!EC_POINT_point2oct(a->group, a->pub_key, a->conv_form, buffer, buf_len, NULL)) { ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_EC_LIB); goto err; } priv_key->publicKey->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07); priv_key->publicKey->flags |= ASN1_STRING_FLAG_BITS_LEFT; if (!M_ASN1_BIT_STRING_set(priv_key->publicKey, buffer, buf_len)) { ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_ASN1_LIB); goto err; } } if ((ret = i2d_EC_PRIVATEKEY(priv_key, out)) == 0) { ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_EC_LIB); goto err; } ok = 1; err: if (buffer) OPENSSL_free(buffer); if (priv_key) EC_PRIVATEKEY_free(priv_key); return (ok ? ret : 0); }
6,481
14,876
80504328987076844450895842737030893820
null
null
null
openssl
1b4a8df38fc9ab3c089ca5765075ee53ec5bd66a
0
int i2o_ECPublicKey(EC_KEY *a, unsigned char **out) { size_t buf_len = 0; int new_buffer = 0; if (a == NULL) { ECerr(EC_F_I2O_ECPUBLICKEY, ERR_R_PASSED_NULL_PARAMETER); return 0; } buf_len = EC_POINT_point2oct(a->group, a->pub_key, a->conv_form, NULL, 0, NULL); if (out == NULL || buf_len == 0) /* out == NULL => just return the length of the octet string */ return buf_len; if (*out == NULL) { if ((*out = OPENSSL_malloc(buf_len)) == NULL) { ECerr(EC_F_I2O_ECPUBLICKEY, ERR_R_MALLOC_FAILURE); return 0; } new_buffer = 1; } if (!EC_POINT_point2oct(a->group, a->pub_key, a->conv_form, *out, buf_len, NULL)) { ECerr(EC_F_I2O_ECPUBLICKEY, ERR_R_EC_LIB); OPENSSL_free(*out); *out = NULL; return 0; } if (!new_buffer) *out += buf_len; return buf_len; }
6,482
14,877
280923021105868507506761298011503474920
null
null
null
openssl
1b4a8df38fc9ab3c089ca5765075ee53ec5bd66a
0
EC_KEY *o2i_ECPublicKey(EC_KEY **a, const unsigned char **in, long len) { EC_KEY *ret = NULL; if (a == NULL || (*a) == NULL || (*a)->group == NULL) { /* * sorry, but a EC_GROUP-structur is necessary to set the public key */ ECerr(EC_F_O2I_ECPUBLICKEY, ERR_R_PASSED_NULL_PARAMETER); return 0; } ret = *a; if (ret->pub_key == NULL && (ret->pub_key = EC_POINT_new(ret->group)) == NULL) { ECerr(EC_F_O2I_ECPUBLICKEY, ERR_R_MALLOC_FAILURE); return 0; } if (!EC_POINT_oct2point(ret->group, ret->pub_key, *in, len, NULL)) { ECerr(EC_F_O2I_ECPUBLICKEY, ERR_R_EC_LIB); return 0; } /* save the point conversion form */ ret->conv_form = (point_conversion_form_t) (*in[0] & ~0x01); *in += len; return ret; }
6,483
14,878
255212790778633919030608777993178901780
null
null
null
openssl
819418110b6fff4a7b96f01a5d68f71df3e3b736
0
int dtls1_check_timeout_num(SSL *s) { unsigned int mtu; s->d1->timeout.num_alerts++; /* Reduce MTU after 2 unsuccessful retransmissions */ if (s->d1->timeout.num_alerts > 2 && !(SSL_get_options(s) & SSL_OP_NO_QUERY_MTU)) { mtu = BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_GET_FALLBACK_MTU, 0, NULL); if (mtu < s->d1->mtu) s->d1->mtu = mtu; } if (s->d1->timeout.num_alerts > DTLS1_TMO_ALERT_COUNT) { /* fail the connection, enough alerts have been sent */ SSLerr(SSL_F_DTLS1_CHECK_TIMEOUT_NUM, SSL_R_READ_TIMEOUT_EXPIRED); return -1; } return 0; }
6,485
14,879
289131795048379886257048442898700198217
null
null
null
openssl
819418110b6fff4a7b96f01a5d68f71df3e3b736
0
void dtls1_clear(SSL *s) { pqueue unprocessed_rcds; pqueue processed_rcds; pqueue buffered_messages; pqueue sent_messages; pqueue buffered_app_data; unsigned int mtu; unsigned int link_mtu; if (s->d1) { unprocessed_rcds = s->d1->unprocessed_rcds.q; processed_rcds = s->d1->processed_rcds.q; buffered_messages = s->d1->buffered_messages; sent_messages = s->d1->sent_messages; buffered_app_data = s->d1->buffered_app_data.q; mtu = s->d1->mtu; link_mtu = s->d1->link_mtu; dtls1_clear_queues(s); memset(s->d1, 0, sizeof(*(s->d1))); if (s->server) { s->d1->cookie_len = sizeof(s->d1->cookie); } if (SSL_get_options(s) & SSL_OP_NO_QUERY_MTU) { s->d1->mtu = mtu; s->d1->link_mtu = link_mtu; } s->d1->unprocessed_rcds.q = unprocessed_rcds; s->d1->processed_rcds.q = processed_rcds; s->d1->buffered_messages = buffered_messages; s->d1->sent_messages = sent_messages; s->d1->buffered_app_data.q = buffered_app_data; } ssl3_clear(s); if (s->options & SSL_OP_CISCO_ANYCONNECT) s->client_version = s->version = DTLS1_BAD_VER; else if (s->method->version == DTLS_ANY_VERSION) s->version = DTLS1_2_VERSION; else s->version = s->method->version; }
6,486
14,880
170335745756763942711873655912912749645
null
null
null
openssl
819418110b6fff4a7b96f01a5d68f71df3e3b736
0
static void dtls1_clear_queues(SSL *s) { pitem *item = NULL; hm_fragment *frag = NULL; DTLS1_RECORD_DATA *rdata; while ((item = pqueue_pop(s->d1->unprocessed_rcds.q)) != NULL) { rdata = (DTLS1_RECORD_DATA *)item->data; if (rdata->rbuf.buf) { OPENSSL_free(rdata->rbuf.buf); } OPENSSL_free(item->data); pitem_free(item); } while ((item = pqueue_pop(s->d1->processed_rcds.q)) != NULL) { rdata = (DTLS1_RECORD_DATA *)item->data; if (rdata->rbuf.buf) { OPENSSL_free(rdata->rbuf.buf); } OPENSSL_free(item->data); pitem_free(item); } while ((item = pqueue_pop(s->d1->buffered_messages)) != NULL) { frag = (hm_fragment *)item->data; dtls1_hm_fragment_free(frag); pitem_free(item); } while ((item = pqueue_pop(s->d1->sent_messages)) != NULL) { frag = (hm_fragment *)item->data; dtls1_hm_fragment_free(frag); pitem_free(item); } while ((item = pqueue_pop(s->d1->buffered_app_data.q)) != NULL) { rdata = (DTLS1_RECORD_DATA *)item->data; if (rdata->rbuf.buf) { OPENSSL_free(rdata->rbuf.buf); } OPENSSL_free(item->data); pitem_free(item); } }
6,487
14,881
338958738742990513343199779805445885514
null
null
null
openssl
819418110b6fff4a7b96f01a5d68f71df3e3b736
0
long dtls1_ctrl(SSL *s, int cmd, long larg, void *parg) { int ret = 0; switch (cmd) { case DTLS_CTRL_GET_TIMEOUT: if (dtls1_get_timeout(s, (struct timeval *)parg) != NULL) { ret = 1; } break; case DTLS_CTRL_HANDLE_TIMEOUT: ret = dtls1_handle_timeout(s); break; case DTLS_CTRL_LISTEN: ret = dtls1_listen(s, parg); break; case SSL_CTRL_CHECK_PROTO_VERSION: /* * For library-internal use; checks that the current protocol is the * highest enabled version (according to s->ctx->method, as version * negotiation may have changed s->method). */ if (s->version == s->ctx->method->version) return 1; /* * Apparently we're using a version-flexible SSL_METHOD (not at its * highest protocol version). */ if (s->ctx->method->version == DTLS_method()->version) { #if DTLS_MAX_VERSION != DTLS1_2_VERSION # error Code needs update for DTLS_method() support beyond DTLS1_2_VERSION. #endif if (!(s->options & SSL_OP_NO_DTLSv1_2)) return s->version == DTLS1_2_VERSION; if (!(s->options & SSL_OP_NO_DTLSv1)) return s->version == DTLS1_VERSION; } return 0; /* Unexpected state; fail closed. */ case DTLS_CTRL_SET_LINK_MTU: if (larg < (long)dtls1_link_min_mtu()) return 0; s->d1->link_mtu = larg; return 1; case DTLS_CTRL_GET_LINK_MIN_MTU: return (long)dtls1_link_min_mtu(); case SSL_CTRL_SET_MTU: /* * We may not have a BIO set yet so can't call dtls1_min_mtu() * We'll have to make do with dtls1_link_min_mtu() and max overhead */ if (larg < (long)dtls1_link_min_mtu() - DTLS1_MAX_MTU_OVERHEAD) return 0; s->d1->mtu = larg; return larg; default: ret = ssl3_ctrl(s, cmd, larg, parg); break; } return (ret); }
6,488
14,882
141868702631606999518983237115674546682
null
null
null
openssl
819418110b6fff4a7b96f01a5d68f71df3e3b736
0
long dtls1_default_timeout(void) { /* * 2 hours, the 24 hours mentioned in the DTLSv1 spec is way too long for * http, the cache would over fill */ return (60 * 60 * 2); }
6,489
14,883
100697575562513212383372648952381499126
null
null
null
openssl
819418110b6fff4a7b96f01a5d68f71df3e3b736
0
void dtls1_double_timeout(SSL *s) { s->d1->timeout_duration *= 2; if (s->d1->timeout_duration > 60) s->d1->timeout_duration = 60; dtls1_start_timer(s); }
6,490
14,884
91847657329529654480927931472230693684
null
null
null
openssl
819418110b6fff4a7b96f01a5d68f71df3e3b736
0
void dtls1_free(SSL *s) { ssl3_free(s); dtls1_clear_queues(s); pqueue_free(s->d1->unprocessed_rcds.q); pqueue_free(s->d1->processed_rcds.q); pqueue_free(s->d1->buffered_messages); pqueue_free(s->d1->sent_messages); pqueue_free(s->d1->buffered_app_data.q); OPENSSL_free(s->d1); s->d1 = NULL; }
6,491
14,885
28031610863617325339996772857096748293
null
null
null
openssl
819418110b6fff4a7b96f01a5d68f71df3e3b736
0
const SSL_CIPHER *dtls1_get_cipher(unsigned int u) { const SSL_CIPHER *ciph = ssl3_get_cipher(u); if (ciph != NULL) { if (ciph->algorithm_enc == SSL_RC4) return NULL; } return ciph; }
6,492
14,886
174432355870250557346425298175459717531
null
null
null
openssl
819418110b6fff4a7b96f01a5d68f71df3e3b736
0
struct timeval *dtls1_get_timeout(SSL *s, struct timeval *timeleft) { struct timeval timenow; /* If no timeout is set, just return NULL */ if (s->d1->next_timeout.tv_sec == 0 && s->d1->next_timeout.tv_usec == 0) { return NULL; } /* Get current time */ get_current_time(&timenow); /* If timer already expired, set remaining time to 0 */ if (s->d1->next_timeout.tv_sec < timenow.tv_sec || (s->d1->next_timeout.tv_sec == timenow.tv_sec && s->d1->next_timeout.tv_usec <= timenow.tv_usec)) { memset(timeleft, 0, sizeof(struct timeval)); return timeleft; } /* Calculate time left until timer expires */ memcpy(timeleft, &(s->d1->next_timeout), sizeof(struct timeval)); timeleft->tv_sec -= timenow.tv_sec; timeleft->tv_usec -= timenow.tv_usec; if (timeleft->tv_usec < 0) { timeleft->tv_sec--; timeleft->tv_usec += 1000000; } /* * If remaining time is less than 15 ms, set it to 0 to prevent issues * because of small devergences with socket timeouts. */ if (timeleft->tv_sec == 0 && timeleft->tv_usec < 15000) { memset(timeleft, 0, sizeof(struct timeval)); } return timeleft; }
6,493
14,887
14921732015833036085779984940156530095
null
null
null
openssl
819418110b6fff4a7b96f01a5d68f71df3e3b736
0
int dtls1_handle_timeout(SSL *s) { /* if no timer is expired, don't do anything */ if (!dtls1_is_timer_expired(s)) { return 0; } dtls1_double_timeout(s); if (dtls1_check_timeout_num(s) < 0) return -1; s->d1->timeout.read_timeouts++; if (s->d1->timeout.read_timeouts > DTLS1_TMO_READ_COUNT) { s->d1->timeout.read_timeouts = 1; } #ifndef OPENSSL_NO_HEARTBEATS if (s->tlsext_hb_pending) { s->tlsext_hb_pending = 0; return dtls1_heartbeat(s); } #endif dtls1_start_timer(s); return dtls1_retransmit_buffered_messages(s); }
6,494
14,888
144319941266468631915908992829327208316
null
null
null
openssl
819418110b6fff4a7b96f01a5d68f71df3e3b736
0
static int dtls1_handshake_write(SSL *s) { return dtls1_do_write(s, SSL3_RT_HANDSHAKE); }
6,495
14,889
316591060547259473826821679412375158437
null
null
null
openssl
819418110b6fff4a7b96f01a5d68f71df3e3b736
0
int dtls1_is_timer_expired(SSL *s) { struct timeval timeleft; /* Get time left until timeout, return false if no timer running */ if (dtls1_get_timeout(s, &timeleft) == NULL) { return 0; } /* Return false if timer is not expired yet */ if (timeleft.tv_sec > 0 || timeleft.tv_usec > 0) { return 0; } /* Timer expired, so return true */ return 1; }
6,496
14,890
181332459036851394206798146248704297263
null
null
null
openssl
819418110b6fff4a7b96f01a5d68f71df3e3b736
0
int dtls1_new(SSL *s) { DTLS1_STATE *d1; if (!ssl3_new(s)) return (0); if ((d1 = OPENSSL_malloc(sizeof *d1)) == NULL) return (0); memset(d1, 0, sizeof *d1); /* d1->handshake_epoch=0; */ d1->unprocessed_rcds.q = pqueue_new(); d1->processed_rcds.q = pqueue_new(); d1->buffered_messages = pqueue_new(); d1->sent_messages = pqueue_new(); d1->buffered_app_data.q = pqueue_new(); if (s->server) { d1->cookie_len = sizeof(s->d1->cookie); } d1->link_mtu = 0; d1->mtu = 0; if (!d1->unprocessed_rcds.q || !d1->processed_rcds.q || !d1->buffered_messages || !d1->sent_messages || !d1->buffered_app_data.q) { if (d1->unprocessed_rcds.q) pqueue_free(d1->unprocessed_rcds.q); if (d1->processed_rcds.q) pqueue_free(d1->processed_rcds.q); if (d1->buffered_messages) pqueue_free(d1->buffered_messages); if (d1->sent_messages) pqueue_free(d1->sent_messages); if (d1->buffered_app_data.q) pqueue_free(d1->buffered_app_data.q); OPENSSL_free(d1); return (0); } s->d1 = d1; s->method->ssl_clear(s); return (1); }
6,497
14,891
319118870969312302631825026447654783056
null
null
null
openssl
819418110b6fff4a7b96f01a5d68f71df3e3b736
0
static void dtls1_set_handshake_header(SSL *s, int htype, unsigned long len) { unsigned char *p = (unsigned char *)s->init_buf->data; dtls1_set_message_header(s, p, htype, len, 0, len); s->init_num = (int)len + DTLS1_HM_HEADER_LENGTH; s->init_off = 0; /* Buffer the message to handle re-xmits */ dtls1_buffer_message(s, 0); }
6,498
14,892
105959742663702248121772327019912855865
null
null
null
openssl
819418110b6fff4a7b96f01a5d68f71df3e3b736
0
void dtls1_start_timer(SSL *s) { #ifndef OPENSSL_NO_SCTP /* Disable timer for SCTP */ if (BIO_dgram_is_sctp(SSL_get_wbio(s))) { memset(&(s->d1->next_timeout), 0, sizeof(struct timeval)); return; } #endif /* If timer is not set, initialize duration with 1 second */ if (s->d1->next_timeout.tv_sec == 0 && s->d1->next_timeout.tv_usec == 0) { s->d1->timeout_duration = 1; } /* Set timeout to current time */ get_current_time(&(s->d1->next_timeout)); /* Add duration to current time */ s->d1->next_timeout.tv_sec += s->d1->timeout_duration; BIO_ctrl(SSL_get_rbio(s), BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT, 0, &(s->d1->next_timeout)); }
6,499
14,893
147716601586709154550185359410611054355
null
null
null
openssl
819418110b6fff4a7b96f01a5d68f71df3e3b736
0
void dtls1_stop_timer(SSL *s) { /* Reset everything */ memset(&(s->d1->timeout), 0, sizeof(struct dtls1_timeout_st)); memset(&(s->d1->next_timeout), 0, sizeof(struct timeval)); s->d1->timeout_duration = 1; BIO_ctrl(SSL_get_rbio(s), BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT, 0, &(s->d1->next_timeout)); /* Clear retransmission buffer */ dtls1_clear_record_buffer(s); }
6,500
14,894
134104865425309836155769882629944546345
null
null
null
openssl
819418110b6fff4a7b96f01a5d68f71df3e3b736
0
static void get_current_time(struct timeval *t) { #if defined(_WIN32) SYSTEMTIME st; union { unsigned __int64 ul; FILETIME ft; } now; GetSystemTime(&st); SystemTimeToFileTime(&st, &now.ft); # ifdef __MINGW32__ now.ul -= 116444736000000000ULL; # else now.ul -= 116444736000000000UI64; /* re-bias to 1/1/1970 */ # endif t->tv_sec = (long)(now.ul / 10000000); t->tv_usec = ((int)(now.ul % 10000000)) / 10; #elif defined(OPENSSL_SYS_VMS) struct timeb tb; ftime(&tb); t->tv_sec = (long)tb.time; t->tv_usec = (long)tb.millitm * 1000; #else gettimeofday(t, NULL); #endif }
6,501
14,895
276524129584419693022011089665856873005
null
null
null
savannah
7f2e4f4f553f6836be7683f66226afac3fa979b8
0
_bdf_add_comment( bdf_font_t* font, char* comment, unsigned long len ) { char* cp; FT_Memory memory = font->memory; FT_Error error = BDF_Err_Ok; if ( FT_RENEW_ARRAY( font->comments, font->comments_len, font->comments_len + len + 1 ) ) goto Exit; cp = font->comments + font->comments_len; FT_MEM_COPY( cp, comment, len ); cp[len] = '\n'; font->comments_len += len + 1; Exit: return error; }
CWE-119
6,502
14,896
158023820506128940218820167981371463012
null
null
null
savannah
7f2e4f4f553f6836be7683f66226afac3fa979b8
0
_bdf_add_property( bdf_font_t* font, char* name, char* value, unsigned long lineno ) { size_t propid; hashnode hn; bdf_property_t *prop, *fp; FT_Memory memory = font->memory; FT_Error error = BDF_Err_Ok; /* First, check whether the property already exists in the font. */ if ( ( hn = hash_lookup( name, (hashtable *)font->internal ) ) != 0 ) { /* The property already exists in the font, so simply replace */ /* the value of the property with the current value. */ fp = font->props + hn->data; switch ( fp->format ) { case BDF_ATOM: /* Delete the current atom if it exists. */ FT_FREE( fp->value.atom ); if ( value && value[0] != 0 ) { if ( FT_STRDUP( fp->value.atom, value ) ) goto Exit; } break; case BDF_INTEGER: fp->value.l = _bdf_atol( value, 0, 10 ); break; case BDF_CARDINAL: fp->value.ul = _bdf_atoul( value, 0, 10 ); break; default: ; } goto Exit; } /* See whether this property type exists yet or not. */ /* If not, create it. */ hn = hash_lookup( name, &(font->proptbl) ); if ( hn == 0 ) { error = bdf_create_property( name, BDF_ATOM, font ); if ( error ) goto Exit; hn = hash_lookup( name, &(font->proptbl) ); } /* Allocate another property if this is overflow. */ if ( font->props_used == font->props_size ) { if ( font->props_size == 0 ) { if ( FT_NEW_ARRAY( font->props, 1 ) ) goto Exit; } else { if ( FT_RENEW_ARRAY( font->props, font->props_size, font->props_size + 1 ) ) goto Exit; } fp = font->props + font->props_size; FT_MEM_ZERO( fp, sizeof ( bdf_property_t ) ); font->props_size++; } propid = hn->data; if ( propid >= _num_bdf_properties ) prop = font->user_props + ( propid - _num_bdf_properties ); else prop = (bdf_property_t*)_bdf_properties + propid; fp = font->props + font->props_used; fp->name = prop->name; fp->format = prop->format; fp->builtin = prop->builtin; switch ( prop->format ) { case BDF_ATOM: fp->value.atom = 0; if ( value != 0 && value[0] ) { if ( FT_STRDUP( fp->value.atom, value ) ) goto Exit; } break; case BDF_INTEGER: fp->value.l = _bdf_atol( value, 0, 10 ); break; case BDF_CARDINAL: fp->value.ul = _bdf_atoul( value, 0, 10 ); break; } /* If the property happens to be a comment, then it doesn't need */ /* to be added to the internal hash table. */ if ( ft_memcmp( name, "COMMENT", 7 ) != 0 ) { /* Add the property to the font property table. */ error = hash_insert( fp->name, font->props_used, (hashtable *)font->internal, memory ); if ( error ) goto Exit; } font->props_used++; /* Some special cases need to be handled here. The DEFAULT_CHAR */ /* property needs to be located if it exists in the property list, the */ /* FONT_ASCENT and FONT_DESCENT need to be assigned if they are */ /* present, and the SPACING property should override the default */ /* spacing. */ if ( ft_memcmp( name, "DEFAULT_CHAR", 12 ) == 0 ) font->default_char = fp->value.l; else if ( ft_memcmp( name, "FONT_ASCENT", 11 ) == 0 ) font->font_ascent = fp->value.l; else if ( ft_memcmp( name, "FONT_DESCENT", 12 ) == 0 ) font->font_descent = fp->value.l; else if ( ft_memcmp( name, "SPACING", 7 ) == 0 ) { if ( !fp->value.atom ) { FT_ERROR(( "_bdf_add_property: " ERRMSG8, lineno, "SPACING" )); error = BDF_Err_Invalid_File_Format; goto Exit; } if ( fp->value.atom[0] == 'p' || fp->value.atom[0] == 'P' ) font->spacing = BDF_PROPORTIONAL; else if ( fp->value.atom[0] == 'm' || fp->value.atom[0] == 'M' ) font->spacing = BDF_MONOWIDTH; else if ( fp->value.atom[0] == 'c' || fp->value.atom[0] == 'C' ) font->spacing = BDF_CHARCELL; } Exit: return error; }
CWE-119
6,503
14,897
34558831116170679020387632693520845022
null
null
null
savannah
7f2e4f4f553f6836be7683f66226afac3fa979b8
0
_bdf_atol( char* s, char** end, int base ) { long v, neg; const unsigned char* dmap; if ( s == 0 || *s == 0 ) return 0; /* Make sure the radix is something recognizable. Default to 10. */ switch ( base ) { case 8: dmap = odigits; break; case 16: dmap = hdigits; break; default: base = 10; dmap = ddigits; break; } /* Check for a minus sign. */ neg = 0; if ( *s == '-' ) { s++; neg = 1; } /* Check for the special hex prefix. */ if ( *s == '0' && ( *( s + 1 ) == 'x' || *( s + 1 ) == 'X' ) ) { base = 16; dmap = hdigits; s += 2; } for ( v = 0; sbitset( dmap, *s ); s++ ) v = v * base + a2i[(int)*s]; if ( end != 0 ) *end = s; return ( !neg ) ? v : -v; }
CWE-119
6,504
14,898
196450300110983879820445622335036332989
null
null
null
savannah
7f2e4f4f553f6836be7683f66226afac3fa979b8
0
_bdf_atos( char* s, char** end, int base ) { short v, neg; const unsigned char* dmap; if ( s == 0 || *s == 0 ) return 0; /* Make sure the radix is something recognizable. Default to 10. */ switch ( base ) { case 8: dmap = odigits; break; case 16: dmap = hdigits; break; default: base = 10; dmap = ddigits; break; } /* Check for a minus. */ neg = 0; if ( *s == '-' ) { s++; neg = 1; } /* Check for the special hex prefix. */ if ( *s == '0' && ( *( s + 1 ) == 'x' || *( s + 1 ) == 'X' ) ) { base = 16; dmap = hdigits; s += 2; } for ( v = 0; sbitset( dmap, *s ); s++ ) v = (short)( v * base + a2i[(int)*s] ); if ( end != 0 ) *end = s; return (short)( ( !neg ) ? v : -v ); }
CWE-119
6,505
14,899
333100058061949558667421994704228942049
null
null
null
savannah
7f2e4f4f553f6836be7683f66226afac3fa979b8
0
_bdf_atoul( char* s, char** end, int base ) { unsigned long v; const unsigned char* dmap; if ( s == 0 || *s == 0 ) return 0; /* Make sure the radix is something recognizable. Default to 10. */ switch ( base ) { case 8: dmap = odigits; break; case 16: dmap = hdigits; break; default: base = 10; dmap = ddigits; break; } /* Check for the special hex prefix. */ if ( *s == '0' && ( *( s + 1 ) == 'x' || *( s + 1 ) == 'X' ) ) { base = 16; dmap = hdigits; s += 2; } for ( v = 0; sbitset( dmap, *s ); s++ ) v = v * base + a2i[(int)*s]; if ( end != 0 ) *end = s; return v; }
CWE-119
6,506
14,900
305255632064333585692304502393924287913
null
null
null
savannah
7f2e4f4f553f6836be7683f66226afac3fa979b8
0
_bdf_is_atom( char* line, unsigned long linelen, char** name, char** value, bdf_font_t* font ) { int hold; char *sp, *ep; bdf_property_t* p; *name = sp = ep = line; while ( *ep && *ep != ' ' && *ep != '\t' ) ep++; hold = -1; if ( *ep ) { hold = *ep; *ep = 0; } p = bdf_get_property( sp, font ); /* Restore the character that was saved before any return can happen. */ if ( hold != -1 ) *ep = (char)hold; /* If the property exists and is not an atom, just return here. */ if ( p && p->format != BDF_ATOM ) return 0; /* The property is an atom. Trim all leading and trailing whitespace */ /* and double quotes for the atom value. */ sp = ep; ep = line + linelen; /* Trim the leading whitespace if it exists. */ if ( *sp ) *sp++ = 0; while ( *sp && ( *sp == ' ' || *sp == '\t' ) ) sp++; /* Trim the leading double quote if it exists. */ if ( *sp == '"' ) sp++; *value = sp; /* Trim the trailing whitespace if it exists. */ while ( ep > sp && ( *( ep - 1 ) == ' ' || *( ep - 1 ) == '\t' ) ) *--ep = 0; /* Trim the trailing double quote if it exists. */ if ( ep > sp && *( ep - 1 ) == '"' ) *--ep = 0; return 1; }
CWE-119
6,507
14,901
119272594404344998863640960551595632302
null
null
null
savannah
7f2e4f4f553f6836be7683f66226afac3fa979b8
0
_bdf_list_done( _bdf_list_t* list ) { FT_Memory memory = list->memory; if ( memory ) { FT_FREE( list->field ); FT_ZERO( list ); } }
CWE-119
6,508
14,902
58518855845036810700749494620083338023
null
null
null
savannah
7f2e4f4f553f6836be7683f66226afac3fa979b8
0
_bdf_list_ensure( _bdf_list_t* list, unsigned long num_items ) /* same as _bdf_list_t.used */ { FT_Error error = BDF_Err_Ok; if ( num_items > list->size ) { unsigned long oldsize = list->size; /* same as _bdf_list_t.size */ unsigned long newsize = oldsize + ( oldsize >> 1 ) + 5; unsigned long bigsize = (unsigned long)( FT_INT_MAX / sizeof ( char* ) ); FT_Memory memory = list->memory; if ( oldsize == bigsize ) { error = BDF_Err_Out_Of_Memory; goto Exit; } else if ( newsize < oldsize || newsize > bigsize ) newsize = bigsize; if ( FT_RENEW_ARRAY( list->field, oldsize, newsize ) ) goto Exit; list->size = newsize; } Exit: return error; }
CWE-119
6,509
14,903
320297282913049008129935985132405411736
null
null
null
savannah
7f2e4f4f553f6836be7683f66226afac3fa979b8
0
_bdf_list_init( _bdf_list_t* list, FT_Memory memory ) { FT_ZERO( list ); list->memory = memory; }
CWE-119
6,510
14,904
169067033503534037316356652622207214336
null
null
null
savannah
7f2e4f4f553f6836be7683f66226afac3fa979b8
0
_bdf_list_join( _bdf_list_t* list, int c, unsigned long *alen ) { unsigned long i, j; char *fp, *dp; *alen = 0; if ( list == 0 || list->used == 0 ) return 0; dp = list->field[0]; for ( i = j = 0; i < list->used; i++ ) { fp = list->field[i]; while ( *fp ) dp[j++] = *fp++; if ( i + 1 < list->used ) dp[j++] = (char)c; } if ( dp != empty ) dp[j] = 0; *alen = j; return dp; }
CWE-119
6,511
14,905
306891429829116077607333268020393793583
null
null
null
savannah
7f2e4f4f553f6836be7683f66226afac3fa979b8
0
_bdf_list_shift( _bdf_list_t* list, unsigned long n ) { unsigned long i, u; if ( list == 0 || list->used == 0 || n == 0 ) return; if ( n >= list->used ) { list->used = 0; return; } for ( u = n, i = 0; u < list->used; i++, u++ ) list->field[i] = list->field[u]; list->used -= n; }
CWE-119
6,512
14,906
72721678065279443951362093496275698084
null
null
null
savannah
7f2e4f4f553f6836be7683f66226afac3fa979b8
0
_bdf_list_split( _bdf_list_t* list, char* separators, char* line, unsigned long linelen ) { int mult, final_empty; char *sp, *ep, *end; char seps[32]; FT_Error error = BDF_Err_Ok; /* Initialize the list. */ list->used = 0; if ( list->size ) { list->field[0] = (char*)empty; list->field[1] = (char*)empty; list->field[2] = (char*)empty; list->field[3] = (char*)empty; list->field[4] = (char*)empty; } /* If the line is empty, then simply return. */ if ( linelen == 0 || line[0] == 0 ) goto Exit; /* In the original code, if the `separators' parameter is NULL or */ /* empty, the list is split into individual bytes. We don't need */ /* this, so an error is signaled. */ if ( separators == 0 || *separators == 0 ) { error = BDF_Err_Invalid_Argument; goto Exit; } /* Prepare the separator bitmap. */ FT_MEM_ZERO( seps, 32 ); /* If the very last character of the separator string is a plus, then */ /* set the `mult' flag to indicate that multiple separators should be */ /* collapsed into one. */ for ( mult = 0, sp = separators; sp && *sp; sp++ ) { if ( *sp == '+' && *( sp + 1 ) == 0 ) mult = 1; else setsbit( seps, *sp ); } /* Break the line up into fields. */ for ( final_empty = 0, sp = ep = line, end = sp + linelen; sp < end && *sp; ) { /* Collect everything that is not a separator. */ for ( ; *ep && !sbitset( seps, *ep ); ep++ ) ; /* Resize the list if necessary. */ if ( list->used == list->size ) { error = _bdf_list_ensure( list, list->used + 1 ); if ( error ) goto Exit; } /* Assign the field appropriately. */ list->field[list->used++] = ( ep > sp ) ? sp : (char*)empty; sp = ep; if ( mult ) { /* If multiple separators should be collapsed, do it now by */ /* setting all the separator characters to 0. */ for ( ; *ep && sbitset( seps, *ep ); ep++ ) *ep = 0; } else if ( *ep != 0 ) /* Don't collapse multiple separators by making them 0, so just */ /* make the one encountered 0. */ *ep++ = 0; final_empty = ( ep > sp && *ep == 0 ); sp = ep; } /* Finally, NULL-terminate the list. */ if ( list->used + final_empty >= list->size ) { error = _bdf_list_ensure( list, list->used + final_empty + 1 ); if ( error ) goto Exit; } if ( final_empty ) list->field[list->used++] = (char*)empty; list->field[list->used] = 0; Exit: return error; }
CWE-119
6,513
14,907
71652461231648827126399626636391592101
null
null
null
savannah
7f2e4f4f553f6836be7683f66226afac3fa979b8
0
_bdf_parse_properties( char* line, unsigned long linelen, unsigned long lineno, void* call_data, void* client_data ) { unsigned long vlen; _bdf_line_func_t* next; _bdf_parse_t* p; char* name; char* value; char nbuf[128]; FT_Error error = BDF_Err_Ok; FT_UNUSED( lineno ); next = (_bdf_line_func_t *)call_data; p = (_bdf_parse_t *) client_data; /* Check for the end of the properties. */ if ( ft_memcmp( line, "ENDPROPERTIES", 13 ) == 0 ) { /* If the FONT_ASCENT or FONT_DESCENT properties have not been */ /* encountered yet, then make sure they are added as properties and */ /* make sure they are set from the font bounding box info. */ /* */ /* This is *always* done regardless of the options, because X11 */ /* requires these two fields to compile fonts. */ if ( bdf_get_font_property( p->font, "FONT_ASCENT" ) == 0 ) { p->font->font_ascent = p->font->bbx.ascent; ft_sprintf( nbuf, "%hd", p->font->bbx.ascent ); error = _bdf_add_property( p->font, (char *)"FONT_ASCENT", nbuf, lineno ); if ( error ) goto Exit; FT_TRACE2(( "_bdf_parse_properties: " ACMSG1, p->font->bbx.ascent )); p->font->modified = 1; } if ( bdf_get_font_property( p->font, "FONT_DESCENT" ) == 0 ) { p->font->font_descent = p->font->bbx.descent; ft_sprintf( nbuf, "%hd", p->font->bbx.descent ); error = _bdf_add_property( p->font, (char *)"FONT_DESCENT", nbuf, lineno ); if ( error ) goto Exit; FT_TRACE2(( "_bdf_parse_properties: " ACMSG2, p->font->bbx.descent )); p->font->modified = 1; } p->flags &= ~_BDF_PROPS; *next = _bdf_parse_glyphs; goto Exit; } /* Ignore the _XFREE86_GLYPH_RANGES properties. */ if ( ft_memcmp( line, "_XFREE86_GLYPH_RANGES", 21 ) == 0 ) goto Exit; /* Handle COMMENT fields and properties in a special way to preserve */ /* the spacing. */ if ( ft_memcmp( line, "COMMENT", 7 ) == 0 ) { name = value = line; value += 7; if ( *value ) *value++ = 0; error = _bdf_add_property( p->font, name, value, lineno ); if ( error ) goto Exit; } else if ( _bdf_is_atom( line, linelen, &name, &value, p->font ) ) { error = _bdf_add_property( p->font, name, value, lineno ); if ( error ) goto Exit; } else { error = _bdf_list_split( &p->list, (char *)" +", line, linelen ); if ( error ) goto Exit; name = p->list.field[0]; _bdf_list_shift( &p->list, 1 ); value = _bdf_list_join( &p->list, ' ', &vlen ); error = _bdf_add_property( p->font, name, value, lineno ); if ( error ) goto Exit; } Exit: return error; }
CWE-119
6,514
14,908
337904500919883932215034246111650207135
null
null
null
savannah
7f2e4f4f553f6836be7683f66226afac3fa979b8
0
_bdf_parse_start( char* line, unsigned long linelen, unsigned long lineno, void* call_data, void* client_data ) { unsigned long slen; _bdf_line_func_t* next; _bdf_parse_t* p; bdf_font_t* font; char *s; FT_Memory memory = NULL; FT_Error error = BDF_Err_Ok; FT_UNUSED( lineno ); /* only used in debug mode */ next = (_bdf_line_func_t *)call_data; p = (_bdf_parse_t *) client_data; if ( p->font ) memory = p->font->memory; /* Check for a comment. This is done to handle those fonts that have */ /* comments before the STARTFONT line for some reason. */ if ( ft_memcmp( line, "COMMENT", 7 ) == 0 ) { if ( p->opts->keep_comments != 0 && p->font != 0 ) { linelen -= 7; s = line + 7; if ( *s != 0 ) { s++; linelen--; } error = _bdf_add_comment( p->font, s, linelen ); if ( error ) goto Exit; /* here font is not defined! */ } goto Exit; } if ( !( p->flags & _BDF_START ) ) { memory = p->memory; if ( ft_memcmp( line, "STARTFONT", 9 ) != 0 ) { /* we don't emit an error message since this code gets */ /* explicitly caught one level higher */ error = BDF_Err_Missing_Startfont_Field; goto Exit; } p->flags = _BDF_START; font = p->font = 0; if ( FT_NEW( font ) ) goto Exit; p->font = font; font->memory = p->memory; p->memory = 0; { /* setup */ size_t i; bdf_property_t* prop; error = hash_init( &(font->proptbl), memory ); if ( error ) goto Exit; for ( i = 0, prop = (bdf_property_t*)_bdf_properties; i < _num_bdf_properties; i++, prop++ ) { error = hash_insert( prop->name, i, &(font->proptbl), memory ); if ( error ) goto Exit; } } if ( FT_ALLOC( p->font->internal, sizeof ( hashtable ) ) ) goto Exit; error = hash_init( (hashtable *)p->font->internal,memory ); if ( error ) goto Exit; p->font->spacing = p->opts->font_spacing; p->font->default_char = -1; goto Exit; } /* Check for the start of the properties. */ if ( ft_memcmp( line, "STARTPROPERTIES", 15 ) == 0 ) { if ( !( p->flags & _BDF_FONT_BBX ) ) { /* Missing the FONTBOUNDINGBOX field. */ FT_ERROR(( "_bdf_parse_start: " ERRMSG1, lineno, "FONTBOUNDINGBOX" )); error = BDF_Err_Missing_Fontboundingbox_Field; goto Exit; } error = _bdf_list_split( &p->list, (char *)" +", line, linelen ); if ( error ) goto Exit; /* at this point, `p->font' can't be NULL */ p->cnt = p->font->props_size = _bdf_atoul( p->list.field[1], 0, 10 ); if ( FT_NEW_ARRAY( p->font->props, p->cnt ) ) { p->font->props_size = 0; goto Exit; } p->flags |= _BDF_PROPS; *next = _bdf_parse_properties; goto Exit; } /* Check for the FONTBOUNDINGBOX field. */ if ( ft_memcmp( line, "FONTBOUNDINGBOX", 15 ) == 0 ) { if ( !( p->flags & _BDF_SIZE ) ) { /* Missing the SIZE field. */ FT_ERROR(( "_bdf_parse_start: " ERRMSG1, lineno, "SIZE" )); error = BDF_Err_Missing_Size_Field; goto Exit; } error = _bdf_list_split( &p->list, (char *)" +", line, linelen ); if ( error ) goto Exit; p->font->bbx.width = _bdf_atos( p->list.field[1], 0, 10 ); p->font->bbx.height = _bdf_atos( p->list.field[2], 0, 10 ); p->font->bbx.x_offset = _bdf_atos( p->list.field[3], 0, 10 ); p->font->bbx.y_offset = _bdf_atos( p->list.field[4], 0, 10 ); p->font->bbx.ascent = (short)( p->font->bbx.height + p->font->bbx.y_offset ); p->font->bbx.descent = (short)( -p->font->bbx.y_offset ); p->flags |= _BDF_FONT_BBX; goto Exit; } /* The next thing to check for is the FONT field. */ if ( ft_memcmp( line, "FONT", 4 ) == 0 ) { error = _bdf_list_split( &p->list, (char *)" +", line, linelen ); if ( error ) goto Exit; _bdf_list_shift( &p->list, 1 ); s = _bdf_list_join( &p->list, ' ', &slen ); if ( !s ) { FT_ERROR(( "_bdf_parse_start: " ERRMSG8, lineno, "FONT" )); error = BDF_Err_Invalid_File_Format; goto Exit; } /* Allowing multiple `FONT' lines (which is invalid) doesn't hurt... */ FT_FREE( p->font->name ); if ( FT_NEW_ARRAY( p->font->name, slen + 1 ) ) goto Exit; FT_MEM_COPY( p->font->name, s, slen + 1 ); /* If the font name is an XLFD name, set the spacing to the one in */ /* the font name. If there is no spacing fall back on the default. */ error = _bdf_set_default_spacing( p->font, p->opts, lineno ); if ( error ) goto Exit; p->flags |= _BDF_FONT_NAME; goto Exit; } /* Check for the SIZE field. */ if ( ft_memcmp( line, "SIZE", 4 ) == 0 ) { if ( !( p->flags & _BDF_FONT_NAME ) ) { /* Missing the FONT field. */ FT_ERROR(( "_bdf_parse_start: " ERRMSG1, lineno, "FONT" )); error = BDF_Err_Missing_Font_Field; goto Exit; } error = _bdf_list_split( &p->list, (char *)" +", line, linelen ); if ( error ) goto Exit; p->font->point_size = _bdf_atoul( p->list.field[1], 0, 10 ); p->font->resolution_x = _bdf_atoul( p->list.field[2], 0, 10 ); p->font->resolution_y = _bdf_atoul( p->list.field[3], 0, 10 ); /* Check for the bits per pixel field. */ if ( p->list.used == 5 ) { unsigned short bitcount, i, shift; p->font->bpp = (unsigned short)_bdf_atos( p->list.field[4], 0, 10 ); /* Only values 1, 2, 4, 8 are allowed. */ shift = p->font->bpp; bitcount = 0; for ( i = 0; shift > 0; i++ ) { if ( shift & 1 ) bitcount = i; shift >>= 1; } shift = (short)( ( bitcount > 3 ) ? 8 : ( 1 << bitcount ) ); if ( p->font->bpp > shift || p->font->bpp != shift ) { /* select next higher value */ p->font->bpp = (unsigned short)( shift << 1 ); FT_TRACE2(( "_bdf_parse_start: " ACMSG11, p->font->bpp )); } } else p->font->bpp = 1; p->flags |= _BDF_SIZE; goto Exit; } /* Check for the CHARS field -- font properties are optional */ if ( ft_memcmp( line, "CHARS", 5 ) == 0 ) { char nbuf[128]; if ( !( p->flags & _BDF_FONT_BBX ) ) { /* Missing the FONTBOUNDINGBOX field. */ FT_ERROR(( "_bdf_parse_start: " ERRMSG1, lineno, "FONTBOUNDINGBOX" )); error = BDF_Err_Missing_Fontboundingbox_Field; goto Exit; } /* Add the two standard X11 properties which are required */ /* for compiling fonts. */ p->font->font_ascent = p->font->bbx.ascent; ft_sprintf( nbuf, "%hd", p->font->bbx.ascent ); error = _bdf_add_property( p->font, (char *)"FONT_ASCENT", nbuf, lineno ); if ( error ) goto Exit; FT_TRACE2(( "_bdf_parse_properties: " ACMSG1, p->font->bbx.ascent )); p->font->font_descent = p->font->bbx.descent; ft_sprintf( nbuf, "%hd", p->font->bbx.descent ); error = _bdf_add_property( p->font, (char *)"FONT_DESCENT", nbuf, lineno ); if ( error ) goto Exit; FT_TRACE2(( "_bdf_parse_properties: " ACMSG2, p->font->bbx.descent )); p->font->modified = 1; *next = _bdf_parse_glyphs; /* A special return value. */ error = -1; goto Exit; } FT_ERROR(( "_bdf_parse_start: " ERRMSG9, lineno )); error = BDF_Err_Invalid_File_Format; Exit: return error; }
CWE-119
6,515
14,909
176427778408371560422220254425938308161
null
null
null
savannah
7f2e4f4f553f6836be7683f66226afac3fa979b8
0
_bdf_readstream( FT_Stream stream, _bdf_line_func_t callback, void* client_data, unsigned long *lno ) { _bdf_line_func_t cb; unsigned long lineno, buf_size; int refill, hold, to_skip; ptrdiff_t bytes, start, end, cursor, avail; char* buf = 0; FT_Memory memory = stream->memory; FT_Error error = BDF_Err_Ok; if ( callback == 0 ) { error = BDF_Err_Invalid_Argument; goto Exit; } /* initial size and allocation of the input buffer */ buf_size = 1024; if ( FT_NEW_ARRAY( buf, buf_size ) ) goto Exit; cb = callback; lineno = 1; buf[0] = 0; start = 0; end = 0; avail = 0; cursor = 0; refill = 1; to_skip = NO_SKIP; bytes = 0; /* make compiler happy */ for (;;) { if ( refill ) { bytes = (ptrdiff_t)FT_Stream_TryRead( stream, (FT_Byte*)buf + cursor, (FT_ULong)( buf_size - cursor ) ); avail = cursor + bytes; cursor = 0; refill = 0; } end = start; /* should we skip an optional character like \n or \r? */ if ( start < avail && buf[start] == to_skip ) { start += 1; to_skip = NO_SKIP; continue; } /* try to find the end of the line */ while ( end < avail && buf[end] != '\n' && buf[end] != '\r' ) end++; /* if we hit the end of the buffer, try shifting its content */ /* or even resizing it */ if ( end >= avail ) { if ( bytes == 0 ) /* last line in file doesn't end in \r or \n */ break; /* ignore it then exit */ if ( start == 0 ) { /* this line is definitely too long; try resizing the input */ /* buffer a bit to handle it. */ FT_ULong new_size; if ( buf_size >= 65536UL ) /* limit ourselves to 64KByte */ { FT_ERROR(( "_bdf_readstream: " ERRMSG6, lineno )); error = BDF_Err_Invalid_Argument; goto Exit; } new_size = buf_size * 2; if ( FT_RENEW_ARRAY( buf, buf_size, new_size ) ) goto Exit; cursor = buf_size; buf_size = new_size; } else { bytes = avail - start; FT_MEM_COPY( buf, buf + start, bytes ); cursor = bytes; avail -= bytes; start = 0; } refill = 1; continue; } /* Temporarily NUL-terminate the line. */ hold = buf[end]; buf[end] = 0; /* XXX: Use encoding independent value for 0x1a */ if ( buf[start] != '#' && buf[start] != 0x1a && end > start ) { error = (*cb)( buf + start, end - start, lineno, (void*)&cb, client_data ); /* Redo if we have encountered CHARS without properties. */ if ( error == -1 ) error = (*cb)( buf + start, end - start, lineno, (void*)&cb, client_data ); if ( error ) break; } lineno += 1; buf[end] = (char)hold; start = end + 1; if ( hold == '\n' ) to_skip = '\r'; else if ( hold == '\r' ) to_skip = '\n'; else to_skip = NO_SKIP; } *lno = lineno; Exit: FT_FREE( buf ); return error; }
CWE-119
6,516
14,910
188470612920133359654732749626876948984
null
null
null
savannah
7f2e4f4f553f6836be7683f66226afac3fa979b8
0
_bdf_set_default_spacing( bdf_font_t* font, bdf_options_t* opts, unsigned long lineno ) { size_t len; char name[256]; _bdf_list_t list; FT_Memory memory; FT_Error error = BDF_Err_Ok; if ( font == 0 || font->name == 0 || font->name[0] == 0 ) { error = BDF_Err_Invalid_Argument; goto Exit; } memory = font->memory; _bdf_list_init( &list, memory ); font->spacing = opts->font_spacing; len = ft_strlen( font->name ) + 1; /* Limit ourselves to 256 characters in the font name. */ if ( len >= 256 ) { FT_ERROR(( "_bdf_set_default_spacing: " ERRMSG7, lineno )); error = BDF_Err_Invalid_Argument; goto Exit; } FT_MEM_COPY( name, font->name, len ); error = _bdf_list_split( &list, (char *)"-", name, len ); if ( error ) goto Fail; if ( list.used == 15 ) { switch ( list.field[11][0] ) { case 'C': case 'c': font->spacing = BDF_CHARCELL; break; case 'M': case 'm': font->spacing = BDF_MONOWIDTH; break; case 'P': case 'p': font->spacing = BDF_PROPORTIONAL; break; } } Fail: _bdf_list_done( &list ); Exit: return error; }
CWE-119
6,517
14,911
123681094405704793593745990785299721141
null
null
null
savannah
7f2e4f4f553f6836be7683f66226afac3fa979b8
0
bdf_free_font( bdf_font_t* font ) { bdf_property_t* prop; unsigned long i; bdf_glyph_t* glyphs; FT_Memory memory; if ( font == 0 ) return; memory = font->memory; FT_FREE( font->name ); /* Free up the internal hash table of property names. */ if ( font->internal ) { hash_free( (hashtable *)font->internal, memory ); FT_FREE( font->internal ); } /* Free up the comment info. */ FT_FREE( font->comments ); /* Free up the properties. */ for ( i = 0; i < font->props_size; i++ ) { if ( font->props[i].format == BDF_ATOM ) FT_FREE( font->props[i].value.atom ); } FT_FREE( font->props ); /* Free up the character info. */ for ( i = 0, glyphs = font->glyphs; i < font->glyphs_used; i++, glyphs++ ) { FT_FREE( glyphs->name ); FT_FREE( glyphs->bitmap ); } for ( i = 0, glyphs = font->unencoded; i < font->unencoded_used; i++, glyphs++ ) { FT_FREE( glyphs->name ); FT_FREE( glyphs->bitmap ); } FT_FREE( font->glyphs ); FT_FREE( font->unencoded ); /* Free up the overflow storage if it was used. */ for ( i = 0, glyphs = font->overflow.glyphs; i < font->overflow.glyphs_used; i++, glyphs++ ) { FT_FREE( glyphs->name ); FT_FREE( glyphs->bitmap ); } FT_FREE( font->overflow.glyphs ); /* bdf_cleanup */ hash_free( &(font->proptbl), memory ); /* Free up the user defined properties. */ for ( prop = font->user_props, i = 0; i < font->nuser_props; i++, prop++ ) { FT_FREE( prop->name ); if ( prop->format == BDF_ATOM ) FT_FREE( prop->value.atom ); } FT_FREE( font->user_props ); /* FREE( font ); */ /* XXX Fixme */ }
CWE-119
6,519
14,912
208427650405724270201196434329750390850
null
null
null
savannah
7f2e4f4f553f6836be7683f66226afac3fa979b8
0
bdf_get_font_property( bdf_font_t* font, const char* name ) { hashnode hn; if ( font == 0 || font->props_size == 0 || name == 0 || *name == 0 ) return 0; hn = hash_lookup( name, (hashtable *)font->internal ); return hn ? ( font->props + hn->data ) : 0; }
CWE-119
6,520
14,913
47153926451912050639996124156742556955
null
null
null
savannah
7f2e4f4f553f6836be7683f66226afac3fa979b8
0
bdf_get_property( char* name, bdf_font_t* font ) { hashnode hn; size_t propid; if ( name == 0 || *name == 0 ) return 0; if ( ( hn = hash_lookup( name, &(font->proptbl) ) ) == 0 ) return 0; propid = hn->data; if ( propid >= _num_bdf_properties ) return font->user_props + ( propid - _num_bdf_properties ); return (bdf_property_t*)_bdf_properties + propid; }
CWE-119
6,521
14,914
177826559973479400069331987216279930457
null
null
null
savannah
7f2e4f4f553f6836be7683f66226afac3fa979b8
0
bdf_load_font( FT_Stream stream, FT_Memory extmemory, bdf_options_t* opts, bdf_font_t* *font ) { unsigned long lineno = 0; /* make compiler happy */ _bdf_parse_t *p = NULL; FT_Memory memory = extmemory; FT_Error error = BDF_Err_Ok; if ( FT_NEW( p ) ) goto Exit; memory = NULL; p->opts = (bdf_options_t*)( ( opts != 0 ) ? opts : &_bdf_opts ); p->minlb = 32767; p->memory = extmemory; /* only during font creation */ _bdf_list_init( &p->list, extmemory ); error = _bdf_readstream( stream, _bdf_parse_start, (void *)p, &lineno ); if ( error ) goto Fail; if ( p->font != 0 ) { /* If the font is not proportional, set the font's monowidth */ /* field to the width of the font bounding box. */ memory = p->font->memory; if ( p->font->spacing != BDF_PROPORTIONAL ) p->font->monowidth = p->font->bbx.width; /* If the number of glyphs loaded is not that of the original count, */ /* indicate the difference. */ if ( p->cnt != p->font->glyphs_used + p->font->unencoded_used ) { FT_TRACE2(( "bdf_load_font: " ACMSG15, p->cnt, p->font->glyphs_used + p->font->unencoded_used )); p->font->modified = 1; } /* Once the font has been loaded, adjust the overall font metrics if */ /* necessary. */ if ( p->opts->correct_metrics != 0 && ( p->font->glyphs_used > 0 || p->font->unencoded_used > 0 ) ) { if ( p->maxrb - p->minlb != p->font->bbx.width ) { FT_TRACE2(( "bdf_load_font: " ACMSG3, p->font->bbx.width, p->maxrb - p->minlb )); p->font->bbx.width = (unsigned short)( p->maxrb - p->minlb ); p->font->modified = 1; } if ( p->font->bbx.x_offset != p->minlb ) { FT_TRACE2(( "bdf_load_font: " ACMSG4, p->font->bbx.x_offset, p->minlb )); p->font->bbx.x_offset = p->minlb; p->font->modified = 1; } if ( p->font->bbx.ascent != p->maxas ) { FT_TRACE2(( "bdf_load_font: " ACMSG5, p->font->bbx.ascent, p->maxas )); p->font->bbx.ascent = p->maxas; p->font->modified = 1; } if ( p->font->bbx.descent != p->maxds ) { FT_TRACE2(( "bdf_load_font: " ACMSG6, p->font->bbx.descent, p->maxds )); p->font->bbx.descent = p->maxds; p->font->bbx.y_offset = (short)( -p->maxds ); p->font->modified = 1; } if ( p->maxas + p->maxds != p->font->bbx.height ) { FT_TRACE2(( "bdf_load_font: " ACMSG7, p->font->bbx.height, p->maxas + p->maxds )); p->font->bbx.height = (unsigned short)( p->maxas + p->maxds ); } if ( p->flags & _BDF_SWIDTH_ADJ ) FT_TRACE2(( "bdf_load_font: " ACMSG8 )); } } if ( p->flags & _BDF_START ) { /* The ENDFONT field was never reached or did not exist. */ if ( !( p->flags & _BDF_GLYPHS ) ) { /* Error happened while parsing header. */ FT_ERROR(( "bdf_load_font: " ERRMSG2, lineno )); error = BDF_Err_Corrupted_Font_Header; goto Exit; } else { /* Error happened when parsing glyphs. */ FT_ERROR(( "bdf_load_font: " ERRMSG3, lineno )); error = BDF_Err_Corrupted_Font_Glyphs; goto Exit; } } if ( p->font != 0 ) { /* Make sure the comments are NULL terminated if they exist. */ memory = p->font->memory; if ( p->font->comments_len > 0 ) { if ( FT_RENEW_ARRAY( p->font->comments, p->font->comments_len, p->font->comments_len + 1 ) ) goto Fail; p->font->comments[p->font->comments_len] = 0; } } else if ( error == BDF_Err_Ok ) error = BDF_Err_Invalid_File_Format; *font = p->font; Exit: if ( p ) { _bdf_list_done( &p->list ); memory = extmemory; FT_FREE( p ); } return error; Fail: bdf_free_font( p->font ); memory = extmemory; FT_FREE( p->font ); goto Exit; }
CWE-119
6,522
14,915
193496153099539395977296701204415696802
null
null
null
savannah
7f2e4f4f553f6836be7683f66226afac3fa979b8
0
by_encoding( const void* a, const void* b ) { bdf_glyph_t *c1, *c2; c1 = (bdf_glyph_t *)a; c2 = (bdf_glyph_t *)b; if ( c1->encoding < c2->encoding ) return -1; if ( c1->encoding > c2->encoding ) return 1; return 0; }
CWE-119
6,523
14,916
52403353390518962338873371870038867871
null
null
null
savannah
7f2e4f4f553f6836be7683f66226afac3fa979b8
0
hash_bucket( const char* key, hashtable* ht ) { const char* kp = key; unsigned long res = 0; hashnode* bp = ht->table, *ndp; /* Mocklisp hash function. */ while ( *kp ) res = ( res << 5 ) - res + *kp++; ndp = bp + ( res % ht->size ); while ( *ndp ) { kp = (*ndp)->key; if ( kp[0] == key[0] && ft_strcmp( kp, key ) == 0 ) break; ndp--; if ( ndp < bp ) ndp = bp + ( ht->size - 1 ); } return ndp; }
CWE-119
6,524
14,917
21205221549940089999595357956278122601
null
null
null
savannah
7f2e4f4f553f6836be7683f66226afac3fa979b8
0
hash_init( hashtable* ht, FT_Memory memory ) { int sz = INITIAL_HT_SIZE; FT_Error error = BDF_Err_Ok; ht->size = sz; ht->limit = sz / 3; ht->used = 0; if ( FT_NEW_ARRAY( ht->table, sz ) ) goto Exit; Exit: return error; }
CWE-119
6,526
14,918
185106914343496995760072988093573827675
null
null
null
savannah
7f2e4f4f553f6836be7683f66226afac3fa979b8
0
hash_insert( char* key, size_t data, hashtable* ht, FT_Memory memory ) { hashnode nn, *bp = hash_bucket( key, ht ); FT_Error error = BDF_Err_Ok; nn = *bp; if ( !nn ) { if ( FT_NEW( nn ) ) goto Exit; *bp = nn; nn->key = key; nn->data = data; if ( ht->used >= ht->limit ) { error = hash_rehash( ht, memory ); if ( error ) goto Exit; } ht->used++; } else nn->data = data; Exit: return error; }
CWE-119
6,527
14,919
255041518469385248599943874591714452284
null
null
null