GMSSL在Window下的編譯


因為工作需要用到SM2算法加解密,網絡上找一圈,沒有合用的,還被騙了一堆積分。

無奈只得自行編譯,從GITHUB的GMSSL下載到最新的SSL庫,VS2012下編譯踩了不少坑,記錄一下

GITHUB鏈接:https://github.com/guanzhi/GmSSL

fork了一下分支:https://github.com/eaglexmw-gmail/GmSSL

首先編譯需要perl、VS2012、NASM三個工具,分別安裝后,由於VS2012與NASM沒有修改PATH環境變量

在執行perl Configure VC-WIN32時會報告工具缺失,可以使用set path=%path%;xxx;方式添加路徑

這里記錄第一個坑,VS2012修改PATH變量,不能只將VC\BIN這樣的目錄加入就完了,需要增加

C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE;C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\Tools;C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\bin;C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\vcpackages;C:\Windows\Microsoft.NET\Framework64\v4.0.30319;C:\Windows\Microsoft.NET\Framework64\v3.5;C:\Windows\Microsoft.NET\Framework\v4.0.30319;C:\Windows\Microsoft.NET\Framework\v3.5;

這么一堆路徑都需要加入,否則還是會編譯出錯,參考:https://www.cnblogs.com/bluestorm/p/3321558.html

最簡便的方法是調用VC提供的批處理,C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\Tools\vsvars32.bat

修改好后,使用nmake即可開始編譯。

編譯時有幾處代碼提示編譯失敗,都是屬於C語言中,變量聲明在有效代碼后(VS嚴格禁止的),調換一下順序即可

diff -r -u -N -w GmSSL-master/crypto/sm9/sm9_asn1.c GmSSL-master_new/crypto/sm9/sm9_asn1.c
--- GmSSL-master/crypto/sm9/sm9_asn1.c    2019-06-15 23:42:35.000000000 +0800
+++ GmSSL-master_new/crypto/sm9/sm9_asn1.c    2019-06-25 18:16:49.931390600 +0800
@@ -285,9 +285,9 @@
 {
     int ret;
     ASN1_OCTET_STRING s;
+    int len = 0;
     s.type = V_ASN1_OCTET_STRING;
     s.data = NULL;
-    int len = 0;
 
     if (inlen > SM9_MAX_PLAINTEXT_LENGTH) {
         SM9err(SM9_F_SM9_CIPHERTEXT_SIZE, SM9_R_PLAINTEXT_TOO_LONG);
diff -r -u -N -w GmSSL-master/crypto/sm9/sm9_rate.c GmSSL-master_new/crypto/sm9/sm9_rate.c
--- GmSSL-master/crypto/sm9/sm9_rate.c    2019-06-15 23:42:35.000000000 +0800
+++ GmSSL-master_new/crypto/sm9/sm9_rate.c    2019-06-25 18:18:18.959395000 +0800
@@ -1303,14 +1303,15 @@
 
 static int fp12_inv(fp12_t r, const fp12_t a, const BIGNUM *p, BN_CTX *ctx)
 {
-    if (fp4_is_zero(a[2])) {
         fp4_t k;
         fp4_t t;
+        fp4_t r0, r1, r2;
+        
+    if (fp4_is_zero(a[2])) {
         if (!fp4_init(t, ctx)) {
             return 0;
         }
 
-        fp4_t r0, r1, r2;
         fp4_init(r0, ctx);
         fp4_init(r1, ctx);
         fp4_init(r2, ctx);
@@ -2239,6 +2240,8 @@
     BIGNUM *k = BN_new();
     int ok;
 
+    fp12_t x, y;
+
     point_init(&G, ctx);
     point_init(&P, ctx);
 
@@ -2280,8 +2283,6 @@
     ok = point_equ_hex(&P, Ppubs, ctx);
     printf("point test %d: %s\n", __LINE__, ok ? "ok" : "error");
 
-    fp12_t x, y;
-
     fp12_init(x, ctx);
     fp12_init(y, ctx);
 
diff -r -u -N -w GmSSL-master/ssl/statem/statem_gmtls.c GmSSL-master_new/ssl/statem/statem_gmtls.c
--- GmSSL-master/ssl/statem/statem_gmtls.c    2019-06-15 23:42:35.000000000 +0800
+++ GmSSL-master_new/ssl/statem/statem_gmtls.c    2019-06-25 18:19:36.844416900 +0800
@@ -303,11 +303,12 @@
 int gmtls_construct_server_certificate(SSL *s)
 {
     unsigned long alg_a;
-    alg_a = s->s3->tmp.new_cipher->algorithm_auth;
     int l;
     unsigned char *p;
     int al = -1;
 
+    alg_a = s->s3->tmp.new_cipher->algorithm_auth;
+
     l = 3 + SSL_HM_HEADER_LENGTH(s);
 
     if (alg_a & SSL_aSM2) {

 patch文件下載地址:https://files.cnblogs.com/files/eaglexmw/gmssl_20190625_patch.7z

編譯成功后,使用nmake install安裝,由於默認安裝路徑在C:\Program Files (x86)\GmSSL,因此,需要管理員權限

管理員權限我通常是采取找到cmd.exe(所在路徑C:\windows\system32),右鍵后管理員運行辦法來完成

安裝后,即可包含、鏈接相應庫等后續開發了。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM