2012-02-28 1 views
2

I에는 OpenSSL CMS의 암호화를 실행하고 나는 다음과 같은 오류가 발생 데모 해독 할 때 : 그것은 CMS_decrypt() 방법에서 발생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: 

합니다.

무엇이 잘못되었는지 알고 계십니까?

업데이트 # 1 :

내가 (그리고 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

문제의 일부는 암호 해독을 위해 작성한 메소드 호출이 약간 번거로운 일일 수 있습니다. 이는 까다로운 문제입니다.

입력 내용 파일을 찾을 수있는 출력 파일이 있습니다.이 파일에 일반 텍스트를 넣을 것으로 예상되는 null 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 기능의 이전 데모 코드에 여전히 있습니다. 나는 그들이 그들의 관습에 더 잘 부합하도록 그것을 바꿨다고 생각한다.

행운을 빕니다