2012-02-28 7 views
2

私は、OpenSSL CMSの暗号化を実行し、デモを解読し、私は次のエラーが表示されますOpenSSLのエラー0200B009不正なファイルディスクリプタ

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を削除し、暗号化方式

アップデート#3でoutを使用しました:対称暗号化に問題があることを

が可能ですタイプ? CBCなど...?

+0

実行するコマンドラインは何ですか? – sarnold

+0

@sarnold:申し訳ありませんが、私はC++(およびobjective-c)でライブラリを使用していることを忘れていました。私はこれを更新するつもりです。 – Chris

+0

ちなみに、パラメータを初期化するコードを追加できますか?私は['CMS_decrypt()'](http://www.openssl.org/docs/crypto/CMS_decrypt.html)が成功すれば '1'を返し、失敗した場合は' 0'を返します。 'if(!error)'は正解ですが扱いにくいです。その変数が 'success'という名前であれば、もっと幸せになると思います。 – sarnold

答えて

0

一部の問題は、復号化のためのメソッド呼び出しがやや面倒であることが考えられます。これは難しい問題です。

あなたは、入力内容ファイルを見つけることが期待される出力ファイルを持っています。これはプレーンテキストを置くことを期待しているため、このエラーも発生します。ここ

は、それは私が何が起こったことはメソッドのシグネチャ自体はいくつかの点で、ライブラリに変更しましたが、この古いメソッドシグネチャことを実際にだったと思い

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

ない

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

を考えるはず方法ですまだCMS機能の古いデモコードになっています。私は彼らが彼らの慣習に合わせてそれを変更したと思う。

幸運

関連する問題