📄 s3_lib.c
字号:
switch (cmd) {#ifndef OPENSSL_NO_RSA case SSL_CTRL_SET_TMP_RSA_CB: { s->cert->rsa_tmp_cb = (RSA *(*)(SSL *, int, int))fp; } break;#endif#ifndef OPENSSL_NO_DH case SSL_CTRL_SET_TMP_DH_CB: { s->cert->dh_tmp_cb = (DH *(*)(SSL *, int, int))fp; } break;#endif#ifndef OPENSSL_NO_ECDH case SSL_CTRL_SET_TMP_ECDH_CB: { s->cert->ecdh_tmp_cb = (EC_KEY *(*)(SSL *, int, int))fp; } break;#endif default: break; } return(ret); }long ssl3_ctx_ctrl(SSL_CTX *ctx, int cmd, long larg, void *parg) { CERT *cert; cert=ctx->cert; switch (cmd) {#ifndef OPENSSL_NO_RSA case SSL_CTRL_NEED_TMP_RSA: if ( (cert->rsa_tmp == NULL) && ((cert->pkeys[SSL_PKEY_RSA_ENC].privatekey == NULL) || (EVP_PKEY_size(cert->pkeys[SSL_PKEY_RSA_ENC].privatekey) > (512/8))) ) return(1); else return(0); /* break; */ case SSL_CTRL_SET_TMP_RSA: { RSA *rsa; int i; rsa=(RSA *)parg; i=1; if (rsa == NULL) i=0; else { if ((rsa=RSAPrivateKey_dup(rsa)) == NULL) i=0; } if (!i) { SSLerr(SSL_F_SSL3_CTX_CTRL,ERR_R_RSA_LIB); return(0); } else { if (cert->rsa_tmp != NULL) RSA_free(cert->rsa_tmp); cert->rsa_tmp=rsa; return(1); } } /* break; */ case SSL_CTRL_SET_TMP_RSA_CB: { SSLerr(SSL_F_SSL3_CTX_CTRL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); return(0); } break;#endif#ifndef OPENSSL_NO_DH case SSL_CTRL_SET_TMP_DH: { DH *new=NULL,*dh; dh=(DH *)parg; if ((new=DHparams_dup(dh)) == NULL) { SSLerr(SSL_F_SSL3_CTX_CTRL,ERR_R_DH_LIB); return 0; } if (!(ctx->options & SSL_OP_SINGLE_DH_USE)) { if (!DH_generate_key(new)) { SSLerr(SSL_F_SSL3_CTX_CTRL,ERR_R_DH_LIB); DH_free(new); return 0; } } if (cert->dh_tmp != NULL) DH_free(cert->dh_tmp); cert->dh_tmp=new; return 1; } /*break; */ case SSL_CTRL_SET_TMP_DH_CB: { SSLerr(SSL_F_SSL3_CTX_CTRL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); return(0); } break;#endif#ifndef OPENSSL_NO_ECDH case SSL_CTRL_SET_TMP_ECDH: { EC_KEY *ecdh = NULL; if (parg == NULL) { SSLerr(SSL_F_SSL3_CTX_CTRL,ERR_R_ECDH_LIB); return 0; } ecdh = EC_KEY_dup((EC_KEY *)parg); if (ecdh == NULL) { SSLerr(SSL_F_SSL3_CTX_CTRL,ERR_R_EC_LIB); return 0; } if (!(ctx->options & SSL_OP_SINGLE_ECDH_USE)) { if (!EC_KEY_generate_key(ecdh)) { EC_KEY_free(ecdh); SSLerr(SSL_F_SSL3_CTX_CTRL,ERR_R_ECDH_LIB); return 0; } } if (cert->ecdh_tmp != NULL) { EC_KEY_free(cert->ecdh_tmp); } cert->ecdh_tmp = ecdh; return 1; } /* break; */ case SSL_CTRL_SET_TMP_ECDH_CB: { SSLerr(SSL_F_SSL3_CTX_CTRL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); return(0); } break;#endif /* !OPENSSL_NO_ECDH */ /* A Thawte special :-) */ case SSL_CTRL_EXTRA_CHAIN_CERT: if (ctx->extra_certs == NULL) { if ((ctx->extra_certs=sk_X509_new_null()) == NULL) return(0); } sk_X509_push(ctx->extra_certs,(X509 *)parg); break; default: return(0); } return(1); }long ssl3_ctx_callback_ctrl(SSL_CTX *ctx, int cmd, void (*fp)(void)) { CERT *cert; cert=ctx->cert; switch (cmd) {#ifndef OPENSSL_NO_RSA case SSL_CTRL_SET_TMP_RSA_CB: { cert->rsa_tmp_cb = (RSA *(*)(SSL *, int, int))fp; } break;#endif#ifndef OPENSSL_NO_DH case SSL_CTRL_SET_TMP_DH_CB: { cert->dh_tmp_cb = (DH *(*)(SSL *, int, int))fp; } break;#endif#ifndef OPENSSL_NO_ECDH case SSL_CTRL_SET_TMP_ECDH_CB: { cert->ecdh_tmp_cb = (EC_KEY *(*)(SSL *, int, int))fp; } break;#endif default: return(0); } return(1); }/* This function needs to check if the ciphers required are actually * available */SSL_CIPHER *ssl3_get_cipher_by_char(const unsigned char *p) { SSL_CIPHER c,*cp; unsigned long id; id=0x03000000L|((unsigned long)p[0]<<8L)|(unsigned long)p[1]; c.id=id; cp = (SSL_CIPHER *)OBJ_bsearch((char *)&c, (char *)ssl3_ciphers, SSL3_NUM_CIPHERS,sizeof(SSL_CIPHER), FP_ICC ssl_cipher_id_cmp); if (cp == NULL || cp->valid == 0) return NULL; else return cp; }int ssl3_put_cipher_by_char(const SSL_CIPHER *c, unsigned char *p) { long l; if (p != NULL) { l=c->id; if ((l & 0xff000000) != 0x03000000) return(0); p[0]=((unsigned char)(l>> 8L))&0xFF; p[1]=((unsigned char)(l ))&0xFF; } return(2); }SSL_CIPHER *ssl3_choose_cipher(SSL *s, STACK_OF(SSL_CIPHER) *clnt, STACK_OF(SSL_CIPHER) *srvr) { SSL_CIPHER *c,*ret=NULL; STACK_OF(SSL_CIPHER) *prio, *allow; int i,j,ok; CERT *cert; unsigned long alg,mask,emask; /* Let's see which ciphers we can support */ cert=s->cert;#if 0 /* Do not set the compare functions, because this may lead to a * reordering by "id". We want to keep the original ordering. * We may pay a price in performance during sk_SSL_CIPHER_find(), * but would have to pay with the price of sk_SSL_CIPHER_dup(). */ sk_SSL_CIPHER_set_cmp_func(srvr, ssl_cipher_ptr_id_cmp); sk_SSL_CIPHER_set_cmp_func(clnt, ssl_cipher_ptr_id_cmp);#endif#ifdef CIPHER_DEBUG printf("Server has %d from %p:\n", sk_SSL_CIPHER_num(srvr), srvr); for(i=0 ; i < sk_SSL_CIPHER_num(srvr) ; ++i) { c=sk_SSL_CIPHER_value(srvr,i); printf("%p:%s\n",c,c->name); } printf("Client sent %d from %p:\n", sk_SSL_CIPHER_num(clnt), clnt); for(i=0 ; i < sk_SSL_CIPHER_num(clnt) ; ++i) { c=sk_SSL_CIPHER_value(clnt,i); printf("%p:%s\n",c,c->name); }#endif if (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE) { prio = srvr; allow = clnt; } else { prio = clnt; allow = srvr; } for (i=0; i<sk_SSL_CIPHER_num(prio); i++) { c=sk_SSL_CIPHER_value(prio,i); ssl_set_cert_masks(cert,c); mask=cert->mask; emask=cert->export_mask; #ifdef KSSL_DEBUG printf("ssl3_choose_cipher %d alg= %lx\n", i,c->algorithms);#endif /* KSSL_DEBUG */ alg=c->algorithms&(SSL_MKEY_MASK|SSL_AUTH_MASK);#ifndef OPENSSL_NO_KRB5 if (alg & SSL_KRB5) { if ( !kssl_keytab_is_available(s->kssl_ctx) ) continue; }#endif /* OPENSSL_NO_KRB5 */ if (SSL_C_IS_EXPORT(c)) { ok=((alg & emask) == alg)?1:0;#ifdef CIPHER_DEBUG printf("%d:[%08lX:%08lX]%p:%s (export)\n",ok,alg,emask, c,c->name);#endif } else { ok=((alg & mask) == alg)?1:0;#ifdef CIPHER_DEBUG printf("%d:[%08lX:%08lX]%p:%s\n",ok,alg,mask,c, c->name);#endif } if (!ok) continue; j=sk_SSL_CIPHER_find(allow,c); if (j >= 0) { ret=sk_SSL_CIPHER_value(allow,j); break; } } return(ret); }int ssl3_get_req_cert_type(SSL *s, unsigned char *p) { int ret=0; unsigned long alg; alg=s->s3->tmp.new_cipher->algorithms;#ifndef OPENSSL_NO_DH if (alg & (SSL_kDHr|SSL_kEDH)) {# ifndef OPENSSL_NO_RSA p[ret++]=SSL3_CT_RSA_FIXED_DH;# endif# ifndef OPENSSL_NO_DSA p[ret++]=SSL3_CT_DSS_FIXED_DH;# endif } if ((s->version == SSL3_VERSION) && (alg & (SSL_kEDH|SSL_kDHd|SSL_kDHr))) {# ifndef OPENSSL_NO_RSA p[ret++]=SSL3_CT_RSA_EPHEMERAL_DH;# endif# ifndef OPENSSL_NO_DSA p[ret++]=SSL3_CT_DSS_EPHEMERAL_DH;# endif }#endif /* !OPENSSL_NO_DH */#ifndef OPENSSL_NO_RSA p[ret++]=SSL3_CT_RSA_SIGN;#endif#ifndef OPENSSL_NO_DSA p[ret++]=SSL3_CT_DSS_SIGN;#endif#ifndef OPENSSL_NO_ECDH /* We should ask for fixed ECDH certificates only * for SSL_kECDH (and not SSL_kECDHE) */ if ((alg & SSL_kECDH) && (s->version >= TLS1_VERSION)) { p[ret++]=TLS_CT_RSA_FIXED_ECDH; p[ret++]=TLS_CT_ECDSA_FIXED_ECDH; }#endif#ifndef OPENSSL_NO_ECDSA /* ECDSA certs can be used with RSA cipher suites as well * so we don't need to check for SSL_kECDH or SSL_kECDHE */ if (s->version >= TLS1_VERSION) { p[ret++]=TLS_CT_ECDSA_SIGN; }#endif return(ret); }int ssl3_shutdown(SSL *s) { /* Don't do anything much if we have not done the handshake or * we don't want to send messages :-) */ if ((s->quiet_shutdown) || (s->state == SSL_ST_BEFORE)) { s->shutdown=(SSL_SENT_SHUTDOWN|SSL_RECEIVED_SHUTDOWN); return(1); } if (!(s->shutdown & SSL_SENT_SHUTDOWN)) { s->shutdown|=SSL_SENT_SHUTDOWN;#if 1 ssl3_send_alert(s,SSL3_AL_WARNING,SSL_AD_CLOSE_NOTIFY);#endif /* our shutdown alert has been sent now, and if it still needs * to be written, s->s3->alert_dispatch will be true */ } else if (s->s3->alert_dispatch) { /* resend it if not sent */#if 1 s->method->ssl_dispatch_alert(s);#endif } else if (!(s->shutdown & SSL_RECEIVED_SHUTDOWN)) { /* If we are waiting for a close from our peer, we are closed */ s->method->ssl_read_bytes(s,0,NULL,0,0); } if ((s->shutdown == (SSL_SENT_SHUTDOWN|SSL_RECEIVED_SHUTDOWN)) && !s->s3->alert_dispatch) return(1); else return(0); }int ssl3_write(SSL *s, const void *buf, int len) { int ret,n;#if 0 if (s->shutdown & SSL_SEND_SHUTDOWN) { s->rwstate=SSL_NOTHING; return(0); }#endif clear_sys_error(); if (s->s3->renegotiate) ssl3_renegotiate_check(s); /* This is an experimental flag that sends the * last handshake message in the same packet as the first * use data - used to see if it helps the TCP protocol during * session-id reuse */ /* The second test is because the buffer may have been removed */ if ((s->s3->flags & SSL3_FLAGS_POP_BUFFER) && (s->wbio == s->bbio)) { /* First time through, we write into the buffer */ if (s->s3->delay_buf_pop_ret == 0) { ret=ssl3_write_bytes(s,SSL3_RT_APPLICATION_DATA, buf,len); if (ret <= 0) return(ret); s->s3->delay_buf_pop_ret=ret; } s->rwstate=SSL_WRITING; n=BIO_flush(s->wbio); if (n <= 0) return(n); s->rwstate=SSL_NOTHING; /* We have flushed the buffer, so remove it */ ssl_free_wbio_buffer(s); s->s3->flags&= ~SSL3_FLAGS_POP_BUFFER; ret=s->s3->delay_buf_pop_ret; s->s3->delay_buf_pop_ret=0; } else { ret=s->method->ssl_write_bytes(s,SSL3_RT_APPLICATION_DATA, buf,len); if (ret <= 0) return(ret); } return(ret); }static int ssl3_read_internal(SSL *s, void *buf, int len, int peek) { int ret; clear_sys_error(); if (s->s3->renegotiate) ssl3_renegotiate_check(s); s->s3->in_read_app_data=1; ret=s->method->ssl_read_bytes(s,SSL3_RT_APPLICATION_DATA,buf,len,peek); if ((ret == -1) && (s->s3->in_read_app_data == 2)) { /* ssl3_read_bytes decided to call s->handshake_func, which * called ssl3_read_bytes to read handshake data. * However, ssl3_read_bytes actually found application data * and thinks that application data makes sense here; so disable * handshake processing and try to read application data again. */ s->in_handshake++; ret=s->method->ssl_read_bytes(s,SSL3_RT_APPLICATION_DATA,buf,len,peek); s->in_handshake--; } else s->s3->in_read_app_data=0; return(ret); }int ssl3_read(SSL *s, void *buf, int len) { return ssl3_read_internal(s, buf, len, 0); }int ssl3_peek(SSL *s, void *buf, int len) { return ssl3_read_internal(s, buf, len, 1); }int ssl3_renegotiate(SSL *s) { if (s->handshake_func == NULL) return(1); if (s->s3->flags & SSL3_FLAGS_NO_RENEGOTIATE_CIPHERS) return(0); s->s3->renegotiate=1; return(1); }int ssl3_renegotiate_check(SSL *s) { int ret=0; if (s->s3->renegotiate) { if ( (s->s3->rbuf.left == 0) && (s->s3->wbuf.left == 0) && !SSL_in_init(s)) {/*if we are the server, and we have sent a 'RENEGOTIATE' message, weneed to go to SSL_ST_ACCEPT.*/ /* SSL_ST_ACCEPT */ s->state=SSL_ST_RENEGOTIATE; s->s3->renegotiate=0; s->s3->num_renegotiations++; s->s3->total_renegotiations++; ret=1; } } return(ret); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -