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
3c66a669dfc7b3792f7af0758ea26fe8502ce70c
0
static int ssl3_check_client_certificate(SSL *s) { unsigned long alg_k; if (!s->cert || !s->cert->key->x509 || !s->cert->key->privatekey) return 0; /* If no suitable signature algorithm can't use certificate */ if (SSL_USE_SIGALGS(s) && !s->cert->key->digest) return 0; /* * If strict mode check suitability of chain before using it. This also * adjusts suite B digest if necessary. */ if (s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT && !tls1_check_chain(s, NULL, NULL, NULL, -2)) return 0; alg_k = s->s3->tmp.new_cipher->algorithm_mkey; /* See if we can use client certificate for fixed DH */ if (alg_k & (SSL_kDHr | SSL_kDHd)) { SESS_CERT *scert = s->session->sess_cert; int i = scert->peer_cert_type; EVP_PKEY *clkey = NULL, *spkey = NULL; clkey = s->cert->key->privatekey; /* If client key not DH assume it can be used */ if (EVP_PKEY_id(clkey) != EVP_PKEY_DH) return 1; if (i >= 0) spkey = X509_get_pubkey(scert->peer_pkeys[i].x509); if (spkey) { /* Compare server and client parameters */ i = EVP_PKEY_cmp_parameters(clkey, spkey); EVP_PKEY_free(spkey); if (i != 1) return 0; } s->s3->flags |= TLS1_FLAGS_SKIP_CERT_VERIFY; } return 1; }
CWE-362
3,584
13,564
67517892675477247261571987682904020385
null
null
null
openssl
3c66a669dfc7b3792f7af0758ea26fe8502ce70c
0
int ssl3_client_hello(SSL *s) { unsigned char *buf; unsigned char *p, *d; int i; unsigned long l; int al = 0; #ifndef OPENSSL_NO_COMP int j; SSL_COMP *comp; #endif buf = (unsigned char *)s->init_buf->data; if (s->state == SSL3_ST_CW_CLNT_HELLO_A) { SSL_SESSION *sess = s->session; if ((sess == NULL) || (sess->ssl_version != s->version) || #ifdef OPENSSL_NO_TLSEXT !sess->session_id_length || #else /* * In the case of EAP-FAST, we can have a pre-shared * "ticket" without a session ID. */ (!sess->session_id_length && !sess->tlsext_tick) || #endif (sess->not_resumable)) { if (!ssl_get_new_session(s, 0)) goto err; } if (s->method->version == DTLS_ANY_VERSION) { /* Determine which DTLS version to use */ int options = s->options; /* If DTLS 1.2 disabled correct the version number */ if (options & SSL_OP_NO_DTLSv1_2) { if (tls1_suiteb(s)) { SSLerr(SSL_F_SSL3_CLIENT_HELLO, SSL_R_ONLY_DTLS_1_2_ALLOWED_IN_SUITEB_MODE); goto err; } /* * Disabling all versions is silly: return an error. */ if (options & SSL_OP_NO_DTLSv1) { SSLerr(SSL_F_SSL3_CLIENT_HELLO, SSL_R_WRONG_SSL_VERSION); goto err; } /* * Update method so we don't use any DTLS 1.2 features. */ s->method = DTLSv1_client_method(); s->version = DTLS1_VERSION; } else { /* * We only support one version: update method */ if (options & SSL_OP_NO_DTLSv1) s->method = DTLSv1_2_client_method(); s->version = DTLS1_2_VERSION; } s->client_version = s->version; } /* else use the pre-loaded session */ p = s->s3->client_random; /* * for DTLS if client_random is initialized, reuse it, we are * required to use same upon reply to HelloVerify */ if (SSL_IS_DTLS(s)) { size_t idx; i = 1; for (idx = 0; idx < sizeof(s->s3->client_random); idx++) { if (p[idx]) { i = 0; break; } } } else i = 1; if (i && ssl_fill_hello_random(s, 0, p, sizeof(s->s3->client_random)) <= 0) goto err; /* Do the message type and length last */ d = p = ssl_handshake_start(s); /*- * version indicates the negotiated version: for example from * an SSLv2/v3 compatible client hello). The client_version * field is the maximum version we permit and it is also * used in RSA encrypted premaster secrets. Some servers can * choke if we initially report a higher version then * renegotiate to a lower one in the premaster secret. This * didn't happen with TLS 1.0 as most servers supported it * but it can with TLS 1.1 or later if the server only supports * 1.0. * * Possible scenario with previous logic: * 1. Client hello indicates TLS 1.2 * 2. Server hello says TLS 1.0 * 3. RSA encrypted premaster secret uses 1.2. * 4. Handhaked proceeds using TLS 1.0. * 5. Server sends hello request to renegotiate. * 6. Client hello indicates TLS v1.0 as we now * know that is maximum server supports. * 7. Server chokes on RSA encrypted premaster secret * containing version 1.0. * * For interoperability it should be OK to always use the * maximum version we support in client hello and then rely * on the checking of version to ensure the servers isn't * being inconsistent: for example initially negotiating with * TLS 1.0 and renegotiating with TLS 1.2. We do this by using * client_version in client hello and not resetting it to * the negotiated version. */ #if 0 *(p++) = s->version >> 8; *(p++) = s->version & 0xff; s->client_version = s->version; #else *(p++) = s->client_version >> 8; *(p++) = s->client_version & 0xff; #endif /* Random stuff */ memcpy(p, s->s3->client_random, SSL3_RANDOM_SIZE); p += SSL3_RANDOM_SIZE; /* Session ID */ if (s->new_session) i = 0; else i = s->session->session_id_length; *(p++) = i; if (i != 0) { if (i > (int)sizeof(s->session->session_id)) { SSLerr(SSL_F_SSL3_CLIENT_HELLO, ERR_R_INTERNAL_ERROR); goto err; } memcpy(p, s->session->session_id, i); p += i; } /* cookie stuff for DTLS */ if (SSL_IS_DTLS(s)) { if (s->d1->cookie_len > sizeof(s->d1->cookie)) { SSLerr(SSL_F_SSL3_CLIENT_HELLO, ERR_R_INTERNAL_ERROR); goto err; } *(p++) = s->d1->cookie_len; memcpy(p, s->d1->cookie, s->d1->cookie_len); p += s->d1->cookie_len; } /* Ciphers supported */ i = ssl_cipher_list_to_bytes(s, SSL_get_ciphers(s), &(p[2]), 0); if (i == 0) { SSLerr(SSL_F_SSL3_CLIENT_HELLO, SSL_R_NO_CIPHERS_AVAILABLE); goto err; } #ifdef OPENSSL_MAX_TLS1_2_CIPHER_LENGTH /* * Some servers hang if client hello > 256 bytes as hack workaround * chop number of supported ciphers to keep it well below this if we * use TLS v1.2 */ if (TLS1_get_version(s) >= TLS1_2_VERSION && i > OPENSSL_MAX_TLS1_2_CIPHER_LENGTH) i = OPENSSL_MAX_TLS1_2_CIPHER_LENGTH & ~1; #endif s2n(i, p); p += i; /* COMPRESSION */ #ifdef OPENSSL_NO_COMP *(p++) = 1; #else if ((s->options & SSL_OP_NO_COMPRESSION) || !s->ctx->comp_methods) j = 0; else j = sk_SSL_COMP_num(s->ctx->comp_methods); *(p++) = 1 + j; for (i = 0; i < j; i++) { comp = sk_SSL_COMP_value(s->ctx->comp_methods, i); *(p++) = comp->id; } #endif *(p++) = 0; /* Add the NULL method */ #ifndef OPENSSL_NO_TLSEXT /* TLS extensions */ if (ssl_prepare_clienthello_tlsext(s) <= 0) { SSLerr(SSL_F_SSL3_CLIENT_HELLO, SSL_R_CLIENTHELLO_TLSEXT); goto err; } if ((p = ssl_add_clienthello_tlsext(s, p, buf + SSL3_RT_MAX_PLAIN_LENGTH, &al)) == NULL) { ssl3_send_alert(s, SSL3_AL_FATAL, al); SSLerr(SSL_F_SSL3_CLIENT_HELLO, ERR_R_INTERNAL_ERROR); goto err; } #endif l = p - d; ssl_set_handshake_header(s, SSL3_MT_CLIENT_HELLO, l); s->state = SSL3_ST_CW_CLNT_HELLO_B; } /* SSL3_ST_CW_CLNT_HELLO_B */ return ssl_do_write(s); err: s->state = SSL_ST_ERR; return (-1); }
CWE-362
3,586
13,565
214405844002430219686499457991821922724
null
null
null
openssl
3c66a669dfc7b3792f7af0758ea26fe8502ce70c
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); s->state = SSL_ST_ERR; return (-1); }
CWE-362
3,588
13,566
211495942264239990477278977490964139674
null
null
null
openssl
3c66a669dfc7b3792f7af0758ea26fe8502ce70c
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); 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 0 { FILE *out; out = fopen("/tmp/vsign.der", "w"); fwrite(p, 1, llen, out); fclose(out); } #endif 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) { if ((s->options & SSL_OP_NETSCAPE_CA_DN_BUG)) goto cont; /* netscape bugs */ 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) { /* If netscape tolerance is on, ignore errors */ if (s->options & SSL_OP_NETSCAPE_CA_DN_BUG) goto cont; else { 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; } if (0) { cont: ERR_clear_error(); } /* 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; goto done; err: s->state = SSL_ST_ERR; done: if (ca_sk != NULL) sk_X509_NAME_pop_free(ca_sk, X509_NAME_free); return (ret); }
CWE-362
3,589
13,567
7436119192577537332567164173831024965
null
null
null
openssl
3c66a669dfc7b3792f7af0758ea26fe8502ce70c
0
static const SSL_METHOD *ssl3_get_client_method(int ver) { if (ver == SSL3_VERSION) return (SSLv3_client_method()); else return (NULL); }
CWE-362
3,590
13,568
308285640408230875494259764004552662040
null
null
null
openssl
3c66a669dfc7b3792f7af0758ea26fe8502ce70c
0
int ssl3_accept(SSL *s) { BUF_MEM *buf; unsigned long alg_k, 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; /* init things to blank */ s->in_handshake++; if (!SSL_in_init(s) || SSL_in_before(s)) SSL_clear(s); if (s->cert == NULL) { SSLerr(SSL_F_SSL3_ACCEPT, SSL_R_NO_CERTIFICATE_SET); return (-1); } #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_ACCEPT; */ case SSL_ST_BEFORE: case SSL_ST_ACCEPT: case SSL_ST_BEFORE | SSL_ST_ACCEPT: case SSL_ST_OK | SSL_ST_ACCEPT: s->server = 1; if (cb != NULL) cb(s, SSL_CB_HANDSHAKE_START, 1); if ((s->version >> 8) != 3) { SSLerr(SSL_F_SSL3_ACCEPT, ERR_R_INTERNAL_ERROR); s->state = SSL_ST_ERR; return -1; } s->type = SSL_ST_ACCEPT; if (s->init_buf == NULL) { if ((buf = BUF_MEM_new()) == NULL) { ret = -1; s->state = SSL_ST_ERR; goto end; } if (!BUF_MEM_grow(buf, SSL3_RT_MAX_PLAIN_LENGTH)) { BUF_MEM_free(buf); ret = -1; s->state = SSL_ST_ERR; goto end; } s->init_buf = buf; } if (!ssl3_setup_buffers(s)) { ret = -1; s->state = SSL_ST_ERR; goto end; } s->init_num = 0; s->s3->flags &= ~TLS1_FLAGS_SKIP_CERT_VERIFY; s->s3->flags &= ~SSL3_FLAGS_CCS_OK; /* * Should have been reset by ssl3_get_finished, too. */ s->s3->change_cipher_spec = 0; if (s->state != SSL_ST_RENEGOTIATE) { /* * Ok, we now need to push on a buffering BIO so that the * output is sent in a way that TCP likes :-) */ if (!ssl_init_wbio_buffer(s, 1)) { ret = -1; s->state = SSL_ST_ERR; goto end; } ssl3_init_finished_mac(s); s->state = SSL3_ST_SR_CLNT_HELLO_A; s->ctx->stats.sess_accept++; } else if (!s->s3->send_connection_binding && !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION)) { /* * Server attempting to renegotiate with client that doesn't * support secure renegotiation. */ SSLerr(SSL_F_SSL3_ACCEPT, SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED); ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); ret = -1; s->state = SSL_ST_ERR; goto end; } else { /* * s->state == SSL_ST_RENEGOTIATE, we will just send a * HelloRequest */ s->ctx->stats.sess_accept_renegotiate++; s->state = SSL3_ST_SW_HELLO_REQ_A; } break; case SSL3_ST_SW_HELLO_REQ_A: case SSL3_ST_SW_HELLO_REQ_B: s->shutdown = 0; ret = ssl3_send_hello_request(s); if (ret <= 0) goto end; s->s3->tmp.next_state = SSL3_ST_SW_HELLO_REQ_C; s->state = SSL3_ST_SW_FLUSH; s->init_num = 0; ssl3_init_finished_mac(s); break; case SSL3_ST_SW_HELLO_REQ_C: s->state = SSL_ST_OK; break; case SSL3_ST_SR_CLNT_HELLO_A: case SSL3_ST_SR_CLNT_HELLO_B: case SSL3_ST_SR_CLNT_HELLO_C: s->shutdown = 0; ret = ssl3_get_client_hello(s); if (ret <= 0) goto end; #ifndef OPENSSL_NO_SRP s->state = SSL3_ST_SR_CLNT_HELLO_D; case SSL3_ST_SR_CLNT_HELLO_D: { int al; if ((ret = ssl_check_srp_ext_ClientHello(s, &al)) < 0) { /* * callback indicates firther work to be done */ s->rwstate = SSL_X509_LOOKUP; goto end; } if (ret != SSL_ERROR_NONE) { ssl3_send_alert(s, SSL3_AL_FATAL, al); /* * This is not really an error but the only means to for * a client to detect whether srp is supported. */ if (al != TLS1_AD_UNKNOWN_PSK_IDENTITY) SSLerr(SSL_F_SSL3_ACCEPT, SSL_R_CLIENTHELLO_TLSEXT); ret = SSL_TLSEXT_ERR_ALERT_FATAL; ret = -1; s->state = SSL_ST_ERR; goto end; } } #endif s->renegotiate = 2; s->state = SSL3_ST_SW_SRVR_HELLO_A; s->init_num = 0; break; case SSL3_ST_SW_SRVR_HELLO_A: case SSL3_ST_SW_SRVR_HELLO_B: ret = ssl3_send_server_hello(s); if (ret <= 0) goto end; #ifndef OPENSSL_NO_TLSEXT if (s->hit) { if (s->tlsext_ticket_expected) s->state = SSL3_ST_SW_SESSION_TICKET_A; else s->state = SSL3_ST_SW_CHANGE_A; } #else if (s->hit) s->state = SSL3_ST_SW_CHANGE_A; #endif else s->state = SSL3_ST_SW_CERT_A; s->init_num = 0; break; case SSL3_ST_SW_CERT_A: case SSL3_ST_SW_CERT_B: /* Check if it is anon DH or anon ECDH, */ /* normal PSK or KRB5 or SRP */ if (! (s->s3->tmp. new_cipher->algorithm_auth & (SSL_aNULL | SSL_aKRB5 | SSL_aSRP)) && !(s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK)) { ret = ssl3_send_server_certificate(s); if (ret <= 0) goto end; #ifndef OPENSSL_NO_TLSEXT if (s->tlsext_status_expected) s->state = SSL3_ST_SW_CERT_STATUS_A; else s->state = SSL3_ST_SW_KEY_EXCH_A; } else { skip = 1; s->state = SSL3_ST_SW_KEY_EXCH_A; } #else } else skip = 1; s->state = SSL3_ST_SW_KEY_EXCH_A; #endif s->init_num = 0; break; case SSL3_ST_SW_KEY_EXCH_A: case SSL3_ST_SW_KEY_EXCH_B: alg_k = s->s3->tmp.new_cipher->algorithm_mkey; /* * clear this, it may get reset by * send_server_key_exchange */ s->s3->tmp.use_rsa_tmp = 0; /* * only send if a DH key exchange, fortezza or RSA but we have a * sign only certificate PSK: may send PSK identity hints For * ECC ciphersuites, we send a serverKeyExchange message only if * the cipher suite is either ECDH-anon or ECDHE. In other cases, * the server certificate contains the server's public key for * key exchange. */ if (0 /* * PSK: send ServerKeyExchange if PSK identity hint if * provided */ #ifndef OPENSSL_NO_PSK || ((alg_k & SSL_kPSK) && s->ctx->psk_identity_hint) #endif #ifndef OPENSSL_NO_SRP /* SRP: send ServerKeyExchange */ || (alg_k & SSL_kSRP) #endif || (alg_k & SSL_kEDH) || (alg_k & SSL_kEECDH) || ((alg_k & SSL_kRSA) && (s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey == NULL || (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) && EVP_PKEY_size(s->cert->pkeys [SSL_PKEY_RSA_ENC].privatekey) * 8 > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher) ) ) ) ) { ret = ssl3_send_server_key_exchange(s); if (ret <= 0) goto end; } else skip = 1; s->state = SSL3_ST_SW_CERT_REQ_A; s->init_num = 0; break; case SSL3_ST_SW_CERT_REQ_A: case SSL3_ST_SW_CERT_REQ_B: if ( /* don't request cert unless asked for it: */ !(s->verify_mode & SSL_VERIFY_PEER) || /* * if SSL_VERIFY_CLIENT_ONCE is set, don't request cert * during re-negotiation: */ ((s->session->peer != NULL) && (s->verify_mode & SSL_VERIFY_CLIENT_ONCE)) || /* * never request cert in anonymous ciphersuites (see * section "Certificate request" in SSL 3 drafts and in * RFC 2246): */ ((s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL) && /* * ... except when the application insists on * verification (against the specs, but s3_clnt.c accepts * this for SSL 3) */ !(s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) || /* * never request cert in Kerberos ciphersuites */ (s->s3->tmp.new_cipher->algorithm_auth & SSL_aKRB5) || /* don't request certificate for SRP auth */ (s->s3->tmp.new_cipher->algorithm_auth & SSL_aSRP) /* * With normal PSK Certificates and Certificate Requests * are omitted */ || (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK)) { /* no cert request */ skip = 1; s->s3->tmp.cert_request = 0; s->state = SSL3_ST_SW_SRVR_DONE_A; if (s->s3->handshake_buffer) { if (!ssl3_digest_cached_records(s)) { s->state = SSL_ST_ERR; return -1; } } } else { s->s3->tmp.cert_request = 1; ret = ssl3_send_certificate_request(s); if (ret <= 0) goto end; #ifndef NETSCAPE_HANG_BUG s->state = SSL3_ST_SW_SRVR_DONE_A; #else s->state = SSL3_ST_SW_FLUSH; s->s3->tmp.next_state = SSL3_ST_SR_CERT_A; #endif s->init_num = 0; } break; case SSL3_ST_SW_SRVR_DONE_A: case SSL3_ST_SW_SRVR_DONE_B: ret = ssl3_send_server_done(s); if (ret <= 0) goto end; s->s3->tmp.next_state = SSL3_ST_SR_CERT_A; s->state = SSL3_ST_SW_FLUSH; s->init_num = 0; break; case SSL3_ST_SW_FLUSH: /* * This code originally checked to see if any data was pending * using BIO_CTRL_INFO and then flushed. This caused problems as * documented in PR#1939. The proposed fix doesn't completely * resolve this issue as buggy implementations of * BIO_CTRL_PENDING still exist. So instead we just flush * unconditionally. */ 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 SSL3_ST_SR_CERT_A: case SSL3_ST_SR_CERT_B: if (s->s3->tmp.cert_request) { ret = ssl3_get_client_certificate(s); if (ret <= 0) goto end; } s->init_num = 0; s->state = SSL3_ST_SR_KEY_EXCH_A; break; case SSL3_ST_SR_KEY_EXCH_A: case SSL3_ST_SR_KEY_EXCH_B: ret = ssl3_get_client_key_exchange(s); if (ret <= 0) goto end; if (ret == 2) { /* * For the ECDH ciphersuites when the client sends its ECDH * pub key in a certificate, the CertificateVerify message is * not sent. Also for GOST ciphersuites when the client uses * its key from the certificate for key exchange. */ #if defined(OPENSSL_NO_TLSEXT) || defined(OPENSSL_NO_NEXTPROTONEG) s->state = SSL3_ST_SR_FINISHED_A; #else if (s->s3->next_proto_neg_seen) s->state = SSL3_ST_SR_NEXT_PROTO_A; else s->state = SSL3_ST_SR_FINISHED_A; #endif s->init_num = 0; } else if (SSL_USE_SIGALGS(s)) { s->state = SSL3_ST_SR_CERT_VRFY_A; s->init_num = 0; if (!s->session->peer) break; /* * For sigalgs freeze the handshake buffer at this point and * digest cached records. */ if (!s->s3->handshake_buffer) { SSLerr(SSL_F_SSL3_ACCEPT, ERR_R_INTERNAL_ERROR); s->state = SSL_ST_ERR; return -1; } s->s3->flags |= TLS1_FLAGS_KEEP_HANDSHAKE; if (!ssl3_digest_cached_records(s)) { s->state = SSL_ST_ERR; return -1; } } else { int offset = 0; int dgst_num; s->state = SSL3_ST_SR_CERT_VRFY_A; s->init_num = 0; /* * We need to get hashes here so if there is a client cert, * it can be verified FIXME - digest processing for * CertificateVerify should be generalized. But it is next * step */ if (s->s3->handshake_buffer) { if (!ssl3_digest_cached_records(s)) { s->state = SSL_ST_ERR; return -1; } } for (dgst_num = 0; dgst_num < SSL_MAX_DIGEST; dgst_num++) if (s->s3->handshake_dgst[dgst_num]) { int dgst_size; s->method->ssl3_enc->cert_verify_mac(s, EVP_MD_CTX_type (s-> s3->handshake_dgst [dgst_num]), &(s->s3-> tmp.cert_verify_md [offset])); dgst_size = EVP_MD_CTX_size(s->s3->handshake_dgst[dgst_num]); if (dgst_size < 0) { s->state = SSL_ST_ERR; ret = -1; goto end; } offset += dgst_size; } } break; case SSL3_ST_SR_CERT_VRFY_A: case SSL3_ST_SR_CERT_VRFY_B: ret = ssl3_get_cert_verify(s); if (ret <= 0) goto end; #if defined(OPENSSL_NO_TLSEXT) || defined(OPENSSL_NO_NEXTPROTONEG) s->state = SSL3_ST_SR_FINISHED_A; #else if (s->s3->next_proto_neg_seen) s->state = SSL3_ST_SR_NEXT_PROTO_A; else s->state = SSL3_ST_SR_FINISHED_A; #endif s->init_num = 0; break; #if !defined(OPENSSL_NO_TLSEXT) && !defined(OPENSSL_NO_NEXTPROTONEG) case SSL3_ST_SR_NEXT_PROTO_A: case SSL3_ST_SR_NEXT_PROTO_B: /* * Enable CCS for NPN. Receiving a CCS clears the flag, so make * sure not to re-enable it to ban duplicates. This *should* be the * first time we have received one - but we check anyway to be * cautious. * s->s3->change_cipher_spec is set when a CCS is * processed in s3_pkt.c, and remains set until * the client's Finished message is read. */ if (!s->s3->change_cipher_spec) s->s3->flags |= SSL3_FLAGS_CCS_OK; ret = ssl3_get_next_proto(s); if (ret <= 0) goto end; s->init_num = 0; s->state = SSL3_ST_SR_FINISHED_A; break; #endif case SSL3_ST_SR_FINISHED_A: case SSL3_ST_SR_FINISHED_B: /* * Enable CCS for handshakes without NPN. In NPN the CCS flag has * already been set. Receiving a CCS clears the flag, so make * sure not to re-enable it to ban duplicates. * s->s3->change_cipher_spec is set when a CCS is * processed in s3_pkt.c, and remains set until * the client's Finished message is read. */ if (!s->s3->change_cipher_spec) s->s3->flags |= SSL3_FLAGS_CCS_OK; ret = ssl3_get_finished(s, SSL3_ST_SR_FINISHED_A, SSL3_ST_SR_FINISHED_B); if (ret <= 0) goto end; if (s->hit) s->state = SSL_ST_OK; #ifndef OPENSSL_NO_TLSEXT else if (s->tlsext_ticket_expected) s->state = SSL3_ST_SW_SESSION_TICKET_A; #endif else s->state = SSL3_ST_SW_CHANGE_A; s->init_num = 0; break; #ifndef OPENSSL_NO_TLSEXT case SSL3_ST_SW_SESSION_TICKET_A: case SSL3_ST_SW_SESSION_TICKET_B: ret = ssl3_send_newsession_ticket(s); if (ret <= 0) goto end; s->state = SSL3_ST_SW_CHANGE_A; s->init_num = 0; break; case SSL3_ST_SW_CERT_STATUS_A: case SSL3_ST_SW_CERT_STATUS_B: ret = ssl3_send_cert_status(s); if (ret <= 0) goto end; s->state = SSL3_ST_SW_KEY_EXCH_A; s->init_num = 0; break; #endif case SSL3_ST_SW_CHANGE_A: case SSL3_ST_SW_CHANGE_B: s->session->cipher = s->s3->tmp.new_cipher; if (!s->method->ssl3_enc->setup_key_block(s)) { ret = -1; s->state = SSL_ST_ERR; goto end; } ret = ssl3_send_change_cipher_spec(s, SSL3_ST_SW_CHANGE_A, SSL3_ST_SW_CHANGE_B); if (ret <= 0) goto end; s->state = SSL3_ST_SW_FINISHED_A; s->init_num = 0; if (!s->method->ssl3_enc->change_cipher_state(s, SSL3_CHANGE_CIPHER_SERVER_WRITE)) { ret = -1; s->state = SSL_ST_ERR; goto end; } break; case SSL3_ST_SW_FINISHED_A: case SSL3_ST_SW_FINISHED_B: ret = ssl3_send_finished(s, SSL3_ST_SW_FINISHED_A, SSL3_ST_SW_FINISHED_B, s->method-> ssl3_enc->server_finished_label, s->method-> ssl3_enc->server_finished_label_len); if (ret <= 0) goto end; s->state = SSL3_ST_SW_FLUSH; if (s->hit) { #if defined(OPENSSL_NO_TLSEXT) || defined(OPENSSL_NO_NEXTPROTONEG) s->s3->tmp.next_state = SSL3_ST_SR_FINISHED_A; #else if (s->s3->next_proto_neg_seen) { s->s3->tmp.next_state = SSL3_ST_SR_NEXT_PROTO_A; } else s->s3->tmp.next_state = SSL3_ST_SR_FINISHED_A; #endif } else s->s3->tmp.next_state = SSL_ST_OK; s->init_num = 0; break; case SSL_ST_OK: /* clean a few things up */ ssl3_cleanup_key_block(s); BUF_MEM_free(s->init_buf); s->init_buf = NULL; /* remove buffering on output */ ssl_free_wbio_buffer(s); s->init_num = 0; if (s->renegotiate == 2) { /* skipped if we just sent a * HelloRequest */ s->renegotiate = 0; s->new_session = 0; ssl_update_cache(s, SSL_SESS_CACHE_SERVER); s->ctx->stats.sess_accept_good++; /* s->server=1; */ s->handshake_func = ssl3_accept; if (cb != NULL) cb(s, SSL_CB_HANDSHAKE_DONE, 1); } ret = 1; goto end; /* break; */ case SSL_ST_ERR: default: SSLerr(SSL_F_SSL3_ACCEPT, SSL_R_UNKNOWN_STATE); ret = -1; goto end; /* break; */ } 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_ACCEPT_LOOP, 1); s->state = new_state; } } skip = 0; }
CWE-362
3,591
13,569
233009993265942286347789891823547368145
null
null
null
openssl
3c66a669dfc7b3792f7af0758ea26fe8502ce70c
0
int ssl3_get_cert_verify(SSL *s) { EVP_PKEY *pkey = NULL; unsigned char *p; int al, ok, ret = 0; long n; int type = 0, i, j; X509 *peer; const EVP_MD *md = NULL; EVP_MD_CTX mctx; EVP_MD_CTX_init(&mctx); /* * We should only process a CertificateVerify message if we have received * a Certificate from the client. If so then |s->session->peer| will be non * NULL. In some instances a CertificateVerify message is not required even * if the peer has sent a Certificate (e.g. such as in the case of static * DH). In that case the ClientKeyExchange processing will skip the * CertificateVerify state so we should not arrive here. */ if (s->session->peer == NULL) { ret = 1; goto end; } n = s->method->ssl_get_message(s, SSL3_ST_SR_CERT_VRFY_A, SSL3_ST_SR_CERT_VRFY_B, SSL3_MT_CERTIFICATE_VERIFY, SSL3_RT_MAX_PLAIN_LENGTH, &ok); if (!ok) return ((int)n); peer = s->session->peer; pkey = X509_get_pubkey(peer); type = X509_certificate_type(peer, pkey); if (!(type & EVP_PKT_SIGN)) { SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, SSL_R_SIGNATURE_FOR_NON_SIGNING_CERTIFICATE); al = SSL_AD_ILLEGAL_PARAMETER; goto f_err; } /* we now have a signature that we need to verify */ p = (unsigned char *)s->init_msg; /* Check for broken implementations of GOST ciphersuites */ /* * If key is GOST and n is exactly 64, it is bare signature without * length field */ if (n == 64 && (pkey->type == NID_id_GostR3410_94 || pkey->type == NID_id_GostR3410_2001)) { i = 64; } else { if (SSL_USE_SIGALGS(s)) { int rv = tls12_check_peer_sigalg(&md, s, p, pkey); if (rv == -1) { al = SSL_AD_INTERNAL_ERROR; goto f_err; } else if (rv == 0) { al = SSL_AD_DECODE_ERROR; goto f_err; } #ifdef SSL_DEBUG fprintf(stderr, "USING TLSv1.2 HASH %s\n", EVP_MD_name(md)); #endif p += 2; n -= 2; } n2s(p, i); n -= 2; if (i > n) { SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, SSL_R_LENGTH_MISMATCH); al = SSL_AD_DECODE_ERROR; goto f_err; } } j = EVP_PKEY_size(pkey); if ((i > j) || (n > j) || (n <= 0)) { SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, SSL_R_WRONG_SIGNATURE_SIZE); al = SSL_AD_DECODE_ERROR; goto f_err; } if (SSL_USE_SIGALGS(s)) { long hdatalen = 0; void *hdata; hdatalen = BIO_get_mem_data(s->s3->handshake_buffer, &hdata); if (hdatalen <= 0) { SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, ERR_R_INTERNAL_ERROR); al = SSL_AD_INTERNAL_ERROR; goto f_err; } #ifdef SSL_DEBUG fprintf(stderr, "Using TLS 1.2 with client verify alg %s\n", EVP_MD_name(md)); #endif if (!EVP_VerifyInit_ex(&mctx, md, NULL) || !EVP_VerifyUpdate(&mctx, hdata, hdatalen)) { SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, ERR_R_EVP_LIB); al = SSL_AD_INTERNAL_ERROR; goto f_err; } if (EVP_VerifyFinal(&mctx, p, i, pkey) <= 0) { al = SSL_AD_DECRYPT_ERROR; SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, SSL_R_BAD_SIGNATURE); goto f_err; } } else #ifndef OPENSSL_NO_RSA if (pkey->type == EVP_PKEY_RSA) { i = RSA_verify(NID_md5_sha1, s->s3->tmp.cert_verify_md, MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH, p, i, pkey->pkey.rsa); if (i < 0) { al = SSL_AD_DECRYPT_ERROR; SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, SSL_R_BAD_RSA_DECRYPT); goto f_err; } if (i == 0) { al = SSL_AD_DECRYPT_ERROR; SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, SSL_R_BAD_RSA_SIGNATURE); goto f_err; } } else #endif #ifndef OPENSSL_NO_DSA if (pkey->type == EVP_PKEY_DSA) { j = DSA_verify(pkey->save_type, &(s->s3->tmp.cert_verify_md[MD5_DIGEST_LENGTH]), SHA_DIGEST_LENGTH, p, i, pkey->pkey.dsa); if (j <= 0) { /* bad signature */ al = SSL_AD_DECRYPT_ERROR; SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, SSL_R_BAD_DSA_SIGNATURE); goto f_err; } } else #endif #ifndef OPENSSL_NO_ECDSA if (pkey->type == EVP_PKEY_EC) { j = ECDSA_verify(pkey->save_type, &(s->s3->tmp.cert_verify_md[MD5_DIGEST_LENGTH]), SHA_DIGEST_LENGTH, p, i, pkey->pkey.ec); if (j <= 0) { /* bad signature */ al = SSL_AD_DECRYPT_ERROR; SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, SSL_R_BAD_ECDSA_SIGNATURE); goto f_err; } } else #endif if (pkey->type == NID_id_GostR3410_94 || pkey->type == NID_id_GostR3410_2001) { unsigned char signature[64]; int idx; EVP_PKEY_CTX *pctx = EVP_PKEY_CTX_new(pkey, NULL); EVP_PKEY_verify_init(pctx); if (i != 64) { fprintf(stderr, "GOST signature length is %d", i); } for (idx = 0; idx < 64; idx++) { signature[63 - idx] = p[idx]; } j = EVP_PKEY_verify(pctx, signature, 64, s->s3->tmp.cert_verify_md, 32); EVP_PKEY_CTX_free(pctx); if (j <= 0) { al = SSL_AD_DECRYPT_ERROR; SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, SSL_R_BAD_ECDSA_SIGNATURE); goto f_err; } } else { SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, ERR_R_INTERNAL_ERROR); al = SSL_AD_UNSUPPORTED_CERTIFICATE; goto f_err; } ret = 1; if (0) { f_err: ssl3_send_alert(s, SSL3_AL_FATAL, al); s->state = SSL_ST_ERR; } end: if (s->s3->handshake_buffer) { BIO_free(s->s3->handshake_buffer); s->s3->handshake_buffer = NULL; s->s3->flags &= ~TLS1_FLAGS_KEEP_HANDSHAKE; } EVP_MD_CTX_cleanup(&mctx); EVP_PKEY_free(pkey); return (ret); }
CWE-362
3,592
13,570
285121677331773871508035905456054156548
null
null
null
openssl
3c66a669dfc7b3792f7af0758ea26fe8502ce70c
0
int ssl3_get_client_hello(SSL *s) { int i, j, ok, al = SSL_AD_INTERNAL_ERROR, ret = -1; unsigned int cookie_len; long n; unsigned long id; unsigned char *p, *d; SSL_CIPHER *c; #ifndef OPENSSL_NO_COMP unsigned char *q; SSL_COMP *comp = NULL; #endif STACK_OF(SSL_CIPHER) *ciphers = NULL; if (s->state == SSL3_ST_SR_CLNT_HELLO_C && !s->first_packet) goto retry_cert; /* * We do this so that we will respond with our native type. If we are * TLSv1 and we get SSLv3, we will respond with TLSv1, This down * switching should be handled by a different method. If we are SSLv3, we * will respond with SSLv3, even if prompted with TLSv1. */ if (s->state == SSL3_ST_SR_CLNT_HELLO_A) { s->state = SSL3_ST_SR_CLNT_HELLO_B; } s->first_packet = 1; n = s->method->ssl_get_message(s, SSL3_ST_SR_CLNT_HELLO_B, SSL3_ST_SR_CLNT_HELLO_C, SSL3_MT_CLIENT_HELLO, SSL3_RT_MAX_PLAIN_LENGTH, &ok); if (!ok) return ((int)n); s->first_packet = 0; d = p = (unsigned char *)s->init_msg; /* * 2 bytes for client version, SSL3_RANDOM_SIZE bytes for random, 1 byte * for session id length */ if (n < 2 + SSL3_RANDOM_SIZE + 1) { al = SSL_AD_DECODE_ERROR; SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_LENGTH_TOO_SHORT); goto f_err; } /* * use version from inside client hello, not from record header (may * differ: see RFC 2246, Appendix E, second paragraph) */ s->client_version = (((int)p[0]) << 8) | (int)p[1]; p += 2; if (SSL_IS_DTLS(s) ? (s->client_version > s->version && s->method->version != DTLS_ANY_VERSION) : (s->client_version < s->version)) { SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_WRONG_VERSION_NUMBER); if ((s->client_version >> 8) == SSL3_VERSION_MAJOR && !s->enc_write_ctx && !s->write_hash) { /* * similar to ssl3_get_record, send alert using remote version * number */ s->version = s->client_version; } al = SSL_AD_PROTOCOL_VERSION; goto f_err; } /* * If we require cookies and this ClientHello doesn't contain one, just * return since we do not want to allocate any memory yet. So check * cookie length... */ if (SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE) { unsigned int session_length, cookie_length; session_length = *(p + SSL3_RANDOM_SIZE); if (p + SSL3_RANDOM_SIZE + session_length + 1 >= d + n) { al = SSL_AD_DECODE_ERROR; SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_LENGTH_TOO_SHORT); goto f_err; } cookie_length = *(p + SSL3_RANDOM_SIZE + session_length + 1); if (cookie_length == 0) return 1; } /* load the client random */ memcpy(s->s3->client_random, p, SSL3_RANDOM_SIZE); p += SSL3_RANDOM_SIZE; /* get the session-id */ j = *(p++); if (p + j > d + n) { al = SSL_AD_DECODE_ERROR; SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_LENGTH_TOO_SHORT); goto f_err; } s->hit = 0; /* * Versions before 0.9.7 always allow clients to resume sessions in * renegotiation. 0.9.7 and later allow this by default, but optionally * ignore resumption requests with flag * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION (it's a new flag rather * than a change to default behavior so that applications relying on this * for security won't even compile against older library versions). * 1.0.1 and later also have a function SSL_renegotiate_abbreviated() to * request renegotiation but not a new session (s->new_session remains * unset): for servers, this essentially just means that the * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION setting will be ignored. */ if ((s->new_session && (s->options & SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION))) { if (!ssl_get_new_session(s, 1)) goto err; } else { i = ssl_get_prev_session(s, p, j, d + n); /* * Only resume if the session's version matches the negotiated * version. * RFC 5246 does not provide much useful advice on resumption * with a different protocol version. It doesn't forbid it but * the sanity of such behaviour would be questionable. * In practice, clients do not accept a version mismatch and * will abort the handshake with an error. */ if (i == 1 && s->version == s->session->ssl_version) { /* previous * session */ s->hit = 1; } else if (i == -1) goto err; else { /* i == 0 */ if (!ssl_get_new_session(s, 1)) goto err; } } p += j; if (SSL_IS_DTLS(s)) { /* cookie stuff */ if (p + 1 > d + n) { al = SSL_AD_DECODE_ERROR; SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_LENGTH_TOO_SHORT); goto f_err; } cookie_len = *(p++); if (p + cookie_len > d + n) { al = SSL_AD_DECODE_ERROR; SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_LENGTH_TOO_SHORT); goto f_err; } /* * The ClientHello may contain a cookie even if the * HelloVerify message has not been sent--make sure that it * does not cause an overflow. */ if (cookie_len > sizeof(s->d1->rcvd_cookie)) { /* too much data */ al = SSL_AD_DECODE_ERROR; SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_COOKIE_MISMATCH); goto f_err; } /* verify the cookie if appropriate option is set. */ if ((SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE) && cookie_len > 0) { memcpy(s->d1->rcvd_cookie, p, cookie_len); if (s->ctx->app_verify_cookie_cb != NULL) { if (s->ctx->app_verify_cookie_cb(s, s->d1->rcvd_cookie, cookie_len) == 0) { al = SSL_AD_HANDSHAKE_FAILURE; SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_COOKIE_MISMATCH); goto f_err; } /* else cookie verification succeeded */ } /* default verification */ else if (memcmp(s->d1->rcvd_cookie, s->d1->cookie, s->d1->cookie_len) != 0) { al = SSL_AD_HANDSHAKE_FAILURE; SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_COOKIE_MISMATCH); goto f_err; } /* Set to -2 so if successful we return 2 */ ret = -2; } p += cookie_len; if (s->method->version == DTLS_ANY_VERSION) { /* Select version to use */ if (s->client_version <= DTLS1_2_VERSION && !(s->options & SSL_OP_NO_DTLSv1_2)) { s->version = DTLS1_2_VERSION; s->method = DTLSv1_2_server_method(); } else if (tls1_suiteb(s)) { SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_ONLY_DTLS_1_2_ALLOWED_IN_SUITEB_MODE); s->version = s->client_version; al = SSL_AD_PROTOCOL_VERSION; goto f_err; } else if (s->client_version <= DTLS1_VERSION && !(s->options & SSL_OP_NO_DTLSv1)) { s->version = DTLS1_VERSION; s->method = DTLSv1_server_method(); } else { SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_WRONG_VERSION_NUMBER); s->version = s->client_version; al = SSL_AD_PROTOCOL_VERSION; goto f_err; } s->session->ssl_version = s->version; } } if (p + 2 > d + n) { al = SSL_AD_DECODE_ERROR; SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_LENGTH_TOO_SHORT); goto f_err; } n2s(p, i); if (i == 0) { al = SSL_AD_ILLEGAL_PARAMETER; SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_NO_CIPHERS_SPECIFIED); goto f_err; } /* i bytes of cipher data + 1 byte for compression length later */ if ((p + i + 1) > (d + n)) { /* not enough data */ al = SSL_AD_DECODE_ERROR; SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_LENGTH_MISMATCH); goto f_err; } if (ssl_bytes_to_cipher_list(s, p, i, &(ciphers)) == NULL) { goto err; } p += i; /* If it is a hit, check that the cipher is in the list */ if (s->hit) { j = 0; id = s->session->cipher->id; #ifdef CIPHER_DEBUG fprintf(stderr, "client sent %d ciphers\n", sk_SSL_CIPHER_num(ciphers)); #endif for (i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) { c = sk_SSL_CIPHER_value(ciphers, i); #ifdef CIPHER_DEBUG fprintf(stderr, "client [%2d of %2d]:%s\n", i, sk_SSL_CIPHER_num(ciphers), SSL_CIPHER_get_name(c)); #endif if (c->id == id) { j = 1; break; } } /* * Disabled because it can be used in a ciphersuite downgrade attack: * CVE-2010-4180. */ #if 0 if (j == 0 && (s->options & SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG) && (sk_SSL_CIPHER_num(ciphers) == 1)) { /* * Special case as client bug workaround: the previously used * cipher may not be in the current list, the client instead * might be trying to continue using a cipher that before wasn't * chosen due to server preferences. We'll have to reject the * connection if the cipher is not enabled, though. */ c = sk_SSL_CIPHER_value(ciphers, 0); if (sk_SSL_CIPHER_find(SSL_get_ciphers(s), c) >= 0) { s->session->cipher = c; j = 1; } } #endif if (j == 0) { /* * we need to have the cipher in the cipher list if we are asked * to reuse it */ al = SSL_AD_ILLEGAL_PARAMETER; SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_REQUIRED_CIPHER_MISSING); goto f_err; } } /* compression */ i = *(p++); if ((p + i) > (d + n)) { /* not enough data */ al = SSL_AD_DECODE_ERROR; SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_LENGTH_MISMATCH); goto f_err; } #ifndef OPENSSL_NO_COMP q = p; #endif for (j = 0; j < i; j++) { if (p[j] == 0) break; } p += i; if (j >= i) { /* no compress */ al = SSL_AD_DECODE_ERROR; SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_NO_COMPRESSION_SPECIFIED); goto f_err; } #ifndef OPENSSL_NO_TLSEXT /* TLS extensions */ if (s->version >= SSL3_VERSION) { if (!ssl_parse_clienthello_tlsext(s, &p, d, n)) { SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_PARSE_TLSEXT); goto err; } } /* * Check if we want to use external pre-shared secret for this handshake * for not reused session only. We need to generate server_random before * calling tls_session_secret_cb in order to allow SessionTicket * processing to use it in key derivation. */ { unsigned char *pos; pos = s->s3->server_random; if (ssl_fill_hello_random(s, 1, pos, SSL3_RANDOM_SIZE) <= 0) { goto f_err; } } if (!s->hit && 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, ciphers, &pref_cipher, s->tls_session_secret_cb_arg)) { s->hit = 1; s->session->ciphers = ciphers; s->session->verify_result = X509_V_OK; ciphers = NULL; /* check if some cipher was preferred by call back */ pref_cipher = pref_cipher ? pref_cipher : ssl3_choose_cipher(s, s-> session->ciphers, SSL_get_ciphers (s)); if (pref_cipher == NULL) { al = SSL_AD_HANDSHAKE_FAILURE; SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_NO_SHARED_CIPHER); goto f_err; } s->session->cipher = pref_cipher; if (s->cipher_list) sk_SSL_CIPHER_free(s->cipher_list); if (s->cipher_list_by_id) sk_SSL_CIPHER_free(s->cipher_list_by_id); s->cipher_list = sk_SSL_CIPHER_dup(s->session->ciphers); s->cipher_list_by_id = sk_SSL_CIPHER_dup(s->session->ciphers); } } #endif /* * Worst case, we will use the NULL compression, but if we have other * options, we will now look for them. We have i-1 compression * algorithms from the client, starting at q. */ s->s3->tmp.new_compression = NULL; #ifndef OPENSSL_NO_COMP /* This only happens if we have a cache hit */ if (s->session->compress_meth != 0) { int m, comp_id = s->session->compress_meth; /* Perform sanity checks on resumed compression algorithm */ /* Can't disable compression */ if (s->options & SSL_OP_NO_COMPRESSION) { SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_INCONSISTENT_COMPRESSION); goto f_err; } /* Look for resumed compression method */ for (m = 0; m < sk_SSL_COMP_num(s->ctx->comp_methods); m++) { comp = sk_SSL_COMP_value(s->ctx->comp_methods, m); if (comp_id == comp->id) { s->s3->tmp.new_compression = comp; break; } } if (s->s3->tmp.new_compression == NULL) { SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_INVALID_COMPRESSION_ALGORITHM); goto f_err; } /* Look for resumed method in compression list */ for (m = 0; m < i; m++) { if (q[m] == comp_id) break; } if (m >= i) { al = SSL_AD_ILLEGAL_PARAMETER; SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_REQUIRED_COMPRESSSION_ALGORITHM_MISSING); goto f_err; } } else if (s->hit) comp = NULL; else if (!(s->options & SSL_OP_NO_COMPRESSION) && s->ctx->comp_methods) { /* See if we have a match */ int m, nn, o, v, done = 0; nn = sk_SSL_COMP_num(s->ctx->comp_methods); for (m = 0; m < nn; m++) { comp = sk_SSL_COMP_value(s->ctx->comp_methods, m); v = comp->id; for (o = 0; o < i; o++) { if (v == q[o]) { done = 1; break; } } if (done) break; } if (done) s->s3->tmp.new_compression = comp; else comp = NULL; } #else /* * 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_CLIENT_HELLO, SSL_R_INCONSISTENT_COMPRESSION); goto f_err; } #endif /* * Given s->session->ciphers and SSL_get_ciphers, we must pick a cipher */ if (!s->hit) { #ifdef OPENSSL_NO_COMP s->session->compress_meth = 0; #else s->session->compress_meth = (comp == NULL) ? 0 : comp->id; #endif if (s->session->ciphers != NULL) sk_SSL_CIPHER_free(s->session->ciphers); s->session->ciphers = ciphers; if (ciphers == NULL) { al = SSL_AD_INTERNAL_ERROR; SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, ERR_R_INTERNAL_ERROR); goto f_err; } ciphers = NULL; if (!tls1_set_server_sigalgs(s)) { SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_CLIENTHELLO_TLSEXT); goto err; } /* Let cert callback update server certificates if required */ retry_cert: if (s->cert->cert_cb) { int rv = s->cert->cert_cb(s, s->cert->cert_cb_arg); if (rv == 0) { al = SSL_AD_INTERNAL_ERROR; SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_CERT_CB_ERROR); goto f_err; } if (rv < 0) { s->rwstate = SSL_X509_LOOKUP; return -1; } s->rwstate = SSL_NOTHING; } c = ssl3_choose_cipher(s, s->session->ciphers, SSL_get_ciphers(s)); if (c == NULL) { al = SSL_AD_HANDSHAKE_FAILURE; SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_NO_SHARED_CIPHER); goto f_err; } s->s3->tmp.new_cipher = c; } else { /* Session-id reuse */ #ifdef REUSE_CIPHER_BUG STACK_OF(SSL_CIPHER) *sk; SSL_CIPHER *nc = NULL; SSL_CIPHER *ec = NULL; if (s->options & SSL_OP_NETSCAPE_DEMO_CIPHER_CHANGE_BUG) { sk = s->session->ciphers; for (i = 0; i < sk_SSL_CIPHER_num(sk); i++) { c = sk_SSL_CIPHER_value(sk, i); if (c->algorithm_enc & SSL_eNULL) nc = c; if (SSL_C_IS_EXPORT(c)) ec = c; } if (nc != NULL) s->s3->tmp.new_cipher = nc; else if (ec != NULL) s->s3->tmp.new_cipher = ec; else s->s3->tmp.new_cipher = s->session->cipher; } else #endif s->s3->tmp.new_cipher = s->session->cipher; } if (!SSL_USE_SIGALGS(s) || !(s->verify_mode & SSL_VERIFY_PEER)) { if (!ssl3_digest_cached_records(s)) goto f_err; } /*- * we now have the following setup. * client_random * cipher_list - our prefered list of ciphers * ciphers - the clients prefered list of ciphers * compression - basically ignored right now * ssl version is set - sslv3 * s->session - The ssl session has been setup. * s->hit - session reuse flag * s->tmp.new_cipher - the new cipher to use. */ /* Handles TLS extensions that we couldn't check earlier */ if (s->version >= SSL3_VERSION) { if (ssl_check_clienthello_tlsext_late(s) <= 0) { SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_CLIENTHELLO_TLSEXT); goto err; } } if (ret < 0) ret = -ret; if (0) { f_err: ssl3_send_alert(s, SSL3_AL_FATAL, al); err: s->state = SSL_ST_ERR; } if (ciphers != NULL) sk_SSL_CIPHER_free(ciphers); return ret < 0 ? -1 : ret; }
CWE-362
3,594
13,571
283743752899700562353727482584586531151
null
null
null
openssl
3c66a669dfc7b3792f7af0758ea26fe8502ce70c
0
static const SSL_METHOD *ssl3_get_server_method(int ver) { if (ver == SSL3_VERSION) return (SSLv3_server_method()); else return (NULL); }
CWE-362
3,595
13,572
147532611868300071535352559296999094547
null
null
null
openssl
3c66a669dfc7b3792f7af0758ea26fe8502ce70c
0
int ssl3_send_certificate_request(SSL *s) { unsigned char *p, *d; int i, j, nl, off, n; STACK_OF(X509_NAME) *sk = NULL; X509_NAME *name; BUF_MEM *buf; if (s->state == SSL3_ST_SW_CERT_REQ_A) { buf = s->init_buf; d = p = ssl_handshake_start(s); /* get the list of acceptable cert types */ p++; n = ssl3_get_req_cert_type(s, p); d[0] = n; p += n; n++; if (SSL_USE_SIGALGS(s)) { const unsigned char *psigs; nl = tls12_get_psigalgs(s, &psigs); s2n(nl, p); memcpy(p, psigs, nl); p += nl; n += nl + 2; } off = n; p += 2; n += 2; sk = SSL_get_client_CA_list(s); nl = 0; if (sk != NULL) { for (i = 0; i < sk_X509_NAME_num(sk); i++) { name = sk_X509_NAME_value(sk, i); j = i2d_X509_NAME(name, NULL); if (!BUF_MEM_grow_clean (buf, SSL_HM_HEADER_LENGTH(s) + n + j + 2)) { SSLerr(SSL_F_SSL3_SEND_CERTIFICATE_REQUEST, ERR_R_BUF_LIB); goto err; } p = ssl_handshake_start(s) + n; if (!(s->options & SSL_OP_NETSCAPE_CA_DN_BUG)) { s2n(j, p); i2d_X509_NAME(name, &p); n += 2 + j; nl += 2 + j; } else { d = p; i2d_X509_NAME(name, &p); j -= 2; s2n(j, d); j += 2; n += j; nl += j; } } } /* else no CA names */ p = ssl_handshake_start(s) + off; s2n(nl, p); ssl_set_handshake_header(s, SSL3_MT_CERTIFICATE_REQUEST, n); #ifdef NETSCAPE_HANG_BUG if (!SSL_IS_DTLS(s)) { if (!BUF_MEM_grow_clean(buf, s->init_num + 4)) { SSLerr(SSL_F_SSL3_SEND_CERTIFICATE_REQUEST, ERR_R_BUF_LIB); goto err; } p = (unsigned char *)s->init_buf->data + s->init_num; /* do the header */ *(p++) = SSL3_MT_SERVER_DONE; *(p++) = 0; *(p++) = 0; *(p++) = 0; s->init_num += 4; } #endif s->state = SSL3_ST_SW_CERT_REQ_B; } /* SSL3_ST_SW_CERT_REQ_B */ return ssl_do_write(s); err: s->state = SSL_ST_ERR; return (-1); }
CWE-362
3,597
13,573
2941111591710346772966899558653917250
null
null
null
openssl
3c66a669dfc7b3792f7af0758ea26fe8502ce70c
0
int ssl3_send_newsession_ticket(SSL *s) { unsigned char *senc = NULL; EVP_CIPHER_CTX ctx; HMAC_CTX hctx; if (s->state == SSL3_ST_SW_SESSION_TICKET_A) { unsigned char *p, *macstart; const unsigned char *const_p; int len, slen_full, slen; SSL_SESSION *sess; unsigned int hlen; SSL_CTX *tctx = s->initial_ctx; unsigned char iv[EVP_MAX_IV_LENGTH]; unsigned char key_name[16]; /* get session encoding length */ slen_full = i2d_SSL_SESSION(s->session, NULL); /* * Some length values are 16 bits, so forget it if session is too * long */ if (slen_full == 0 || slen_full > 0xFF00) { s->state = SSL_ST_ERR; return -1; } senc = OPENSSL_malloc(slen_full); if (!senc) { s->state = SSL_ST_ERR; return -1; } EVP_CIPHER_CTX_init(&ctx); HMAC_CTX_init(&hctx); p = senc; if (!i2d_SSL_SESSION(s->session, &p)) goto err; /* * create a fresh copy (not shared with other threads) to clean up */ const_p = senc; sess = d2i_SSL_SESSION(NULL, &const_p, slen_full); if (sess == NULL) goto err; sess->session_id_length = 0; /* ID is irrelevant for the ticket */ slen = i2d_SSL_SESSION(sess, NULL); if (slen == 0 || slen > slen_full) { /* shouldn't ever happen */ SSL_SESSION_free(sess); goto err; } p = senc; if (!i2d_SSL_SESSION(sess, &p)) { SSL_SESSION_free(sess); goto err; } SSL_SESSION_free(sess); /*- * Grow buffer if need be: the length calculation is as * follows handshake_header_length + * 4 (ticket lifetime hint) + 2 (ticket length) + * 16 (key name) + max_iv_len (iv length) + * session_length + max_enc_block_size (max encrypted session * length) + max_md_size (HMAC). */ if (!BUF_MEM_grow(s->init_buf, SSL_HM_HEADER_LENGTH(s) + 22 + EVP_MAX_IV_LENGTH + EVP_MAX_BLOCK_LENGTH + EVP_MAX_MD_SIZE + slen)) goto err; p = ssl_handshake_start(s); /* * Initialize HMAC and cipher contexts. If callback present it does * all the work otherwise use generated values from parent ctx. */ if (tctx->tlsext_ticket_key_cb) { if (tctx->tlsext_ticket_key_cb(s, key_name, iv, &ctx, &hctx, 1) < 0) goto err; } else { if (RAND_bytes(iv, 16) <= 0) goto err; if (!EVP_EncryptInit_ex(&ctx, EVP_aes_128_cbc(), NULL, tctx->tlsext_tick_aes_key, iv)) goto err; if (!HMAC_Init_ex(&hctx, tctx->tlsext_tick_hmac_key, 16, tlsext_tick_md(), NULL)) goto err; memcpy(key_name, tctx->tlsext_tick_key_name, 16); } /* * Ticket lifetime hint (advisory only): We leave this unspecified * for resumed session (for simplicity), and guess that tickets for * new sessions will live as long as their sessions. */ l2n(s->hit ? 0 : s->session->timeout, p); /* Skip ticket length for now */ p += 2; /* Output key name */ macstart = p; memcpy(p, key_name, 16); p += 16; /* output IV */ memcpy(p, iv, EVP_CIPHER_CTX_iv_length(&ctx)); p += EVP_CIPHER_CTX_iv_length(&ctx); /* Encrypt session data */ if (!EVP_EncryptUpdate(&ctx, p, &len, senc, slen)) goto err; p += len; if (!EVP_EncryptFinal(&ctx, p, &len)) goto err; p += len; if (!HMAC_Update(&hctx, macstart, p - macstart)) goto err; if (!HMAC_Final(&hctx, p, &hlen)) goto err; EVP_CIPHER_CTX_cleanup(&ctx); HMAC_CTX_cleanup(&hctx); p += hlen; /* Now write out lengths: p points to end of data written */ /* Total length */ len = p - ssl_handshake_start(s); /* Skip ticket lifetime hint */ p = ssl_handshake_start(s) + 4; s2n(len - 6, p); ssl_set_handshake_header(s, SSL3_MT_NEWSESSION_TICKET, len); s->state = SSL3_ST_SW_SESSION_TICKET_B; OPENSSL_free(senc); } /* SSL3_ST_SW_SESSION_TICKET_B */ return ssl_do_write(s); err: if (senc) OPENSSL_free(senc); EVP_CIPHER_CTX_cleanup(&ctx); HMAC_CTX_cleanup(&hctx); s->state = SSL_ST_ERR; return -1; }
CWE-362
3,599
13,574
10470158153408619406808335427009877163
null
null
null
openssl
3c66a669dfc7b3792f7af0758ea26fe8502ce70c
0
int ssl3_send_server_certificate(SSL *s) { CERT_PKEY *cpk; if (s->state == SSL3_ST_SW_CERT_A) { cpk = ssl_get_server_send_pkey(s); if (cpk == NULL) { /* VRS: allow null cert if auth == KRB5 */ if ((s->s3->tmp.new_cipher->algorithm_auth != SSL_aKRB5) || (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kKRB5)) { SSLerr(SSL_F_SSL3_SEND_SERVER_CERTIFICATE, ERR_R_INTERNAL_ERROR); s->state = SSL_ST_ERR; return (0); } } if (!ssl3_output_cert_chain(s, cpk)) { SSLerr(SSL_F_SSL3_SEND_SERVER_CERTIFICATE, ERR_R_INTERNAL_ERROR); s->state = SSL_ST_ERR; return (0); } s->state = SSL3_ST_SW_CERT_B; } /* SSL3_ST_SW_CERT_B */ return ssl_do_write(s); }
CWE-362
3,600
13,575
215102114165653636988517299715558323713
null
null
null
openssl
3c66a669dfc7b3792f7af0758ea26fe8502ce70c
0
int ssl3_send_server_done(SSL *s) { if (s->state == SSL3_ST_SW_SRVR_DONE_A) { ssl_set_handshake_header(s, SSL3_MT_SERVER_DONE, 0); s->state = SSL3_ST_SW_SRVR_DONE_B; } /* SSL3_ST_SW_SRVR_DONE_B */ return ssl_do_write(s); }
CWE-362
3,601
13,576
218418145374555088514099714777611237444
null
null
null
openssl
3c66a669dfc7b3792f7af0758ea26fe8502ce70c
0
int ssl3_send_server_hello(SSL *s) { unsigned char *buf; unsigned char *p, *d; int i, sl; int al = 0; unsigned long l; if (s->state == SSL3_ST_SW_SRVR_HELLO_A) { buf = (unsigned char *)s->init_buf->data; #ifdef OPENSSL_NO_TLSEXT p = s->s3->server_random; if (ssl_fill_hello_random(s, 1, p, SSL3_RANDOM_SIZE) <= 0) { s->state = SSL_ST_ERR; return -1; } #endif /* Do the message type and length last */ d = p = ssl_handshake_start(s); *(p++) = s->version >> 8; *(p++) = s->version & 0xff; /* Random stuff */ memcpy(p, s->s3->server_random, SSL3_RANDOM_SIZE); p += SSL3_RANDOM_SIZE; /*- * There are several cases for the session ID to send * back in the server hello: * - For session reuse from the session cache, * we send back the old session ID. * - If stateless session reuse (using a session ticket) * is successful, we send back the client's "session ID" * (which doesn't actually identify the session). * - If it is a new session, we send back the new * session ID. * - However, if we want the new session to be single-use, * we send back a 0-length session ID. * s->hit is non-zero in either case of session reuse, * so the following won't overwrite an ID that we're supposed * to send back. */ if (!(s->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER) && !s->hit) s->session->session_id_length = 0; sl = s->session->session_id_length; if (sl > (int)sizeof(s->session->session_id)) { SSLerr(SSL_F_SSL3_SEND_SERVER_HELLO, ERR_R_INTERNAL_ERROR); s->state = SSL_ST_ERR; return -1; } *(p++) = sl; memcpy(p, s->session->session_id, sl); p += sl; /* put the cipher */ i = ssl3_put_cipher_by_char(s->s3->tmp.new_cipher, p); p += i; /* put the compression method */ #ifdef OPENSSL_NO_COMP *(p++) = 0; #else if (s->s3->tmp.new_compression == NULL) *(p++) = 0; else *(p++) = s->s3->tmp.new_compression->id; #endif #ifndef OPENSSL_NO_TLSEXT if (ssl_prepare_serverhello_tlsext(s) <= 0) { SSLerr(SSL_F_SSL3_SEND_SERVER_HELLO, SSL_R_SERVERHELLO_TLSEXT); s->state = SSL_ST_ERR; return -1; } if ((p = ssl_add_serverhello_tlsext(s, p, buf + SSL3_RT_MAX_PLAIN_LENGTH, &al)) == NULL) { ssl3_send_alert(s, SSL3_AL_FATAL, al); SSLerr(SSL_F_SSL3_SEND_SERVER_HELLO, ERR_R_INTERNAL_ERROR); s->state = SSL_ST_ERR; return -1; } #endif /* do the header */ l = (p - d); ssl_set_handshake_header(s, SSL3_MT_SERVER_HELLO, l); s->state = SSL3_ST_SW_SRVR_HELLO_B; } /* SSL3_ST_SW_SRVR_HELLO_B */ return ssl_do_write(s); }
CWE-362
3,602
13,577
103066719295706073746079984543122460830
null
null
null
openssl
3c66a669dfc7b3792f7af0758ea26fe8502ce70c
0
int ssl3_send_server_key_exchange(SSL *s) { #ifndef OPENSSL_NO_RSA unsigned char *q; int j, num; RSA *rsa; unsigned char md_buf[MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH]; unsigned int u; #endif #ifndef OPENSSL_NO_DH DH *dh = NULL, *dhp; #endif #ifndef OPENSSL_NO_ECDH EC_KEY *ecdh = NULL, *ecdhp; unsigned char *encodedPoint = NULL; int encodedlen = 0; int curve_id = 0; BN_CTX *bn_ctx = NULL; #endif EVP_PKEY *pkey; const EVP_MD *md = NULL; unsigned char *p, *d; int al, i; unsigned long type; int n; CERT *cert; BIGNUM *r[4]; int nr[4], kn; BUF_MEM *buf; EVP_MD_CTX md_ctx; EVP_MD_CTX_init(&md_ctx); if (s->state == SSL3_ST_SW_KEY_EXCH_A) { type = s->s3->tmp.new_cipher->algorithm_mkey; cert = s->cert; buf = s->init_buf; r[0] = r[1] = r[2] = r[3] = NULL; n = 0; #ifndef OPENSSL_NO_RSA if (type & SSL_kRSA) { rsa = cert->rsa_tmp; if ((rsa == NULL) && (s->cert->rsa_tmp_cb != NULL)) { rsa = s->cert->rsa_tmp_cb(s, SSL_C_IS_EXPORT(s->s3-> tmp.new_cipher), SSL_C_EXPORT_PKEYLENGTH(s->s3-> tmp.new_cipher)); if (rsa == NULL) { al = SSL_AD_HANDSHAKE_FAILURE; SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, SSL_R_ERROR_GENERATING_TMP_RSA_KEY); goto f_err; } RSA_up_ref(rsa); cert->rsa_tmp = rsa; } if (rsa == NULL) { al = SSL_AD_HANDSHAKE_FAILURE; SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, SSL_R_MISSING_TMP_RSA_KEY); goto f_err; } r[0] = rsa->n; r[1] = rsa->e; s->s3->tmp.use_rsa_tmp = 1; } else #endif #ifndef OPENSSL_NO_DH if (type & SSL_kEDH) { dhp = cert->dh_tmp; if ((dhp == NULL) && (s->cert->dh_tmp_cb != NULL)) dhp = s->cert->dh_tmp_cb(s, SSL_C_IS_EXPORT(s->s3-> tmp.new_cipher), SSL_C_EXPORT_PKEYLENGTH(s->s3-> tmp.new_cipher)); if (dhp == NULL) { al = SSL_AD_HANDSHAKE_FAILURE; SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, SSL_R_MISSING_TMP_DH_KEY); goto f_err; } if (s->s3->tmp.dh != NULL) { SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR); goto err; } if ((dh = DHparams_dup(dhp)) == NULL) { SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_R_DH_LIB); goto err; } s->s3->tmp.dh = dh; if ((dhp->pub_key == NULL || dhp->priv_key == NULL || (s->options & SSL_OP_SINGLE_DH_USE))) { if (!DH_generate_key(dh)) { SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_R_DH_LIB); goto err; } } else { dh->pub_key = BN_dup(dhp->pub_key); dh->priv_key = BN_dup(dhp->priv_key); if ((dh->pub_key == NULL) || (dh->priv_key == NULL)) { SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_R_DH_LIB); goto err; } } r[0] = dh->p; r[1] = dh->g; r[2] = dh->pub_key; } else #endif #ifndef OPENSSL_NO_ECDH if (type & SSL_kEECDH) { const EC_GROUP *group; ecdhp = cert->ecdh_tmp; if (s->cert->ecdh_tmp_auto) { /* Get NID of appropriate shared curve */ int nid = tls1_shared_curve(s, -2); if (nid != NID_undef) ecdhp = EC_KEY_new_by_curve_name(nid); } else if ((ecdhp == NULL) && s->cert->ecdh_tmp_cb) { ecdhp = s->cert->ecdh_tmp_cb(s, SSL_C_IS_EXPORT(s->s3-> tmp.new_cipher), SSL_C_EXPORT_PKEYLENGTH(s-> s3->tmp.new_cipher)); } if (ecdhp == NULL) { al = SSL_AD_HANDSHAKE_FAILURE; SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, SSL_R_MISSING_TMP_ECDH_KEY); goto f_err; } if (s->s3->tmp.ecdh != NULL) { SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR); goto err; } /* Duplicate the ECDH structure. */ if (ecdhp == NULL) { SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_R_ECDH_LIB); goto err; } if (s->cert->ecdh_tmp_auto) ecdh = ecdhp; else if ((ecdh = EC_KEY_dup(ecdhp)) == NULL) { SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_R_ECDH_LIB); goto err; } s->s3->tmp.ecdh = ecdh; if ((EC_KEY_get0_public_key(ecdh) == NULL) || (EC_KEY_get0_private_key(ecdh) == NULL) || (s->options & SSL_OP_SINGLE_ECDH_USE)) { if (!EC_KEY_generate_key(ecdh)) { SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_R_ECDH_LIB); goto err; } } if (((group = EC_KEY_get0_group(ecdh)) == NULL) || (EC_KEY_get0_public_key(ecdh) == NULL) || (EC_KEY_get0_private_key(ecdh) == NULL)) { SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_R_ECDH_LIB); goto err; } if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) && (EC_GROUP_get_degree(group) > 163)) { SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, SSL_R_ECGROUP_TOO_LARGE_FOR_CIPHER); goto err; } /* * XXX: For now, we only support ephemeral ECDH keys over named * (not generic) curves. For supported named curves, curve_id is * non-zero. */ if ((curve_id = tls1_ec_nid2curve_id(EC_GROUP_get_curve_name(group))) == 0) { SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, SSL_R_UNSUPPORTED_ELLIPTIC_CURVE); goto err; } /* * Encode the public key. First check the size of encoding and * allocate memory accordingly. */ encodedlen = EC_POINT_point2oct(group, EC_KEY_get0_public_key(ecdh), POINT_CONVERSION_UNCOMPRESSED, NULL, 0, NULL); encodedPoint = (unsigned char *) OPENSSL_malloc(encodedlen * sizeof(unsigned char)); bn_ctx = BN_CTX_new(); if ((encodedPoint == NULL) || (bn_ctx == NULL)) { SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE); goto err; } encodedlen = EC_POINT_point2oct(group, EC_KEY_get0_public_key(ecdh), POINT_CONVERSION_UNCOMPRESSED, encodedPoint, encodedlen, bn_ctx); if (encodedlen == 0) { SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_R_ECDH_LIB); goto err; } BN_CTX_free(bn_ctx); bn_ctx = NULL; /* * XXX: For now, we only support named (not generic) curves in * ECDH ephemeral key exchanges. In this situation, we need four * additional bytes to encode the entire ServerECDHParams * structure. */ n = 4 + encodedlen; /* * We'll generate the serverKeyExchange message explicitly so we * can set these to NULLs */ r[0] = NULL; r[1] = NULL; r[2] = NULL; r[3] = NULL; } else #endif /* !OPENSSL_NO_ECDH */ #ifndef OPENSSL_NO_PSK if (type & SSL_kPSK) { /* * reserve size for record length and PSK identity hint */ n += 2 + strlen(s->ctx->psk_identity_hint); } else #endif /* !OPENSSL_NO_PSK */ #ifndef OPENSSL_NO_SRP if (type & SSL_kSRP) { if ((s->srp_ctx.N == NULL) || (s->srp_ctx.g == NULL) || (s->srp_ctx.s == NULL) || (s->srp_ctx.B == NULL)) { SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, SSL_R_MISSING_SRP_PARAM); goto err; } r[0] = s->srp_ctx.N; r[1] = s->srp_ctx.g; r[2] = s->srp_ctx.s; r[3] = s->srp_ctx.B; } else #endif { al = SSL_AD_HANDSHAKE_FAILURE; SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE); goto f_err; } for (i = 0; i < 4 && r[i] != NULL; i++) { nr[i] = BN_num_bytes(r[i]); #ifndef OPENSSL_NO_SRP if ((i == 2) && (type & SSL_kSRP)) n += 1 + nr[i]; else #endif n += 2 + nr[i]; } if (!(s->s3->tmp.new_cipher->algorithm_auth & (SSL_aNULL | SSL_aSRP)) && !(s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK)) { if ((pkey = ssl_get_sign_pkey(s, s->s3->tmp.new_cipher, &md)) == NULL) { al = SSL_AD_DECODE_ERROR; goto f_err; } kn = EVP_PKEY_size(pkey); } else { pkey = NULL; kn = 0; } if (!BUF_MEM_grow_clean(buf, n + SSL_HM_HEADER_LENGTH(s) + kn)) { SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_LIB_BUF); goto err; } d = p = ssl_handshake_start(s); for (i = 0; i < 4 && r[i] != NULL; i++) { #ifndef OPENSSL_NO_SRP if ((i == 2) && (type & SSL_kSRP)) { *p = nr[i]; p++; } else #endif s2n(nr[i], p); BN_bn2bin(r[i], p); p += nr[i]; } #ifndef OPENSSL_NO_ECDH if (type & SSL_kEECDH) { /* * XXX: For now, we only support named (not generic) curves. In * this situation, the serverKeyExchange message has: [1 byte * CurveType], [2 byte CurveName] [1 byte length of encoded * point], followed by the actual encoded point itself */ *p = NAMED_CURVE_TYPE; p += 1; *p = 0; p += 1; *p = curve_id; p += 1; *p = encodedlen; p += 1; memcpy((unsigned char *)p, (unsigned char *)encodedPoint, encodedlen); OPENSSL_free(encodedPoint); encodedPoint = NULL; p += encodedlen; } #endif #ifndef OPENSSL_NO_PSK if (type & SSL_kPSK) { /* copy PSK identity hint */ s2n(strlen(s->ctx->psk_identity_hint), p); strncpy((char *)p, s->ctx->psk_identity_hint, strlen(s->ctx->psk_identity_hint)); p += strlen(s->ctx->psk_identity_hint); } #endif /* not anonymous */ if (pkey != NULL) { /* * n is the length of the params, they start at &(d[4]) and p * points to the space at the end. */ #ifndef OPENSSL_NO_RSA if (pkey->type == EVP_PKEY_RSA && !SSL_USE_SIGALGS(s)) { q = md_buf; j = 0; 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, d, n); EVP_DigestFinal_ex(&md_ctx, q, (unsigned int *)&i); q += i; j += i; } if (RSA_sign(NID_md5_sha1, md_buf, j, &(p[2]), &u, pkey->pkey.rsa) <= 0) { SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_LIB_RSA); goto err; } s2n(u, p); n += u + 2; } else #endif if (md) { /* send signature algorithm */ if (SSL_USE_SIGALGS(s)) { if (!tls12_get_sigandhash(p, pkey, md)) { /* Should never happen */ al = SSL_AD_INTERNAL_ERROR; SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR); goto f_err; } p += 2; } #ifdef SSL_DEBUG fprintf(stderr, "Using hash %s\n", EVP_MD_name(md)); #endif EVP_SignInit_ex(&md_ctx, md, NULL); EVP_SignUpdate(&md_ctx, &(s->s3->client_random[0]), SSL3_RANDOM_SIZE); EVP_SignUpdate(&md_ctx, &(s->s3->server_random[0]), SSL3_RANDOM_SIZE); EVP_SignUpdate(&md_ctx, d, n); if (!EVP_SignFinal(&md_ctx, &(p[2]), (unsigned int *)&i, pkey)) { SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_LIB_EVP); goto err; } s2n(i, p); n += i + 2; if (SSL_USE_SIGALGS(s)) n += 2; } else { /* Is this error check actually needed? */ al = SSL_AD_HANDSHAKE_FAILURE; SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, SSL_R_UNKNOWN_PKEY_TYPE); goto f_err; } } ssl_set_handshake_header(s, SSL3_MT_SERVER_KEY_EXCHANGE, n); } s->state = SSL3_ST_SW_KEY_EXCH_B; EVP_MD_CTX_cleanup(&md_ctx); return ssl_do_write(s); f_err: ssl3_send_alert(s, SSL3_AL_FATAL, al); err: #ifndef OPENSSL_NO_ECDH if (encodedPoint != NULL) OPENSSL_free(encodedPoint); BN_CTX_free(bn_ctx); #endif EVP_MD_CTX_cleanup(&md_ctx); s->state = SSL_ST_ERR; return (-1); }
CWE-362
3,603
13,578
81142419134413159595001644158967770584
null
null
null
openssl
3c66a669dfc7b3792f7af0758ea26fe8502ce70c
0
static int ssl_check_srp_ext_ClientHello(SSL *s, int *al) { int ret = SSL_ERROR_NONE; *al = SSL_AD_UNRECOGNIZED_NAME; if ((s->s3->tmp.new_cipher->algorithm_mkey & SSL_kSRP) && (s->srp_ctx.TLS_ext_srp_username_callback != NULL)) { if (s->srp_ctx.login == NULL) { /* * RFC 5054 says SHOULD reject, we do so if There is no srp * login name */ ret = SSL3_AL_FATAL; *al = SSL_AD_UNKNOWN_PSK_IDENTITY; } else { ret = SSL_srp_server_param_with_username(s, al); } } return ret; }
CWE-362
3,604
13,579
85839205761044078713880950284677770988
null
null
null
openssl
cc598f321fbac9c04da5766243ed55d55948637d
0
ASN1_VALUE *ASN1_item_d2i(ASN1_VALUE **pval, const unsigned char **in, long len, const ASN1_ITEM *it) { ASN1_TLC c; ASN1_VALUE *ptmpval = NULL; if (!pval) pval = &ptmpval; asn1_tlc_clear_nc(&c); if (ASN1_item_ex_d2i(pval, in, len, it, -1, 0, 0, &c) > 0) return *pval; return NULL; }
CWE-200
3,605
13,580
56924376915945159998387451278681503237
null
null
null
openssl
cc598f321fbac9c04da5766243ed55d55948637d
0
unsigned long ASN1_tag2bit(int tag) { if ((tag < 0) || (tag > 30)) return 0; return tag2bit[tag]; }
CWE-200
3,606
13,581
296483563498789150490623353318831782088
null
null
null
openssl
cc598f321fbac9c04da5766243ed55d55948637d
0
int ASN1_template_d2i(ASN1_VALUE **pval, const unsigned char **in, long len, const ASN1_TEMPLATE *tt) { ASN1_TLC c; asn1_tlc_clear_nc(&c); return asn1_template_ex_d2i(pval, in, len, tt, 0, &c); }
CWE-200
3,607
13,582
143922456380892708280229288811986247429
null
null
null
openssl
d8541d7e9e63bf5f343af24644046c8d96498c17
0
static int do_rsa_print(BIO *bp, const RSA *x, int off, int priv) { char *str; const char *s; unsigned char *m = NULL; int ret = 0, mod_len = 0; size_t buf_len = 0; update_buflen(x->n, &buf_len); update_buflen(x->e, &buf_len); if (priv) { update_buflen(x->d, &buf_len); update_buflen(x->p, &buf_len); update_buflen(x->q, &buf_len); update_buflen(x->dmp1, &buf_len); update_buflen(x->dmq1, &buf_len); update_buflen(x->iqmp, &buf_len); } m = (unsigned char *)OPENSSL_malloc(buf_len + 10); if (m == NULL) { RSAerr(RSA_F_DO_RSA_PRINT, ERR_R_MALLOC_FAILURE); goto err; } if (x->n != NULL) mod_len = BN_num_bits(x->n); if (!BIO_indent(bp, off, 128)) goto err; if (priv && x->d) { if (BIO_printf(bp, "Private-Key: (%d bit)\n", mod_len) <= 0) goto err; str = "modulus:"; s = "publicExponent:"; } else { if (BIO_printf(bp, "Public-Key: (%d bit)\n", mod_len) <= 0) goto err; str = "Modulus:"; s = "Exponent:"; } if (!ASN1_bn_print(bp, str, x->n, m, off)) goto err; if (!ASN1_bn_print(bp, s, x->e, m, off)) goto err; if (priv) { if (!ASN1_bn_print(bp, "privateExponent:", x->d, m, off)) goto err; if (!ASN1_bn_print(bp, "prime1:", x->p, m, off)) goto err; if (!ASN1_bn_print(bp, "prime2:", x->q, m, off)) goto err; if (!ASN1_bn_print(bp, "exponent1:", x->dmp1, m, off)) goto err; if (!ASN1_bn_print(bp, "exponent2:", x->dmq1, m, off)) goto err; if (!ASN1_bn_print(bp, "coefficient:", x->iqmp, m, off)) goto err; } ret = 1; err: if (m != NULL) OPENSSL_free(m); return (ret); }
3,608
13,583
37006256747963082334410106418542527605
null
null
null
openssl
d8541d7e9e63bf5f343af24644046c8d96498c17
0
static void int_rsa_free(EVP_PKEY *pkey) { RSA_free(pkey->pkey.rsa); }
3,609
13,584
77618323264868245472362376581951725318
null
null
null
openssl
d8541d7e9e63bf5f343af24644046c8d96498c17
0
static int int_rsa_size(const EVP_PKEY *pkey) { return RSA_size(pkey->pkey.rsa); }
3,610
13,585
106925079340062183861188470474703630563
null
null
null
openssl
d8541d7e9e63bf5f343af24644046c8d96498c17
0
static int old_rsa_priv_decode(EVP_PKEY *pkey, const unsigned char **pder, int derlen) { RSA *rsa; if (!(rsa = d2i_RSAPrivateKey(NULL, pder, derlen))) { RSAerr(RSA_F_OLD_RSA_PRIV_DECODE, ERR_R_RSA_LIB); return 0; } EVP_PKEY_assign_RSA(pkey, rsa); return 1; }
3,611
13,586
330641867910428912352398945568627308205
null
null
null
openssl
d8541d7e9e63bf5f343af24644046c8d96498c17
0
static int old_rsa_priv_encode(const EVP_PKEY *pkey, unsigned char **pder) { return i2d_RSAPrivateKey(pkey->pkey.rsa, pder); }
3,612
13,587
165407946617353666764072534087775047844
null
null
null
openssl
d8541d7e9e63bf5f343af24644046c8d96498c17
0
static int rsa_item_sign(EVP_MD_CTX *ctx, const ASN1_ITEM *it, void *asn, X509_ALGOR *alg1, X509_ALGOR *alg2, ASN1_BIT_STRING *sig) { int pad_mode; EVP_PKEY_CTX *pkctx = ctx->pctx; if (EVP_PKEY_CTX_get_rsa_padding(pkctx, &pad_mode) <= 0) return 0; if (pad_mode == RSA_PKCS1_PADDING) return 2; if (pad_mode == RSA_PKCS1_PSS_PADDING) { const EVP_MD *sigmd, *mgf1md; RSA_PSS_PARAMS *pss = NULL; X509_ALGOR *mgf1alg = NULL; ASN1_STRING *os1 = NULL, *os2 = NULL; EVP_PKEY *pk = EVP_PKEY_CTX_get0_pkey(pkctx); int saltlen, rv = 0; sigmd = EVP_MD_CTX_md(ctx); if (EVP_PKEY_CTX_get_rsa_mgf1_md(pkctx, &mgf1md) <= 0) goto err; if (!EVP_PKEY_CTX_get_rsa_pss_saltlen(pkctx, &saltlen)) goto err; if (saltlen == -1) saltlen = EVP_MD_size(sigmd); else if (saltlen == -2) { saltlen = EVP_PKEY_size(pk) - EVP_MD_size(sigmd) - 2; if (((EVP_PKEY_bits(pk) - 1) & 0x7) == 0) saltlen--; } pss = RSA_PSS_PARAMS_new(); if (!pss) goto err; if (saltlen != 20) { pss->saltLength = ASN1_INTEGER_new(); if (!pss->saltLength) goto err; if (!ASN1_INTEGER_set(pss->saltLength, saltlen)) goto err; } if (EVP_MD_type(sigmd) != NID_sha1) { pss->hashAlgorithm = X509_ALGOR_new(); if (!pss->hashAlgorithm) goto err; X509_ALGOR_set_md(pss->hashAlgorithm, sigmd); } if (EVP_MD_type(mgf1md) != NID_sha1) { ASN1_STRING *stmp = NULL; /* need to embed algorithm ID inside another */ mgf1alg = X509_ALGOR_new(); X509_ALGOR_set_md(mgf1alg, mgf1md); if (!ASN1_item_pack(mgf1alg, ASN1_ITEM_rptr(X509_ALGOR), &stmp)) goto err; pss->maskGenAlgorithm = X509_ALGOR_new(); if (!pss->maskGenAlgorithm) goto err; X509_ALGOR_set0(pss->maskGenAlgorithm, OBJ_nid2obj(NID_mgf1), V_ASN1_SEQUENCE, stmp); } /* Finally create string with pss parameter encoding. */ if (!ASN1_item_pack(pss, ASN1_ITEM_rptr(RSA_PSS_PARAMS), &os1)) goto err; if (alg2) { os2 = ASN1_STRING_dup(os1); if (!os2) goto err; X509_ALGOR_set0(alg2, OBJ_nid2obj(NID_rsassaPss), V_ASN1_SEQUENCE, os2); } X509_ALGOR_set0(alg1, OBJ_nid2obj(NID_rsassaPss), V_ASN1_SEQUENCE, os1); os1 = os2 = NULL; rv = 3; err: if (mgf1alg) X509_ALGOR_free(mgf1alg); if (pss) RSA_PSS_PARAMS_free(pss); if (os1) ASN1_STRING_free(os1); return rv; } return 2; }
3,614
13,588
274421554309508261956092184661304159768
null
null
null
openssl
d8541d7e9e63bf5f343af24644046c8d96498c17
0
static int rsa_item_verify(EVP_MD_CTX *ctx, const ASN1_ITEM *it, void *asn, X509_ALGOR *sigalg, ASN1_BIT_STRING *sig, EVP_PKEY *pkey) { int rv = -1; int saltlen; const EVP_MD *mgf1md = NULL, *md = NULL; RSA_PSS_PARAMS *pss; X509_ALGOR *maskHash; EVP_PKEY_CTX *pkctx; /* Sanity check: make sure it is PSS */ if (OBJ_obj2nid(sigalg->algorithm) != NID_rsassaPss) { RSAerr(RSA_F_RSA_ITEM_VERIFY, RSA_R_UNSUPPORTED_SIGNATURE_TYPE); return -1; } /* Decode PSS parameters */ pss = rsa_pss_decode(sigalg, &maskHash); if (pss == NULL) { RSAerr(RSA_F_RSA_ITEM_VERIFY, RSA_R_INVALID_PSS_PARAMETERS); goto err; } /* Check mask and lookup mask hash algorithm */ if (pss->maskGenAlgorithm) { if (OBJ_obj2nid(pss->maskGenAlgorithm->algorithm) != NID_mgf1) { RSAerr(RSA_F_RSA_ITEM_VERIFY, RSA_R_UNSUPPORTED_MASK_ALGORITHM); goto err; } if (!maskHash) { RSAerr(RSA_F_RSA_ITEM_VERIFY, RSA_R_UNSUPPORTED_MASK_PARAMETER); goto err; } mgf1md = EVP_get_digestbyobj(maskHash->algorithm); if (mgf1md == NULL) { RSAerr(RSA_F_RSA_ITEM_VERIFY, RSA_R_UNKNOWN_MASK_DIGEST); goto err; } } else mgf1md = EVP_sha1(); if (pss->hashAlgorithm) { md = EVP_get_digestbyobj(pss->hashAlgorithm->algorithm); if (md == NULL) { RSAerr(RSA_F_RSA_ITEM_VERIFY, RSA_R_UNKNOWN_PSS_DIGEST); goto err; } } else md = EVP_sha1(); if (pss->saltLength) { saltlen = ASN1_INTEGER_get(pss->saltLength); /* * Could perform more salt length sanity checks but the main RSA * routines will trap other invalid values anyway. */ if (saltlen < 0) { RSAerr(RSA_F_RSA_ITEM_VERIFY, RSA_R_INVALID_SALT_LENGTH); goto err; } } else saltlen = 20; /* * low-level routines support only trailer field 0xbc (value 1) and * PKCS#1 says we should reject any other value anyway. */ if (pss->trailerField && ASN1_INTEGER_get(pss->trailerField) != 1) { RSAerr(RSA_F_RSA_ITEM_VERIFY, RSA_R_INVALID_TRAILER); goto err; } /* We have all parameters now set up context */ if (!EVP_DigestVerifyInit(ctx, &pkctx, md, NULL, pkey)) goto err; if (EVP_PKEY_CTX_set_rsa_padding(pkctx, RSA_PKCS1_PSS_PADDING) <= 0) goto err; if (EVP_PKEY_CTX_set_rsa_pss_saltlen(pkctx, saltlen) <= 0) goto err; if (EVP_PKEY_CTX_set_rsa_mgf1_md(pkctx, mgf1md) <= 0) goto err; /* Carry on */ rv = 2; err: RSA_PSS_PARAMS_free(pss); if (maskHash) X509_ALGOR_free(maskHash); return rv; }
3,615
13,589
225198595055658593083569460354588929636
null
null
null
openssl
d8541d7e9e63bf5f343af24644046c8d96498c17
0
static int rsa_pkey_ctrl(EVP_PKEY *pkey, int op, long arg1, void *arg2) { X509_ALGOR *alg = NULL; switch (op) { case ASN1_PKEY_CTRL_PKCS7_SIGN: if (arg1 == 0) PKCS7_SIGNER_INFO_get0_algs(arg2, NULL, NULL, &alg); break; case ASN1_PKEY_CTRL_PKCS7_ENCRYPT: if (arg1 == 0) PKCS7_RECIP_INFO_get0_alg(arg2, &alg); break; #ifndef OPENSSL_NO_CMS case ASN1_PKEY_CTRL_CMS_SIGN: if (arg1 == 0) CMS_SignerInfo_get0_algs(arg2, NULL, NULL, NULL, &alg); break; case ASN1_PKEY_CTRL_CMS_ENVELOPE: if (arg1 == 0) CMS_RecipientInfo_ktri_get0_algs(arg2, NULL, NULL, &alg); break; #endif case ASN1_PKEY_CTRL_DEFAULT_MD_NID: *(int *)arg2 = NID_sha1; return 1; default: return -2; } if (alg) X509_ALGOR_set0(alg, OBJ_nid2obj(NID_rsaEncryption), V_ASN1_NULL, 0); return 1; }
3,616
13,590
312271974090736845166194803183097629731
null
null
null
openssl
d8541d7e9e63bf5f343af24644046c8d96498c17
0
static int rsa_priv_encode(PKCS8_PRIV_KEY_INFO *p8, const EVP_PKEY *pkey) { unsigned char *rk = NULL; int rklen; rklen = i2d_RSAPrivateKey(pkey->pkey.rsa, &rk); if (rklen <= 0) { RSAerr(RSA_F_RSA_PRIV_ENCODE, ERR_R_MALLOC_FAILURE); return 0; } if (!PKCS8_pkey_set0(p8, OBJ_nid2obj(NID_rsaEncryption), 0, V_ASN1_NULL, NULL, rk, rklen)) { RSAerr(RSA_F_RSA_PRIV_ENCODE, ERR_R_MALLOC_FAILURE); return 0; } return 1; }
3,618
13,591
327335193359600847017996078590354273132
null
null
null
openssl
d8541d7e9e63bf5f343af24644046c8d96498c17
0
static int rsa_priv_print(BIO *bp, const EVP_PKEY *pkey, int indent, ASN1_PCTX *ctx) { return do_rsa_print(bp, pkey->pkey.rsa, indent, 1); }
3,619
13,592
173333761183685488120264535776594456915
null
null
null
openssl
d8541d7e9e63bf5f343af24644046c8d96498c17
0
static int rsa_pub_cmp(const EVP_PKEY *a, const EVP_PKEY *b) { if (BN_cmp(b->pkey.rsa->n, a->pkey.rsa->n) != 0 || BN_cmp(b->pkey.rsa->e, a->pkey.rsa->e) != 0) return 0; return 1; }
3,620
13,593
304991824643946399674410612512798672624
null
null
null
openssl
d8541d7e9e63bf5f343af24644046c8d96498c17
0
static int rsa_pub_decode(EVP_PKEY *pkey, X509_PUBKEY *pubkey) { const unsigned char *p; int pklen; RSA *rsa = NULL; if (!X509_PUBKEY_get0_param(NULL, &p, &pklen, NULL, pubkey)) return 0; if (!(rsa = d2i_RSAPublicKey(NULL, &p, pklen))) { RSAerr(RSA_F_RSA_PUB_DECODE, ERR_R_RSA_LIB); return 0; } EVP_PKEY_assign_RSA(pkey, rsa); return 1; }
3,621
13,594
298330116769423258794647015761564461409
null
null
null
openssl
d8541d7e9e63bf5f343af24644046c8d96498c17
0
static int rsa_pub_encode(X509_PUBKEY *pk, const EVP_PKEY *pkey) { unsigned char *penc = NULL; int penclen; penclen = i2d_RSAPublicKey(pkey->pkey.rsa, &penc); if (penclen <= 0) return 0; if (X509_PUBKEY_set0_param(pk, OBJ_nid2obj(EVP_PKEY_RSA), V_ASN1_NULL, NULL, penc, penclen)) return 1; OPENSSL_free(penc); return 0; }
3,622
13,595
337825385505932061780158937130964013531
null
null
null
openssl
d8541d7e9e63bf5f343af24644046c8d96498c17
0
static int rsa_pub_print(BIO *bp, const EVP_PKEY *pkey, int indent, ASN1_PCTX *ctx) { return do_rsa_print(bp, pkey->pkey.rsa, indent, 0); }
3,623
13,596
96774142527680018540073361843768013296
null
null
null
openssl
d8541d7e9e63bf5f343af24644046c8d96498c17
0
static int rsa_sig_print(BIO *bp, const X509_ALGOR *sigalg, const ASN1_STRING *sig, int indent, ASN1_PCTX *pctx) { if (OBJ_obj2nid(sigalg->algorithm) == NID_rsassaPss) { int rv; RSA_PSS_PARAMS *pss; X509_ALGOR *maskHash; pss = rsa_pss_decode(sigalg, &maskHash); rv = rsa_pss_param_print(bp, pss, maskHash, indent); if (pss) RSA_PSS_PARAMS_free(pss); if (maskHash) X509_ALGOR_free(maskHash); if (!rv) return 0; } else if (!sig && BIO_puts(bp, "\n") <= 0) return 0; if (sig) return X509_signature_dump(bp, sig, indent); return 1; }
3,624
13,597
326674300711213300592122146738588634544
null
null
null
openssl
d8541d7e9e63bf5f343af24644046c8d96498c17
0
static void update_buflen(const BIGNUM *b, size_t *pbuflen) { size_t i; if (!b) return; if (*pbuflen < (i = (size_t)BN_num_bytes(b))) *pbuflen = i; }
3,625
13,598
302416200957628906921640420670463876679
null
null
null
openssl
c394a488942387246653833359a5c94b5832674e
0
static const EVP_MD *rsa_algor_to_md(X509_ALGOR *alg) { const EVP_MD *md; if (!alg) return EVP_sha1(); md = EVP_get_digestbyobj(alg->algorithm); if (md == NULL) RSAerr(RSA_F_RSA_ALGOR_TO_MD, RSA_R_UNKNOWN_DIGEST); return md; }
3,626
13,599
128645114571165368089309661264056967746
null
null
null
openssl
c394a488942387246653833359a5c94b5832674e
0
static int rsa_cms_decrypt(CMS_RecipientInfo *ri) { EVP_PKEY_CTX *pkctx; X509_ALGOR *cmsalg; int nid; int rv = -1; unsigned char *label = NULL; int labellen = 0; const EVP_MD *mgf1md = NULL, *md = NULL; RSA_OAEP_PARAMS *oaep; X509_ALGOR *maskHash; pkctx = CMS_RecipientInfo_get0_pkey_ctx(ri); if (!pkctx) return 0; if (!CMS_RecipientInfo_ktri_get0_algs(ri, NULL, NULL, &cmsalg)) return -1; nid = OBJ_obj2nid(cmsalg->algorithm); if (nid == NID_rsaEncryption) return 1; if (nid != NID_rsaesOaep) { RSAerr(RSA_F_RSA_CMS_DECRYPT, RSA_R_UNSUPPORTED_ENCRYPTION_TYPE); return -1; } /* Decode OAEP parameters */ oaep = rsa_oaep_decode(cmsalg, &maskHash); if (oaep == NULL) { RSAerr(RSA_F_RSA_CMS_DECRYPT, RSA_R_INVALID_OAEP_PARAMETERS); goto err; } mgf1md = rsa_mgf1_to_md(oaep->maskGenFunc, maskHash); if (!mgf1md) goto err; md = rsa_algor_to_md(oaep->hashFunc); if (!md) goto err; if (oaep->pSourceFunc) { X509_ALGOR *plab = oaep->pSourceFunc; if (OBJ_obj2nid(plab->algorithm) != NID_pSpecified) { RSAerr(RSA_F_RSA_CMS_DECRYPT, RSA_R_UNSUPPORTED_LABEL_SOURCE); goto err; } if (plab->parameter->type != V_ASN1_OCTET_STRING) { RSAerr(RSA_F_RSA_CMS_DECRYPT, RSA_R_INVALID_LABEL); goto err; } label = plab->parameter->value.octet_string->data; /* Stop label being freed when OAEP parameters are freed */ plab->parameter->value.octet_string->data = NULL; labellen = plab->parameter->value.octet_string->length; } if (EVP_PKEY_CTX_set_rsa_padding(pkctx, RSA_PKCS1_OAEP_PADDING) <= 0) goto err; if (EVP_PKEY_CTX_set_rsa_oaep_md(pkctx, md) <= 0) goto err; if (EVP_PKEY_CTX_set_rsa_mgf1_md(pkctx, mgf1md) <= 0) goto err; if (EVP_PKEY_CTX_set0_rsa_oaep_label(pkctx, label, labellen) <= 0) goto err; /* Carry on */ rv = 1; err: RSA_OAEP_PARAMS_free(oaep); if (maskHash) X509_ALGOR_free(maskHash); return rv; }
3,627
13,600
183140855938878459519572066398332532961
null
null
null
openssl
c394a488942387246653833359a5c94b5832674e
0
static int rsa_cms_encrypt(CMS_RecipientInfo *ri) { const EVP_MD *md, *mgf1md; RSA_OAEP_PARAMS *oaep = NULL; ASN1_STRING *os = NULL; X509_ALGOR *alg; EVP_PKEY_CTX *pkctx = CMS_RecipientInfo_get0_pkey_ctx(ri); int pad_mode = RSA_PKCS1_PADDING, rv = 0, labellen; unsigned char *label; CMS_RecipientInfo_ktri_get0_algs(ri, NULL, NULL, &alg); if (pkctx) { if (EVP_PKEY_CTX_get_rsa_padding(pkctx, &pad_mode) <= 0) return 0; } if (pad_mode == RSA_PKCS1_PADDING) { X509_ALGOR_set0(alg, OBJ_nid2obj(NID_rsaEncryption), V_ASN1_NULL, 0); return 1; } /* Not supported */ if (pad_mode != RSA_PKCS1_OAEP_PADDING) return 0; if (EVP_PKEY_CTX_get_rsa_oaep_md(pkctx, &md) <= 0) goto err; if (EVP_PKEY_CTX_get_rsa_mgf1_md(pkctx, &mgf1md) <= 0) goto err; labellen = EVP_PKEY_CTX_get0_rsa_oaep_label(pkctx, &label); if (labellen < 0) goto err; oaep = RSA_OAEP_PARAMS_new(); if (!oaep) goto err; if (!rsa_md_to_algor(&oaep->hashFunc, md)) goto err; if (!rsa_md_to_mgf1(&oaep->maskGenFunc, mgf1md)) goto err; if (labellen > 0) { ASN1_OCTET_STRING *los = ASN1_OCTET_STRING_new(); oaep->pSourceFunc = X509_ALGOR_new(); if (!oaep->pSourceFunc) goto err; if (!los) goto err; if (!ASN1_OCTET_STRING_set(los, label, labellen)) { ASN1_OCTET_STRING_free(los); goto err; } X509_ALGOR_set0(oaep->pSourceFunc, OBJ_nid2obj(NID_pSpecified), V_ASN1_OCTET_STRING, los); } /* create string with pss parameter encoding. */ if (!ASN1_item_pack(oaep, ASN1_ITEM_rptr(RSA_OAEP_PARAMS), &os)) goto err; X509_ALGOR_set0(alg, OBJ_nid2obj(NID_rsaesOaep), V_ASN1_SEQUENCE, os); os = NULL; rv = 1; err: if (oaep) RSA_OAEP_PARAMS_free(oaep); if (os) ASN1_STRING_free(os); return rv; }
3,628
13,601
243384843246714785127441374349042413680
null
null
null
openssl
c394a488942387246653833359a5c94b5832674e
0
static int rsa_cms_sign(CMS_SignerInfo *si) { int pad_mode = RSA_PKCS1_PADDING; X509_ALGOR *alg; EVP_PKEY_CTX *pkctx = CMS_SignerInfo_get0_pkey_ctx(si); ASN1_STRING *os = NULL; CMS_SignerInfo_get0_algs(si, NULL, NULL, NULL, &alg); if (pkctx) { if (EVP_PKEY_CTX_get_rsa_padding(pkctx, &pad_mode) <= 0) return 0; } if (pad_mode == RSA_PKCS1_PADDING) { X509_ALGOR_set0(alg, OBJ_nid2obj(NID_rsaEncryption), V_ASN1_NULL, 0); return 1; } /* We don't support it */ if (pad_mode != RSA_PKCS1_PSS_PADDING) return 0; os = rsa_ctx_to_pss(pkctx); if (!os) return 0; X509_ALGOR_set0(alg, OBJ_nid2obj(NID_rsassaPss), V_ASN1_SEQUENCE, os); return 1; }
3,629
13,602
276574696567545506208760231734622906102
null
null
null
openssl
c394a488942387246653833359a5c94b5832674e
0
static int rsa_cms_verify(CMS_SignerInfo *si) { int nid, nid2; X509_ALGOR *alg; EVP_PKEY_CTX *pkctx = CMS_SignerInfo_get0_pkey_ctx(si); CMS_SignerInfo_get0_algs(si, NULL, NULL, NULL, &alg); nid = OBJ_obj2nid(alg->algorithm); if (nid == NID_rsaEncryption) return 1; if (nid == NID_rsassaPss) return rsa_pss_to_ctx(NULL, pkctx, alg, NULL); /* Workaround for some implementation that use a signature OID */ if (OBJ_find_sigid_algs(nid, NULL, &nid2)) { if (nid2 == NID_rsaEncryption) return 1; } return 0; }
3,630
13,603
178019848219503384883842329492114953168
null
null
null
openssl
c394a488942387246653833359a5c94b5832674e
0
static ASN1_STRING *rsa_ctx_to_pss(EVP_PKEY_CTX *pkctx) { const EVP_MD *sigmd, *mgf1md; RSA_PSS_PARAMS *pss = NULL; ASN1_STRING *os = NULL; EVP_PKEY *pk = EVP_PKEY_CTX_get0_pkey(pkctx); int saltlen, rv = 0; if (EVP_PKEY_CTX_get_signature_md(pkctx, &sigmd) <= 0) goto err; if (EVP_PKEY_CTX_get_rsa_mgf1_md(pkctx, &mgf1md) <= 0) goto err; if (!EVP_PKEY_CTX_get_rsa_pss_saltlen(pkctx, &saltlen)) goto err; if (saltlen == -1) saltlen = EVP_MD_size(sigmd); else if (saltlen == -2) { saltlen = EVP_PKEY_size(pk) - EVP_MD_size(sigmd) - 2; if (((EVP_PKEY_bits(pk) - 1) & 0x7) == 0) saltlen--; } pss = RSA_PSS_PARAMS_new(); if (!pss) goto err; if (saltlen != 20) { pss->saltLength = ASN1_INTEGER_new(); if (!pss->saltLength) goto err; if (!ASN1_INTEGER_set(pss->saltLength, saltlen)) goto err; } if (!rsa_md_to_algor(&pss->hashAlgorithm, sigmd)) goto err; if (!rsa_md_to_mgf1(&pss->maskGenAlgorithm, mgf1md)) goto err; /* Finally create string with pss parameter encoding. */ if (!ASN1_item_pack(pss, ASN1_ITEM_rptr(RSA_PSS_PARAMS), &os)) goto err; rv = 1; err: if (pss) RSA_PSS_PARAMS_free(pss); if (rv) return os; if (os) ASN1_STRING_free(os); return NULL; }
3,631
13,604
171551876413025990245607414054484221300
null
null
null
openssl
c394a488942387246653833359a5c94b5832674e
0
static int rsa_item_verify(EVP_MD_CTX *ctx, const ASN1_ITEM *it, void *asn, X509_ALGOR *sigalg, ASN1_BIT_STRING *sig, EVP_PKEY *pkey) { /* Sanity check: make sure it is PSS */ if (OBJ_obj2nid(sigalg->algorithm) != NID_rsassaPss) { RSAerr(RSA_F_RSA_ITEM_VERIFY, RSA_R_UNSUPPORTED_SIGNATURE_TYPE); return -1; } if (rsa_pss_to_ctx(ctx, NULL, sigalg, pkey) > 0) { /* Carry on */ return 2; } return -1; }
3,632
13,605
77101590144459579778625442218007764315
null
null
null
openssl
c394a488942387246653833359a5c94b5832674e
0
static int rsa_md_to_mgf1(X509_ALGOR **palg, const EVP_MD *mgf1md) { X509_ALGOR *algtmp = NULL; ASN1_STRING *stmp = NULL; *palg = NULL; if (EVP_MD_type(mgf1md) == NID_sha1) return 1; /* need to embed algorithm ID inside another */ if (!rsa_md_to_algor(&algtmp, mgf1md)) goto err; if (!ASN1_item_pack(algtmp, ASN1_ITEM_rptr(X509_ALGOR), &stmp)) goto err; *palg = X509_ALGOR_new(); if (!*palg) goto err; X509_ALGOR_set0(*palg, OBJ_nid2obj(NID_mgf1), V_ASN1_SEQUENCE, stmp); stmp = NULL; err: if (stmp) ASN1_STRING_free(stmp); if (algtmp) X509_ALGOR_free(algtmp); if (*palg) return 1; return 0; }
3,634
13,606
183639737457582920006343797376073975038
null
null
null
openssl
c394a488942387246653833359a5c94b5832674e
0
static const EVP_MD *rsa_mgf1_to_md(X509_ALGOR *alg, X509_ALGOR *maskHash) { const EVP_MD *md; if (!alg) return EVP_sha1(); /* Check mask and lookup mask hash algorithm */ if (OBJ_obj2nid(alg->algorithm) != NID_mgf1) { RSAerr(RSA_F_RSA_MGF1_TO_MD, RSA_R_UNSUPPORTED_MASK_ALGORITHM); return NULL; } if (!maskHash) { RSAerr(RSA_F_RSA_MGF1_TO_MD, RSA_R_UNSUPPORTED_MASK_PARAMETER); return NULL; } md = EVP_get_digestbyobj(maskHash->algorithm); if (md == NULL) { RSAerr(RSA_F_RSA_MGF1_TO_MD, RSA_R_UNKNOWN_MASK_DIGEST); return NULL; } return md; }
3,635
13,607
236214195906257559662673486580438100962
null
null
null
openssl
c394a488942387246653833359a5c94b5832674e
0
static int rsa_pkey_ctrl(EVP_PKEY *pkey, int op, long arg1, void *arg2) { X509_ALGOR *alg = NULL; switch (op) { case ASN1_PKEY_CTRL_PKCS7_SIGN: if (arg1 == 0) PKCS7_SIGNER_INFO_get0_algs(arg2, NULL, NULL, &alg); break; case ASN1_PKEY_CTRL_PKCS7_ENCRYPT: if (arg1 == 0) PKCS7_RECIP_INFO_get0_alg(arg2, &alg); break; #ifndef OPENSSL_NO_CMS case ASN1_PKEY_CTRL_CMS_SIGN: if (arg1 == 0) return rsa_cms_sign(arg2); else if (arg1 == 1) return rsa_cms_verify(arg2); break; case ASN1_PKEY_CTRL_CMS_ENVELOPE: if (arg1 == 0) return rsa_cms_encrypt(arg2); else if (arg1 == 1) return rsa_cms_decrypt(arg2); break; case ASN1_PKEY_CTRL_CMS_RI_TYPE: *(int *)arg2 = CMS_RECIPINFO_TRANS; return 1; #endif case ASN1_PKEY_CTRL_DEFAULT_MD_NID: *(int *)arg2 = NID_sha256; return 1; default: return -2; } if (alg) X509_ALGOR_set0(alg, OBJ_nid2obj(NID_rsaEncryption), V_ASN1_NULL, 0); return 1; }
3,637
13,608
321685565712203012193694093146588099219
null
null
null
openssl
c394a488942387246653833359a5c94b5832674e
0
static int rsa_pss_to_ctx(EVP_MD_CTX *ctx, EVP_PKEY_CTX *pkctx, X509_ALGOR *sigalg, EVP_PKEY *pkey) { int rv = -1; int saltlen; const EVP_MD *mgf1md = NULL, *md = NULL; RSA_PSS_PARAMS *pss; X509_ALGOR *maskHash; /* Sanity check: make sure it is PSS */ if (OBJ_obj2nid(sigalg->algorithm) != NID_rsassaPss) { RSAerr(RSA_F_RSA_PSS_TO_CTX, RSA_R_UNSUPPORTED_SIGNATURE_TYPE); return -1; } /* Decode PSS parameters */ pss = rsa_pss_decode(sigalg, &maskHash); if (pss == NULL) { RSAerr(RSA_F_RSA_PSS_TO_CTX, RSA_R_INVALID_PSS_PARAMETERS); goto err; } mgf1md = rsa_mgf1_to_md(pss->maskGenAlgorithm, maskHash); if (!mgf1md) goto err; md = rsa_algor_to_md(pss->hashAlgorithm); if (!md) goto err; if (pss->saltLength) { saltlen = ASN1_INTEGER_get(pss->saltLength); /* * Could perform more salt length sanity checks but the main RSA * routines will trap other invalid values anyway. */ if (saltlen < 0) { RSAerr(RSA_F_RSA_PSS_TO_CTX, RSA_R_INVALID_SALT_LENGTH); goto err; } } else saltlen = 20; /* * low-level routines support only trailer field 0xbc (value 1) and * PKCS#1 says we should reject any other value anyway. */ if (pss->trailerField && ASN1_INTEGER_get(pss->trailerField) != 1) { RSAerr(RSA_F_RSA_PSS_TO_CTX, RSA_R_INVALID_TRAILER); goto err; } /* We have all parameters now set up context */ if (pkey) { if (!EVP_DigestVerifyInit(ctx, &pkctx, md, NULL, pkey)) goto err; } else { const EVP_MD *checkmd; if (EVP_PKEY_CTX_get_signature_md(pkctx, &checkmd) <= 0) goto err; if (EVP_MD_type(md) != EVP_MD_type(checkmd)) { RSAerr(RSA_F_RSA_PSS_TO_CTX, RSA_R_DIGEST_DOES_NOT_MATCH); goto err; } } if (EVP_PKEY_CTX_set_rsa_padding(pkctx, RSA_PKCS1_PSS_PADDING) <= 0) goto err; if (EVP_PKEY_CTX_set_rsa_pss_saltlen(pkctx, saltlen) <= 0) goto err; if (EVP_PKEY_CTX_set_rsa_mgf1_md(pkctx, mgf1md) <= 0) goto err; /* Carry on */ rv = 1; err: RSA_PSS_PARAMS_free(pss); if (maskHash) X509_ALGOR_free(maskHash); return rv; }
3,639
13,609
161256368911651195691594405808399007839
null
null
null
openssl
d73cc256c8e256c32ed959456101b73ba9842f72
0
static int genprime_cb(int p, int n, BN_GENCB *arg) { char c = '*'; if (p == 0) c = '.'; if (p == 1) c = '+'; if (p == 2) c = '*'; if (p == 3) c = '\n'; putc(c, stderr); fflush(stderr); return 1; }
CWE-200
3,640
13,610
114771442931326598079381433683571282190
null
null
null
openssl
d73cc256c8e256c32ed959456101b73ba9842f72
0
int main(int argc, char *argv[]) { BN_CTX *ctx; BIO *out; char *outfile = NULL; results = 0; RAND_seed(rnd_seed, sizeof rnd_seed); /* or BN_generate_prime may fail */ argc--; argv++; while (argc >= 1) { if (strcmp(*argv, "-results") == 0) results = 1; else if (strcmp(*argv, "-out") == 0) { if (--argc < 1) break; outfile = *(++argv); } argc--; argv++; } ctx = BN_CTX_new(); if (ctx == NULL) EXIT(1); out = BIO_new(BIO_s_file()); if (out == NULL) EXIT(1); if (outfile == NULL) { BIO_set_fp(out, stdout, BIO_NOCLOSE); } else { if (!BIO_write_filename(out, outfile)) { perror(outfile); EXIT(1); } } if (!results) BIO_puts(out, "obase=16\nibase=16\n"); message(out, "BN_add"); if (!test_add(out)) goto err; (void)BIO_flush(out); message(out, "BN_sub"); if (!test_sub(out)) goto err; (void)BIO_flush(out); message(out, "BN_lshift1"); if (!test_lshift1(out)) goto err; (void)BIO_flush(out); message(out, "BN_lshift (fixed)"); if (!test_lshift(out, ctx, BN_bin2bn(lst, sizeof(lst) - 1, NULL))) goto err; (void)BIO_flush(out); message(out, "BN_lshift"); if (!test_lshift(out, ctx, NULL)) goto err; (void)BIO_flush(out); message(out, "BN_rshift1"); if (!test_rshift1(out)) goto err; (void)BIO_flush(out); message(out, "BN_rshift"); if (!test_rshift(out, ctx)) goto err; (void)BIO_flush(out); message(out, "BN_sqr"); if (!test_sqr(out, ctx)) goto err; (void)BIO_flush(out); message(out, "BN_mul"); if (!test_mul(out)) goto err; (void)BIO_flush(out); message(out, "BN_div"); if (!test_div(out, ctx)) goto err; (void)BIO_flush(out); message(out, "BN_div_word"); if (!test_div_word(out)) goto err; (void)BIO_flush(out); message(out, "BN_div_recp"); if (!test_div_recp(out, ctx)) goto err; (void)BIO_flush(out); message(out, "BN_mod"); if (!test_mod(out, ctx)) goto err; (void)BIO_flush(out); message(out, "BN_mod_mul"); if (!test_mod_mul(out, ctx)) goto err; (void)BIO_flush(out); message(out, "BN_mont"); if (!test_mont(out, ctx)) goto err; (void)BIO_flush(out); message(out, "BN_mod_exp"); if (!test_mod_exp(out, ctx)) goto err; (void)BIO_flush(out); message(out, "BN_mod_exp_mont_consttime"); if (!test_mod_exp_mont_consttime(out, ctx)) goto err; if (!test_mod_exp_mont5(out, ctx)) goto err; (void)BIO_flush(out); message(out, "BN_exp"); if (!test_exp(out, ctx)) goto err; (void)BIO_flush(out); message(out, "BN_kronecker"); if (!test_kron(out, ctx)) goto err; (void)BIO_flush(out); message(out, "BN_mod_sqrt"); if (!test_sqrt(out, ctx)) goto err; (void)BIO_flush(out); #ifndef OPENSSL_NO_EC2M message(out, "BN_GF2m_add"); if (!test_gf2m_add(out)) goto err; (void)BIO_flush(out); message(out, "BN_GF2m_mod"); if (!test_gf2m_mod(out)) goto err; (void)BIO_flush(out); message(out, "BN_GF2m_mod_mul"); if (!test_gf2m_mod_mul(out, ctx)) goto err; (void)BIO_flush(out); message(out, "BN_GF2m_mod_sqr"); if (!test_gf2m_mod_sqr(out, ctx)) goto err; (void)BIO_flush(out); message(out, "BN_GF2m_mod_inv"); if (!test_gf2m_mod_inv(out, ctx)) goto err; (void)BIO_flush(out); message(out, "BN_GF2m_mod_div"); if (!test_gf2m_mod_div(out, ctx)) goto err; (void)BIO_flush(out); message(out, "BN_GF2m_mod_exp"); if (!test_gf2m_mod_exp(out, ctx)) goto err; (void)BIO_flush(out); message(out, "BN_GF2m_mod_sqrt"); if (!test_gf2m_mod_sqrt(out, ctx)) goto err; (void)BIO_flush(out); message(out, "BN_GF2m_mod_solve_quad"); if (!test_gf2m_mod_solve_quad(out, ctx)) goto err; (void)BIO_flush(out); #endif BN_CTX_free(ctx); BIO_free(out); EXIT(0); err: BIO_puts(out, "1\n"); /* make sure the Perl script fed by bc * notices the failure, see test_bn in * test/Makefile.ssl */ (void)BIO_flush(out); ERR_load_crypto_strings(); ERR_print_errors_fp(stderr); EXIT(1); return (1); }
CWE-200
3,641
13,611
248491926062698333070471928205701869505
null
null
null
openssl
d73cc256c8e256c32ed959456101b73ba9842f72
0
static void message(BIO *out, char *m) { fprintf(stderr, "test %s\n", m); BIO_puts(out, "print \"test "); BIO_puts(out, m); BIO_puts(out, "\\n\"\n"); }
CWE-200
3,642
13,612
290458347769995398740392313978013073884
null
null
null
openssl
d73cc256c8e256c32ed959456101b73ba9842f72
0
int rand_neg(void) { static unsigned int neg = 0; static int sign[8] = { 0, 0, 0, 1, 1, 0, 1, 1 }; return (sign[(neg++) % 8]); }
CWE-200
3,644
13,613
315270713411848343305839957727145433879
null
null
null
openssl
d73cc256c8e256c32ed959456101b73ba9842f72
0
int test_div_word(BIO *bp) { BIGNUM a, b; BN_ULONG r, s; int i; BN_init(&a); BN_init(&b); for (i = 0; i < num0; i++) { do { BN_bntest_rand(&a, 512, -1, 0); BN_bntest_rand(&b, BN_BITS2, -1, 0); } while (BN_is_zero(&b)); s = b.d[0]; BN_copy(&b, &a); r = BN_div_word(&b, s); if (bp != NULL) { if (!results) { BN_print(bp, &a); BIO_puts(bp, " / "); print_word(bp, s); BIO_puts(bp, " - "); } BN_print(bp, &b); BIO_puts(bp, "\n"); if (!results) { BN_print(bp, &a); BIO_puts(bp, " % "); print_word(bp, s); BIO_puts(bp, " - "); } print_word(bp, r); BIO_puts(bp, "\n"); } BN_mul_word(&b, s); BN_add_word(&b, r); BN_sub(&b, &a, &b); if (!BN_is_zero(&b)) { fprintf(stderr, "Division (word) test failed!\n"); return 0; } } BN_free(&a); BN_free(&b); return (1); }
CWE-200
3,648
13,614
173622985013939915682290104326583959432
null
null
null
openssl
d73cc256c8e256c32ed959456101b73ba9842f72
0
int test_exp(BIO *bp, BN_CTX *ctx) { BIGNUM *a, *b, *d, *e, *one; int i; a = BN_new(); b = BN_new(); d = BN_new(); e = BN_new(); one = BN_new(); BN_one(one); for (i = 0; i < num2; i++) { BN_bntest_rand(a, 20 + i * 5, 0, 0); BN_bntest_rand(b, 2 + i, 0, 0); if (BN_exp(d, a, b, ctx) <= 0) return (0); if (bp != NULL) { if (!results) { BN_print(bp, a); BIO_puts(bp, " ^ "); BN_print(bp, b); BIO_puts(bp, " - "); } BN_print(bp, d); BIO_puts(bp, "\n"); } BN_one(e); for (; !BN_is_zero(b); BN_sub(b, b, one)) BN_mul(e, e, a, ctx); BN_sub(e, e, d); if (!BN_is_zero(e)) { fprintf(stderr, "Exponentiation test failed!\n"); return 0; } } BN_free(a); BN_free(b); BN_free(d); BN_free(e); BN_free(one); return (1); }
CWE-200
3,649
13,615
314607505871592289816498814304606903235
null
null
null
openssl
d73cc256c8e256c32ed959456101b73ba9842f72
0
int test_gf2m_add(BIO *bp) { BIGNUM a, b, c; int i, ret = 0; BN_init(&a); BN_init(&b); BN_init(&c); for (i = 0; i < num0; i++) { BN_rand(&a, 512, 0, 0); BN_copy(&b, BN_value_one()); a.neg = rand_neg(); b.neg = rand_neg(); BN_GF2m_add(&c, &a, &b); # if 0 /* make test uses ouput in bc but bc can't * handle GF(2^m) arithmetic */ if (bp != NULL) { if (!results) { BN_print(bp, &a); BIO_puts(bp, " ^ "); BN_print(bp, &b); BIO_puts(bp, " = "); } BN_print(bp, &c); BIO_puts(bp, "\n"); } # endif /* Test that two added values have the correct parity. */ if ((BN_is_odd(&a) && BN_is_odd(&c)) || (!BN_is_odd(&a) && !BN_is_odd(&c))) { fprintf(stderr, "GF(2^m) addition test (a) failed!\n"); goto err; } BN_GF2m_add(&c, &c, &c); /* Test that c + c = 0. */ if (!BN_is_zero(&c)) { fprintf(stderr, "GF(2^m) addition test (b) failed!\n"); goto err; } } ret = 1; err: BN_free(&a); BN_free(&b); BN_free(&c); return ret; }
CWE-200
3,650
13,616
182835053435320613757204380961744806539
null
null
null
openssl
d73cc256c8e256c32ed959456101b73ba9842f72
0
int test_gf2m_mod(BIO *bp) { BIGNUM *a, *b[2], *c, *d, *e; int i, j, ret = 0; int p0[] = { 163, 7, 6, 3, 0, -1 }; int p1[] = { 193, 15, 0, -1 }; a = BN_new(); b[0] = BN_new(); b[1] = BN_new(); c = BN_new(); d = BN_new(); e = BN_new(); BN_GF2m_arr2poly(p0, b[0]); BN_GF2m_arr2poly(p1, b[1]); for (i = 0; i < num0; i++) { BN_bntest_rand(a, 1024, 0, 0); for (j = 0; j < 2; j++) { BN_GF2m_mod(c, a, b[j]); # if 0 /* make test uses ouput in bc but bc can't * handle GF(2^m) arithmetic */ if (bp != NULL) { if (!results) { BN_print(bp, a); BIO_puts(bp, " % "); BN_print(bp, b[j]); BIO_puts(bp, " - "); BN_print(bp, c); BIO_puts(bp, "\n"); } } # endif BN_GF2m_add(d, a, c); BN_GF2m_mod(e, d, b[j]); /* Test that a + (a mod p) mod p == 0. */ if (!BN_is_zero(e)) { fprintf(stderr, "GF(2^m) modulo test failed!\n"); goto err; } } } ret = 1; err: BN_free(a); BN_free(b[0]); BN_free(b[1]); BN_free(c); BN_free(d); BN_free(e); return ret; }
CWE-200
3,651
13,617
21634361059683403475148322074051472141
null
null
null
openssl
d73cc256c8e256c32ed959456101b73ba9842f72
0
int test_gf2m_mod_exp(BIO *bp, BN_CTX *ctx) { BIGNUM *a, *b[2], *c, *d, *e, *f; int i, j, ret = 0; int p0[] = { 163, 7, 6, 3, 0, -1 }; int p1[] = { 193, 15, 0, -1 }; a = BN_new(); b[0] = BN_new(); b[1] = BN_new(); c = BN_new(); d = BN_new(); e = BN_new(); f = BN_new(); BN_GF2m_arr2poly(p0, b[0]); BN_GF2m_arr2poly(p1, b[1]); for (i = 0; i < num0; i++) { BN_bntest_rand(a, 512, 0, 0); BN_bntest_rand(c, 512, 0, 0); BN_bntest_rand(d, 512, 0, 0); for (j = 0; j < 2; j++) { BN_GF2m_mod_exp(e, a, c, b[j], ctx); BN_GF2m_mod_exp(f, a, d, b[j], ctx); BN_GF2m_mod_mul(e, e, f, b[j], ctx); BN_add(f, c, d); BN_GF2m_mod_exp(f, a, f, b[j], ctx); # if 0 /* make test uses ouput in bc but bc can't * handle GF(2^m) arithmetic */ if (bp != NULL) { if (!results) { BN_print(bp, a); BIO_puts(bp, " ^ ("); BN_print(bp, c); BIO_puts(bp, " + "); BN_print(bp, d); BIO_puts(bp, ") = "); BN_print(bp, e); BIO_puts(bp, "; - "); BN_print(bp, f); BIO_puts(bp, " % "); BN_print(bp, b[j]); BIO_puts(bp, "\n"); } } # endif BN_GF2m_add(f, e, f); /* Test that a^(c+d)=a^c*a^d. */ if (!BN_is_zero(f)) { fprintf(stderr, "GF(2^m) modular exponentiation test failed!\n"); goto err; } } } ret = 1; err: BN_free(a); BN_free(b[0]); BN_free(b[1]); BN_free(c); BN_free(d); BN_free(e); BN_free(f); return ret; }
CWE-200
3,653
13,618
122690308133369613053750928088372381813
null
null
null
openssl
d73cc256c8e256c32ed959456101b73ba9842f72
0
int test_gf2m_mod_inv(BIO *bp, BN_CTX *ctx) { BIGNUM *a, *b[2], *c, *d; int i, j, ret = 0; int p0[] = { 163, 7, 6, 3, 0, -1 }; int p1[] = { 193, 15, 0, -1 }; a = BN_new(); b[0] = BN_new(); b[1] = BN_new(); c = BN_new(); d = BN_new(); BN_GF2m_arr2poly(p0, b[0]); BN_GF2m_arr2poly(p1, b[1]); for (i = 0; i < num0; i++) { BN_bntest_rand(a, 512, 0, 0); for (j = 0; j < 2; j++) { BN_GF2m_mod_inv(c, a, b[j], ctx); BN_GF2m_mod_mul(d, a, c, b[j], ctx); # if 0 /* make test uses ouput in bc but bc can't * handle GF(2^m) arithmetic */ if (bp != NULL) { if (!results) { BN_print(bp, a); BIO_puts(bp, " * "); BN_print(bp, c); BIO_puts(bp, " - 1 % "); BN_print(bp, b[j]); BIO_puts(bp, "\n"); } } # endif /* Test that ((1/a)*a) = 1. */ if (!BN_is_one(d)) { fprintf(stderr, "GF(2^m) modular inversion test failed!\n"); goto err; } } } ret = 1; err: BN_free(a); BN_free(b[0]); BN_free(b[1]); BN_free(c); BN_free(d); return ret; }
CWE-200
3,654
13,619
223129621008559753393946021886724837408
null
null
null
openssl
d73cc256c8e256c32ed959456101b73ba9842f72
0
int test_gf2m_mod_mul(BIO *bp, BN_CTX *ctx) { BIGNUM *a, *b[2], *c, *d, *e, *f, *g, *h; int i, j, ret = 0; int p0[] = { 163, 7, 6, 3, 0, -1 }; int p1[] = { 193, 15, 0, -1 }; a = BN_new(); b[0] = BN_new(); b[1] = BN_new(); c = BN_new(); d = BN_new(); e = BN_new(); f = BN_new(); g = BN_new(); h = BN_new(); BN_GF2m_arr2poly(p0, b[0]); BN_GF2m_arr2poly(p1, b[1]); for (i = 0; i < num0; i++) { BN_bntest_rand(a, 1024, 0, 0); BN_bntest_rand(c, 1024, 0, 0); BN_bntest_rand(d, 1024, 0, 0); for (j = 0; j < 2; j++) { BN_GF2m_mod_mul(e, a, c, b[j], ctx); # if 0 /* make test uses ouput in bc but bc can't * handle GF(2^m) arithmetic */ if (bp != NULL) { if (!results) { BN_print(bp, a); BIO_puts(bp, " * "); BN_print(bp, c); BIO_puts(bp, " % "); BN_print(bp, b[j]); BIO_puts(bp, " - "); BN_print(bp, e); BIO_puts(bp, "\n"); } } # endif BN_GF2m_add(f, a, d); BN_GF2m_mod_mul(g, f, c, b[j], ctx); BN_GF2m_mod_mul(h, d, c, b[j], ctx); BN_GF2m_add(f, e, g); BN_GF2m_add(f, f, h); /* Test that (a+d)*c = a*c + d*c. */ if (!BN_is_zero(f)) { fprintf(stderr, "GF(2^m) modular multiplication test failed!\n"); goto err; } } } ret = 1; err: BN_free(a); BN_free(b[0]); BN_free(b[1]); BN_free(c); BN_free(d); BN_free(e); BN_free(f); BN_free(g); BN_free(h); return ret; }
CWE-200
3,655
13,620
18488159027097211499590970273007359999
null
null
null
openssl
d73cc256c8e256c32ed959456101b73ba9842f72
0
int test_gf2m_mod_solve_quad(BIO *bp, BN_CTX *ctx) { BIGNUM *a, *b[2], *c, *d, *e; int i, j, s = 0, t, ret = 0; int p0[] = { 163, 7, 6, 3, 0, -1 }; int p1[] = { 193, 15, 0, -1 }; a = BN_new(); b[0] = BN_new(); b[1] = BN_new(); c = BN_new(); d = BN_new(); e = BN_new(); BN_GF2m_arr2poly(p0, b[0]); BN_GF2m_arr2poly(p1, b[1]); for (i = 0; i < num0; i++) { BN_bntest_rand(a, 512, 0, 0); for (j = 0; j < 2; j++) { t = BN_GF2m_mod_solve_quad(c, a, b[j], ctx); if (t) { s++; BN_GF2m_mod_sqr(d, c, b[j], ctx); BN_GF2m_add(d, c, d); BN_GF2m_mod(e, a, b[j]); # if 0 /* make test uses ouput in bc but bc can't * handle GF(2^m) arithmetic */ if (bp != NULL) { if (!results) { BN_print(bp, c); BIO_puts(bp, " is root of z^2 + z = "); BN_print(bp, a); BIO_puts(bp, " % "); BN_print(bp, b[j]); BIO_puts(bp, "\n"); } } # endif BN_GF2m_add(e, e, d); /* * Test that solution of quadratic c satisfies c^2 + c = a. */ if (!BN_is_zero(e)) { fprintf(stderr, "GF(2^m) modular solve quadratic test failed!\n"); goto err; } } else { # if 0 /* make test uses ouput in bc but bc can't * handle GF(2^m) arithmetic */ if (bp != NULL) { if (!results) { BIO_puts(bp, "There are no roots of z^2 + z = "); BN_print(bp, a); BIO_puts(bp, " % "); BN_print(bp, b[j]); BIO_puts(bp, "\n"); } } # endif } } } if (s == 0) { fprintf(stderr, "All %i tests of GF(2^m) modular solve quadratic resulted in no roots;\n", num0); fprintf(stderr, "this is very unlikely and probably indicates an error.\n"); goto err; } ret = 1; err: BN_free(a); BN_free(b[0]); BN_free(b[1]); BN_free(c); BN_free(d); BN_free(e); return ret; }
CWE-200
3,656
13,621
261358809066854407055603914064865163129
null
null
null
openssl
d73cc256c8e256c32ed959456101b73ba9842f72
0
int test_gf2m_mod_sqr(BIO *bp, BN_CTX *ctx) { BIGNUM *a, *b[2], *c, *d; int i, j, ret = 0; int p0[] = { 163, 7, 6, 3, 0, -1 }; int p1[] = { 193, 15, 0, -1 }; a = BN_new(); b[0] = BN_new(); b[1] = BN_new(); c = BN_new(); d = BN_new(); BN_GF2m_arr2poly(p0, b[0]); BN_GF2m_arr2poly(p1, b[1]); for (i = 0; i < num0; i++) { BN_bntest_rand(a, 1024, 0, 0); for (j = 0; j < 2; j++) { BN_GF2m_mod_sqr(c, a, b[j], ctx); BN_copy(d, a); BN_GF2m_mod_mul(d, a, d, b[j], ctx); # if 0 /* make test uses ouput in bc but bc can't * handle GF(2^m) arithmetic */ if (bp != NULL) { if (!results) { BN_print(bp, a); BIO_puts(bp, " ^ 2 % "); BN_print(bp, b[j]); BIO_puts(bp, " = "); BN_print(bp, c); BIO_puts(bp, "; a * a = "); BN_print(bp, d); BIO_puts(bp, "\n"); } } # endif BN_GF2m_add(d, c, d); /* Test that a*a = a^2. */ if (!BN_is_zero(d)) { fprintf(stderr, "GF(2^m) modular squaring test failed!\n"); goto err; } } } ret = 1; err: BN_free(a); BN_free(b[0]); BN_free(b[1]); BN_free(c); BN_free(d); return ret; }
CWE-200
3,657
13,622
311355775794222619223676861605260406462
null
null
null
openssl
d73cc256c8e256c32ed959456101b73ba9842f72
0
int test_gf2m_mod_sqrt(BIO *bp, BN_CTX *ctx) { BIGNUM *a, *b[2], *c, *d, *e, *f; int i, j, ret = 0; int p0[] = { 163, 7, 6, 3, 0, -1 }; int p1[] = { 193, 15, 0, -1 }; a = BN_new(); b[0] = BN_new(); b[1] = BN_new(); c = BN_new(); d = BN_new(); e = BN_new(); f = BN_new(); BN_GF2m_arr2poly(p0, b[0]); BN_GF2m_arr2poly(p1, b[1]); for (i = 0; i < num0; i++) { BN_bntest_rand(a, 512, 0, 0); for (j = 0; j < 2; j++) { BN_GF2m_mod(c, a, b[j]); BN_GF2m_mod_sqrt(d, a, b[j], ctx); BN_GF2m_mod_sqr(e, d, b[j], ctx); # if 0 /* make test uses ouput in bc but bc can't * handle GF(2^m) arithmetic */ if (bp != NULL) { if (!results) { BN_print(bp, d); BIO_puts(bp, " ^ 2 - "); BN_print(bp, a); BIO_puts(bp, "\n"); } } # endif BN_GF2m_add(f, c, e); /* Test that d^2 = a, where d = sqrt(a). */ if (!BN_is_zero(f)) { fprintf(stderr, "GF(2^m) modular square root test failed!\n"); goto err; } } } ret = 1; err: BN_free(a); BN_free(b[0]); BN_free(b[1]); BN_free(c); BN_free(d); BN_free(e); BN_free(f); return ret; }
CWE-200
3,658
13,623
41153617269425569037931765779382190601
null
null
null
openssl
d73cc256c8e256c32ed959456101b73ba9842f72
0
int test_kron(BIO *bp, BN_CTX *ctx) { BN_GENCB cb; BIGNUM *a, *b, *r, *t; int i; int legendre, kronecker; int ret = 0; a = BN_new(); b = BN_new(); r = BN_new(); t = BN_new(); if (a == NULL || b == NULL || r == NULL || t == NULL) goto err; BN_GENCB_set(&cb, genprime_cb, NULL); /* * We test BN_kronecker(a, b, ctx) just for b odd (Jacobi symbol). In * this case we know that if b is prime, then BN_kronecker(a, b, ctx) is * congruent to $a^{(b-1)/2}$, modulo $b$ (Legendre symbol). So we * generate a random prime b and compare these values for a number of * random a's. (That is, we run the Solovay-Strassen primality test to * confirm that b is prime, except that we don't want to test whether b * is prime but whether BN_kronecker works.) */ if (!BN_generate_prime_ex(b, 512, 0, NULL, NULL, &cb)) goto err; b->neg = rand_neg(); putc('\n', stderr); for (i = 0; i < num0; i++) { if (!BN_bntest_rand(a, 512, 0, 0)) goto err; a->neg = rand_neg(); /* t := (|b|-1)/2 (note that b is odd) */ if (!BN_copy(t, b)) goto err; t->neg = 0; if (!BN_sub_word(t, 1)) goto err; if (!BN_rshift1(t, t)) goto err; /* r := a^t mod b */ b->neg = 0; if (!BN_mod_exp_recp(r, a, t, b, ctx)) goto err; b->neg = 1; if (BN_is_word(r, 1)) legendre = 1; else if (BN_is_zero(r)) legendre = 0; else { if (!BN_add_word(r, 1)) goto err; if (0 != BN_ucmp(r, b)) { fprintf(stderr, "Legendre symbol computation failed\n"); goto err; } legendre = -1; } kronecker = BN_kronecker(a, b, ctx); if (kronecker < -1) goto err; /* we actually need BN_kronecker(a, |b|) */ if (a->neg && b->neg) kronecker = -kronecker; if (legendre != kronecker) { fprintf(stderr, "legendre != kronecker; a = "); BN_print_fp(stderr, a); fprintf(stderr, ", b = "); BN_print_fp(stderr, b); fprintf(stderr, "\n"); goto err; } putc('.', stderr); fflush(stderr); } putc('\n', stderr); fflush(stderr); ret = 1; err: if (a != NULL) BN_free(a); if (b != NULL) BN_free(b); if (r != NULL) BN_free(r); if (t != NULL) BN_free(t); return ret; }
CWE-200
3,659
13,624
20770279326418320097236458016446383718
null
null
null
openssl
d73cc256c8e256c32ed959456101b73ba9842f72
0
int test_lshift1(BIO *bp) { BIGNUM *a, *b, *c; int i; a = BN_new(); b = BN_new(); c = BN_new(); BN_bntest_rand(a, 200, 0, 0); a->neg = rand_neg(); for (i = 0; i < num0; i++) { BN_lshift1(b, a); if (bp != NULL) { if (!results) { BN_print(bp, a); BIO_puts(bp, " * 2"); BIO_puts(bp, " - "); } BN_print(bp, b); BIO_puts(bp, "\n"); } BN_add(c, a, a); BN_sub(a, b, c); if (!BN_is_zero(a)) { fprintf(stderr, "Left shift one test failed!\n"); return 0; } BN_copy(a, b); } BN_free(a); BN_free(b); BN_free(c); return (1); }
CWE-200
3,661
13,625
132322936794212661672167697875816390547
null
null
null
openssl
d73cc256c8e256c32ed959456101b73ba9842f72
0
int test_mod(BIO *bp, BN_CTX *ctx) { BIGNUM *a, *b, *c, *d, *e; int i; a = BN_new(); b = BN_new(); c = BN_new(); d = BN_new(); e = BN_new(); BN_bntest_rand(a, 1024, 0, 0); for (i = 0; i < num0; i++) { BN_bntest_rand(b, 450 + i * 10, 0, 0); a->neg = rand_neg(); b->neg = rand_neg(); BN_mod(c, a, b, ctx); if (bp != NULL) { if (!results) { BN_print(bp, a); BIO_puts(bp, " % "); BN_print(bp, b); BIO_puts(bp, " - "); } BN_print(bp, c); BIO_puts(bp, "\n"); } BN_div(d, e, a, b, ctx); BN_sub(e, e, c); if (!BN_is_zero(e)) { fprintf(stderr, "Modulo test failed!\n"); return 0; } } BN_free(a); BN_free(b); BN_free(c); BN_free(d); BN_free(e); return (1); }
CWE-200
3,662
13,626
42967209063961133424670587469841695912
null
null
null
openssl
d73cc256c8e256c32ed959456101b73ba9842f72
0
int test_mod_exp_mont5(BIO *bp, BN_CTX *ctx) { BIGNUM *a, *p, *m, *d, *e; BN_MONT_CTX *mont; a = BN_new(); p = BN_new(); m = BN_new(); d = BN_new(); e = BN_new(); mont = BN_MONT_CTX_new(); BN_bntest_rand(m, 1024, 0, 1); /* must be odd for montgomery */ /* Zero exponent */ BN_bntest_rand(a, 1024, 0, 0); BN_zero(p); if (!BN_mod_exp_mont_consttime(d, a, p, m, ctx, NULL)) return 0; if (!BN_is_one(d)) { fprintf(stderr, "Modular exponentiation test failed!\n"); return 0; } /* Zero input */ BN_bntest_rand(p, 1024, 0, 0); BN_zero(a); if (!BN_mod_exp_mont_consttime(d, a, p, m, ctx, NULL)) return 0; if (!BN_is_zero(d)) { fprintf(stderr, "Modular exponentiation test failed!\n"); return 0; } /* * Craft an input whose Montgomery representation is 1, i.e., shorter * than the modulus m, in order to test the const time precomputation * scattering/gathering. */ BN_one(a); BN_MONT_CTX_set(mont, m, ctx); if (!BN_from_montgomery(e, a, mont, ctx)) return 0; if (!BN_mod_exp_mont_consttime(d, e, p, m, ctx, NULL)) return 0; if (!BN_mod_exp_simple(a, e, p, m, ctx)) return 0; if (BN_cmp(a, d) != 0) { fprintf(stderr, "Modular exponentiation test failed!\n"); return 0; } /* Finally, some regular test vectors. */ BN_bntest_rand(e, 1024, 0, 0); if (!BN_mod_exp_mont_consttime(d, e, p, m, ctx, NULL)) return 0; if (!BN_mod_exp_simple(a, e, p, m, ctx)) return 0; if (BN_cmp(a, d) != 0) { fprintf(stderr, "Modular exponentiation test failed!\n"); return 0; } BN_MONT_CTX_free(mont); BN_free(a); BN_free(p); BN_free(m); BN_free(d); BN_free(e); return (1); }
CWE-200
3,663
13,627
78197504153796528125186209632065974502
null
null
null
openssl
d73cc256c8e256c32ed959456101b73ba9842f72
0
int test_mod_mul(BIO *bp, BN_CTX *ctx) { BIGNUM *a, *b, *c, *d, *e; int i, j; a = BN_new(); b = BN_new(); c = BN_new(); d = BN_new(); e = BN_new(); BN_one(a); BN_one(b); BN_zero(c); if (BN_mod_mul(e, a, b, c, ctx)) { fprintf(stderr, "BN_mod_mul with zero modulus succeeded!\n"); return 0; } for (j = 0; j < 3; j++) { BN_bntest_rand(c, 1024, 0, 0); for (i = 0; i < num0; i++) { BN_bntest_rand(a, 475 + i * 10, 0, 0); BN_bntest_rand(b, 425 + i * 11, 0, 0); a->neg = rand_neg(); b->neg = rand_neg(); if (!BN_mod_mul(e, a, b, c, ctx)) { unsigned long l; while ((l = ERR_get_error())) fprintf(stderr, "ERROR:%s\n", ERR_error_string(l, NULL)); EXIT(1); } if (bp != NULL) { if (!results) { BN_print(bp, a); BIO_puts(bp, " * "); BN_print(bp, b); BIO_puts(bp, " % "); BN_print(bp, c); if ((a->neg ^ b->neg) && !BN_is_zero(e)) { /* * If (a*b) % c is negative, c must be added in order * to obtain the normalized remainder (new with * OpenSSL 0.9.7, previous versions of BN_mod_mul * could generate negative results) */ BIO_puts(bp, " + "); BN_print(bp, c); } BIO_puts(bp, " - "); } BN_print(bp, e); BIO_puts(bp, "\n"); } BN_mul(d, a, b, ctx); BN_sub(d, d, e); BN_div(a, b, d, c, ctx); if (!BN_is_zero(b)) { fprintf(stderr, "Modulo multiply test failed!\n"); ERR_print_errors_fp(stderr); return 0; } } } BN_free(a); BN_free(b); BN_free(c); BN_free(d); BN_free(e); return (1); }
CWE-200
3,664
13,628
170178984418212997620836378389147831592
null
null
null
openssl
d73cc256c8e256c32ed959456101b73ba9842f72
0
int test_mont(BIO *bp, BN_CTX *ctx) { BIGNUM a, b, c, d, A, B; BIGNUM n; int i; BN_MONT_CTX *mont; BN_init(&a); BN_init(&b); BN_init(&c); BN_init(&d); BN_init(&A); BN_init(&B); BN_init(&n); mont = BN_MONT_CTX_new(); if (mont == NULL) return 0; BN_zero(&n); if (BN_MONT_CTX_set(mont, &n, ctx)) { fprintf(stderr, "BN_MONT_CTX_set succeeded for zero modulus!\n"); return 0; } BN_set_word(&n, 16); if (BN_MONT_CTX_set(mont, &n, ctx)) { fprintf(stderr, "BN_MONT_CTX_set succeeded for even modulus!\n"); return 0; } BN_bntest_rand(&a, 100, 0, 0); BN_bntest_rand(&b, 100, 0, 0); for (i = 0; i < num2; i++) { int bits = (200 * (i + 1)) / num2; if (bits == 0) continue; BN_bntest_rand(&n, bits, 0, 1); BN_MONT_CTX_set(mont, &n, ctx); BN_nnmod(&a, &a, &n, ctx); BN_nnmod(&b, &b, &n, ctx); BN_to_montgomery(&A, &a, mont, ctx); BN_to_montgomery(&B, &b, mont, ctx); BN_mod_mul_montgomery(&c, &A, &B, mont, ctx); BN_from_montgomery(&A, &c, mont, ctx); if (bp != NULL) { if (!results) { #ifdef undef fprintf(stderr, "%d * %d %% %d\n", BN_num_bits(&a), BN_num_bits(&b), BN_num_bits(mont->N)); #endif BN_print(bp, &a); BIO_puts(bp, " * "); BN_print(bp, &b); BIO_puts(bp, " % "); BN_print(bp, &(mont->N)); BIO_puts(bp, " - "); } BN_print(bp, &A); BIO_puts(bp, "\n"); } BN_mod_mul(&d, &a, &b, &n, ctx); BN_sub(&d, &d, &A); if (!BN_is_zero(&d)) { fprintf(stderr, "Montgomery multiplication test failed!\n"); return 0; } } BN_MONT_CTX_free(mont); BN_free(&a); BN_free(&b); BN_free(&c); BN_free(&d); BN_free(&A); BN_free(&B); BN_free(&n); return (1); }
CWE-200
3,665
13,629
241561346062650441412210639110109183619
null
null
null
openssl
d73cc256c8e256c32ed959456101b73ba9842f72
0
int test_mul(BIO *bp) { BIGNUM a, b, c, d, e; int i; BN_CTX *ctx; ctx = BN_CTX_new(); if (ctx == NULL) EXIT(1); BN_init(&a); BN_init(&b); BN_init(&c); BN_init(&d); BN_init(&e); for (i = 0; i < num0 + num1; i++) { if (i <= num1) { BN_bntest_rand(&a, 100, 0, 0); BN_bntest_rand(&b, 100, 0, 0); } else BN_bntest_rand(&b, i - num1, 0, 0); a.neg = rand_neg(); b.neg = rand_neg(); BN_mul(&c, &a, &b, ctx); if (bp != NULL) { if (!results) { BN_print(bp, &a); BIO_puts(bp, " * "); BN_print(bp, &b); BIO_puts(bp, " - "); } BN_print(bp, &c); BIO_puts(bp, "\n"); } BN_div(&d, &e, &c, &a, ctx); BN_sub(&d, &d, &b); if (!BN_is_zero(&d) || !BN_is_zero(&e)) { fprintf(stderr, "Multiplication test failed!\n"); return 0; } } BN_free(&a); BN_free(&b); BN_free(&c); BN_free(&d); BN_free(&e); BN_CTX_free(ctx); return (1); }
CWE-200
3,666
13,630
167219198747716207506778795012919296508
null
null
null
openssl
d73cc256c8e256c32ed959456101b73ba9842f72
0
int test_rshift(BIO *bp, BN_CTX *ctx) { BIGNUM *a, *b, *c, *d, *e; int i; a = BN_new(); b = BN_new(); c = BN_new(); d = BN_new(); e = BN_new(); BN_one(c); BN_bntest_rand(a, 200, 0, 0); a->neg = rand_neg(); for (i = 0; i < num0; i++) { BN_rshift(b, a, i + 1); BN_add(c, c, c); if (bp != NULL) { if (!results) { BN_print(bp, a); BIO_puts(bp, " / "); BN_print(bp, c); BIO_puts(bp, " - "); } BN_print(bp, b); BIO_puts(bp, "\n"); } BN_div(d, e, a, c, ctx); BN_sub(d, d, b); if (!BN_is_zero(d)) { fprintf(stderr, "Right shift test failed!\n"); return 0; } } BN_free(a); BN_free(b); BN_free(c); BN_free(d); BN_free(e); return (1); }
CWE-200
3,667
13,631
334132643627048569505128085944493485853
null
null
null
openssl
d73cc256c8e256c32ed959456101b73ba9842f72
0
int test_rshift1(BIO *bp) { BIGNUM *a, *b, *c; int i; a = BN_new(); b = BN_new(); c = BN_new(); BN_bntest_rand(a, 200, 0, 0); a->neg = rand_neg(); for (i = 0; i < num0; i++) { BN_rshift1(b, a); if (bp != NULL) { if (!results) { BN_print(bp, a); BIO_puts(bp, " / 2"); BIO_puts(bp, " - "); } BN_print(bp, b); BIO_puts(bp, "\n"); } BN_sub(c, a, b); BN_sub(c, c, b); if (!BN_is_zero(c) && !BN_abs_is_word(c, 1)) { fprintf(stderr, "Right shift one test failed!\n"); return 0; } BN_copy(a, b); } BN_free(a); BN_free(b); BN_free(c); return (1); }
CWE-200
3,668
13,632
153095803577955950838270034004354230495
null
null
null
openssl
d73cc256c8e256c32ed959456101b73ba9842f72
0
int test_sqr(BIO *bp, BN_CTX *ctx) { BIGNUM *a, *c, *d, *e; int i, ret = 0; a = BN_new(); c = BN_new(); d = BN_new(); e = BN_new(); if (a == NULL || c == NULL || d == NULL || e == NULL) { goto err; } for (i = 0; i < num0; i++) { BN_bntest_rand(a, 40 + i * 10, 0, 0); a->neg = rand_neg(); BN_sqr(c, a, ctx); if (bp != NULL) { if (!results) { BN_print(bp, a); BIO_puts(bp, " * "); BN_print(bp, a); BIO_puts(bp, " - "); } BN_print(bp, c); BIO_puts(bp, "\n"); } BN_div(d, e, c, a, ctx); BN_sub(d, d, a); if (!BN_is_zero(d) || !BN_is_zero(e)) { fprintf(stderr, "Square test failed!\n"); goto err; } } /* Regression test for a BN_sqr overflow bug. */ BN_hex2bn(&a, "80000000000000008000000000000001" "FFFFFFFFFFFFFFFE0000000000000000"); BN_sqr(c, a, ctx); if (bp != NULL) { if (!results) { BN_print(bp, a); BIO_puts(bp, " * "); BN_print(bp, a); BIO_puts(bp, " - "); } BN_print(bp, c); BIO_puts(bp, "\n"); } BN_mul(d, a, a, ctx); if (BN_cmp(c, d)) { fprintf(stderr, "Square test failed: BN_sqr and BN_mul produce " "different results!\n"); goto err; } /* Regression test for a BN_sqr overflow bug. */ BN_hex2bn(&a, "80000000000000000000000080000001" "FFFFFFFE000000000000000000000000"); BN_sqr(c, a, ctx); if (bp != NULL) { if (!results) { BN_print(bp, a); BIO_puts(bp, " * "); BN_print(bp, a); BIO_puts(bp, " - "); } BN_print(bp, c); BIO_puts(bp, "\n"); } BN_mul(d, a, a, ctx); if (BN_cmp(c, d)) { fprintf(stderr, "Square test failed: BN_sqr and BN_mul produce " "different results!\n"); goto err; } ret = 1; err: if (a != NULL) BN_free(a); if (c != NULL) BN_free(c); if (d != NULL) BN_free(d); if (e != NULL) BN_free(e); return ret; }
CWE-200
3,669
13,633
241854136749094213171920753463424039494
null
null
null
openssl
d73cc256c8e256c32ed959456101b73ba9842f72
0
int test_sub(BIO *bp) { BIGNUM a, b, c; int i; BN_init(&a); BN_init(&b); BN_init(&c); for (i = 0; i < num0 + num1; i++) { if (i < num1) { BN_bntest_rand(&a, 512, 0, 0); BN_copy(&b, &a); if (BN_set_bit(&a, i) == 0) return (0); BN_add_word(&b, i); } else { BN_bntest_rand(&b, 400 + i - num1, 0, 0); a.neg = rand_neg(); b.neg = rand_neg(); } BN_sub(&c, &a, &b); if (bp != NULL) { if (!results) { BN_print(bp, &a); BIO_puts(bp, " - "); BN_print(bp, &b); BIO_puts(bp, " - "); } BN_print(bp, &c); BIO_puts(bp, "\n"); } BN_add(&c, &c, &b); BN_sub(&c, &c, &a); if (!BN_is_zero(&c)) { fprintf(stderr, "Subtract test failed!\n"); return 0; } } BN_free(&a); BN_free(&b); BN_free(&c); return (1); }
CWE-200
3,671
13,634
119939730536742271227746968291502037880
null
null
null
dbus
954d75b2b64e4799f360d2a6bf9cff6d9fee37e7
0
_dbus_accept (int listen_fd) { int client_fd; struct sockaddr addr; socklen_t addrlen; #ifdef HAVE_ACCEPT4 dbus_bool_t cloexec_done; #endif addrlen = sizeof (addr); retry: #ifdef HAVE_ACCEPT4 /* We assume that if accept4 is available SOCK_CLOEXEC is too */ client_fd = accept4 (listen_fd, &addr, &addrlen, SOCK_CLOEXEC); cloexec_done = client_fd >= 0; if (client_fd < 0 && errno == ENOSYS) #endif { client_fd = accept (listen_fd, &addr, &addrlen); } if (client_fd < 0) { if (errno == EINTR) goto retry; } _dbus_verbose ("client fd %d accepted\n", client_fd); #ifdef HAVE_ACCEPT4 if (!cloexec_done) #endif { _dbus_fd_set_close_on_exec(client_fd); } return client_fd; }
CWE-20
3,709
13,635
18899235928474069872038742630388990737
null
null
null
dbus
954d75b2b64e4799f360d2a6bf9cff6d9fee37e7
0
_dbus_append_keyring_directory_for_credentials (DBusString *directory, DBusCredentials *credentials) { DBusString homedir; DBusString dotdir; dbus_uid_t uid; _dbus_assert (credentials != NULL); _dbus_assert (!_dbus_credentials_are_anonymous (credentials)); if (!_dbus_string_init (&homedir)) return FALSE; uid = _dbus_credentials_get_unix_uid (credentials); _dbus_assert (uid != DBUS_UID_UNSET); if (!_dbus_homedir_from_uid (uid, &homedir)) goto failed; #ifdef DBUS_BUILD_TESTS { const char *override; override = _dbus_getenv ("DBUS_TEST_HOMEDIR"); if (override != NULL && *override != '\0') { _dbus_string_set_length (&homedir, 0); if (!_dbus_string_append (&homedir, override)) goto failed; _dbus_verbose ("Using fake homedir for testing: %s\n", _dbus_string_get_const_data (&homedir)); } else { static dbus_bool_t already_warned = FALSE; if (!already_warned) { _dbus_warn ("Using your real home directory for testing, set DBUS_TEST_HOMEDIR to avoid\n"); already_warned = TRUE; } } } #endif _dbus_string_init_const (&dotdir, ".dbus-keyrings"); if (!_dbus_concat_dir_and_file (&homedir, &dotdir)) goto failed; if (!_dbus_string_copy (&homedir, 0, directory, _dbus_string_get_length (directory))) { goto failed; } _dbus_string_free (&homedir); return TRUE; failed: _dbus_string_free (&homedir); return FALSE; }
CWE-20
3,710
13,636
32377860682565321936815448019809203082
null
null
null
dbus
954d75b2b64e4799f360d2a6bf9cff6d9fee37e7
0
_dbus_append_session_config_file (DBusString *str) { return _dbus_string_append (str, DBUS_SESSION_CONFIG_FILE); }
CWE-20
3,711
13,637
162341769354939115647743806721034514085
null
null
null
dbus
954d75b2b64e4799f360d2a6bf9cff6d9fee37e7
0
_dbus_append_system_config_file (DBusString *str) { return _dbus_string_append (str, DBUS_SYSTEM_CONFIG_FILE); }
CWE-20
3,712
13,638
311112297029132336791177929654520972809
null
null
null
dbus
954d75b2b64e4799f360d2a6bf9cff6d9fee37e7
0
_dbus_append_user_from_current_process (DBusString *str) { return _dbus_string_append_uint (str, _dbus_geteuid ()); }
CWE-20
3,713
13,639
278278841111376750467612184809470373874
null
null
null
dbus
954d75b2b64e4799f360d2a6bf9cff6d9fee37e7
0
_dbus_atomic_dec (DBusAtomic *atomic) { #if DBUS_USE_SYNC return __sync_sub_and_fetch(&atomic->value, 1)+1; #else dbus_int32_t res; _DBUS_LOCK (atomic); res = atomic->value; atomic->value -= 1; _DBUS_UNLOCK (atomic); return res; #endif }
CWE-20
3,714
13,640
307055209791529880823801585347918473839
null
null
null
dbus
954d75b2b64e4799f360d2a6bf9cff6d9fee37e7
0
_dbus_atomic_get (DBusAtomic *atomic) { #if DBUS_USE_SYNC __sync_synchronize (); return atomic->value; #else dbus_int32_t res; _DBUS_LOCK (atomic); res = atomic->value; _DBUS_UNLOCK (atomic); return res; #endif }
CWE-20
3,715
13,641
33379528192673386325909415999582236458
null
null
null
dbus
954d75b2b64e4799f360d2a6bf9cff6d9fee37e7
0
_dbus_atomic_inc (DBusAtomic *atomic) { #if DBUS_USE_SYNC return __sync_add_and_fetch(&atomic->value, 1)-1; #else dbus_int32_t res; _DBUS_LOCK (atomic); res = atomic->value; atomic->value += 1; _DBUS_UNLOCK (atomic); return res; #endif }
CWE-20
3,716
13,642
158299350218027865037061043061804236704
null
null
null
dbus
954d75b2b64e4799f360d2a6bf9cff6d9fee37e7
0
_dbus_check_dir_is_private_to_user (DBusString *dir, DBusError *error) { const char *directory; struct stat sb; _DBUS_ASSERT_ERROR_IS_CLEAR (error); directory = _dbus_string_get_const_data (dir); if (stat (directory, &sb) < 0) { dbus_set_error (error, _dbus_error_from_errno (errno), "%s", _dbus_strerror (errno)); return FALSE; } if ((S_IROTH & sb.st_mode) || (S_IWOTH & sb.st_mode) || (S_IRGRP & sb.st_mode) || (S_IWGRP & sb.st_mode)) { dbus_set_error (error, DBUS_ERROR_FAILED, "%s directory is not private to the user", directory); return FALSE; } return TRUE; }
CWE-20
3,717
13,643
259878105064986931868870620001329963281
null
null
null
dbus
954d75b2b64e4799f360d2a6bf9cff6d9fee37e7
0
_dbus_close (int fd, DBusError *error) { _DBUS_ASSERT_ERROR_IS_CLEAR (error); again: if (close (fd) < 0) { if (errno == EINTR) goto again; dbus_set_error (error, _dbus_error_from_errno (errno), "Could not close fd %d", fd); return FALSE; } return TRUE; }
CWE-20
3,719
13,644
129262601366857142659030916061957251370
null
null
null
dbus
954d75b2b64e4799f360d2a6bf9cff6d9fee37e7
0
_dbus_close_all (void) { int maxfds, i; #ifdef __linux__ DIR *d; /* On Linux we can optimize this a bit if /proc is available. If it isn't available, fall back to the brute force way. */ d = opendir ("/proc/self/fd"); if (d) { for (;;) { struct dirent buf, *de; int k, fd; long l; char *e = NULL; k = readdir_r (d, &buf, &de); if (k != 0 || !de) break; if (de->d_name[0] == '.') continue; errno = 0; l = strtol (de->d_name, &e, 10); if (errno != 0 || e == NULL || *e != '\0') continue; fd = (int) l; if (fd < 3) continue; if (fd == dirfd (d)) continue; close (fd); } closedir (d); return; } #endif maxfds = sysconf (_SC_OPEN_MAX); /* Pick something reasonable if for some reason sysconf says * unlimited. */ if (maxfds < 0) maxfds = 1024; /* close all inherited fds */ for (i = 3; i < maxfds; i++) close (i); }
CWE-20
3,720
13,645
100869209603451168132330712754400439947
null
null
null
dbus
954d75b2b64e4799f360d2a6bf9cff6d9fee37e7
0
_dbus_close_socket (int fd, DBusError *error) { return _dbus_close (fd, error); }
CWE-20
3,721
13,646
269444556601129409105442600116120087784
null
null
null
dbus
954d75b2b64e4799f360d2a6bf9cff6d9fee37e7
0
_dbus_concat_dir_and_file (DBusString *dir, const DBusString *next_component) { dbus_bool_t dir_ends_in_slash; dbus_bool_t file_starts_with_slash; if (_dbus_string_get_length (dir) == 0 || _dbus_string_get_length (next_component) == 0) return TRUE; dir_ends_in_slash = '/' == _dbus_string_get_byte (dir, _dbus_string_get_length (dir) - 1); file_starts_with_slash = '/' == _dbus_string_get_byte (next_component, 0); if (dir_ends_in_slash && file_starts_with_slash) { _dbus_string_shorten (dir, 1); } else if (!(dir_ends_in_slash || file_starts_with_slash)) { if (!_dbus_string_append_byte (dir, '/')) return FALSE; } return _dbus_string_copy (next_component, 0, dir, _dbus_string_get_length (dir)); }
CWE-20
3,722
13,647
261311066739557863727049695248492119140
null
null
null
dbus
954d75b2b64e4799f360d2a6bf9cff6d9fee37e7
0
_dbus_connect_exec (const char *path, char *const argv[], DBusError *error) { int fds[2]; pid_t pid; _DBUS_ASSERT_ERROR_IS_CLEAR (error); _dbus_verbose ("connecting to process %s\n", path); if (socketpair (AF_UNIX, SOCK_STREAM #ifdef SOCK_CLOEXEC |SOCK_CLOEXEC #endif , 0, fds) < 0) { dbus_set_error (error, _dbus_error_from_errno (errno), "Failed to create socket pair: %s", _dbus_strerror (errno)); return -1; } _dbus_fd_set_close_on_exec (fds[0]); _dbus_fd_set_close_on_exec (fds[1]); pid = fork (); if (pid < 0) { dbus_set_error (error, _dbus_error_from_errno (errno), "Failed to fork() to call %s: %s", path, _dbus_strerror (errno)); close (fds[0]); close (fds[1]); return -1; } if (pid == 0) { /* child */ close (fds[0]); dup2 (fds[1], STDIN_FILENO); dup2 (fds[1], STDOUT_FILENO); if (fds[1] != STDIN_FILENO && fds[1] != STDOUT_FILENO) close (fds[1]); /* Inherit STDERR and the controlling terminal from the parent */ _dbus_close_all (); execvp (path, argv); fprintf (stderr, "Failed to execute process %s: %s\n", path, _dbus_strerror (errno)); _exit(1); } /* parent */ close (fds[1]); if (!_dbus_set_fd_nonblocking (fds[0], error)) { _DBUS_ASSERT_ERROR_IS_SET (error); close (fds[0]); return -1; } return fds[0]; }
CWE-20
3,723
13,648
128963779985617435172263665282451412956
null
null
null
dbus
954d75b2b64e4799f360d2a6bf9cff6d9fee37e7
0
_dbus_connect_tcp_socket (const char *host, const char *port, const char *family, DBusError *error) { return _dbus_connect_tcp_socket_with_nonce (host, port, family, (const char*)NULL, error); }
CWE-20
3,724
13,649
17585082648457541700040261350325362406
null
null
null
dbus
954d75b2b64e4799f360d2a6bf9cff6d9fee37e7
0
_dbus_connect_tcp_socket_with_nonce (const char *host, const char *port, const char *family, const char *noncefile, DBusError *error) { int saved_errno = 0; int fd = -1, res; struct addrinfo hints; struct addrinfo *ai, *tmp; _DBUS_ASSERT_ERROR_IS_CLEAR(error); _DBUS_ZERO (hints); if (!family) hints.ai_family = AF_UNSPEC; else if (!strcmp(family, "ipv4")) hints.ai_family = AF_INET; else if (!strcmp(family, "ipv6")) hints.ai_family = AF_INET6; else { dbus_set_error (error, DBUS_ERROR_BAD_ADDRESS, "Unknown address family %s", family); return -1; } hints.ai_protocol = IPPROTO_TCP; hints.ai_socktype = SOCK_STREAM; hints.ai_flags = AI_ADDRCONFIG; if ((res = getaddrinfo(host, port, &hints, &ai)) != 0) { dbus_set_error (error, _dbus_error_from_errno (errno), "Failed to lookup host/port: \"%s:%s\": %s (%d)", host, port, gai_strerror(res), res); return -1; } tmp = ai; while (tmp) { if (!_dbus_open_socket (&fd, tmp->ai_family, SOCK_STREAM, 0, error)) { freeaddrinfo(ai); _DBUS_ASSERT_ERROR_IS_SET(error); return -1; } _DBUS_ASSERT_ERROR_IS_CLEAR(error); if (connect (fd, (struct sockaddr*) tmp->ai_addr, tmp->ai_addrlen) < 0) { saved_errno = errno; _dbus_close(fd, NULL); fd = -1; tmp = tmp->ai_next; continue; } break; } freeaddrinfo(ai); if (fd == -1) { dbus_set_error (error, _dbus_error_from_errno (saved_errno), "Failed to connect to socket \"%s:%s\" %s", host, port, _dbus_strerror(saved_errno)); return -1; } if (noncefile != NULL) { DBusString noncefileStr; dbus_bool_t ret; _dbus_string_init_const (&noncefileStr, noncefile); ret = _dbus_send_nonce (fd, &noncefileStr, error); _dbus_string_free (&noncefileStr); if (!ret) { _dbus_close (fd, NULL); return -1; } } if (!_dbus_set_fd_nonblocking (fd, error)) { _dbus_close (fd, NULL); return -1; } return fd; }
CWE-20
3,725
13,650
257402961365799322780560746599216669886
null
null
null
dbus
954d75b2b64e4799f360d2a6bf9cff6d9fee37e7
0
_dbus_connect_unix_socket (const char *path, dbus_bool_t abstract, DBusError *error) { int fd; size_t path_len; struct sockaddr_un addr; _DBUS_ASSERT_ERROR_IS_CLEAR (error); _dbus_verbose ("connecting to unix socket %s abstract=%d\n", path, abstract); if (!_dbus_open_unix_socket (&fd, error)) { _DBUS_ASSERT_ERROR_IS_SET(error); return -1; } _DBUS_ASSERT_ERROR_IS_CLEAR(error); _DBUS_ZERO (addr); addr.sun_family = AF_UNIX; path_len = strlen (path); if (abstract) { #ifdef HAVE_ABSTRACT_SOCKETS addr.sun_path[0] = '\0'; /* this is what says "use abstract" */ path_len++; /* Account for the extra nul byte added to the start of sun_path */ if (path_len > _DBUS_MAX_SUN_PATH_LENGTH) { dbus_set_error (error, DBUS_ERROR_BAD_ADDRESS, "Abstract socket name too long\n"); _dbus_close (fd, NULL); return -1; } strncpy (&addr.sun_path[1], path, path_len); /* _dbus_verbose_bytes (addr.sun_path, sizeof (addr.sun_path)); */ #else /* HAVE_ABSTRACT_SOCKETS */ dbus_set_error (error, DBUS_ERROR_NOT_SUPPORTED, "Operating system does not support abstract socket namespace\n"); _dbus_close (fd, NULL); return -1; #endif /* ! HAVE_ABSTRACT_SOCKETS */ } else { if (path_len > _DBUS_MAX_SUN_PATH_LENGTH) { dbus_set_error (error, DBUS_ERROR_BAD_ADDRESS, "Socket name too long\n"); _dbus_close (fd, NULL); return -1; } strncpy (addr.sun_path, path, path_len); } if (connect (fd, (struct sockaddr*) &addr, _DBUS_STRUCT_OFFSET (struct sockaddr_un, sun_path) + path_len) < 0) { dbus_set_error (error, _dbus_error_from_errno (errno), "Failed to connect to socket %s: %s", path, _dbus_strerror (errno)); _dbus_close (fd, NULL); return -1; } if (!_dbus_set_fd_nonblocking (fd, error)) { _DBUS_ASSERT_ERROR_IS_SET (error); _dbus_close (fd, NULL); return -1; } return fd; }
CWE-20
3,726
13,651
97295288219415105533556541478376907104
null
null
null
dbus
954d75b2b64e4799f360d2a6bf9cff6d9fee37e7
0
_dbus_create_directory (const DBusString *filename, DBusError *error) { const char *filename_c; _DBUS_ASSERT_ERROR_IS_CLEAR (error); filename_c = _dbus_string_get_const_data (filename); if (mkdir (filename_c, 0700) < 0) { if (errno == EEXIST) return TRUE; dbus_set_error (error, DBUS_ERROR_FAILED, "Failed to create directory %s: %s\n", filename_c, _dbus_strerror (errno)); return FALSE; } else return TRUE; }
CWE-20
3,727
13,652
321471199067481309035865359504960864896
null
null
null
dbus
954d75b2b64e4799f360d2a6bf9cff6d9fee37e7
0
_dbus_credentials_add_from_current_process (DBusCredentials *credentials) { /* The POSIX spec certainly doesn't promise this, but * we need these assertions to fail as soon as we're wrong about * it so we can do the porting fixups */ _dbus_assert (sizeof (pid_t) <= sizeof (dbus_pid_t)); _dbus_assert (sizeof (uid_t) <= sizeof (dbus_uid_t)); _dbus_assert (sizeof (gid_t) <= sizeof (dbus_gid_t)); if (!_dbus_credentials_add_unix_pid(credentials, _dbus_getpid())) return FALSE; if (!_dbus_credentials_add_unix_uid(credentials, _dbus_geteuid())) return FALSE; return TRUE; }
CWE-20
3,728
13,653
66610718775351489964694549018038515661
null
null
null
dbus
954d75b2b64e4799f360d2a6bf9cff6d9fee37e7
0
_dbus_daemon_publish_session_bus_address (const char* addr, const char *scope) { return TRUE; }
CWE-20
3,729
13,654
319377010490281029352804229829783927940
null
null
null
dbus
954d75b2b64e4799f360d2a6bf9cff6d9fee37e7
0
_dbus_daemon_unpublish_session_bus_address (void) { }
CWE-20
3,730
13,655
298494947409084859721316831455310354643
null
null
null
dbus
954d75b2b64e4799f360d2a6bf9cff6d9fee37e7
0
_dbus_delete_directory (const DBusString *filename, DBusError *error) { const char *filename_c; _DBUS_ASSERT_ERROR_IS_CLEAR (error); filename_c = _dbus_string_get_const_data (filename); if (rmdir (filename_c) != 0) { dbus_set_error (error, DBUS_ERROR_FAILED, "Failed to remove directory %s: %s\n", filename_c, _dbus_strerror (errno)); return FALSE; } return TRUE; }
CWE-20
3,731
13,656
257138863691393272657513147026174753183
null
null
null
dbus
954d75b2b64e4799f360d2a6bf9cff6d9fee37e7
0
_dbus_disable_sigpipe (void) { signal (SIGPIPE, SIG_IGN); }
CWE-20
3,732
13,657
229480251533666982728979556933728034841
null
null
null
dbus
954d75b2b64e4799f360d2a6bf9cff6d9fee37e7
0
_dbus_dup(int fd, DBusError *error) { int new_fd; #ifdef F_DUPFD_CLOEXEC dbus_bool_t cloexec_done; new_fd = fcntl(fd, F_DUPFD_CLOEXEC, 3); cloexec_done = new_fd >= 0; if (new_fd < 0 && errno == EINVAL) #endif { new_fd = fcntl(fd, F_DUPFD, 3); } if (new_fd < 0) { dbus_set_error (error, _dbus_error_from_errno (errno), "Could not duplicate fd %d", fd); return -1; } #ifdef F_DUPFD_CLOEXEC if (!cloexec_done) #endif { _dbus_fd_set_close_on_exec(new_fd); } return new_fd; }
CWE-20
3,733
13,658
12040075064447767973568541492673393585
null
null
null
dbus
954d75b2b64e4799f360d2a6bf9cff6d9fee37e7
0
_dbus_fd_set_close_on_exec (intptr_t fd) { int val; val = fcntl (fd, F_GETFD, 0); if (val < 0) return; val |= FD_CLOEXEC; fcntl (fd, F_SETFD, val); }
CWE-20
3,735
13,659
291365032075561940727522341841362679684
null
null
null
dbus
954d75b2b64e4799f360d2a6bf9cff6d9fee37e7
0
_dbus_flush_caches (void) { _dbus_user_database_flush_system (); }
CWE-20
3,736
13,660
143954947249955830413119239168751484466
null
null
null
dbus
954d75b2b64e4799f360d2a6bf9cff6d9fee37e7
0
_dbus_full_duplex_pipe (int *fd1, int *fd2, dbus_bool_t blocking, DBusError *error) { #ifdef HAVE_SOCKETPAIR int fds[2]; int retval; #ifdef SOCK_CLOEXEC dbus_bool_t cloexec_done; retval = socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0, fds); cloexec_done = retval >= 0; if (retval < 0 && errno == EINVAL) #endif { retval = socketpair(AF_UNIX, SOCK_STREAM, 0, fds); } if (retval < 0) { dbus_set_error (error, _dbus_error_from_errno (errno), "Could not create full-duplex pipe"); return FALSE; } _DBUS_ASSERT_ERROR_IS_CLEAR (error); #ifdef SOCK_CLOEXEC if (!cloexec_done) #endif { _dbus_fd_set_close_on_exec (fds[0]); _dbus_fd_set_close_on_exec (fds[1]); } if (!blocking && (!_dbus_set_fd_nonblocking (fds[0], NULL) || !_dbus_set_fd_nonblocking (fds[1], NULL))) { dbus_set_error (error, _dbus_error_from_errno (errno), "Could not set full-duplex pipe nonblocking"); _dbus_close (fds[0], NULL); _dbus_close (fds[1], NULL); return FALSE; } *fd1 = fds[0]; *fd2 = fds[1]; _dbus_verbose ("full-duplex pipe %d <-> %d\n", *fd1, *fd2); return TRUE; #else _dbus_warn ("_dbus_full_duplex_pipe() not implemented on this OS\n"); dbus_set_error (error, DBUS_ERROR_FAILED, "_dbus_full_duplex_pipe() not implemented on this OS"); return FALSE; #endif }
CWE-20
3,737
13,661
212533615345964395577394357097928436351
null
null
null
dbus
954d75b2b64e4799f360d2a6bf9cff6d9fee37e7
0
_dbus_generate_pseudorandom_bytes (DBusString *str, int n_bytes) { int old_len; char *p; old_len = _dbus_string_get_length (str); if (!_dbus_string_lengthen (str, n_bytes)) return FALSE; p = _dbus_string_get_data_len (str, old_len, n_bytes); _dbus_generate_pseudorandom_bytes_buffer (p, n_bytes); return TRUE; }
CWE-20
3,738
13,662
276347025044490189519205844474604763395
null
null
null
dbus
954d75b2b64e4799f360d2a6bf9cff6d9fee37e7
0
_dbus_generate_random_bytes (DBusString *str, int n_bytes) { int old_len; int fd; /* FALSE return means "no memory", if it could * mean something else then we'd need to return * a DBusError. So we always fall back to pseudorandom * if the I/O fails. */ old_len = _dbus_string_get_length (str); fd = -1; /* note, urandom on linux will fall back to pseudorandom */ fd = open ("/dev/urandom", O_RDONLY); if (fd < 0) return _dbus_generate_pseudorandom_bytes (str, n_bytes); _dbus_verbose ("/dev/urandom fd %d opened\n", fd); if (_dbus_read (fd, str, n_bytes) != n_bytes) { _dbus_close (fd, NULL); _dbus_string_set_length (str, old_len); return _dbus_generate_pseudorandom_bytes (str, n_bytes); } _dbus_verbose ("Read %d bytes from /dev/urandom\n", n_bytes); _dbus_close (fd, NULL); return TRUE; }
CWE-20
3,739
13,663
101699243844237157204720209029277748813
null
null
null