📄 xyssl - client source code.htm
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3c.org/TR/1999/REC-html401-19991224/loose.dtd">
<!-- saved from url=(0037)http://xyssl.org/code/source/ssl_cli/ -->
<HTML><HEAD><TITLE>XySSL - SSL/TLS Client source code</TITLE>
<META http-equiv=Content-Type content="text/html; charset=iso-8859-1">
<META http-equiv=Content-Script-Type content=text/javascript><LINK
href="XySSL - Client source code_files/style5.css" type=text/css
rel=stylesheet><LINK title="XySSL RSS feed" href="../../../rss.xml"
type=application/rss+xml rel=alternate><LINK href="../../../favicon.ico"
type=image/x-icon rel="shortcut icon"><!--[if IE]>
<STYLE type=text/css>#page {
}
</STYLE>
<![endif]-->
<META content="MSHTML 6.00.3790.2954" name=GENERATOR></HEAD>
<BODY text=#000000 bgColor=#ffffff>
<SCRIPT type=text/javascript></SCRIPT>
<DIV id=page>
<CENTER>
<TABLE cellSpacing=1 cellPadding=1 border=0>
<TBODY>
<TR>
<TD align=right><A href="http://xyssl.org/"><IMG height=70 alt=" XySSL "
src="XySSL - Client source code_files/logo.gif" width=205 border=0>
</A></TD>
<TD align=middle>
<DIV id=menutop>
<CENTER>
<TABLE cellSpacing=12 cellPadding=12 border=0>
<TBODY>
<TR align=middle>
<TD onmouseover="this.bgColor='#ffe7d6';"
onmouseout="this.bgColor='#f0f0f0';" bgColor=#f0f0f0><A
href="http://xyssl.org/code/">Source code</A> </TD>
<TD onmouseover="this.bgColor='#d6ffc7';"
onmouseout="this.bgColor='#f0f0f0';" bgColor=#f0f0f0><A
href="http://xyssl.org/docs/">Documentation</A> </TD>
<TD onmouseover="this.bgColor='#defbff';"
onmouseout="this.bgColor='#f0f0f0';" bgColor=#f0f0f0><A
href="http://xyssl.org/forum/">Community</A> </TD>
<TD onmouseover="this.bgColor='#fff0b0';"
onmouseout="this.bgColor='#f0f0f0';" bgColor=#f0f0f0><A
href="http://xyssl.org/about/"> About </A>
</TD></TR></TBODY></TABLE></CENTER></DIV></TD></TR>
<TR>
<TD class=menudiv align=right colSpan=2><!-- <A href="../../../enroll/">XySSL Root CA</A> <A HREF="https://xyssl.org/code/source/ssl_cli/"> <IMG alt="SSL" border=0 src="../../../static/ssl.gif" style="vertical-align: middle"></A> --></TD></TR>
<TR align=middle>
<TD class=pagehead colSpan=2><BR> <SPAN
class=underline> SSL/TLS Client source code </SPAN> <BR>
</TD></TR>
<TR align=left>
<TD class=pagebody colSpan=2>
<P>
<CENTER><B><A href="http://xyssl.org/code/source/md2/">MD2</A>.<A
href="http://xyssl.org/code/source/md4/">MD4</A>.<A
href="http://xyssl.org/code/source/md5/">MD5</A> | <A
href="http://xyssl.org/code/source/sha1/">SHA-1</A>.<A
href="http://xyssl.org/code/source/sha2/">SHA-256</A> | <A
href="http://xyssl.org/code/source/arc4/">ARC4</A>.<A
href="http://xyssl.org/code/source/des/">3DES</A>.<A
href="http://xyssl.org/code/source/aes/">AES</A> | <A
href="http://xyssl.org/code/source/bignum/">BN</A>.<A
href="http://xyssl.org/code/source/dhm/">DH</A>.<A
href="http://xyssl.org/code/source/rsa/">RSA</A> | <A
href="http://xyssl.org/code/source/x509read/">X509</A>.<A
href="http://xyssl.org/code/source/ssl_tls/">SSL/TLS</A>.<A
href="http://xyssl.org/code/source/ssl_cli/"><U>CLI</U></A>.<A
href="http://xyssl.org/code/source/ssl_srv/">SRV</A>
</B></CENTER><BR>
<TABLE cellSpacing=2 cellPadding=4 border=0>
<TBODY>
<TR>
<TD class=general style="FONT-SIZE: 100%" align=middle>Quickjump to:
<A
href="http://xyssl.org/code/source/ssl_cli/ssl_cli.c"><B>ssl_cli.c</B></A>
</TD></TR>
<TR>
<TD> </TD></TR>
<TR>
<TD class=general align=middle><A
href="http://xyssl.org/code/source/ssl_cli/ssl_cli.c"><B>C source
file: ssl_cli.c</B></A> </TD></TR>
<TR>
<TD class=general><PRE>/*
* SSLv3/TLSv1 client-side functions
*
* Copyright (C) 2006-2007 Christophe Devine
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License, version 2.1 as published by the Free Software Foundation.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA
*/
#ifndef _CRT_SECURE_NO_DEPRECATE
#define _CRT_SECURE_NO_DEPRECATE 1
#endif
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include "xyssl/ssl.h"
static int ssl_write_client_hello( ssl_context *ssl )
{
int i, n;
time_t t;
unsigned char *buf, *p;
md5_starts( &ssl->hs_md5 );
sha1_starts( &ssl->hs_sha1 );
ssl->major_ver = SSLV3_MAJOR_VERSION;
ssl->minor_ver = SSLV3_MINOR_VERSION;
/*
* 0 . 0 handshake type
* 1 . 3 handshake length
* 4 . 5 highest version supported
* 6 . 9 current UNIX time
* 10 . 37 random bytes
*/
buf = ssl->out_msg;
p = buf + 4;
*p++ = ssl->max_ver[0] = SSLV3_MAJOR_VERSION;
*p++ = ssl->max_ver[1] = TLS10_MINOR_VERSION;
t = time( NULL );
*p++ = (unsigned char)( t >> 24 );
*p++ = (unsigned char)( t >> 16 );
*p++ = (unsigned char)( t >> 8 );
*p++ = (unsigned char)( t );
for( i = 28; i > 0; i-- )
*p++ = ssl->rng_f( ssl->rng_d );
memcpy( ssl->randbytes, buf + 6, 32 );
/*
* 38 . 38 session id length
* 39 . 39+n session id
* 40+n . 41+n cipherlist length
* 42+n . .. cipherlist
* .. . .. compression alg. (0)
*/
n = ( ssl->sidlen > 0 && ssl->sidlen <= 32 )
? ssl->sidlen : 0;
*p++ = n;
for( i = 0; i < n; i++ )
*p++ = ssl->sessid[i];
for( n = 0; ssl->cipherlist[n] != 0; n++ );
*p++ = ( n >> 7 );
*p++ = ( n << 1 );
for( i = 0; i < n; i++ )
{
*p++ = ( ssl->cipherlist[i] >> 8 );
*p++ = ( ssl->cipherlist[i] );
}
*p++ = 1;
*p++ = SSL_COMPRESS_NULL;
ssl->out_msglen = p - buf;
ssl->out_msgtype = SSL_MSG_HANDSHAKE;
ssl->out_msg[0] = SSL_HS_CLIENT_HELLO;
ssl->state++;
return( ssl_write_record( ssl, 0 ) );
}
static int ssl_parse_server_hello( ssl_context *ssl )
{
int ret, i, n;
unsigned char *buf;
/*
* 0 . 0 handshake type
* 1 . 3 handshake length
* 4 . 5 protocol version
* 6 . 9 UNIX time()
* 10 . 37 random bytes
*/
buf = ssl->in_msg;
if( ( ret = ssl_read_record( ssl, 0 ) ) != 0 )
return( ret );
if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
return( ERR_SSL_UNEXPECTED_MESSAGE );
if( ssl->in_hslen < 42 ||
buf[0] != SSL_HS_SERVER_HELLO ||
buf[4] != SSLV3_MAJOR_VERSION )
return( ERR_SSL_BAD_HS_SERVER_HELLO );
if( buf[5] != SSLV3_MINOR_VERSION &&
buf[5] != TLS10_MINOR_VERSION )
return( ERR_SSL_BAD_HS_SERVER_HELLO );
ssl->minor_ver = buf[5];
memcpy( ssl->randbytes + 32, buf + 6, 32 );
/*
* 38 . 38 session id length
* 39 . 38+n session id
* 39+n . 40+n chosen cipher
* 41+n . 41+n chosen compression alg.
*/
n = buf[38];
if( n < 0 || n > 32 || ssl->in_hslen != 42 + n )
return( ERR_SSL_BAD_HS_SERVER_HELLO );
ssl->cipher = ( (int) buf[39 + n] << 8 )
| ( (int) buf[40 + n] );
if( n > 0 && memcmp( ssl->sessid, buf + 39, n ) == 0 )
{
/*
* Server accepted to resume the session
*/
ssl->state = SSL_SERVER_CHANGE_CIPHER_SPEC;
ssl->resumed = 1;
ssl_derive_keys( ssl );
}
else
{
ssl->state++;
ssl->resumed = 0;
ssl->sidlen = n;
memcpy( ssl->sessid, buf + 39, n );
}
i = 0;
while( 1 )
{
if( ssl->cipherlist[i] == 0 )
return( ERR_SSL_NO_CIPHER_CHOSEN );
if( ssl->cipherlist[i++] == ssl->cipher )
break;
}
if( buf[41 + n] != SSL_COMPRESS_NULL )
return( ERR_SSL_BAD_HS_SERVER_HELLO );
return( 0 );
}
static int ssl_parse_server_key_exchange( ssl_context *ssl )
{
int ret, n;
unsigned char *p, *end;
unsigned char hash[36];
md5_context md5;
sha1_context sha1;
if( ssl->cipher != SSL3_EDH_RSA_DES_168_SHA &&
ssl->cipher != TLS1_EDH_RSA_AES_256_SHA )
{
ssl->state++;
return( 0 );
}
#if defined(NO_DHM)
return( ERR_SSL_FEATURE_UNAVAILABLE );
#else
if( ( ret = ssl_read_record( ssl, 0 ) ) != 0 )
return( ret );
if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
return( ERR_SSL_UNEXPECTED_MESSAGE );
if( ssl->in_msg[0] != SSL_HS_SERVER_KEY_EXCHANGE )
return( ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
/*
* Ephemeral DH parameters:
*
* struct {
* opaque dh_p<1..2^16-1>;
* opaque dh_g<1..2^16-1>;
* opaque dh_Ys<1..2^16-1>;
* } ServerDHParams;
*/
p = ssl->in_msg + 4;
end = ssl->in_msg + ssl->in_hslen;
if( ( ret = dhm_read_params( &ssl->dhm_ctx, &p, end ) ) != 0 )
return( ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE | ret );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -