Windows平台 VS2019 编译 openssl1.1.1以及gmssl

前序:总结一下openssl源码在windows平台编译的问题。linux平台上相对比较简单,和其他源码编译方式一样,基本都是./configure --prefix="path" -> make ->make install完成。windows其实和linux大致是一个思路,只不过windows需要一些辅助工具,所以直观感觉就比较麻烦一点,因此做下记录,仅供大家一起学习。

openssl在版本1.1以后,编译后的库的名称都变了,和linux下保持一致了。从原来的libeay32.dll -> libcrypto.dll, ssleay32.dll -> libssl.dll。

前期的准备工作:

1:需要安装perl脚本工具(这个可以自行在网上查询相关的安装资料)

2:需要安装vs2019开发工具,这个可以直接上官网,目前vs最新版本是vs2019,首页就是了Visual Studio 2019 IDE - 适用于 Windows 的编程软件 (microsoft.com)

3:需要下载openssl源码,这个可以直接上官网,目前最新版本已经是3开头了,但是鉴于稳定性和兼容性考虑,并没有考虑去尝试它。/source/old/index.html (openssl.org)

正式开始:

1:需要打开命令行 windows+r键,敲命令cmd

2:开打vs2019开发工具的环境初始化脚本,并执行这个脚本

Windows平台 VS2019 编译 openssl1.1.1以及gmssl_第1张图片

Windows平台 VS2019 编译 openssl1.1.1以及gmssl_第2张图片

3:切换到openssl源码的路径

4:执行命令生成makefile文件(可以参考安装目录下的INSTALL文件)

4.1生成32位库文件

perl Configure VC-WIN32 -no-asm --prefix="D:\Program Files (x86)\openssl-1.1.1"

4.2生成64位库文件

perl Configure VC-WIN64A -no-asm --prefix="D:\Program Files (x86)\openssl-1.1.1"

Windows平台 VS2019 编译 openssl1.1.1以及gmssl_第3张图片

5:执行makefile 文件

命令:nmake

Windows平台 VS2019 编译 openssl1.1.1以及gmssl_第4张图片

6:执行命令测试程序

命令:nmake test

7:安装

命令:nmake install

Windows平台 VS2019 编译 openssl1.1.1以及gmssl_第5张图片

8:如果要继续编译64位库,建议安装目录重新指定,然后记得清理编译后的目标文件

命令:nmake clean

9:编译64位(-no-shared)这个静态库编译之后不能用

Windows平台 VS2019 编译 openssl1.1.1以及gmssl_第6张图片

二、编译gmssl版本

编译之前,先介绍下出处,这是北大的GM实验室创造,一直在维护这个版本,源码地址github.comicon-default.png?t=LA92https://github.com/guanzhi/GmSSL/archive/master.zip编译之前需要修改两处,否则后面编译会报错。

1:Configure文件,

use if $^O ne "VMS", 'File::Glob' => qw/glob/;

修改为

use if $^O ne "VMS", 'File::Glob' => qw/:glob/;

2:crypto\evp\names2.c

/*
 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
 *
 * Licensed under the OpenSSL license (the "License").  You may not use
 * this file except in compliance with the License.  You can obtain a copy
 * in the file LICENSE in the source distribution or at
 * https://www.openssl.org/source/license.html
 */



#include 
#include "internal/cryptlib.h"
#include 
#include 
#include 
#include "internal/evp_int.h"

/*
 * use MD5 as default:
 *	X509_REQ_to_X509		x509_r2x.c
 *	X509_issuer_and_serial_hash	x509_cmp.c
 *	X509_NAME_hash_old		x509_cmp.c
 *	PEM_ASN1_write_bio		pem_lib.c
 */
const EVP_MD *EVP_get_default_digest(void)
{
#if !defined(OPENSSL_NO_MD5)
	return EVP_md5();
#elif !defined(OPENSSL_NO_SHA)
	return EVP_sha1();
#elif !defined(OPENSSL_NO_SM3)
	return EVP_sm3();
#elif !defined(OPENSSL_NO_RIPEMD)
	return EVP_rmd160();
#else
	return NULL;
#endif
}

修改为:

/*

Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
Licensed under the OpenSSL license (the "License"). You may not use
this file except in compliance with the License. You can obtain a copy
in the file LICENSE in the source distribution or at
https://www.openssl.org/source/license.html
*/
#include 
#include "internal/cryptlib.h"
#include 
#include 
#include 
#include "internal/evp_int.h"

const EVP_CIPHER *EVP_get_default_cipher(void)
{
return NULL;
}

/*

use MD5 as default:
X509_REQ_to_X509 x509_r2x.c
X509_issuer_and_serial_hash x509_cmp.c
X509_NAME_hash_old x509_cmp.c
PEM_ASN1_write_bio pem_lib.c
*/
const EVP_MD *EVP_get_default_digest(void)
{
#if !defined(OPENSSL_NO_MD5)
return EVP_md5();
#elif !defined(OPENSSL_NO_SHA)
return EVP_sha1();
#elif !defined(OPENSSL_NO_SM3)
return EVP_sm3();
#elif !defined(OPENSSL_NO_RIPEMD)
return EVP_rmd160();
#else
return NULL;
#endif
}
static void cipher_name_len(const EVP_CIPHER *cipher, const char *from,
const char *to, void *x)
{
*((int *)x) += strlen(EVP_CIPHER_name(cipher));
}

static void cipher_name(const EVP_CIPHER *cipher, const char *from,
const char *to, void *x)
{
strcat((char *)x, EVP_CIPHER_name(cipher));
}

char *EVP_get_ciphernames(int aliases)
{
char *ret = NULL;
int len = 0;
EVP_CIPHER_do_all_sorted(cipher_name_len, &len);

ret = OPENSSL_zalloc(len);
if (!ret) {
	return NULL;
}

EVP_CIPHER_do_all_sorted(cipher_name, ret);
return ret;
}

char *EVP_get_digestnames(int aliases)
{
return "sm3:sha1:sha256";
}

接下来就可以编译了,编译方式和上述的openssl一样

perl Configure VC-WIN32 -no-asm --prefix="C:\Program Files (x86)\GmSSL-master\build"

nmake install

安装到最后,会报错perl拷贝,这个不会影响gmssl的使用,大家不要担心。

Windows平台 VS2019 编译 openssl1.1.1以及gmssl_第7张图片

 

你可能感兴趣的:(openssl,windows,microsoft,perl)