⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 xyssl - client source code.htm

📁 ssl客户端的源代码实现
💻 HTM
📖 第 1 页 / 共 2 页
字号:
<!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&nbsp;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/">&nbsp;About&nbsp;</A> 
      </TD></TR></TBODY></TABLE></CENTER></DIV></TD></TR>
  <TR>
    <TD class=menudiv align=right colSpan=2><!--    <A href="../../../enroll/">XySSL Root CA</A>    &nbsp;    <A HREF="https://xyssl.org/code/source/ssl_cli/">    <IMG alt="SSL" border=0 src="../../../static/ssl.gif"         style="vertical-align: middle"></A>&nbsp; &nbsp;--></TD></TR>
  <TR align=middle>
    <TD class=pagehead colSpan=2><BR>&nbsp; <SPAN 
      class=underline>&nbsp;SSL/TLS Client source code&nbsp;</SPAN> <BR>&nbsp; 
    </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>&nbsp; 
      <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>&nbsp;</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 &lt;string.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;time.h&gt;

#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( &amp;ssl-&gt;hs_md5  );
    sha1_starts( &amp;ssl-&gt;hs_sha1 );

    ssl-&gt;major_ver = SSLV3_MAJOR_VERSION;
    ssl-&gt;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-&gt;out_msg;
    p = buf + 4;

    *p++ = ssl-&gt;max_ver[0] = SSLV3_MAJOR_VERSION;
    *p++ = ssl-&gt;max_ver[1] = TLS10_MINOR_VERSION;

    t = time( NULL );
    *p++ = (unsigned char)( t &gt;&gt; 24 );
    *p++ = (unsigned char)( t &gt;&gt; 16 );
    *p++ = (unsigned char)( t &gt;&gt;  8 );
    *p++ = (unsigned char)( t       );

    for( i = 28; i &gt; 0; i-- )
        *p++ = ssl-&gt;rng_f( ssl-&gt;rng_d );

    memcpy( ssl-&gt;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-&gt;sidlen &gt; 0 &amp;&amp; ssl-&gt;sidlen &lt;= 32 )
        ? ssl-&gt;sidlen : 0;

    *p++ = n;
    for( i = 0; i &lt; n; i++ )
        *p++ = ssl-&gt;sessid[i];
    
    for( n = 0; ssl-&gt;cipherlist[n] != 0; n++ );
    *p++ = ( n &gt;&gt; 7 );
    *p++ = ( n &lt;&lt; 1 );

    for( i = 0; i &lt; n; i++ )
    {
        *p++ = ( ssl-&gt;cipherlist[i] &gt;&gt; 8 );
        *p++ = ( ssl-&gt;cipherlist[i]      );
    }

    *p++ = 1;
    *p++ = SSL_COMPRESS_NULL;

    ssl-&gt;out_msglen  = p - buf;
    ssl-&gt;out_msgtype = SSL_MSG_HANDSHAKE;
    ssl-&gt;out_msg[0]  = SSL_HS_CLIENT_HELLO;

    ssl-&gt;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-&gt;in_msg;

    if( ( ret = ssl_read_record( ssl, 0 ) ) != 0 )
        return( ret );

    if( ssl-&gt;in_msgtype != SSL_MSG_HANDSHAKE )
        return( ERR_SSL_UNEXPECTED_MESSAGE );

    if( ssl-&gt;in_hslen &lt; 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 &amp;&amp;
        buf[5] != TLS10_MINOR_VERSION )
        return( ERR_SSL_BAD_HS_SERVER_HELLO );

    ssl-&gt;minor_ver = buf[5];

    memcpy( ssl-&gt;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 &lt; 0 || n &gt; 32 || ssl-&gt;in_hslen != 42 + n )
        return( ERR_SSL_BAD_HS_SERVER_HELLO );

    ssl-&gt;cipher = ( (int) buf[39 + n] &lt;&lt; 8 )
                | ( (int) buf[40 + n]      );

    if( n &gt; 0 &amp;&amp; memcmp( ssl-&gt;sessid, buf + 39, n ) == 0 )
    {
        /*
         * Server accepted to resume the session
         */
        ssl-&gt;state = SSL_SERVER_CHANGE_CIPHER_SPEC;
        ssl-&gt;resumed = 1;
        ssl_derive_keys( ssl );
    }
    else
    {
        ssl-&gt;state++;
        ssl-&gt;resumed = 0;
        ssl-&gt;sidlen = n;
        memcpy( ssl-&gt;sessid, buf + 39, n );
    }

    i = 0;
    while( 1 )
    {
        if( ssl-&gt;cipherlist[i] == 0 )
            return( ERR_SSL_NO_CIPHER_CHOSEN );

        if( ssl-&gt;cipherlist[i++] == ssl-&gt;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-&gt;cipher != SSL3_EDH_RSA_DES_168_SHA &amp;&amp;
        ssl-&gt;cipher != TLS1_EDH_RSA_AES_256_SHA )
    {
        ssl-&gt;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-&gt;in_msgtype != SSL_MSG_HANDSHAKE )
        return( ERR_SSL_UNEXPECTED_MESSAGE );

    if( ssl-&gt;in_msg[0] != SSL_HS_SERVER_KEY_EXCHANGE )
        return( ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );

    /*
     * Ephemeral DH parameters:
     *
     * struct {
     *     opaque dh_p&lt;1..2^16-1&gt;;
     *     opaque dh_g&lt;1..2^16-1&gt;;
     *     opaque dh_Ys&lt;1..2^16-1&gt;;
     * } ServerDHParams;
     */
    p   = ssl-&gt;in_msg + 4;
    end = ssl-&gt;in_msg + ssl-&gt;in_hslen;

    if( ( ret = dhm_read_params( &amp;ssl-&gt;dhm_ctx, &amp;p, end ) ) != 0 )
        return( ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE | ret );

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -