当前位置:   article > 正文

将SM2根证书预置到chromium中_国密boringssl

国密boringssl

最近花了很多精力在做chromium的GmSSL适配,协议和算法都已经完成,这篇文章是关于将SM2根证书预置到chromium中

我的开发测试环境是macos12.4,从chromium的代码和文档中得知证书获取和校验都是通过操作系统以及native api接口完成,如果需要国密证书验证和校验成功,需要对chromium做适配和开发,分为2步骤走:

1、chromium内置根证书

2、chromium调整验证流程

一、前提

1、在开始之前,您需要对开源项目chromium以及密码算法、安全通讯协议有一定的了解

2、你已经完成BoringSSL的密码算法、安全通讯协议的适配

二、国密根证书内置

1、下载根证书

这里我使用的是沃通和中国金融根证书

沃通

沃通SM2中级根证书-沃通WoTrus SSL证书!沃通CA SM2顶级根证书已经签发了如下中级根证书,用于颁发各种认证级别的SSL证书、客户端证书和时间戳证书。https://www.wotrus.com/root/SM2_intermediateca.htm

下载国密SM2根证书即可

 

中国金融

CPS_数字证书服务协议_证书体验平台_证书下载平台_证书链下载_申请表下载_全球服务器证书_相关下载_中国金融认证中心中国金融认证中心(CFCA)为客户提供专业的CPS、证书体验、证书下载、证书链下载、申请表下载、全球服务器证书、反欺诈全球服务器证书、证书服务协议、证书安全提示等方面综合、全面、立体的资料下载服务。https://www.cfca.com.cn/zhengshuzizhu/

在下载专区中下载生成证书链SM2

 

2、将证书转成pem格式

  1. openssl x509 -in WoTrus-SM2.crt -out WoTrus-SM2.pem
  2. openssl x509 -in CFCA_CS_SM2_CA.cer -out CFCA_CS_SM2_CA.pem

3、获取指纹签名的sha256值

  1. openssl x509 -fingerprint -sha256 -in WoTrus-SM2.pem
  2. sha256 Fingerprint=B2:CF:D0:68:B4:D7:FC:A1:21:07:AF:41:65:CC:DA:61:34:38:DD:BD:90:16:9F:5F:4B:11:1C:AF:A2:21:D4:35

去掉多余的信息得到值:B2CFD068B4D7FCA12107AF4165CCDA613438DDBD90169F5F4B111CAFA221D435

将WoTrus-SM2.pem改名为B2CFD068B4D7FCA12107AF4165CCDA613438DDBD90169F5F4B111CAFA221D435.pem

  1. openssl x509 -fingerprint -sha256 -in CFCA_CS_SM2_CA.pem
  2. sha256 Fingerprint=5F:C0:9A:7D:B2:B9:1E:87:6C:BE:A8:70:60:A9:FE:DF:02:91:69:81:50:97:78:53:BC:4B:5A:A6:54:98:9F:BD

去掉多余的信息得到值:5FC09A7DB2B91E876CBEA87060A9FEDF0291698150977853BC4B5AA654989FBD

将CFCA_CS_SM2_CA.pem改名为B2CFD068B4D7FCA12107AF4165CCDA613438DDBD90169F5F4B111CAFA221D435.pem

三、chromium代码调整

主要调整的代码部分是net网络模块, 主要加上 chrome_root_store_supported支持

1、net/features.gni

chrome_root_store_supported默认只支持windows,调整下支持macos, 将

chrome_root_store_supported = is_win

改为

chrome_root_store_supported = is_win || is_mac

2、将pem文件放入指定位置

将B2CFD068B4D7FCA12107AF4165CCDA613438DDBD90169F5F4B111CAFA221D435.pem、B2CFD068B4D7FCA12107AF4165CCDA613438DDBD90169F5F4B111CAFA221D435.pem放到net/data/ssl/chrome_root_store/store/certs下

3、修改net/data/ssl/chrome_root_store/store/root_store.textproto文件

路径就是刚才放置的路径

  1. # Network WoTrus-SM2
  2. trust_anchors {
  3.   filename: "B2CFD068B4D7FCA12107AF4165CCDA613438DDBD90169F5F4B111CAFA221D435.pem"
  4. }
  5. # Network CFCA_CS_SM2_CA
  6. trust_anchors {
  7.   filename: "5FC09A7DB2B91E876CBEA87060A9FEDF0291698150977853BC4B5AA654989FBD.pem"
  8. }

4、修gn工程文件net/data/ssl/chrome_root_store/BUILD.gn文件

将pem文件添加到build.gn中,编译后会生成chrome-root-store-inc.cc中间文件

  1. # Generate C++ include file for the Chrome root store.
  2. compiled_action("gen_root_store_inc") {
  3.   tool = "//net/tools/root_store_tool:root_store_tool"
  4.   # It'd be really nice to list an input as "store/certs/*", but it doesn't seem
  5.   # to work. So we list them all out.
  6.   inputs = [
  7.     "store/root_store.textproto",
  8.     "store/certs/02ed0eb28c14da45165c566791700d6451d7fb56f0b2ab1d3b8eb070e56edff5.pem",
  9.     "store/certs/0376ab1d54c5f9803ce4b2e201a0ee7eef7b57b636e8a93c9b8d4860c96f5fa7.pem",
  10.     ….
  11.     "store/certs/fd73dad31c644ff1b43bef0ccdda96710b9cd9875eca7e31707af3e96d522bbd.pem",
  12.     "store/certs/B2CFD068B4D7FCA12107AF4165CCDA613438DDBD90169F5F4B111CAFA221D435.pem",
  13.     "store/certs/5FC09A7DB2B91E876CBEA87060A9FEDF0291698150977853BC4B5AA654989FBD.pem",
  14.   ]
  15.   outputs = [ "${target_gen_dir}/chrome-root-store-inc.cc" ]
  16.   args = [
  17.     "--root-store-dir=" + rebase_path("store", root_build_dir),
  18.     "--write-cpp=" + rebase_path("${target_gen_dir}/chrome-root-store-inc.cc",
  19.                                  root_build_dir),
  20.   ]
  21. }

