【问题标题】:error:0906D06C:PEM routines:PEM_read_bio:no start错误:0906D06C:PEM 例程:PEM_read_bio:无启动
【发布时间】:2018-03-17 03:34:34
【问题描述】:

得到这个非常烦人的错误。 error:0906D06C:PEM routines:PEM_read_bio:no start

代码:

RSA* publickey = cWrapper.getPublicKey("C:/rsa-stuff/public.pem");
QByteArray plain = "The man in the black fled into the desert and the gunslinger followed...";
QByteArray encrypted = cWrapper.encryptRSA(publickey, plain);

在 encryptRSA() 中:

const char* publicKeyStr = data.constData();
qDebug() << publicKeyStr;
BIO* bio = BIO_new_mem_buf((void*)publicKeyStr, -1);
BIO_set_flags(bio, BIO_FLAGS_BASE64_NO_NL);
RSA* rsaPubKey = PEM_read_bio_RSAPublicKey(bio, NULL, NULL, NULL);
if(!rsaPubKey) {
  qDebug() << "Could not load public key, " << ERR_error_string(ERR_get_error(), NULL); // error is here
}
BIO_free(bio);

这是我读取文件的方式:

QByteArray data;
QFile file(filename);

if(!file.open(QFile::ReadOnly))
{
    printf("Error reading file: %s\n", file.errorString());
    return data;
}

data = file.readAll();
file.close();
return data;

当我打印出 publicKeyStr 时,看起来不错。这是启用了所有字符的记事本++ 视图:

有人知道我做错了什么吗?超级烦人的问题:(

首先,这不是this 的问题,因为我没有得到受信任的部分。无论如何,我确实尝试了所有“解决方案”,但都没有奏效,同样的错误。

【问题讨论】:

标签: c++ windows qt openssl


【解决方案1】:

您的 RSA 公钥采用 SubjectPublicKeyInfo PEM 格式,但您正尝试使用 PEM_read_bio_RSAPublicKey 读取它,该PEM_read_bio_RSAPublicKey 尝试读取 PKCS#1 格式的 PEM RSA 密钥。尝试改用PEM_read_bio_RSA_PUBKEY

https://www.openssl.org/docs/man1.1.0/crypto/PEM_read_bio_RSAPublicKey.html

【讨论】:

    【解决方案2】:

    我在移植的 openSSL1.1.0f 上遇到了同样的错误。从 mqtt 客户端连接读取根证书时,错误出现在我的记录器中,直到我发现我已将 ERR_put_error() 直接转发到我的记录器,而在 openssl 中 - “真实”错误处理保存在ERR_STATE 错误缓冲区,因此有时(就像在这种情况下),错误是“预期的”,并且 ERR_STATE 错误缓冲区被清除(在任何人检查之前)。

    在 crypto/pem/pem_info.c,第 65 行:

    i = PEM_read_bio(bp, &name, &header, &data, &len);
        if (i == 0) {
            error = ERR_GET_REASON(ERR_peek_last_error());
            if (error == PEM_R_NO_START_LINE) {
                ERR_clear_error();
                break;
            }
            goto err;
    

    意味着它通过 PEM_read_bio 中的 BIO_gets 运行,直到它返回零,如果你得到这个 PEM_R_NO_START_LINE,那么这只是一种说它完成的方式。

    不过,到那时,错误已经出现在我的记录器中。因此,对于任何对他或她直接从 ERR_put_error 转发的错误感到困惑的人,请在错误处理例程中使用ERR_print_errors_fp(stderr);。就我而言,因为我没有标准错误,所以我做了一个补丁版本,比如:

        void errorhandling()
        {
            unsigned long l;
            char buf[256];
            const char *file, *data;
            int line, flags;
    
            while ((l = ERR_get_error_line_data(&file, &line, &data, &flags)) != 0)
            {
                ERR_error_string_n(l, buf, sizeof buf);
                printf("%s:%s:%d:%s\n", buf, file, line, (flags & ERR_TXT_STRING) ? data : "");
            }
        }
    

    【讨论】:

      猜你喜欢
      • 2015-09-11
      • 2020-04-08
      • 2016-06-26
      • 2011-04-05
      • 2018-12-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-03
      相关资源
      最近更新 更多