OpenSSL 错误 0200B009 错误的文件描述符

标签 openssl encryption demo

当我运行 OpenSSL CMS 加密和解密演示时,我收到以下错误:

Error Decrypting Data
2900476676:error:0200B009:system library:fread:Bad file descriptor:bss_file.c:245:
2900476676:error:20082002:BIO routines:FILE_READ:system lib:bss_file.c:246:
2900476676:error:0606506D:digital envelope routines:EVP_DecryptFinal_ex:wrong final block length:evp_enc.c:460:

它发生在 CMS_decrypt() 方法中。

谁知道这是怎么回事?

更新#1:

我在 objective-c 中使用这个库(并且也在 C++ 中尝试过)。 它发生在这个部分:

    int error = CMS_decrypt(cms, rkey, rcert, /*out*/ bout, NULL, 0);
    if (!error) {
        fprintf(stderr, "Error Decrypting Data\n");
        ERR_print_errors_fp(stderr);
        printf("error code: %d\n", ERR_get_error());
        assert(false);
    }

更新#2:

添加了完整的解密源。

- (void) decryptOrig {
    BIO *in = NULL, *out = NULL, *tbio = NULL;
    X509 *rcert = NULL;
    EVP_PKEY *rkey = NULL;
    CMS_ContentInfo *cms = NULL;
    int ret = 1;

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];


    NSString *iosPathToFile = [NSString stringWithFormat:@"%@/encrypted.enc", documentsDirectory]; //[[NSBundle mainBundle] pathForResource:@"encrypted" ofType:@"enc"];
    NSString *iosPathToCertificate = [[NSBundle mainBundle] pathForResource:@"signer" ofType:@"pem"];
    NSString *iosPathToKey = [[NSBundle mainBundle] pathForResource:@"christof" ofType:@"key"];


    NSString *iosPathToOrigFinal = [NSString stringWithFormat:@"%@/original.txt", documentsDirectory];




    OpenSSL_add_all_algorithms();
    ERR_load_crypto_strings();

    /* Read in recipient certificate and private key */
    tbio = BIO_new_file([iosPathToCertificate cStringUsingEncoding:NSUTF8StringEncoding], "r");



    if (!tbio)
        goto err;

    rcert = PEM_read_bio_X509(tbio, NULL, 0, NULL);

    /*BIO *output = BIO_new(BIO_s_mem());
    X509_print(output, rcert);
    char *temp = malloc(50000);
    BIO_read(output, temp, 50000);

    printf("cert: %s", temp);*/

    //temp for output
    BIO *bout = BIO_new_fp (stdout, BIO_NOCLOSE);


    BIO_reset(tbio);

    rkey = PEM_read_bio_PrivateKey(tbio, NULL, 0, NULL);

    //EVP_PKEY_print_private(bout, rkey, 0, NULL);

    if (!rcert || !rkey)
        goto err;

    /* Open S/MIME message to decrypt */

    in = BIO_new_file([iosPathToFile cStringUsingEncoding:NSUTF8StringEncoding], "r");



    if (!in)
        goto err;

    /* Parse message */
    cms = SMIME_read_CMS(in, NULL);

    //CMS_ContentInfo_print_ctx(bout, cms, 0, NULL);


    if (!cms)
        goto err;

    out = BIO_new_file([iosPathToOrigFinal cStringUsingEncoding:NSUTF8StringEncoding], "w");
    NSLog(iosPathToOrigFinal);
    /*char *mytestoutput = malloc(50000);
    memset(mytestoutput, 0, 50000);
    out = BIO_new_mem_buf(mytestoutput, 50000);*/

    if (!out)
        assert(false);

    /* Decrypt S/MIME message */
    int error = CMS_decrypt(cms, rkey, rcert, out, NULL, 0);
    if (!error) {
        fprintf(stderr, "Error Decrypting Data\n");
        ERR_print_errors_fp(stderr);
        printf("error code: %d\n", ERR_get_error());
        assert(false);
    }


    ret = 0;

err:

    if (ret)
    {
        fprintf(stderr, "Error Decrypting Data\n");
        ERR_print_errors_fp(stderr);
    }

    if (cms)
        CMS_ContentInfo_free(cms);
    if (rcert)
        X509_free(rcert);
    if (rkey)
        EVP_PKEY_free(rkey);

    if (in)
        BIO_free(in);
    if (out)
        BIO_free(out);
    if (tbio)
        BIO_free(tbio);

    return ret;

}

我删除了 bout 并在加密方法中使用了 out

更新 #3:

会不会是对称加密类型有问题? CBC 等...?

最佳答案

部分问题可能是您为解密所做的方法调用有点麻烦 - 这是一个棘手的问题。

您有输出文件,它希望在其中找到输入内容文件 - 这也会在它希望放置纯文本的地方留下一个空的 bio,因此它会返回此错误。

我觉得应该是这样

int error = CMS_decrypt(cms, rkey, rcert, NULL, out, 0);

不是

int error = CMS_decrypt(cms, rkey, rcert, out, NULL, 0); // won't decrypt 

我认为实际上发生的事情是库中的方法签名本身在某个时候发生了变化,但是这个旧的方法签名仍然存在于 cms 功能的一些较旧的演示代码中。我假设他们改变了它以更好地适应他们的惯例。

祝你好运

关于OpenSSL 错误 0200B009 错误的文件描述符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9478874/

相关文章:

real-time - 有哪些实时数据源?

tensorflow - 如何在 tensorflow playground 的第 4 个数据集上实现 0(训练和测试)损失?

本地主机上的 fsockopen 演示

c - 如何使用 BIO 读取 ssl 响应的主体

c++ - 链接错误 : undefined reference to EVP_CIPHER_CTX_ and EVP_CIPHER_CTX_init

security - 为什么银行对 ATM 卡使用 4 针密码?

javascript - 如何在运行时创建 SignalR 组

node.js - 生成 SSL key 以使用 node.js

c++ - OpenSSL和JSON文件加解密

凯撒密码 (C) : Read from textfile