00001 #include "rsa.h"
00002
00003 int pass_cb(char *buf, int size, int rwflag, void *u){
00004 int len;
00005 char *tmp;
00006
00007 printf("Enter pass phrase for \"%s\"\n", u);
00008
00009
00010 tmp = "hello";
00011 len = strlen(tmp);
00012
00013 if (len <= 0) return 0;
00014
00015 if (len > size) len = size;
00016 memcpy(buf, tmp, len);
00017 return len;
00018 }
00019
00020 namespace n2nc {
00021 namespace security {
00022
00023 Rsa::Rsa() {
00024 this->m_pub_isloaded = false ;
00025 this->m_pri_isloaded = false ;
00026 ::ERR_load_crypto_strings();
00027 }
00028
00029 int Rsa::loadPubFromFile(std::string filename){
00030 FILE *fp ;
00031 if ( (fp = fopen(filename.c_str(),"r")) == NULL) { std::cerr << "error loading file" << std::endl ; exit (1) ;}
00032
00033 this->m_pub_rsa = PEM_read_RSAPublicKey(fp,NULL,NULL,NULL);
00034 if (!this->m_pub_rsa){
00035 std::cerr << ERR_error_string(ERR_get_error(),NULL) << std::endl ;
00036 std::cerr << "error loading public key IN PKCS #1 RSAPublicKey" << std::endl ;
00037 std::cerr << "trying X.509 subjectPublicKeyInfo format(openssl rsa command)" << std::endl ;
00038 fclose(fp);
00039 fp = fopen(filename.c_str(),"r");
00040 this->m_pub_rsa = PEM_read_RSA_PUBKEY(fp,NULL,NULL,NULL);
00041 if (!this->m_pub_rsa){
00042 std::cerr << "X.509 subjectPublicKeyInfo format FATAL ERROR ON LOADING." << std::endl ;
00043 exit (-1) ;
00044 }
00045
00046 }
00047 fclose(fp);
00048 std::cerr << "RSA Public Key Loaded!!" << std::endl ;
00049 return this->m_pub_isloaded = true ;
00050 }
00051
00052 int Rsa::loadPriFromFile(std::string filename){
00053 FILE *fp ;
00054 if ( (fp = fopen(filename.c_str(),"r")) == NULL) { std::cerr << "error loading file" << std::endl ; exit (1) ;}
00055 this->m_pri_rsa = PEM_read_RSAPrivateKey(fp,NULL,pass_cb,NULL);
00056 if (!this->m_pri_rsa){
00057 std::cerr << ERR_error_string(ERR_get_error(),NULL) << std::endl ;
00058 std::cerr << "error loading private key" << std::endl ;
00059 exit(1);
00060 }
00061 fclose(fp);
00062 if(RSA_check_key(this->m_pri_rsa) < 1){
00063 std::cerr << "Error: RSA Key read failure" << std::endl ;
00064 exit (1);
00065 }
00066 return this->m_pri_isloaded = true ;
00067 }
00068
00069 int n2nc::security::Rsa::getID(){
00070
00071 }
00072
00073 int Rsa::encrypt(void * inbuf, void * outbuf, size_t inlen){
00074 int outw ;
00075 if (!this->m_pub_isloaded) exit( -1);
00076 outw = RSA_public_encrypt(inlen, (u_char*)inbuf, (u_char*)outbuf, this->m_pub_rsa , RSA_PKCS1_OAEP_PADDING);
00077 if (outw == -1) std::cerr << ERR_error_string(ERR_get_error(), NULL) << std::cerr ;
00078 return outw ;
00079 }
00080
00081 int Rsa::decrypt(void * inbuf, void * outbuf, size_t inlen){
00082 int outw ;
00083 if (!this->m_pri_isloaded) exit( -1);
00084 outw = RSA_private_decrypt(inlen, (u_char*)inbuf, (u_char*)outbuf, this->m_pri_rsa , RSA_PKCS1_OAEP_PADDING);
00085 if (outw == -1) std::cerr << ERR_error_string(ERR_get_error(), NULL) << std::cerr ;
00086 return outw ;
00087 }
00088
00089
00090 int Rsa::genkey(std::string filename)
00091 {
00092 std::string name ;
00093 FILE *fp ;
00094
00095 ::srand(time(NULL));
00096
00097 this->m_pub_rsa = NULL ;
00098 this->m_pub_rsa = RSA_generate_key(1024,65537UL,NULL,NULL);
00099
00100 name = filename + "pub" ;
00101 fp=fopen(name.c_str(),"w");
00102 PEM_write_RSAPublicKey(fp,this->m_pub_rsa);
00103 fclose(fp);
00104
00105 name = filename + "pri" ;
00106 fp=fopen(name.c_str(),"w");
00107 PEM_write_RSAPrivateKey(fp,this->m_pub_rsa,NULL,NULL,0,0,NULL);
00108 fclose(fp);
00109
00110
00111 }
00112
00113
00114
00115
00116 Rsa::~Rsa() {
00117
00118 }
00119
00120
00121 }
00122 }
00123
00124
00125
00126
00127
00128