Changeset 4647

Show
Ignore:
Timestamp:
02/17/07 16:22:38 (2 years ago)
Author:
morris
Message:

Add wi_rsa_init_with_private_key()

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • libwired/trunk/libwired/misc/wi-crypto.c

    r4645 r4647  
    4747        RSA                                                                     *rsa; 
    4848        wi_data_t                                                       *public_key; 
     49        wi_data_t                                                       *private_key; 
    4950}; 
    5051 
    5152static void                                                             _wi_rsa_dealloc(wi_runtime_instance_t *); 
    5253static wi_string_t *                                    _wi_rsa_description(wi_runtime_instance_t *); 
    53  
    54 static wi_data_t *                                              _wi_rsa_public_key(wi_rsa_t *); 
    5554 
    5655static wi_runtime_id_t                                  _wi_rsa_runtime_id = WI_RUNTIME_ID_NULL; 
     
    132131        } 
    133132         
    134         rsa->public_key = wi_retain(_wi_rsa_public_key(rsa)); 
    135          
    136         if(!rsa->public_key) { 
    137                 wi_release(rsa); 
    138  
    139                 return NULL; 
    140         } 
    141          
    142133        return rsa; 
    143134} 
     
    170161        } 
    171162         
    172         rsa->public_key = wi_retain(_wi_rsa_public_key(rsa)); 
    173          
    174         if(!rsa->public_key) { 
     163        return rsa; 
     164
     165 
     166 
     167 
     168wi_rsa_t * wi_rsa_init_with_private_key(wi_rsa_t *rsa, wi_data_t *data) { 
     169        const unsigned char     *buffer; 
     170        long                            length; 
     171         
     172        buffer = wi_data_bytes(data); 
     173        length = wi_data_length(data); 
     174         
     175        rsa->rsa = d2i_RSAPrivateKey(NULL, &buffer, length); 
     176 
     177        if(!rsa->rsa) { 
     178                wi_error_set_openssl_error(); 
     179                 
    175180                wi_release(rsa); 
    176  
    177                 return NULL; 
    178         } 
     181                 
     182                return NULL; 
     183        } 
     184         
     185        rsa->private_key = wi_retain(data); 
    179186         
    180187        return rsa; 
     
    184191 
    185192wi_rsa_t * wi_rsa_init_with_public_key(wi_rsa_t *rsa, wi_data_t *data) { 
    186         unsigned char *buffer; 
    187         long                    length; 
    188          
    189         buffer = (unsigned char *) wi_data_bytes(data); 
     193        const unsigned char   *buffer; 
     194        long                           length; 
     195         
     196        buffer = wi_data_bytes(data); 
    190197        length = wi_data_length(data); 
    191198         
    192         rsa->rsa = d2i_RSA_PUBKEY(NULL, &buffer, length); 
     199        rsa->rsa = d2i_RSAPublicKey(NULL, (const unsigned char **) &buffer, length); 
    193200 
    194201        if(!rsa->rsa) { 
     
    211218         
    212219        RSA_free(rsa->rsa); 
     220         
    213221        wi_release(rsa->public_key); 
     222        wi_release(rsa->private_key); 
    214223} 
    215224 
     
    223232                rsa, 
    224233                rsa->rsa, 
    225                 RSA_size(rsa->rsa) * 8); 
    226 
    227  
    228  
    229  
    230 #pragma mark - 
    231  
    232 static wi_data_t * _wi_rsa_public_key(wi_rsa_t *rsa) { 
    233         wi_data_t               *data; 
     234                wi_rsa_bits(rsa)); 
     235
     236 
     237 
     238 
     239#pragma mark - 
     240 
     241wi_data_t * wi_rsa_public_key(wi_rsa_t *rsa) { 
    234242        unsigned char   *buffer; 
    235243        int                             length; 
    236244 
    237         buffer = NULL; 
    238         length = i2d_RSA_PUBKEY(rsa->rsa, &buffer); 
    239          
    240         if(length <= 0) { 
    241                 wi_error_set_openssl_error(); 
    242                  
    243                 return NULL; 
    244         } 
    245          
    246         data = wi_data_with_bytes(buffer, length); 
    247  
    248         OPENSSL_free(buffer); 
    249          
    250         return data; 
    251 
    252  
    253  
    254  
    255 #pragma mark - 
    256  
    257 wi_data_t * wi_rsa_public_key(wi_rsa_t *rsa) { 
     245        if(!rsa->public_key) { 
     246                buffer = NULL; 
     247                length = i2d_RSAPublicKey(rsa->rsa, &buffer); 
     248                 
     249                if(length <= 0) { 
     250                        wi_error_set_openssl_error(); 
     251                         
     252                        return NULL; 
     253                } 
     254                 
     255                rsa->public_key = wi_data_init_with_bytes(wi_data_alloc(), buffer, length); 
     256 
     257                OPENSSL_free(buffer); 
     258        } 
     259         
    258260        return rsa->public_key; 
     261} 
     262 
     263 
     264 
     265wi_data_t * wi_rsa_private_key(wi_rsa_t *rsa) { 
     266        unsigned char   *buffer; 
     267        int                             length; 
     268 
     269        if(!rsa->private_key) { 
     270                buffer = NULL; 
     271                length = i2d_RSAPrivateKey(rsa->rsa, &buffer); 
     272                 
     273                if(length <= 0) { 
     274                        wi_error_set_openssl_error(); 
     275                         
     276                        return NULL; 
     277                } 
     278                 
     279                rsa->private_key = wi_data_init_with_bytes(wi_data_alloc(), buffer, length); 
     280 
     281                OPENSSL_free(buffer); 
     282        } 
     283         
     284        return rsa->private_key; 
     285} 
     286 
     287 
     288 
     289wi_uinteger_t wi_rsa_bits(wi_rsa_t *rsa) { 
     290        return RSA_size(rsa->rsa) * 8; 
    259291} 
    260292 
     
    532564 
    533565 
    534 #pragma mark - 
    535  
    536566wi_cipher_type_t wi_cipher_type(wi_cipher_t *cipher) { 
    537567        return cipher->type; 
  • libwired/trunk/libwired/misc/wi-crypto.h

    r4645 r4647  
    5050WI_EXPORT wi_rsa_t *                                    wi_rsa_init_with_bits(wi_rsa_t *, wi_uinteger_t); 
    5151WI_EXPORT wi_rsa_t *                                    wi_rsa_init_with_pem_file(wi_rsa_t *, wi_string_t *); 
     52WI_EXPORT wi_rsa_t *                                    wi_rsa_init_with_private_key(wi_rsa_t *, wi_data_t *); 
    5253WI_EXPORT wi_rsa_t *                                    wi_rsa_init_with_public_key(wi_rsa_t *, wi_data_t *); 
    5354 
    5455WI_EXPORT wi_data_t *                                   wi_rsa_public_key(wi_rsa_t *); 
     56WI_EXPORT wi_data_t *                                   wi_rsa_private_key(wi_rsa_t *); 
     57WI_EXPORT wi_uinteger_t                                 wi_rsa_bits(wi_rsa_t *); 
    5558 
    5659WI_EXPORT wi_data_t *                                   wi_rsa_encrypt(wi_rsa_t *, wi_data_t *); 
     
    6871WI_EXPORT wi_data_t *                                   wi_cipher_key(wi_cipher_t *); 
    6972WI_EXPORT wi_data_t *                                   wi_cipher_iv(wi_cipher_t *); 
    70  
    7173WI_EXPORT wi_cipher_type_t                              wi_cipher_type(wi_cipher_t *); 
    7274WI_EXPORT wi_string_t *                                 wi_cipher_name(wi_cipher_t *);