2014-08-29 42 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