2023-03-08 在licensecc中如何使用自己的公钥?

今天收到一位朋友的留言,说生成license时只用到了私钥,并没有体现公钥在什么地方,这样自己的软件如何去检查是否是自己的公钥生成的license呢?

确实我在上一篇 2023-02-17 使用licensecc生成证书的坑 中也只是提到了私钥,并没有提到公钥。
因为我当时也没去深究这个问题。

后面我在真正验证如何验证license时才去做了下研究。

正确的步骤应该是将licensecc作为git的submodule引入自己的项目中,在自己的CMakeLists.txt项目中去定义一个component,或是在CMake中定义LCC_PROJECT_NAME这个变量,这样项目会自动生成公钥和私钥。
此时如果要对这个软件授权,就需要将这里自动生成的私钥拷贝出来,用lccgen 去生成证书。

下面给出大致的关键步骤:

1、CMakeLists.txt文件配置

# 为了能够让cmake自动找到licensecc
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/cmake/")

#方式一
find_package(licensecc 2.0.0 REQUIRED COMPONENTS "testLCCLicensecc")
#方式二
set(LCC_PROJECT_NAME "testLCCLicensecc" CACHE STRING)

# 链接licensecc库
target_link_libraries(testLCC PRIVATE Qt${QT_VERSION_MAJOR}::Widgets licensecc::licensecc_static)

2、添加licensecc作为submodule

git submodule add -b develop https://github.com/open-license-manager/licensecc.git extern/licensecc
git submodule update --init --recursive 

3、在用cmake 完成config和generate后,会在项目目录中发现多了几个东西


image.png

在project文件夹下就有可用于生成license的私钥

image.png

4、可以用以下代码用于验证license的有效性

#include "mainwindow.h"

#include <QApplication>
#include <iostream>
#include <licensecc/licensecc.h>

using namespace std;
int main(int argc, char *argv[]) {
  map<LCC_EVENT_TYPE, string> stringByEventType;
  stringByEventType[LICENSE_OK] = "OK ";
  stringByEventType[LICENSE_FILE_NOT_FOUND] = "license file not found ";
  stringByEventType[LICENSE_SERVER_NOT_FOUND] =
      "license server can't be contacted ";
  stringByEventType[ENVIRONMENT_VARIABLE_NOT_DEFINED] =
      "environment variable not defined ";
  stringByEventType[FILE_FORMAT_NOT_RECOGNIZED] =
      "license file has invalid format (not .ini file) ";
  stringByEventType[LICENSE_MALFORMED] =
      "some mandatory field are missing, or data can't be fully read. ";
  stringByEventType[PRODUCT_NOT_LICENSED] = "this product was not licensed ";
  stringByEventType[PRODUCT_EXPIRED] = "license expired ";
  stringByEventType[LICENSE_CORRUPTED] =
      "license signature didn't match with current license ";
  stringByEventType[IDENTIFIERS_MISMATCH] =
      "Calculated identifier and the one provided in license didn't match";

  LicenseInfo licenseInfo;
  size_t pc_id_sz = LCC_API_PC_IDENTIFIER_SIZE + 1;
  char pc_identifier[LCC_API_PC_IDENTIFIER_SIZE + 1];
  identify_pc(STRATEGY_DEFAULT, pc_identifier, &pc_id_sz, nullptr);
  cout << "hardware id is :" << endl;
  cout << "    " << pc_identifier << endl;

  LCC_EVENT_TYPE result = acquire_license(nullptr, nullptr, &licenseInfo);

  if (result == LICENSE_OK) {
    cout << "license OK" << endl;
    if (!licenseInfo.linked_to_pc) {
      cout << "No hardware signature in license file. This is a 'demo' license "
              "that works on every pc."
           << endl
           << "To generate a 'single pc' license call 'issue license' with "
              "option -s "
           << endl
           << "and the hardware identifier obtained before." << endl
           << endl;
    }
  }
  if (result != LICENSE_OK) {
    cout << "license ERROR :" << endl;
    cout << "    " << stringByEventType[result].c_str() << endl;
    if (identify_pc(STRATEGY_DEFAULT, pc_identifier, &pc_id_sz, nullptr)) {
      cout << "hardware id is :" << endl;
      cout << "    " << pc_identifier << endl;
    } else {
      cerr << "errors in identify_pc" << endl;
    }
  }

  QApplication a(argc, argv);
  MainWindow w;
  w.show();
  return a.exec();
}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
禁止转载,如需转载请通过简信或评论联系作者。

推荐阅读更多精彩内容