• <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>

            S.l.e!ep.¢%

            像打了激速一樣,以四倍的速度運(yùn)轉(zhuǎn),開(kāi)心的工作
            簡(jiǎn)單、開(kāi)放、平等的公司文化;尊重個(gè)性、自由與個(gè)人價(jià)值;
            posts - 1098, comments - 335, trackbacks - 0, articles - 1
              C++博客 :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

            使用 Openssl驗(yàn)證證書(shū)鏈(轉(zhuǎn))

            Link:http://blog.csdn.net/rabbit729/archive/2009/02/06/3866525.aspx

            項(xiàng) 目中遇到使用Openssl驗(yàn)證證書(shū)鏈的問(wèn)題,在網(wǎng)上找了很長(zhǎng)時(shí)間,發(fā)現(xiàn)這方面的資料很少,通過(guò)多方努力,總算實(shí)現(xiàn)了基本功能,為了給大家提供一下參考, 本人實(shí)現(xiàn)了一個(gè)驗(yàn)證證書(shū)鏈的類,以供參考,由于本人也是剛剛接觸Openssl,如果有不正確的地方,請(qǐng)大家多多指導(dǎo)

            1. /************************************************************************/??
            2. /*??????????????????????????VerifyDCChain.h?????????????????????????????*/??
            3. /************************************************************************/??
            4. #ifndef?VERIFYDCCHAIN_H_??
            5. #define?VERIFYDCCHAIN_H_??
            6. ??
            7. #include?<openssl/bio.h>??
            8. #include?<openssl/err.h>??
            9. #include?<openssl/x509.h>??
            10. #include?<openssl/x509v3.h>??
            11. #include?<openssl/pem.h>??
            12. #include?<openssl/crypto.h>??
            13. #include?<string>??
            14. #include?<iostream>??
            15. using?namespace?std;??
            16. ??
            17. class?VerifyDCChain??
            18. {??
            19. public:??
            20. ????VerifyDCChain();??
            21. ????~VerifyDCChain();??
            22. ??
            23. ????/*?
            24. ????*?初 始化證書(shū)鏈堆棧m_chain?
            25. ????*?@param[in]?certChains?證書(shū)鏈中各個(gè)證 書(shū)文件名數(shù)組?
            26. ????*?@param[in]?num?證書(shū)鏈中證書(shū)個(gè)數(shù)?
            27. ????*/??
            28. ????int?Init(const?string*?certChains,?const?int?num);??
            29. ??
            30. ????/*?
            31. ????*?使 用給定的證書(shū)鏈驗(yàn)證葉子證書(shū)?
            32. ????*?@param[in]?certFile?需要驗(yàn)證的葉子證書(shū)文 件名?
            33. ????*/??
            34. ????int?verify(const?char*?certFile);??
            35. private:??
            36. ??
            37. ????/*?
            38. ????*?加 載證書(shū)文件?
            39. ????*?@param[in]?certFile?需要加載的證書(shū)文件名?
            40. ????*/??
            41. ????X509*?load_certfile(const?char*?certFile);??
            42. private:??
            43. ????X509*?m_leaf;??
            44. ????STACK_OF(X509)*?m_chain;??????
            45. };??
            46. ??
            47. #endif??
            48. ??
            49. /************************************************************************/??
            50. /*??????????????????????????VerifyDCChain.cpp???????????????????????????*/??
            51. /************************************************************************/??
            52. ??
            53. #include?"VerifyDCChain.h"??
            54. ??
            55. VerifyDCChain::VerifyDCChain():m_leaf(NULL),?m_chain(NULL)??
            56. {??
            57. ????CRYPTO_malloc_init();???
            58. ????OpenSSL_add_all_algorithms();??
            59. }??
            60. ??
            61. VerifyDCChain::~VerifyDCChain()??
            62. {??
            63. ????if(m_leaf?!=?NULL)???
            64. ????{??
            65. ????????X509_free(m_leaf);??
            66. ????}??
            67. ????if?(m_chain?!=NULL)??
            68. ????{??
            69. ????????sk_X509_free(m_chain);??
            70. ????}??
            71. }??
            72. ??
            73. int?VerifyDCChain::Init(const?string*?certChains,?const?int?num)??
            74. {??
            75. ????int?ret?=?0;??
            76. ????X509*?temp?=?new?X509;??
            77. ????m_chain?=?sk_X509_new_null();??
            78. ??
            79. ????//?注 意此處加載證書(shū)鏈中證書(shū)的順序沒(méi)有要求,因?yàn)??
            80. ????//?在X509_verify_cert()函 數(shù)中會(huì)對(duì)證書(shū)鏈中的證書(shū)??
            81. ????//?進(jìn)行排序??
            82. ????for?(int?i?=?0;?i?<?num;?i++)??
            83. ????{??
            84. ????????temp?=?load_certfile(certChains[i].c_str());??
            85. ????????sk_X509_push(m_chain,?temp);??
            86. ????}??
            87. ????return?1;??
            88. }??
            89. ??
            90. int?VerifyDCChain::verify(const?char*?certFile)??
            91. {??
            92. ????int?ret?=?0;??
            93. ????X509_STORE?*store=NULL;??
            94. ????X509_STORE_CTX?ctx;??
            95. ????m_leaf?=?new?X509();??
            96. ??
            97. ????// 創(chuàng)建X509_store對(duì)象,用來(lái)存儲(chǔ)證書(shū)、撤銷列表等??
            98. ????store=X509_STORE_new();??
            99. ??
            100. ????//?載 入葉子證書(shū)??
            101. ????m_leaf?=?load_certfile(certFile);??
            102. ??
            103. ????// 設(shè)置驗(yàn)證標(biāo)記?都驗(yàn)證那些項(xiàng)?X509_V_FLAG_CRL_CHECK_ALL表示全部驗(yàn)證??
            104. ????X509_STORE_set_flags(store,X509_V_FLAG_CRL_CHECK_ALL);??
            105. ????// 初始化CTX?這個(gè)類就是所謂的上下文?該類收集完必要的信息數(shù)據(jù)?可以進(jìn)行驗(yàn)證??
            106. ????//?此處 X509_STORE_CTX_init最后一個(gè)參數(shù)為NULL,表示不加載證書(shū)撤銷列表CPL??
            107. ????if(!X509_STORE_CTX_init(&ctx,store?,m_leaf,NULL))??
            108. ????{??
            109. ????????ret?=?0;??
            110. ????????goto?end;??
            111. ????}??
            112. ??
            113. ????if(m_chain?==?NULL)??
            114. ????{??
            115. ????????cout<<" 加載證書(shū)鏈?zhǔn)?\n"<<endl;??
            116. ????????ret?=?0;??
            117. ????????goto?end;??
            118. ????}??
            119. ????else??
            120. ????{??
            121. ????????// 將證書(shū)鏈存入CTX??
            122. ????????X509_STORE_CTX_trusted_stack(&ctx,?m_chain);??
            123. ????}??
            124. ??
            125. ????// 證書(shū)鏈?zhǔn)津?yàn)證??
            126. ????if(1?==?X509_verify_cert(&ctx))??
            127. ????????ret?=?1;??
            128. ????else??
            129. ????????ret?=?0;??
            130. end:??
            131. ????X509_STORE_CTX_cleanup(&ctx);??
            132. ????if(store)X509_STORE_free(store);??
            133. ????return?ret;??
            134. }??
            135. ??
            136. X509*?VerifyDCChain::load_certfile(const?char*?certFile)??
            137. {??
            138. ????X509*?cert?=?NULL;??
            139. ????BIO*?in?=?NULL;??
            140. ??
            141. ????if(certFile==NULL)??
            142. ????????goto?end;??
            143. ????in?=?BIO_new_file(certFile,"r");??
            144. ????if(in==NULL)??
            145. ????????goto?end;??
            146. ????// 將IO中數(shù)據(jù)以PEM格式讀入到X509對(duì)象??
            147. ????cert?=?PEM_read_bio_X509(in,NULL,NULL,NULL);??
            148. ????if(cert?==?NULL)??
            149. ????????goto?end;??
            150. end:??
            151. ????if(in)BIO_free(in);??
            152. ????return?cert;??
            153. }??
            154. ??
            155. /************************************************************************/??
            156. /*?????????????????????????????????test.cpp?????????????????????????????*/??
            157. /************************************************************************/??
            158. ??
            159. #include?"VerifyDCChain.h"??
            160. #include?<iostream>??
            161. using?namespace?std;??
            162. ??
            163. void?main(void)??
            164. {??
            165. ????VerifyDCChain?m_check;??
            166. ??
            167. ????//?注 意此處加載證書(shū)鏈中證書(shū)文件名的順序沒(méi)有要求,??
            168. ????//?因?yàn)樵? X509_verify_cert()函數(shù)中會(huì)對(duì)證書(shū)鏈中的??
            169. ????//?證書(shū)進(jìn)行排序??
            170. ????string?certChains[4]?=?{"5.crt",?"4.crt",?"3.crt",?"2.crt"};??
            171. ????m_check.Init(certChains,?4);??
            172. ??
            173. ????if?(1?==?m_check.verify("1.crt"))??
            174. ????{??
            175. ????????cout<<"OK!"<<endl;??
            176. ????}??
            177. ????else??
            178. ????{??
            179. ????????cout<<"ERROR!"<<endl;??
            180. ????}?????
            181. }??
            亚洲va中文字幕无码久久不卡| 久久最近最新中文字幕大全| 久久精品无码免费不卡| 国产精品午夜久久| 久久免费视频1| 国产精品久久亚洲不卡动漫| 一本综合久久国产二区| 久久99亚洲网美利坚合众国| 久久免费国产精品| 久久人人妻人人爽人人爽| 久久精品这里只有精99品| 亚洲日本va中文字幕久久| 精品久久久久中文字| 久久香综合精品久久伊人| 久久亚洲av无码精品浪潮| 国产精品欧美久久久天天影视| 亚洲综合久久夜AV | 伊人丁香狠狠色综合久久| 日韩精品久久久久久久电影蜜臀 | 久久精品国产精品国产精品污| 久久99精品久久久久久9蜜桃| 精品少妇人妻av无码久久| 久久精品无码一区二区WWW| 国产精品热久久无码av| 日本免费久久久久久久网站| 人妻少妇久久中文字幕| 国内精品久久久久影院亚洲| 精品久久久久久久久久中文字幕| 精品无码久久久久久午夜| 久久久无码精品亚洲日韩按摩| 久久久这里只有精品加勒比| 欧美精品丝袜久久久中文字幕 | 久久午夜综合久久| 国产精品成人无码久久久久久| 99久久超碰中文字幕伊人| 国产精品一区二区久久国产| 7777久久久国产精品消防器材| 久久亚洲精品国产亚洲老地址| 午夜精品久久影院蜜桃| 亚洲国产成人乱码精品女人久久久不卡| 精品久久久久久无码免费|