Kiran-authentication-devices源码探秘:UKey设备绑定与认证流程的实现原理

📅 2026/7/3 14:16:03 👁️ 阅读次数 📝 编程学习
Kiran-authentication-devices源码探秘:UKey设备绑定与认证流程的实现原理

Kiran-authentication-devices源码探秘:UKey设备绑定与认证流程的实现原理

【免费下载链接】kiran-authentication-devicesKiran authentication services Management Device Compatibility layer项目地址: https://gitcode.com/openeuler/kiran-authentication-devices

前往项目官网免费下载:https://ar.openeuler.org/ar/

Kiran-authentication-devices是openEuler生态中一款重要的认证设备兼容层项目,它为UKey等各类认证设备提供了统一的管理接口。本文将深入剖析UKey设备的绑定与认证流程实现原理,帮助开发者快速理解核心功能模块。

UKey设备绑定流程的核心实现

UKey设备绑定是用户使用UKey进行身份认证的前提,该功能主要在src/device/ukey/ukey-skf-device.cpp文件中实现。整个绑定过程可以分为设备初始化、容器创建、密钥对生成和特征存储四个关键步骤。

设备初始化与序列号获取

当UKey设备插入时,系统会通过UKeySKFDevice类的构造函数进行初始化。设备首先会尝试获取UKey的序列号,若获取失败则会启动定时器每隔1秒重试一次,直到成功获取或超时。

UKeySKFDevice::UKeySKFDevice(const QString &vid, const QString &pid, DriverPtr driver, QObject *parent) : AuthDevice{vid, pid, driver, parent} { setDeviceType(DEVICE_TYPE_UKey); setDriverName(ConfigHelper::getDriverName(vid, pid)); m_driverLibPath = ConfigHelper::getLibPath(vid, pid); if (!initSerialNumber()) { m_reInitSerialNumberTimer.start(1000); } connect(&m_reInitSerialNumberTimer, &QTimer::timeout, this, &UKeySKFDevice::initSerialNumber); }

容器创建与密钥对生成

在绑定过程中,系统会先检查UKey是否已绑定。若未绑定,则加载对应的驱动库,连接设备并创建应用容器。容器创建完成后,系统会生成ECC密钥对,公钥将用于后续的身份验证。

ULONG UKeySKFDevice::createContainer(const QString &pin, DEVHANDLE devHandle, HAPPLICATION *appHandle, HCONTAINER *containerHandle) { ULONG ulReval = m_driver->devAuth(devHandle); if (ulReval != SAR_OK) { KLOG_ERROR() << "Device auth failure: " << m_driver->getErrorReason(ulReval); return ulReval; } ulReval = m_driver->createApplication(devHandle, pin, UKEY_APP_NAME, appHandle); if (ulReval != SAR_OK) { KLOG_ERROR() << "create application failed:" << m_driver->getErrorReason(ulReval); return ulReval; } ulReval = m_driver->createContainer(*appHandle, pin, UKEY_CONTAINER_NAME, &m_retryCount, containerHandle); return ulReval; }

特征存储与绑定完成

公钥生成后,系统会将其进行MD5哈希处理生成唯一的特征ID,并将公钥和设备信息存储到特征数据库中。存储成功后,绑定流程完成并通知用户。

QString featureID = QCryptographicHash::hash(keyFeature, QCryptographicHash::Md5).toHex(); DeviceInfo deviceInfo = this->deviceInfo(); if (FeatureDB::getInstance()->addFeature(featureID, keyFeature, deviceInfo, deviceType(), deviceSerialNumber())) { notifyUKeyEnrollProcess(ENROLL_PROCESS_SUCCESS, SAR_OK, featureID); }

UKey设备认证流程的实现细节

认证流程是UKey设备的核心功能,它通过验证用户提供的PIN码和UKey中的密钥对来确认用户身份。认证功能同样在src/device/ukey/ukey-skf-device.cpp文件中实现。

认证初始化与PIN码验证

