首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >SSL探03

SSL探03

作者头像
全栈程序员站长
发布2022-01-12 14:38:04
发布2022-01-12 14:38:04
27800
代码可运行
举报
运行总次数:0
代码可运行

本文探讨了Openssl的Engine机械.Openssl硬件引擎(Engine)可以使用户比較easy地将自己的硬件增加到openssl中去,替换其提供的软件算法.

ENGINE 是 OPENSSL 预留的用以载入第三方加密库引擎,主要包含了动态库载入的代码和加密函数指针管理的一系列接口.如果要使用 Engine(如果你已经载入上 该 Engine 了 ) , 那 么 首 先 要 加 载 该Engine(比方 ENGINE_load_XXXX),然后选择要使用的算法或者使用支持的全部加密算法。这样你的应用程序在调用加解密算法时,它就会指向你载入的动态库里的加解密算法,而不是原先的 OPENSSL的 库里的加解密算法.

使用Engine的基本流程:

①//Engine_load_xxxx(); 初始化Engine对象,对engine的属性及方法进行设置(自己实现的算法),将engine载入到系统中, ②//e =Engine_by_id(“ID_ali”); 获取engine ③选择使用哪些算法 ENGINE_set_default(ENGINE *e, int Flag) 当中 Flag 的说明例如以下: ENGINE_METHOD_ALL 使用全部存在的算法(默认) ENGINE_METHOD_RSA 仅使用 RSA 算法 ENGINE_METHOD_DSA 仅使用 DSA 算法 ENGINE_METHOD_DH 仅使用 DH 算法 ENGINE_METHOD_RAND 仅使用随机数算法 ENGINE_METHOD_CIPHERS 仅使用对称加解密算法 ENGINE_METHOD_DIGESTS 仅使用摘要算法 ④//以对称加密为例,将engine传入方法就可以.

EVP_EncryptInit_ex(ctx,ciper,e,key,iv); 这样便使用engine中的算法替换掉了SSL的自带算法.

说明:

a.ENGINE_load_hwcipher();这种方法进行Engine的初始化.

代码语言:javascript
代码运行次数:0
运行
复制
void ENGINE_load_hwcipher() {
	ENGINE *e_hw = engine_hwcipher();
	if (!e_hw)
		return;
	ENGINE_add(e_hw);
	ENGINE_free(e_hw);
	ERR_clear_error();
}

当中又调用engine_hwcipher()

代码语言:javascript
代码运行次数:0
运行
复制
static ENGINE *engine_hwcipher(void) {
	ENGINE *ret = ENGINE_new();
	if (!ret)
		return NULL;
	if (!bind_helper(ret)) {
		ENGINE_free(ret);
		return NULL;
	}
	return ret;
}

engine_hwcipher()中调用bind_helper(ENGINE *e);来看看bind_helper(ENGINE *e)的实现

代码语言:javascript
代码运行次数:0
运行
复制
static int bind_helper(ENGINE *e) {
	int ret;

	ret = ENGINE_set_id(e, engine_hw_id);
	if (ret != 1) {
		printf("ENGINE_set_id failed\n");
		return 0;
	}
	ret = ENGINE_set_name(e, engine_hw_name);
	if (ret != 1) {
		printf("ENGINE_set_name failed\n");
		return 0;
	}
	ret = ENGINE_set_RSA(e, &hw_rsa);
	if (ret != 1) {
		printf("ENGINE_set_RSA failed\n");
		return 0;
	}
	ret = ENGINE_set_RAND(e, &hw_rand);
	if (ret != 1) {
		printf("ENGINE_set_RAND failed\n");
		return 0;
	}
	ret = ENGINE_set_destroy_function(e, hw_destroy);
	if (ret != 1) {
		printf("ENGINE_set_destroy_function failed\n");
		return 0;
	}
	ret = ENGINE_set_init_function(e, hw_init);
	if (ret != 1) {
		printf("ENGINE_set_init_function failed\n");
		return 0;
	}
	ret = ENGINE_set_finish_function(e, hw_finish);
	if (ret != 1) {
		printf("ENGINE_set_finish_function failed\n");
		return 0;
	}
	ret = ENGINE_set_ctrl_function(e, hw_ctrl);
	if (ret != 1) {
		printf("ENGINE_set_ctrl_function failed\n");
		return 0;
	}
	ret = ENGINE_set_load_privkey_function(e, hw_load_privkey);
	if (ret != 1) {
		printf("ENGINE_set_load_privkey_function failed\n");
		return 0;
	}
	ret = ENGINE_set_load_pubkey_function(e, hw_load_pubkey);
	if (ret != 1) {
		printf("ENGINE_set_load_pubkey_function failed\n");
		return 0;
	}
	ret = ENGINE_set_cmd_defns(e, hw_cmd_defns);
	if (ret != 1) {
		printf("ENGINE_set_cmd_defns failed\n");
		return 0;
	}
	ret = ENGINE_set_ciphers(e, hw_ciphers);
	if (ret != 1) {
		printf("ENGINE_set_ciphers failed\n");
		return 0;
	}
	ret = ENGINE_set_digests(e, hw_md);
	if (ret != 1) {
		printf("ENGINE_set_digests failed\n");
		return 0;
	}
	return 1;
}

bind_helper(ENGINE *e)方法中对engine结构体中的属性及方法进行设置,自己实现各种加解密算法.

b.至此,engine的初始化工作完毕,然后 e = ENGINE_by_id(“ID_hw”);获取自己须要的engine.

c.选择要使用的算法.ENGINE_set_default(ENGINE *e, int Flag)

c.将engine传入加解密调用函数就可以. EVP_EncryptInit_ex(&ciph_ctx, cipher, e, key, iv);

这样便实现了使用自己定义算法替换openssl中默认算法.具体的代码可參考OpenSSL 源码中的 Demos/Engines

版权声明:本文博主原创文章。博客,未经同意不得转载。

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/117021.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档