自此静态证书部分添加完毕

下面对代码修改,以支持证书链的校验

5、调整证书信任存储代码

1) net/cert/internal/system_trust_store.cc

默认MACOS只支持从操作系统中获取证书并校验,这里改成浏览器内部预置证书校验

找到MACOS支持部分

修改下面代码

  1. std::unique_ptr<SystemTrustStore> CreateSslSystemTrustStoreChromeRoot() {
  2.   return std::make_unique<DummySystemTrustStore>();
  3. }

  1. #if BUILDFLAG(CHROME_ROOT_STORE_SUPPORTED)
  2. std::unique_ptr<SystemTrustStore> CreateSslSystemTrustStoreChromeRoot() {
  3.   return std::make_unique<SystemTrustStoreChrome>(
  4.       std::make_unique<TrustStoreChrome>(), std::make_unique<TrustStoreMac>(kSecPolicyAppleSSL, TrustStoreMac::TrustImplType::kDomainCache, 512));
  5. }
  6. #else
  7. std::unique_ptr<SystemTrustStore> CreateSslSystemTrustStoreChromeRoot() {
  8.   return std::make_unique<DummySystemTrustStore>();
  9. }
  10. #endif

2) net/cert/cert_verify_proc.cc

增加BUILTIN_CERT_VERIFIER_FEATURE_SUPPORTED支持

修改静态方法

  1. scoped_refptr<CertVerifyProc> CertVerifyProc::CreateBuiltinVerifyProc(
  2.     scoped_refptr<CertNetFetcher> cert_net_fetcher) {
  3.   return CreateCertVerifyProcBuiltin(std::move(cert_net_fetcher),
  4.                                      CreateSslSystemTrustStore());
  5. }

  1. #if BUILDFLAG(CHROME_ROOT_STORE_SUPPORTED)
  2. // static
  3. scoped_refptr<CertVerifyProc> CertVerifyProc::CreateBuiltinVerifyProc(
  4.     scoped_refptr<CertNetFetcher> cert_net_fetcher) {
  5.   return CreateCertVerifyProcBuiltin(std::move(cert_net_fetcher),
  6.                                      CreateSslSystemTrustStoreChromeRoot());
  7. }
  8. #else
  9. // static
  10. scoped_refptr<CertVerifyProc> CertVerifyProc::CreateBuiltinVerifyProc(
  11.     scoped_refptr<CertNetFetcher> cert_net_fetcher) {
  12.   return CreateCertVerifyProcBuiltin(std::move(cert_net_fetcher),
  13.                                      CreateSslSystemTrustStore());
  14. }
  15. #endif

3) services/cert_verifier/cert_verifier_creation.cc

修改证书校验创建流程

  1. scoped_refptr<net::CertVerifyProc> CreateOldDefaultWithoutCaching(
  2.     scoped_refptr<net::CertNetFetcher> cert_net_fetcher) {
  3.   scoped_refptr<net::CertVerifyProc> verify_proc;
  4. #if defined(OS_FUCHSIA) || defined(OS_LINUX) || defined(OS_CHROMEOS)
  5.   verify_proc =
  6.       net::CertVerifyProc::CreateBuiltinVerifyProc(std::move(cert_net_fetcher));
  7. #else
  8.   verify_proc =
  9.       net::CertVerifyProc::CreateSystemVerifyProc(std::move(cert_net_fetcher));
  10. #endif
  11.   return verify_proc;
  12. }

  1. scoped_refptr<net::CertVerifyProc> CreateOldDefaultWithoutCaching(
  2. scoped_refptr<net::CertNetFetcher> cert_net_fetcher) {
  3. scoped_refptr<net::CertVerifyProc> verify_proc;
  4. #if defined(OS_FUCHSIA) || defined(OS_LINUX) || defined(OS_CHROMEOS)
  5. verify_proc =
  6. net::CertVerifyProc::CreateBuiltinVerifyProc(std::move(cert_net_fetcher));
  7. #elif BUILDFLAG(BUILTIN_CERT_VERIFIER_FEATURE_SUPPORTED)
  8. // Create by david.chin on 07/26/2023
  9. if (base::FeatureList::IsEnabled(net::features::kCertVerifierBuiltinFeature)) {
  10. verify_proc =
  11. net::CertVerifyProc::CreateBuiltinVerifyProc(std::move(cert_net_fetcher));
  12. } else {
  13. verify_proc =
  14. net::CertVerifyProc::CreateSystemVerifyProc(std::move(cert_net_fetcher));
  15. }
  16. #else
  17. verify_proc =
  18. net::CertVerifyProc::CreateSystemVerifyProc(std::move(cert_net_fetcher));
  19. #endif
  20. return verify_proc;
  21. }

6、编译chromium,等待数小时

效果图

 

沃通测试网站

https://sm2only.ovssl.cn/

 

中国银行(只支持SM2)

https://ebssec.boc.cn/

 

四、写在最后

当然,国密的适配还有不少工作要做,这里只是做了macos的国密根证书内置,后续包括windows、linux、国产操作系统等适配会持续进行。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Li_阴宅/article/detail/1001036
推荐阅读
相关标签
  

闽ICP备14008679号