认证开始时,系统会先检查用户提供的PIN码是否为空。若PIN码有效,则加载驱动库并获取需要验证的特征列表。

void UKeySKFDevice::doingIdentifyStart(const QString &value) { QJsonValue ukeyValue = Utils::getValueFromJsonString(value, AUTH_DEVICE_JSON_KEY_UKEY); auto jsonObject = ukeyValue.toObject(); QString pin = jsonObject.value(AUTH_DEVICE_JSON_KEY_PIN).toString(); if (pin.isEmpty()) { QString message = tr("The pin code cannot be empty!"); Q_EMIT m_dbusAdaptor->IdentifyStatus("", IDENTIFY_STATUS_NOT_MATCH, message); internalStopIdentify(); return; } m_driver = new UKeySKFDriver(); if (!m_driver->loadLibrary(m_driverLibPath)) { KLOG_ERROR() << "load library failed"; notifyUKeyEnrollProcess(ENROLL_PROCESS_FAIL); internalStopIdentify(); return; } }

密钥验证与身份确认

系统会遍历存储的公钥列表,尝试使用用户提供的PIN码打开UKey中的应用和容器,然后使用私钥对数据进行签名,并使用存储的公钥验证签名是否有效。若验证通过,则确认用户身份。

void UKeySKFDevice::identifyKeyFeature(const QString &pin, QByteArray keyFeature) { DEVHANDLE devHandle = m_driver->connectDev(deviceSerialNumber()); if (!devHandle) { notifyUKeyIdentifyProcess(IDENTIFY_PROCESS_NO_MATCH); return; } // 打开应用和容器 // ... ECCSIGNATUREBLOB Signature = {0}; ret = m_driver->authSignData(containerHandle, devHandle, Signature); if (ret != SAR_OK) { KLOG_DEBUG() << "auth sign data failed:" << m_driver->getErrorReason(ret); notifyUKeyIdentifyProcess(IDENTIFY_PROCESS_NO_MATCH, ret); return; } ECCPUBLICKEYBLOB *eccPubKey = (ECCPUBLICKEYBLOB *)keyFeature.data(); ret = m_driver->verifyData(devHandle, Signature, eccPubKey); if (ret != SAR_OK) { KLOG_DEBUG() << "verify data failed:" << m_driver->getErrorReason(ret); notifyUKeyIdentifyProcess(IDENTIFY_PROCESS_NO_MATCH, ret); } else { QString featureID = FeatureDB::getInstance()->getFeatureID(keyFeature); notifyUKeyIdentifyProcess(IDENTIFY_PROCESS_MACTCH, ret, featureID); } }

关键配置文件解析

UKey设备的配置信息主要存储在以下几个文件中:

  • data/ukey-manager.conf: UKey管理器的配置文件
  • data/com.kylinsec.Kiran.AuthDevice.Device.conf: 设备配置文件
  • data/driver.conf: 驱动配置文件

这些配置文件定义了UKey设备的驱动路径、VID/PID信息和其他关键参数,确保系统能够正确识别和使用UKey设备。

总结与使用建议

Kiran-authentication-devices项目通过UKeySKFDevice类实现了UKey设备的绑定与认证功能,核心流程包括设备初始化、容器创建、密钥对生成、特征存储和身份验证等步骤。开发者在使用或扩展该功能时,建议:

  1. 深入理解src/device/ukey/ukey-skf-device.cpp中的核心方法
  2. 注意配置文件中的驱动路径和设备信息设置
  3. 处理好PIN码错误和设备连接失败等异常情况
  4. 遵循项目的许可证要求,保留必要的版权信息

通过本文的解析,相信开发者能够快速掌握UKey设备绑定与认证流程的实现原理,并在此基础上进行二次开发或问题排查。

【免费下载链接】kiran-authentication-devicesKiran authentication services Management Device Compatibility layer项目地址: https://gitcode.com/openeuler/kiran-authentication-devices

创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考