Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Related Pages  

Rsa.cpp

Go to the documentation of this file.
00001 #include "headers.h"
00002 
00083 
00084 const int Rsa::blockSize = RSA_BLOCK_SIZE; 
00085 
00086 Rsa::Rsa()
00087 {
00088     m_rsa = NULL;
00089 } // ctor
00090 
00091 
00092 Rsa::~Rsa()
00093 {
00094     if (m_rsa) {
00095         RSA_free (m_rsa);
00096     }
00097 } // dtor
00098 
00099 
00106 bool 
00107 Rsa::loadKey(Cert &cert)
00108 {
00109     FUNCTION_INIT_VARS(bool);
00110 
00111     EVP_PKEY *key = NULL;
00112     
00113     if (m_rsa) {
00114         RSA_free (m_rsa);
00115         m_rsa = NULL;
00116     }
00117 
00118     FUNCTION_BODY {
00119         X509* x509_cert;
00120     
00121         x509_cert = cert.getCert();
00122         if (!x509_cert) {
00123             RETURN (false);
00124         }
00125     
00126         key = X509_get_pubkey(x509_cert);
00127         if (!key) {
00128             RETURN (false);
00129         }
00130 
00131         m_rsa = EVP_PKEY_get1_RSA(key);
00132         if (!m_rsa) {
00133             RETURN (false);
00134         }
00135         
00136         RETURN (true);
00137     }
00138     
00139     FUNCTION_FINALIZE {
00140         if (key) {
00141             EVP_PKEY_free (key);
00142         }
00143         if (!RETURN_VALUE) {
00144             if (m_rsa) {
00145                 RSA_free (m_rsa);
00146                 m_rsa = NULL;
00147             }
00148         }
00149     }
00150     
00151     FUNCTION_RETURN;
00152 } // fn loadKey
00153 
00154 
00161 bool 
00162 Rsa::loadKey(char* file, char* pass)
00163 {
00164     FUNCTION_INIT_VARS(bool);
00165     
00166     BIO      *in  = NULL;
00167     EVP_PKEY *key = NULL;
00168     
00169     if (m_rsa) {
00170         RSA_free (m_rsa);
00171         m_rsa = NULL;
00172     }
00173   
00174     FUNCTION_BODY {
00175         in = BIO_new(BIO_s_file_internal());
00176         if (in == NULL) {
00177             RETURN (false);
00178         }
00179 
00180         if (BIO_read_filename(in, file) <= 0) {
00181             RETURN (false);
00182         }
00183 
00184         key = PEM_read_bio_PrivateKey(in, NULL, NULL, pass);
00185         if (key == NULL) {
00186             RETURN (false);
00187         }
00188 
00189         m_rsa = EVP_PKEY_get1_RSA(key);
00190         if (!m_rsa) {
00191             RETURN (false);
00192         }
00193 
00194         RETURN (true);
00195     }
00196     
00197     FUNCTION_FINALIZE {
00198         if (in) {
00199             BIO_free (in);
00200         }
00201         if (key) {
00202             EVP_PKEY_free (key);
00203         }
00204         if (!RETURN_VALUE) {
00205             if (m_rsa) {
00206                 RSA_free (m_rsa);
00207                 m_rsa = NULL;
00208             }
00209         }
00210     }
00211     
00212     FUNCTION_RETURN;
00213 } // fn loadKey
00214 
00215 
00225 int  
00226 Rsa::publicEncrypt (unsigned char* in, int inlen, unsigned char* out)
00227 {
00228     if (!m_rsa) {
00229         return -1;
00230     }
00231     
00232     return RSA_public_encrypt(inlen, in, out, m_rsa, RSA_PKCS1_PADDING);
00233 }
00234 
00244 int  
00245 Rsa::privateDecrypt(unsigned char* in, int inlen, unsigned char* out)
00246 {
00247     if (!m_rsa) {
00248         return -1;
00249     }
00250     
00251     return RSA_private_decrypt(inlen, in, out, m_rsa, RSA_PKCS1_PADDING);
00252 } // fn privateDecrypt
00253 
00254 
00264 int 
00265 Rsa::privateEncrypt(unsigned char* in, int inlen, unsigned char* out)
00266 {
00267     if (!m_rsa) {
00268         return -1;
00269     }
00270     
00271     return RSA_private_encrypt(inlen, in, out, m_rsa, RSA_PKCS1_PADDING);
00272 } // fn privateEncrypt
00273 
00274 
00284 int 
00285 Rsa::publicDecrypt (unsigned char* in, int inlen, unsigned char* out)
00286 {
00287     if (!m_rsa) {
00288         return -1;
00289     }
00290     
00291     return RSA_public_decrypt(inlen, in, out, m_rsa, RSA_PKCS1_PADDING);
00292 } // fn publicDecrypt
00293 

Generated at Thu Jul 11 13:31:51 2002 for Peekabooty by doxygen1.2.9 written by Dimitri van Heesch, © 1997-2001