linux 在使用libnss API PK11_Authenticate()时获取SEC_ERROR_BAD_PASSWORD错误

bsxbgnwa  于 2022-11-22  发布在  Linux
关注(0)|答案(1)|浏览(185)

我们有一个使用案例来列出来自USB加密狗(ePass 2003 Auto等类型)的证书,这些证书将用于签署PDF。

环境:Ubuntu 20.04
:libnss* API和其他类似于libssl的库加密库
LibreOffice检测/列出证书所执行的步骤:

1.与USB加密狗交互的lib/驱动程序可在供应商的站点上获得。例如,在我们的示例中--〉https://www.hypersecu.com/updates-india。需要根据上述说明安装该驱动程序。我们将在**/usr/lib中安装libcastle_v2.so.1.0.0**
1.创建类似**/home//.pki/nssdb的nssdb路径
1.使用
modutil**(一个libnss实用程序)链接已创建的nssdb路径、usb插槽/令牌和lib,命令如下:

  • modutil -数据库目录-添加“”-库文件/usr/lib/libcastle_v2.so.1.0.0*

1.要确认是否已添加,请使用以下命令:

  • modutil -数据库目录sql:〈nssdb路径〉-列表 *
    LibreOffice(开发人员-7.3.6.2):

1.工具--〉选项--〉安全--〉证书
1.设置指向nssdb路径的证书路径
1.重新启动应用程序
1.文件--〉数字签名
1.输入USB标记密码
1.将列出证书

我们尝试的是:

PK11_SetPasswordFunc( GetPasswordFunction ) ; // GetPasswordFunction() returns the password of USB token

if (NSS_InitReadWrite("/home/<user>/.pki/nssdb") == SECSuccess)  // Initialize nssdb
{
    printf("\n----------->NSS_Init successful");

    cert_handle = CERT_GetDefaultCertDB();

    if (cert_handle == NULL)
    {

        error = PR_GetError();
        printf("\n----------->Unable to get cert_handle:%s (%d)", PR_ErrorToName(error), (int)error);
    }
    else
    {
        printf("\n----------->Got cert_handle");

        PK11SlotList * slotList = PK11_GetAllTokens( CKM_INVALID_MECHANISM, PR_FALSE, PR_FALSE, NULL ) ;

        if(slotList == NULL) {

            error = PR_GetError();
            printf("\n----------->PK11_GetAllTokens failed !:%s (%d)", PR_ErrorToName(error), (int)error);

        } else {

            PK11SlotInfo * usb_token = NULL;

            for (PK11SlotListElement* slotEle = slotList->head ; slotEle != NULL; slotEle = slotEle->next)
            {
                PK11SlotInfo * pSlot = slotEle->slot ;

                if(pSlot != NULL)
                {
                    printf("\n----------->SlotName(%s) TokenName(%s)", PK11_GetSlotName(pSlot), PK11_GetTokenName(pSlot));

                    if(PK11_IsHW(pSlot) && PK11_IsRemovable(pSlot)){ // select the USB-token in the slots list

                        usb_token = pSlot;
                        break;
                    }
                } else {
                    printf("\n----------->pSlot is empty");
                }
            }// end of for

            if(usb_token != NULL){

                printf("\n----------->Found USB-TOKEN SlotName(%s) TokenName(%s)", PK11_GetSlotName(usb_token), PK11_GetTokenName(usb_token));

                if (PK11_NeedLogin(usb_token)){

                    SECStatus nRet = PK11_Authenticate(usb_token, PR_TRUE, NULL);

                    if(nRet != SECSuccess){
                        error = PR_GetError();
                        printf("\n----------->PK11_Authenticate failed !:%s (%d)", PR_ErrorToName(error), (int)error);
                        printf("\n----------->PORT_GetError() !:(%d)", PORT_GetError());

                        if(PORT_GetError() != SEC_ERROR_IO) {
                            printf("\n----------->NoPassword Exception");
                        } else {
                            printf("\n----------->Some other Exception");
                        }
                    }else {
                        printf("\n----------->PK11_Authenticate successful !");
                    }
                }
            }

            PK11_FreeSlotList(slotList);
        }
    }

    PK11_SetPasswordFunc( NULL ) ;
    PK11_LogoutAll();
    NSS_Shutdown();
}
else
{

    error = PR_GetError();
    printf("\n----------->NSS_Init failed:%s (%d)", PR_ErrorToName(error), (int)error);
}

即使GetPasswordFunction()中返回的密码是正确的,我们也会得到以下错误:PK 11身份验证失败!:错误密码错误(-8177)
任何帮助都是感激不尽的!提前感谢!

vuktfyat

vuktfyat1#

解决了!
在调试PK11_Authenticate()和流程中的相关函数时,发现从密码回调函数(根据我的示例代码--〉GetPasswordFunction())返回的密码为NULL。这很奇怪,因为我已经调试了该函数并确保密码正确返回。
在检查libnss中的其他函数时,我开始了解PORT_Alloc(),它是一个类似于malloc()的内存分配器(secport. h)。我使用了普通的C风格字符串内存分配。在尝试使用PORT_Alloc()进行分配时,它工作了!

相关问题