Hi, The code for my project example is attached. The error code is as well.
The error remains. For generating Key, the callback is called. For getting Private Key, the callback is not called. What's wrong here? I really appreciate any help. Thanks. Leonardo
<<attachment: openssl_error.JPG>>
#include <iostream> using namespace std; #include "openssl/bio.h" #include "openssl/err.h" #include "openssl/bn.h" #include "openssl/rsa.h" #include "openssl/evp.h" #include "openssl/x509.h" #include "openssl/pem.h" #include "openssl/rand.h" #include <stdio.h> #include <string.h> #include <time.h> #include <stdarg.h> typedef struct pw_cb_data { const void *password; const char *prompt_info; } PW_CB_DATA; static int rsa_cb(char *buf, int size, int rwflag, void *u) { int len; PW_CB_DATA *cb_data = (PW_CB_DATA *)u; len = strlen((char *)cb_data->password); if (len <= 0) return 0; /* if too long, truncate */ if (len > size) len = size; memcpy(buf, cb_data->password, len); return len; } bool genRSAKpr(char *passwd, char *path_Kpr, int bits) { PW_CB_DATA cb_data; RSA *rsa; const EVP_CIPHER *enc; BIO *bio; BIGNUM *bn; unsigned long f4; int ret = 1; bn = BN_new(); if (bn == NULL) ret = 0; if (ret) bio = BIO_new(BIO_s_file()); if (bio == NULL) ret = 0; if (ret && !BIO_write_filename(bio,path_Kpr)) ret = 0; if (ret) rsa = RSA_new(); if (rsa == NULL) ret = 0; f4 = RSA_F4; if (ret && !BN_set_word(bn, f4)) ret = 0; if (ret && RSA_generate_key_ex(rsa, bits, bn, NULL)<0) ret = 0; if (passwd != '\0') { enc=EVP_aes_256_cbc(); if (enc == NULL) ret = 0; } cb_data.password = passwd; cb_data.prompt_info = path_Kpr; if (ret && !PEM_write_bio_RSAPrivateKey(bio,rsa,enc,NULL,0,(pem_password_cb *)rsa_cb, &cb_data)) ret = 0; if (enc != NULL) { enc = NULL; } if (rsa != NULL) { RSA_free(rsa); rsa = NULL; } if (bio != NULL) { BIO_free_all(bio); bio = NULL; } if (bn != NULL) { BN_free(bn); bn = NULL; } if (ret != 0) return true; return false; } bool genRSAKpu(char *passwd, char *path_Kpr, char *path_Kpu) { PW_CB_DATA cb_data; RSA *rsa; BIO *bio; EVP_PKEY *pkey; int ret = 1; FILE *file; //KPU if (ret) bio = BIO_new(BIO_s_file()); if (bio == NULL) ret = 0; if (ret && !BIO_read_filename(bio,path_Kpr)) ret = 0; cb_data.password = passwd; cb_data.prompt_info = path_Kpr; if (ret) pkey=PEM_read_bio_PrivateKey(bio,NULL,(pem_password_cb *)rsa_cb, &cb_data); if (pkey != NULL) rsa = EVP_PKEY_get1_RSA(pkey); else { ret = 0; file=fopen("Err_openssl.log","a+"); if (file!=NULL) { ERR_print_errors_fp(file); fclose(file); } } if (rsa == NULL) ret = 0; if (ret && !BIO_write_filename(bio,path_Kpu)) ret = 0; if (ret && !PEM_write_bio_RSA_PUBKEY(bio,rsa)) ret = 0; if (rsa != NULL) { RSA_free(rsa); rsa = NULL; } if (pkey != NULL) { EVP_PKEY_free(pkey); pkey = NULL; } if (bio != NULL) { BIO_free_all(bio); bio = NULL; } if (!ret) return false; return true; } int main() { cout << SSLeay_version(SSLEAY_VERSION) << endl; if (genRSAKpr("passwd","C:\\log\\Kpr.pem",1024)) cout << "Kpr Generated!" << endl; else cout << "Error generationg Kpr!" << endl; if (genRSAKpu("passwd","C:\\log\\Kpr.pem","C:\\log\\Kpu.pem")) cout << "Kpu Generated!" << endl; else cout << "Error generationg Kpu!" << endl; return 0; }