• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            woomsg

            在路上

            openssl使用(補)

            1. 使用OpenSSL內置的數據結構BIO可以方便的創建安全和非安全鏈接,
            在IBM Web上的"使用OpenSSL進行安全編程"系列的三篇文章是個不錯的入門:
            http://www.ibm.com/developerworks/cn/linux/l-openssl.html

            http://www.ibm.com/developerworks/cn/linux/l-openssl2.html
            http://www.ibm.com/developerworks/cn/linux/l-openssl3.html

            安全鏈接的簡要概述:
            安全連接要求在連接建立后進行握手。在握手過程中,服務器向客戶機發送一個證書, 然后,客戶機根據一組可信任證書來核實該證書。它還將檢查證書,以確保它沒有過期。要 檢驗證書是可信任的,需要在連接建立之前提前加載一個可信任證書庫。
            只有在服務器發出請求時,客戶機才會向服務器發送一個證書。該過程叫做客戶機認證。使用證書, 在客戶機和服務器之間傳遞密碼參數,以建立安全連接。盡管握手是在建立連接之后才進行的,但是客戶機或服務器可以在任何時刻請求進行一次新的握手。

            附帶兩個Demo:分別是使用BIO建立普通的socket鏈接和ssl鏈接, 并下載google得主頁.

             1#include "openssl/ssl.h"
             2#include "openssl/bio.h"
             3#include "openssl/err.h"
             4
             5
             6#include <iostream>
             7#include <winsock2.h>
             8
             9
            10#pragma comment( lib, "ws2_32.lib" )
            11#pragma comment( lib, "libeay32.lib" )
            12#pragma comment( lib, "ssleay32.lib" )
            13
            14
            15int main( int argc, char* argv[] ) {
            16  ////////////
            17  // 初始化 //
            18  ////////////
            19  BIO* bio;
            20  int ret;
            21  char* request = "GET / HTTP/1.1\x0D\x0AHost: www.google.com\x0D\x0A\x43onnection: Close\x0D\x0A\x0D\x0A";
            22  char buf[1024];
            23
            24  ERR_load_BIO_strings();
            25  SSL_load_error_strings();
            26  SSLeay_add_ssl_algorithms();
            27
            28
            29  //////////////
            30  // 建立鏈接 //
            31  //////////////
            32
            33  bio = BIO_new_connect("www.google.com:80");
            34  if(bio == NULL) {
            35    std::cout<<"BIO_new_connect error."<<std::endl;
            36    return -1;
            37  }

            38
            39  if(BIO_do_connect(bio) <= 0{
            40    std::cout<<"BIO_new_connect error."<<std::endl;
            41    BIO_free_all(bio);
            42    return -1;
            43  }

            44
            45  // 發送請求
            46  BIO_write(bio, request, strlen(request));
            47
            48  // 接收數據
            49  for(;;) {
            50    ret = BIO_read(bio, buf, 1023);
            51    if(ret <= 0{
            52      break;
            53    }

            54    buf[ret] = 0;
            55    std::cout<<buf<<std::endl;
            56  }

            57
            58  BIO_free_all(bio);
            59  return 0;
            60}

             1#include "openssl/ssl.h"
             2#include "openssl/bio.h"
             3#include "openssl/err.h"
             4
             5
             6#include <iostream>
             7#include <winsock2.h>
             8
             9
            10#pragma comment( lib, "ws2_32.lib" )
            11#pragma comment( lib, "libeay32.lib" )
            12#pragma comment( lib, "ssleay32.lib" )
            13
            14// 我們默認對服務器的證書都是可信的,沒有進行服務器證書的驗證.
            15int main( int argc, char* argv[] ) {
            16  ////////////
            17  // 初始化 //
            18  ////////////
            19  SSL* ssl;
            20  SSL_CTX* ctx;
            21
            22  BIO* bio;
            23  int ret;
            24  char* request = "GET / HTTP/1.1\x0D\x0AHost: www.google.com\x0D\x0A\x43onnection: Close\x0D\x0A\x0D\x0A";
            25  char buf[1024];
            26
            27  ERR_load_BIO_strings();
            28  SSL_load_error_strings();
            29  SSLeay_add_ssl_algorithms();
            30
            31  ctx = SSL_CTX_new ( SSLv23_client_method() );
            32  if (!ctx) {
            33    ERR_print_errors_fp(stderr);
            34    std::cout<<"SSL_CTX_new error."<<std::endl;
            35    return -1;
            36  }

            37
            38  //////////////
            39  // 建立鏈接 //
            40  //////////////
            41  bio = BIO_new_ssl_connect(ctx);
            42  BIO_get_ssl(bio, & ssl);
            43  SSL_set_mode(ssl, SSL_MODE_AUTO_RETRY);
            44  BIO_set_conn_hostname(bio, "www.google.com:443");
            45  if(BIO_do_connect(bio) <= 0{
            46    std::cout<<"BIO_do_connect error."<<std::endl;
            47    return -1;
            48  }

            49  
            50
            51  // 發送請求
            52  BIO_write(bio, request, strlen(request));
            53
            54  // 接收數據
            55  for(;;) {
            56    ret = BIO_read(bio, buf, 1023);
            57    if(ret <= 0{
            58      break;
            59    }

            60    buf[ret] = 0;
            61    std::cout<<buf<<std::endl;
            62  }

            63
            64  BIO_free_all(bio);
            65  SSL_CTX_free(ctx);
            66  return 0;
            67}

            68

            posted on 2008-10-22 10:48 ysong.lee 閱讀(2788) 評論(10)  編輯 收藏 引用

            Feedback

            # re: openssl使用(補) 2009-03-25 09:54 PGG

            如何使用DTLS?  回復  更多評論   

            # re: openssl使用(補) 2010-06-15 03:44 ReillyMelva

            When you are in the corner and have no cash to move out from that point, you would need to receive the <a href="http://lowest-rate-loans.com/topics/credit-loans">credit loans</a>. Just because that should aid you unquestionably. I get commercial loan every single year and feel great just because of it.   回復  更多評論   

            # re: openssl使用(補) 2010-09-28 21:55 essay writer

            Thanks a lot that you created the good enough outcome associated with this topic. Though, to find the professional essay writing service, all of us must have some knowledge about custom write.   回復  更多評論   

            # re: openssl使用(補) 2011-09-30 14:14 loans

            Following my own analysis, billions of persons on our planet get the business loans at good banks. Thus, there's great possibilities to find a student loan in any country.   回復  更多評論   

            # re: openssl使用(補) 2013-04-10 02:34 essay writing service review

            Have no clue which firm to choose to get aid from? Look through QualityEssays testimonials best-writing-services.com, and take a right choice.  回復  更多評論   

            # re: openssl使用(補) 2013-04-10 13:56 resume writing samples

            Can’t complete remarkable CV yourself? Paper writing agencies accessible online will help you out with pleasure whenever you desire to try buying resume online.  回復  更多評論   

            免费精品久久天干天干| 青青草国产精品久久久久| 伊人色综合久久天天网| 99精品久久精品一区二区| 国内精品久久久久久99| 久久99精品久久久久久不卡| 国产精品99久久久精品无码| 国产Av激情久久无码天堂| 久久亚洲av无码精品浪潮| 久久青青草原精品国产| 国产精品亚洲综合专区片高清久久久 | 久久久精品一区二区三区| 国产精品免费久久| 人妻无码αv中文字幕久久琪琪布 人妻无码精品久久亚瑟影视 | 狠狠色狠狠色综合久久| 夜夜亚洲天天久久| 无码日韩人妻精品久久蜜桃| 国产香蕉97碰碰久久人人| 久久综合88熟人妻| 少妇无套内谢久久久久| 国产一区二区精品久久凹凸| 狠狠干狠狠久久| 国产精品久久久久AV福利动漫| 久久影视综合亚洲| 国产精品gz久久久| 狠狠干狠狠久久| 国产精品美女久久久久久2018| 无码久久精品国产亚洲Av影片 | 一本一道久久a久久精品综合| 狠狠色丁香婷婷久久综合不卡| 久久综合噜噜激激的五月天| 国产A级毛片久久久精品毛片| 伊人色综合九久久天天蜜桃| 一级做a爰片久久毛片免费陪 | 伊人久久成人成综合网222| 国产精品无码久久综合网| 亚洲午夜久久影院| 久久国产高清一区二区三区| 91亚洲国产成人久久精品网址| 婷婷久久综合九色综合98| 色综合久久综精品|