基于 TrueLicense 的项目证书验证

2,052 阅读2分钟

基于 TrueLicense 的项目证书验证

使用场景

1、 开发的软件产品在交付使用的时候,往往有一段时间的试用期,这期间我们不希望自己的代码被客户二次拷贝,这个时候 license 就派上用场了,license 的功能包括设定有效期、绑定 ip、绑定 mac 等。

2、 授权方直接生成一个 license 给使用方使用,如果需要延长试用期,也只需要重新生成一份 license 即可,无需手动修改源代码。

原理简介

1、TrueLicense 是一个开源的证书管理引擎,详细介绍见 https://truelicense.java.net/

2、license 授权机制的原理

  • 生成密钥对,包含私钥和公钥。

  • 授权者保留私钥,使用私钥对授权信息诸如使用截止日期,mac 地址等内容生成 license 签名证书。

  • 公钥给使用者,放在代码中使用,用于验证 license 签名证书是否符合使用条件

生成证书

利用jdk keytool工具制作证书

keytool -genkeypair -keysize 1024 -validity 3650 -alias "privateKey" -keystore "privateKeys.keystore" -storepass "deepglint_store_pwd123" -keypass "deepglint_key_pwd123" -dname "CN=localhost, OU=localhost, O=localhost, L=SH, ST=SH, C=CN"

利用jdk keytool工具导出证书文件

keytool -exportcert -alias "privateKey" -keystore "privateKeys.keystore" -storepass "deepglint_store_pwd123" -file "certfile.cer"

利用jdk keytool工具将证书文件导入到证书库中

keytool -import -alias "publicCert" -file "certfile.cer" -keystore "publicCerts.keystore" -storepass "deepglint_store_pwd123"

两个子项目说明

  • lic-auth-server:用于开发者给客户生成License证书的示例代码
  • lic-auth-client:模拟需要给客户部署的业务项目

获取服务器信息

http://127.0.0.1:10000/license/getServerInfos

给客户机生成license

http://127.0.0.1:10000/license/generateLicense

header
Content-Type application/json;charset=UTF-8

{
 "subject""license_sub",    #证书subject
 "privateAlias""privateKey",  #秘钥别名       
 "keyPass""deepglint_key_pwd123",    #秘钥口令
 "storePass""deepglint_store_pwd123",   #秘钥库口令
 "licensePath""/Users/mengfanxiao/Documents/work/license/cert/license.lic",   #存放license文件位置
 "privateKeysStorePath""/Users/mengfanxiao/Documents/work/license/cert/privateKeys.keystore",     #秘钥库文件文件
 "issuedTime""2020-11-25 00:00:01",  #license有效期起始时间  
 "expiryTime""2020-11-25 22:00:00",   #license有效期截止时间 
 "licenseCheckModel": {
  "ipAddress": ["192.168.5.121"],       #客户机ip
  "macAddress": ["A4-83-E7-BE-3D-D9"],  #客户机mac地址   
  "cpuSerial""",         #客户机cpu序列号
  "mainBoardSerial"""    #客户机主板序列号
 }
}

在客户机使用license

在项目启动的时候安装证书

访问接口进行测试

http://127.0.0.1:10001/auth/api/1.0/getUserInfo

header

Content-Type application/json;charset=UTF-8

如果证书过期

源码

https://gitee.com/pingfanrenbiji/lic-auth