当前位置: 首页 > java >正文

使用GmSSL v3.1.1实现SM2证书认证

        1、首先使用gmssl命令生成根证书、客户端公私钥,然后使用根证书签发客户端证书;

        2、然后编写代码完成认证功能,使用根证书验证客户端证书是否由自己签发,然后使用客户端证书验证客户端私钥对随机数的签名是否正确。

        第一部分生成根证书和客户端证书的命令:

# 生成私钥,用于生成根证书
# gmssl 3.1.1要求必须使用密码加密私钥
gmssl sm2keygen -pass 123456 -out root.key# 生成自签名根证书
gmssl certgen \-C CN \-ST Beijing \-L Beijing \-O MyCA \-OU RootCA \-CN RootCA \-days 3650 \-key root.key \-pass 123456 \-key_usage keyCertSign \-out root.crt# 生成客户端公私钥
gmssl sm2keygen -pass 123456 -out client.key# 生成客户端证书请求(CSR)
gmssl reqgen \-C CN \-ST Beijing \-L Beijing \-O MyClient \-OU Client \-CN client.example.com \-key client.key \-pass 123456 \-out client.csr# 签发客户端证书
gmssl reqsign \-in client.csr \-days 3650 \-key_usage digitalSignature \-cacert root.crt \-key root.key \-pass 123456 \-out client.crt

        第二部分进行客户端证书认证和签名验签的代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <gmssl/x509.h>
#include <gmssl/sm2.h>
#include <gmssl/sm3.h>
#include <gmssl/pem.h>
#include <gmssl/error.h>
#include <gmssl/base64.h>#define ROOT_CERT_FILE   "root.crt"
#define CLIENT_CERT_FILE "client.crt"
#define CLIENT_KEY_FILE  "client.key"
#define ROOT_KEY_FILE    "root.key"
#define DATA_TO_SIGN     "This is the data to sign."// 读取加密的SM2私钥(PKCS#8 PEM),解密并导入SM2_KEY
int load_encrypted_sm2_private_key(const char *filename, const char *password, SM2_KEY *key) {FILE *fp = fopen(filename, "r");if (!fp) {perror(filename);return -1;}if (sm2_private_key_info_decrypt_from_pem(key, password, fp) != 1) {fprintf(stderr, "Failed to decrypt/load private key: %s\n", filename);fclose(fp);return -1;}fclose(fp);return 0;
}int main(void) {uint8_t root_cert[4096], client_cert[4096];size_t root_cert_len = 0, client_cert_len = 0;SM2_KEY client_key, client_pubkey;uint8_t dgst[32];uint8_t sig[SM2_MAX_SIGNATURE_SIZE];size_t siglen = 0;int ret = 1;// 1. 读取根证书FILE *fp = fopen(ROOT_CERT_FILE, "r");if (!fp) {perror("open root.crt");return 1;}if (x509_cert_from_pem(root_cert, &root_cert_len, sizeof(root_cert), fp) != 1) {fprintf(stderr, "Failed to load root certificate\n");fclose(fp);return 1;}fclose(fp);// 2. 读取客户端证书fp = fopen(CLIENT_CERT_FILE, "r");if (!fp) {perror("open client.crt");return 1;}if (x509_cert_from_pem(client_cert, &client_cert_len, sizeof(client_cert), fp) != 1) {fprintf(stderr, "Failed to load client certificate\n");fclose(fp);return 1;}fclose(fp);// 3. 验证客户端证书if (x509_cert_verify_by_ca_cert(client_cert, client_cert_len, root_cert,root_cert_len, SM2_DEFAULT_ID, SM2_DEFAULT_ID_LENGTH) != 1) {fprintf(stderr, "Client certificate verify failed\n");goto end;}printf("Client certificate verified successfully.\n");// 4. 读取加密的客户端私钥(需要口令)char client_pass[7] = {0x31,0x32,0x33,0x34,0x35,0x36,0x00};if (load_encrypted_sm2_private_key(CLIENT_KEY_FILE, client_pass, &client_key) != 0) {fprintf(stderr, "Failed to load client private key\n");goto end;}// 5. 计算数据摘要const uint8_t *data = (const uint8_t *)DATA_TO_SIGN;size_t data_len = strlen(DATA_TO_SIGN);sm3_digest(data, data_len, dgst);// 6. 签名if (sm2_sign(&client_key, dgst, sig, &siglen) != 1) {fprintf(stderr, "SM2 sign failed\n");goto end;}printf("Data signed successfully.\nSignature: ");for (size_t i = 0; i < siglen; i++) printf("%02x", sig[i]);printf("\n");// 7. 从证书中提取公钥if (x509_cert_get_subject_public_key(client_cert, client_cert_len, &client_pubkey) != 1) {fprintf(stderr, "Failed to extract public key from client certificate\n");goto end;}// 8. 验签if (sm2_verify(&client_pubkey, dgst, sig, siglen) != 1) {fprintf(stderr, "Signature verification failed\n");goto end;}printf("Signature verified successfully.\n");ret = 0;end://sm2_key_cleanup(&client_key);//sm2_key_cleanup(&client_pubkey);return ret;
}

        在centos7平台使用GmSSL V3.1.1亲测可行。

http://www.xdnf.cn/news/6339.html

相关文章:

  • Uniapp 安卓实现讯飞语音听写(复制即用)
  • WEB安全--Java安全--LazyMap_CC1利用链
  • RPC框架源码分析学习(二)
  • Spring+LangChain4j小智医疗项目
  • ultalytics代码中模型接收多层输入的处理
  • [训练和优化] 3. 模型优化
  • 学习小组实验1
  • 和为target问题汇总
  • Spark SQL 之 Analyzer
  • Ubuntu Linux bash的相关默认配置文件内容 .profile .bashrc, /etc/profile, /etc/bash.bashrc等
  • 2025年5月-信息系统项目管理师高级-软考高项-成本计算题
  • 为什么doris是实时的?
  • 一个基于 Spring Boot 的实现,用于代理百度 AI 的 OCR 接口
  • 06Spring—AOP @Around环绕通知的进阶思考
  • 【愚公系列】《Manus极简入门》040-科技与组织升级顾问:“项目掌舵人”
  • 第35周Zookkeeper+Dubbo JDK不同版本介绍
  • 75.xilinx复数乘法器IP核调试
  • 麒麟系统下Tomcat部署Java Web程序(WAR包)及全链路问题排查指南
  • R语言的专业网站top5推荐
  • recvfrom和sendto函数中地址参数的作用
  • Redis学习打卡-Day2-缓存更新策略、主动更新策略、缓存穿透、缓存雪崩、缓存击穿
  • Cocos Creator 3.8.5 构建依赖环境配置文档
  • 从零开始创建一个 Next.js 项目并实现一个 TodoList 示例
  • 计算机网络八股
  • Reactor模型详解与C++实现
  • 云原生数据库排障新挑战:AI驱动与分布式架构深度解析
  • 什么是抖动以及如何使用抖动缓冲区来减少抖动?
  • 深度解析IP静态的工作原理,IP静态的应用场景又哪些?
  • 实现可靠的 WebSocket 连接:心跳与自动重连的最佳实践
  • [c语言日寄]数据结构:栈