2014-08-29 83 views
1

我試圖生成使用OpenSSL具有以下功能RSA密鑰:分割故障,則產生RSA密鑰使用OpenSSL的

RSA *genRSA() { 
    clear(); 
    mvprintw(0, 0, "Generating RSA key...\n"); 
    RAND_load_file("/dev/random", 4096); 
    BIGNUM *e = BN_new(); 
    BN_set_word(e, RSA_F4); 
    RSA *rsa; 
    while (getch() != '\n'); // the program does reach this point 
    if (!RSA_generate_key_ex(rsa, 4096, e, 0)) { // seg fault must occur on this line 
    while (getch() != '\n'); // never gets here 
    printw("ERROR: Failed to create RSA key\n"); 
    return NULL; 
    } 
    while (getch() != '\n'); // or here 
    BN_free(e); 
    if (!RSA_check_key(rsa)) { 
    printw("ERROR: Key failed validation\n"); 
    return NULL; 
    } 
    printw("Key generation completed successfully\n"); 
    return rsa; 
} 

我沒有收到比一些過時的OS X之外的其他任何編譯器警告(即可以造成問題?)。爲什麼我會遇到seg錯誤?

+0

如果你確定它是RSA_generate_key_ex()函數中的錯誤,那麼它可能是你的輸入參數。驗證每個函數是否與預期的一樣。 – Ender 2014-08-29 05:32:54

+0

這也可能是一個問題:''RAND_load_file(「/ dev/random」,4096);'。你要求的是字節,而不是位。還有很多。它可能會耗盡設備,並且可能會阻塞很長時間。要達到相當於4096位密鑰,您需要大約140位的安全級別。 140/8 = 17.5字節:'RAND_load_file(「/ dev/random」,18);'。 – jww 2014-08-29 06:48:27

+0

@jww對我來說幸運的是,這並不需要太長時間,但我會將你的建議納入思考。 – carloabelli 2014-08-29 06:49:55

回答

2

無需瞭解您正在使用的庫什麼,這是不正確的:

RSA *rsa; 
while (getch() != '\n'); // the program does reach this point 
    if (!RSA_generate_key_ex(rsa, 4096, e, 0)) 

您正在使用未初始化的指針調用rsaRSA_generate_key_ex。除非嘗試使用它,並且如您所見,崩潰,否則RSA_generate_key_ex函數將無法對其執行任何操作。

因此,閱讀該函數的文檔,瞭解第一個參數應該是什麼。也許這應該是這樣的:

RSA rsa; 
while (getch() != '\n'); // the program does reach this point 
    if (!RSA_generate_key_ex(&rsa, 4096, e, 0)) 

如果是這樣的話,那麼你需要你的返回類型更改爲RSA,而不是RSA*(我假設RSA是一個結構或typedef的一個類型,可以是按價值安全返回)。

+0

我想通過改變行到'RSA * rsa = RSA_new();'我需要首先初始化RSA。謝謝您的幫助! – carloabelli 2014-08-29 05:28:08