OpenSSL 1_1_0e BN_print_fp无效

时间:2017-07-28 08:26:40

标签: c openssl

我正在使用Openssl 1_1_0e,我无法弄清楚我的代码有什么问题:

#include <openssl/rsa.h>

int main(){

    BIGNUM          *bne = NULL; 
    unsigned long   e = RSA_F4;
    RSA             *r = NULL;

    bne = BN_new();
    BN_set_word(bne,e);
    r = RSA_new();

    BIGNUM *n = NULL;
    BIGNUM *d = NULL;
    RSA_get0_key((const RSA *) r, (const BIGNUM **) &n, NULL, (const BIGNUM **) &d);

    BN_print_fp(stdout, n);
    RSA_free(r);
    BN_free(bne);

    return  0;
}

Valgrind说大小4的读数无效:

==8066== Invalid read of size 4
==8066==    at 0x4EF603E: BN_print (in /home/roman/Dropbox/uni/RSA/my_work/library/lib/libcrypto.so.1.1)
==8066==    by 0x4EF662D: BN_print_fp (in /home/roman/Dropbox/uni/RSA/my_work/library/lib/libcrypto.so.1.1)
==8066==    by 0x40093B: main (in /home/roman/Dropbox/uni/RSA/my_work/sharedLibraryTest)
==8066==  Address 0x10 is not stack'd, malloc'd or (recently) free'd

我的代码出了什么问题?它看起来很好。

1 个答案:

答案 0 :(得分:1)

我无法直接测试这段代码,因为我没有constructor() { this.loadQuestionsFromCache() .then(this.refreshQuestions) // eslint-disable-line promise/prefer-await-to-then .then(this.startListeningForStateChanges) // eslint-disable-line promise/prefer-await-to-then .catch(() => { Alert.alert( 'Ooops!', 'Something went wrong when I tried to load everyones questions :( please try refreshing me', [{text: 'OK'}], {cancelable: false} ) }) } @action.bound updateIsListRefreshing(isRefreshing: boolean) { this.isListRefreshing = isRefreshing } @action('Set question list') setQuestions(questions: Question) { this.questions = questions } handleAppStateChange = (nextAppState: string) => { if (nextAppState === 'active') { this.refreshQuestions() } } @action.bound startListeningForStateChanges() { AppState.addEventListener('change', this.handleAppStateChange) } @action.bound async refreshQuestions() { if (this.isListRefreshing) { return } try { this.updateIsListRefreshing(true) const response = await fetch(serverURL) if (response.status === 200) { const questionsText = await response.text() this.setQuestions(JSON.parse(questionsText).map(Question.of)) await AsyncStorage.setItem('questions', questionsText) } this.updateIsListRefreshing(false) } catch (error) { this.updateIsListRefreshing(false) } } OpenSSL documentation

  

可以通过调用RSA_get0_key()获取RSA_get0_keyne参数。 如果尚未设置,则d*n*e将设置为NULL。否则,它们被设置为指向各自值的指针。这些指针直接指向值的内部表示,因此不应被调用者释放。

你正在调用*d,但是在RSA对象中没有设置这些BIGNUM - 据我所知RSA_new();没有做到 - 怎么可能它,因为生成它们需要一个漫长的时间。因此RSA_new()设置为n指针;当NULL尝试读取偏移量为16的BN_print_fp结构的成员时出现错误。(即BIGNUM

最小例子:

*(uint32_t*)((char*)NULL + 16)

使用#include <openssl/rsa.h> int main(void) { BN_print_fp(stdout, NULL); } 进行编译,然后使用gcc test.c -lssl -lcrypto

valgrind