• <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.¢%

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

            使用 Openssl驗證證書鏈(轉)

            Posted on 2010-07-02 08:23 S.l.e!ep.¢% 閱讀(3694) 評論(0)  編輯 收藏 引用 所屬分類: OpenSSL

            使用 Openssl驗證證書鏈(轉)

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

            項 目中遇到使用Openssl驗證證書鏈的問題,在網上找了很長時間,發現這方面的資料很少,通過多方努力,總算實現了基本功能,為了給大家提供一下參考, 本人實現了一個驗證證書鏈的類,以供參考,由于本人也是剛剛接觸Openssl,如果有不正確的地方,請大家多多指導

            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. ????*?初 始化證書鏈堆棧m_chain?
            25. ????*?@param[in]?certChains?證書鏈中各個證 書文件名數組?
            26. ????*?@param[in]?num?證書鏈中證書個數?
            27. ????*/??
            28. ????int?Init(const?string*?certChains,?const?int?num);??
            29. ??
            30. ????/*?
            31. ????*?使 用給定的證書鏈驗證葉子證書?
            32. ????*?@param[in]?certFile?需要驗證的葉子證書文 件名?
            33. ????*/??
            34. ????int?verify(const?char*?certFile);??
            35. private:??
            36. ??
            37. ????/*?
            38. ????*?加 載證書文件?
            39. ????*?@param[in]?certFile?需要加載的證書文件名?
            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. ????//?注 意此處加載證書鏈中證書的順序沒有要求,因為??
            80. ????//?在X509_verify_cert()函 數中會對證書鏈中的證書??
            81. ????//?進行排序??
            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. ????// 創建X509_store對象,用來存儲證書、撤銷列表等??
            98. ????store=X509_STORE_new();??
            99. ??
            100. ????//?載 入葉子證書??
            101. ????m_leaf?=?load_certfile(certFile);??
            102. ??
            103. ????// 設置驗證標記?都驗證那些項?X509_V_FLAG_CRL_CHECK_ALL表示全部驗證??
            104. ????X509_STORE_set_flags(store,X509_V_FLAG_CRL_CHECK_ALL);??
            105. ????// 初始化CTX?這個類就是所謂的上下文?該類收集完必要的信息數據?可以進行驗證??
            106. ????//?此處 X509_STORE_CTX_init最后一個參數為NULL,表示不加載證書撤銷列表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<<" 加載證書鏈失敗!\n"<<endl;??
            116. ????????ret?=?0;??
            117. ????????goto?end;??
            118. ????}??
            119. ????else??
            120. ????{??
            121. ????????// 將證書鏈存入CTX??
            122. ????????X509_STORE_CTX_trusted_stack(&ctx,?m_chain);??
            123. ????}??
            124. ??
            125. ????// 證書鏈式驗證??
            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中數據以PEM格式讀入到X509對象??
            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. ????//?注 意此處加載證書鏈中證書文件名的順序沒有要求,??
            168. ????//?因為在 X509_verify_cert()函數中會對證書鏈中的??
            169. ????//?證書進行排序??
            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. }??
            久久综合亚洲色一区二区三区| 国产成人精品综合久久久| 久久精品国产99久久香蕉| 久久精品国产免费一区| 国产精品久久久久9999高清| 香蕉久久av一区二区三区| 无码精品久久久天天影视| 久久久久成人精品无码中文字幕 | 欧美一区二区久久精品| 精品久久久久久无码不卡| 国内精品九九久久精品| 亚洲精品美女久久777777| 久久香综合精品久久伊人| 91精品国产9l久久久久| 青青青国产成人久久111网站| 久久99热国产这有精品| 欧美日韩精品久久久久| 国色天香久久久久久久小说| 99re这里只有精品热久久| 国产 亚洲 欧美 另类 久久| 天天综合久久一二三区| 久久久精品国产sm调教网站 | 久久99精品国产麻豆宅宅| 久久久久亚洲AV无码观看| 久久久久久夜精品精品免费啦| 国产精品久久久久久吹潮| 久久国产福利免费| 午夜精品久久久久久中宇| 99精品伊人久久久大香线蕉| 香蕉久久久久久狠狠色| 狠狠色噜噜狠狠狠狠狠色综合久久| 国产精品久久久久一区二区三区| 日本亚洲色大成网站WWW久久 | 成人午夜精品久久久久久久小说| 久久本道综合久久伊人| 熟妇人妻久久中文字幕| 久久人妻少妇嫩草AV无码蜜桃| 久久精品人人做人人爽97| 青青久久精品国产免费看| 粉嫩小泬无遮挡久久久久久| 国产精品99久久久精品无码 |