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变量,不能只将VCBIN这样的目录加入就完了,需要增加

C:Program Files (x86)Microsoft Visual Studio 11.0Common7IDE;C:Program Files (x86)Microsoft Visual Studio 11.0Common7Tools;C:Program Files (x86)Microsoft Visual Studio 11.0VCin;C:Program Files (x86)Microsoft Visual Studio 11.0VCvcpackages;C:WindowsMicrosoft.NETFramework64v4.0.30319;C:WindowsMicrosoft.NETFramework64v3.5;C:WindowsMicrosoft.NETFrameworkv4.0.30319;C:WindowsMicrosoft.NETFrameworkv3.5;

这么一堆路径都需要加入,否则还是会编译出错,参考:https://www.cnblogs.com/bluestorm/p/3321558.html

最简便的方法是调用VC提供的批处理,C:Program Files (x86)Microsoft Visual Studio 11.0Common7Toolsvsvars32.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
", __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:windowssystem32),右键后管理员运行办法来完成

安装后,即可包含、链接相应库等后续开发了。

原文地址:https://www.cnblogs.com/eaglexmw/p/11087994.html