• <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. }??
            国产欧美久久久精品| 久久精品国产亚洲精品2020| 久久久久国色AV免费观看| 国内精品久久久久久不卡影院 | 亚洲国产精品热久久| 久久久中文字幕日本| 久久人妻少妇嫩草AV蜜桃| 国产一久久香蕉国产线看观看| 久久夜色撩人精品国产小说| 久久精品国产亚洲AV影院 | 国产精品九九久久精品女同亚洲欧美日韩综合区| 99久久无码一区人妻a黑| 国产成人无码精品久久久免费| 色偷偷88欧美精品久久久| 国产精品禁18久久久夂久| 人人狠狠综合久久亚洲| 69SEX久久精品国产麻豆| 四虎国产精品成人免费久久| 久久A级毛片免费观看| 欧美午夜A∨大片久久 | 国产99精品久久| 伊人久久精品影院| 久久久久人妻一区精品 | 热re99久久6国产精品免费| 国产精品嫩草影院久久| 久久91精品国产91久久麻豆| 精品国产99久久久久久麻豆 | 久久天堂电影网| 久久亚洲中文字幕精品有坂深雪 | 99久久精品费精品国产一区二区 | 久久亚洲sm情趣捆绑调教| 国产成人精品免费久久久久| 热re99久久精品国99热| 久久香综合精品久久伊人| 久久av无码专区亚洲av桃花岛| 久久久久久久女国产乱让韩| 久久精品亚洲中文字幕无码麻豆| 久久久久99精品成人片试看| 国产精品久久午夜夜伦鲁鲁| 伊人久久免费视频| 久久亚洲精品无码VA大香大香|