Changeset 4623

Show
Ignore:
Timestamp:
02/14/07 17:39:11 (2 years ago)
Author:
morris
Message:

Even more p7 stuff:

- Add encryption abstractions for RSA+AES/BF
- Fix memory leaks in specs

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • libwired/trunk/libwired/base/wi-assert.c

    r4540 r4623  
    3838 
    3939 
     40 
    4041static void wi_assert_default_handler(wi_string_t *file, wi_uinteger_t line, wi_string_t *fmt, ...) { 
    4142        wi_string_t             *string; 
  • libwired/trunk/libwired/base/wi-base.c

    r4590 r4623  
    5151 
    5252 
     53 
    5354void wi_initialize(void) { 
    5455        wi_runtime_register(); 
     
    6970         
    7071#ifdef WI_P7 
     72        wi_p7_crypto_register(); 
    7173        wi_p7_message_register(); 
    7274        wi_p7_socket_register(); 
     
    119121         
    120122#ifdef WI_P7 
     123        wi_p7_crypto_initialize(); 
    121124        wi_p7_message_initialize(); 
    122125        wi_p7_socket_initialize(); 
  • libwired/trunk/libwired/base/wi-error.c

    r4601 r4623  
    139139 
    140140 
     141 
    141142void wi_error_register(void) { 
    142143        _wi_error_runtime_id = wi_runtime_register_class(&_wi_error_runtime_class); 
     
    148149#ifdef WI_SSL 
    149150        SSL_load_error_strings(); 
     151#endif 
     152         
     153#ifdef WI_CRYPTO 
     154        ERR_load_crypto_strings(); 
    150155#endif 
    151156} 
  • libwired/trunk/libwired/base/wi-private.h

    r4590 r4623  
    4242typedef void *                                  wi_enumerator_func_t(wi_runtime_instance_t *, void *); 
    4343 
     44 
    4445WI_EXPORT void                                  wi_address_register(void); 
    4546WI_EXPORT void                                  wi_array_register(void); 
     
    5455WI_EXPORT void                                  wi_log_register(void); 
    5556WI_EXPORT void                                  wi_number_register(void); 
     57WI_EXPORT void                                  wi_p7_crypto_register(void); 
    5658WI_EXPORT void                                  wi_p7_message_register(void); 
    5759WI_EXPORT void                                  wi_p7_socket_register(void); 
     
    8587WI_EXPORT void                                  wi_log_initialize(void); 
    8688WI_EXPORT void                                  wi_number_initialize(void); 
     89WI_EXPORT void                                  wi_p7_crypto_initialize(void); 
    8790WI_EXPORT void                                  wi_p7_message_initialize(void); 
    8891WI_EXPORT void                                  wi_p7_socket_initialize(void); 
  • libwired/trunk/libwired/base/wi-tests.c

    r4562 r4623  
    7575 
    7676 
     77 
    7778void wi_test_register(void) { 
    7879        _wi_test_runtime_id = wi_runtime_register_class(&_wi_test_runtime_class); 
  • libwired/trunk/libwired/base/wi-version.c

    r4511 r4623  
    3535 
    3636 
     37 
    3738void wi_version_register(void) { 
    3839} 
  • libwired/trunk/libwired/collections/wi-array.c

    r4575 r4623  
    151151        _wi_array_hash 
    152152}; 
     153 
    153154 
    154155 
  • libwired/trunk/libwired/collections/wi-enumerator.c

    r4575 r4623  
    8484        NULL 
    8585}; 
     86 
    8687 
    8788 
  • libwired/trunk/libwired/collections/wi-hash.c

    r4575 r4623  
    186186 
    187187 
     188 
    188189void wi_hash_register(void) { 
    189190        _wi_hash_runtime_id = wi_runtime_register_class(&_wi_hash_runtime_class); 
     
    642643static void _wi_hash_bucket_remove(wi_hash_t *hash, _wi_hash_bucket_t *bucket) { 
    643644        _WI_HASH_KEY_RELEASE(hash, bucket->key); 
    644         _WI_HASH_KEY_RELEASE(hash, bucket->data); 
     645        _WI_HASH_VALUE_RELEASE(hash, bucket->data); 
    645646         
    646647        bucket->link = hash->bucket_free_list; 
  • libwired/trunk/libwired/collections/wi-set.c

    r4575 r4623  
    149149 
    150150 
     151 
    151152void wi_set_register(void) { 
    152153        _wi_set_runtime_id = wi_runtime_register_class(&_wi_set_runtime_class); 
  • libwired/trunk/libwired/data/wi-data.c

    r4610 r4623  
    5454        wi_uinteger_t                                           length; 
    5555        wi_uinteger_t                                           capacity; 
     56        wi_boolean_t                                            free; 
    5657}; 
    5758 
     
    103104 
    104105 
     106wi_data_t * wi_data_with_bytes(const void *bytes, wi_uinteger_t length) { 
     107        return wi_autorelease(wi_data_init_with_bytes(wi_data_alloc(), bytes, length)); 
     108} 
     109 
     110 
     111 
     112wi_data_t * wi_data_with_bytes_no_copy(void *bytes, wi_uinteger_t length, wi_boolean_t free) { 
     113        return wi_autorelease(wi_data_init_with_bytes_no_copy(wi_data_alloc(), bytes, length, free)); 
     114} 
     115 
     116 
     117 
    105118wi_data_t * wi_data_with_random_bytes(wi_uinteger_t length) { 
    106119        return wi_autorelease(wi_data_init_with_random_bytes(wi_data_alloc(), length)); 
     
    132145        data->capacity  = WI_MAX(wi_exp2m1(wi_log2(capacity) + 1), _WI_DATA_MIN_SIZE); 
    133146        data->bytes             = wi_malloc(data->capacity); 
     147        data->free              = true; 
    134148         
    135149        return data; 
     
    142156 
    143157        memcpy(data->bytes, bytes, length); 
     158         
    144159        data->length = length; 
     160 
     161        return data; 
     162} 
     163 
     164 
     165 
     166wi_data_t * wi_data_init_with_bytes_no_copy(wi_data_t *data, void *bytes, wi_uinteger_t length, wi_boolean_t free) { 
     167        data->bytes             = bytes; 
     168        data->capacity  = length; 
     169        data->length    = length; 
     170        data->free              = free; 
    145171         
    146172        return data; 
     
    268294        wi_data_t               *data = instance; 
    269295         
    270         wi_free(data->bytes); 
     296        if(data->free) 
     297                wi_free(data->bytes); 
    271298} 
    272299 
  • libwired/trunk/libwired/data/wi-data.h

    r4610 r4623  
    3939 
    4040WI_EXPORT wi_data_t *                                   wi_data(void); 
     41WI_EXPORT wi_data_t *                                   wi_data_with_bytes(const void *, wi_uinteger_t); 
     42WI_EXPORT wi_data_t *                                   wi_data_with_bytes_no_copy(void *, wi_uinteger_t, wi_boolean_t); 
     43WI_EXPORT wi_data_t *                                   wi_data_with_random_bytes(wi_uinteger_t); 
    4144WI_EXPORT wi_data_t *                                   wi_data_with_base64(wi_string_t *); 
    42 WI_EXPORT wi_data_t *                                   wi_data_with_random_bytes(wi_uinteger_t); 
    4345 
    4446WI_EXPORT wi_data_t *                                   wi_data_alloc(void); 
     
    4648WI_EXPORT wi_data_t *                                   wi_data_init_with_capacity(wi_data_t *, wi_uinteger_t); 
    4749WI_EXPORT wi_data_t *                                   wi_data_init_with_bytes(wi_data_t *, const void *, wi_uinteger_t); 
     50WI_EXPORT wi_data_t *                                   wi_data_init_with_bytes_no_copy(wi_data_t *, void *, wi_uinteger_t, wi_boolean_t); 
    4851WI_EXPORT wi_data_t *                                   wi_data_init_with_random_bytes(wi_data_t *, wi_uinteger_t); 
    4952WI_EXPORT wi_data_t *                                   wi_data_init_with_base64(wi_data_t *, wi_string_t *); 
  • libwired/trunk/libwired/data/wi-number.c

    r4575 r4623  
    104104 
    105105 
     106 
    106107void wi_number_register(void) { 
    107108        _wi_number_runtime_id = wi_runtime_register_class(&_wi_number_runtime_class); 
  • libwired/trunk/libwired/data/wi-regexp.c

    r4511 r4623  
    7272 
    7373 
     74 
    7475void wi_regexp_register(void) { 
    7576        _wi_regexp_runtime_id = wi_runtime_register_class(&_wi_regexp_runtime_class); 
  • libwired/trunk/libwired/data/wi-settings.c

    r4511 r4623  
    103103 
    104104 
     105 
    105106void wi_settings_register(void) { 
    106107        _wi_settings_runtime_id = wi_runtime_register_class(&_wi_settings_runtime_class); 
  • libwired/trunk/libwired/data/wi-string.c

    r4617 r4623  
    158158 
    159159 
     160 
    160161void wi_string_register(void) { 
    161162        _wi_string_runtime_id = wi_runtime_register_class(&_wi_string_runtime_class); 
     
    218219 
    219220 
     221wi_string_t * wi_string_with_data(wi_data_t *data) { 
     222        return wi_autorelease(wi_string_init_with_data(wi_string_alloc(), data)); 
     223} 
     224 
     225 
     226 
    220227wi_string_t * wi_string_with_bytes(const void *buffer, wi_uinteger_t size) { 
    221228        return wi_autorelease(wi_string_init_with_bytes(wi_string_alloc(), buffer, size)); 
     
    327334 
    328335wi_string_t * wi_string_init_with_contents_of_file(wi_string_t *string, wi_string_t *path) { 
    329         wi_file_t       *file;  
    330          
    331         file = wi_file_for_reading(path);  
    332          
    333         if(!file) {  
    334                 wi_release(string);  
    335                  
    336                 return NULL;  
    337         }  
    338          
    339         return wi_file_read_to_end_of_file(file);  
     336        wi_file_t       *file; 
     337         
     338        wi_release(string); 
     339                 
     340        file = wi_file_for_reading(path); 
     341         
     342        if(!file) 
     343                return NULL; 
     344         
     345        return wi_retain(wi_file_read_to_end_of_file(file)); 
    340346} 
    341347 
  • libwired/trunk/libwired/data/wi-string.h

    r4617 r4623  
    5656WI_EXPORT wi_string_t *                                         wi_string_with_cstring(const char *); 
    5757WI_EXPORT wi_string_t *                                         wi_string_with_format(wi_string_t *, ...); 
     58WI_EXPORT wi_string_t *                                         wi_string_with_data(wi_data_t *); 
    5859WI_EXPORT wi_string_t *                                         wi_string_with_bytes(const void *, wi_uinteger_t); 
    5960 
  • libwired/trunk/libwired/data/wi-url.c

    r4511 r4623  
    7575 
    7676 
     77 
    7778void wi_url_register(void) { 
    7879        _wi_url_runtime_id = wi_runtime_register_class(&_wi_url_runtime_class); 
  • libwired/trunk/libwired/file/wi-file.c

    r4617 r4623  
    109109 
    110110 
     111 
    111112void wi_file_register(void) { 
    112113        _wi_file_runtime_id = wi_runtime_register_class(&_wi_file_runtime_class); 
     
    789790                wi_string_append_bytes(string, buffer, bytes); 
    790791         
    791         if(bytes <= 0) { 
    792                 wi_release(string); 
    793                  
    794                 string = NULL; 
    795         } 
    796          
    797792        return wi_autorelease(string); 
    798793} 
  • libwired/trunk/libwired/net/wi-address.c

    r4575 r4623  
    100100 
    101101 
     102 
    102103void wi_address_register(void) { 
    103104        _wi_address_runtime_id = wi_runtime_register_class(&_wi_address_runtime_class); 
  • libwired/trunk/libwired/net/wi-ip.c

    r4437 r4623  
    5252 
    5353 
     54 
    5455wi_uinteger_t wi_ip_version(wi_string_t *ip) { 
    5556        struct sockaddr_in              sa_in; 
  • libwired/trunk/libwired/net/wi-socket.c

    r4590 r4623  
    143143        NULL 
    144144}; 
     145 
    145146 
    146147 
  • libwired/trunk/libwired/p7/wi-p7-base.h

    r4578 r4623  
    3434typedef struct _wi_p7_spec                      wi_p7_spec_t; 
    3535 
    36 #endif 
     36#endif /* WI_ARRAY_H */ 
  • libwired/trunk/libwired/p7/wi-p7-message.c

    r4614 r4623  
    9494#pragma mark - 
    9595 
     96wi_p7_message_t * wi_p7_message_with_name(wi_string_t *message_name, wi_p7_socket_t *p7_socket) { 
     97        return wi_autorelease(wi_p7_message_init_with_name(wi_p7_message_alloc(), message_name, p7_socket)); 
     98} 
     99 
     100 
     101 
     102#pragma mark - 
     103 
    96104wi_p7_message_t * wi_p7_message_alloc(void) { 
    97105    return wi_runtime_create_instance(_wi_p7_message_runtime_id, sizeof(wi_p7_message_t)); 
     
    208216        wi_string_append_string(description, WI_STR(")}")); 
    209217                                                         
    210         return description
     218        return wi_autorelease(description)
    211219} 
    212220 
  • libwired/trunk/libwired/p7/wi-p7-message.h

    r4601 r4623  
    7474WI_EXPORT wi_runtime_id_t                       wi_p7_message_runtime_id(void); 
    7575 
     76WI_EXPORT wi_p7_message_t *                     wi_p7_message_with_name(wi_string_t *, wi_p7_socket_t *); 
     77 
    7678WI_EXPORT wi_p7_message_t *                     wi_p7_message_alloc(void); 
    7779WI_EXPORT wi_p7_message_t *                     wi_p7_message_init_with_name(wi_p7_message_t *, wi_string_t *, wi_p7_socket_t *); 
     
    108110WI_EXPORT wi_date_t *                           wi_p7_message_date_for_name(wi_p7_message_t *, wi_string_t *); 
    109111 
    110 #endif 
     112#endif /* WI_ARRAY_H */ 
  • libwired/trunk/libwired/p7/wi-p7-private.h

    r4614 r4623  
    6666WI_EXPORT wi_integer_t                          wi_p7_xml_integer_for_attribute(xmlNodePtr, wi_string_t *); 
    6767 
    68 #endif 
     68#endif /* WI_ARRAY_H */ 
  • libwired/trunk/libwired/p7/wi-p7-socket.c

    r4620 r4623  
    3434#include <wired/wi-log.h> 
    3535#include <wired/wi-p7-base.h> 
     36#include <wired/wi-p7-crypto.h> 
    3637#include <wired/wi-p7-message.h> 
    3738#include <wired/wi-p7-socket.h> 
     
    5758#define _WI_P7_SOCKET_BINARY_MAGIC_LENGTH       52 
    5859 
    59 #define _WI_P7_SOCKET_OPTIONS_ENCRYPTION_ENABLED(options) \ 
    60         (((options) & WI_P7_ENCRYPTION_RSA_AES) || ((options) & WI_P7_ENCRYPTION_RSA_BF)) 
     60#define _WI_P7_SOCKET_OPTIONS_ENCRYPTION_ENABLED(options)       \ 
     61        (((options) & WI_P7_ENCRYPTION_RSA_AES_256) ||                  \ 
     62         ((options) & WI_P7_ENCRYPTION_RSA_BF_128)) 
    6163 
    6264 
     
    7274         
    7375        wi_boolean_t                                                    encryption_enabled; 
    74         EVP_PKEY                                                                *private_key; 
    75         EVP_PKEY                                                                *public_key; 
    76         RSA                                                                             *rsa_key; 
    77         const EVP_CIPHER                                                *cipher; 
    78         EVP_CIPHER_CTX                                                  cipher_ctx; 
     76        wi_p7_rsa_t                                                             *rsa; 
     77        wi_p7_cipher_t                                                  *cipher; 
    7978         
    8079        wi_boolean_t                                                    compression_enabled; 
     
    9190        _WI_P7_SOCKET_DECOMPRESS, 
    9291}; 
    93 typedef enum _wi_p7_socket_compression  _wi_p7_socket_compression_t; 
    94  
    95  
    96 static void                                                             _wi_p7_socket_dealloc(wi_runtime_instance_t *); 
    97 static wi_string_t *                                    _wi_p7_socket_description(wi_runtime_instance_t *); 
    98  
    99 static wi_boolean_t                                             _wi_p7_socket_connect_handshake(wi_p7_socket_t *, wi_time_interval_t, wi_p7_options_t); 
    100 static wi_boolean_t                                             _wi_p7_socket_accept_handshake(wi_p7_socket_t *, wi_time_interval_t, wi_p7_options_t); 
    101 static wi_boolean_t                                             _wi_p7_socket_connect_key_exchange(wi_p7_socket_t *, wi_time_interval_t, wi_string_t *, wi_string_t *); 
    102 static wi_boolean_t                                             _wi_p7_socket_accept_key_exchange(wi_p7_socket_t *, wi_time_interval_t); 
    103 static wi_boolean_t                                             _wi_p7_socket_send_compatibility_check(wi_p7_socket_t *, wi_time_interval_t); 
    104 static wi_boolean_t                                             _wi_p7_socket_receive_compatibility_check(wi_p7_socket_t *, wi_time_interval_t); 
    105 static const EVP_CIPHER *                               _wi_p7_socket_cipher(wi_p7_socket_t *); 
    106 static void                                                             _wi_p7_socket_configure_cipher_ctx(wi_p7_socket_t *); 
    107 static wi_boolean_t                                             _wi_p7_socket_public_encrypt_buffer(wi_p7_socket_t *, const void *, uint32_t, void **, uint32_t *); 
    108 static wi_boolean_t                                             _wi_p7_socket_public_decrypt_buffer(wi_p7_socket_t *, const void *, uint32_t, void **, uint32_t *); 
    109 static wi_boolean_t                                             _wi_p7_socket_private_encrypt_buffer(wi_p7_socket_t *, const void *, uint32_t, void **, uint32_t *); 
    110 static wi_boolean_t                                             _wi_p7_socket_private_decrypt_buffer(wi_p7_socket_t *, const void *, uint32_t, void **, uint32_t *); 
    111 static wi_boolean_t                                             _wi_p7_socket_configure_compression_streams(wi_p7_socket_t *); 
    112 static wi_boolean_t                                             _wi_p7_socket_xcompress_buffer(wi_p7_socket_t *, _wi_p7_socket_compression_t, const void *, uint32_t, void **, uint32_t *); 
    113 static int                                                              _wi_p7_socket_xflate_buffer(z_stream *, _wi_p7_socket_compression_t, const void *, uint32_t, uint32_t *, void *, uint32_t *); 
    114 static wi_p7_message_t *                                _wi_p7_socket_read_binary_message(wi_p7_socket_t *, wi_time_interval_t, uint32_t); 
    115 static wi_p7_message_t *                                _wi_p7_socket_read_xml_message(wi_p7_socket_t *, wi_time_interval_t, wi_string_t *); 
    116  
    117  
    118 wi_p7_socket_password_provider_func_t   *wi_p7_socket_password_provider = NULL; 
    119  
    120 static wi_runtime_id_t                  _wi_p7_socket_runtime_id = WI_RUNTIME_ID_NULL; 
    121 static wi_runtime_class_t               _wi_p7_socket_runtime_class = { 
     92typedef enum _wi_p7_socket_compression          _wi_p7_socket_compression_t; 
     93 
     94 
     95static void                                                                     _wi_p7_socket_dealloc(wi_runtime_instance_t *); 
     96static wi_string_t *                                            _wi_p7_socket_description(wi_runtime_instance_t *); 
     97 
     98static wi_boolean_t                                                     _wi_p7_socket_connect_handshake(wi_p7_socket_t *, wi_time_interval_t, wi_p7_options_t); 
     99static wi_boolean_t                                                     _wi_p7_socket_accept_handshake(wi_p7_socket_t *, wi_time_interval_t, wi_p7_options_t); 
     100static wi_boolean_t                                                     _wi_p7_socket_connect_key_exchange(wi_p7_socket_t *, wi_time_interval_t, wi_string_t *, wi_string_t *); 
     101static wi_boolean_t                                                     _wi_p7_socket_accept_key_exchange(wi_p7_socket_t *, wi_time_interval_t); 
     102static wi_boolean_t                                                     _wi_p7_socket_send_compatibility_check(wi_p7_socket_t *, wi_time_interval_t); 
     103static wi_boolean_t                                                     _wi_p7_socket_receive_compatibility_check(wi_p7_socket_t *, wi_time_interval_t); 
     104static wi_p7_cipher_type_t                                      _wi_p7_socket_cipher(wi_p7_socket_t *); 
     105static wi_boolean_t                                                     _wi_p7_socket_configure_compression_streams(wi_p7_socket_t *); 
     106static wi_boolean_t                                                     _wi_p7_socket_xcompress_buffer(wi_p7_socket_t *, _wi_p7_socket_compression_t, const void *, uint32_t, void **, uint32_t *); 
     107static int                                                                      _wi_p7_socket_xflate_buffer(z_stream *, _wi_p7_socket_compression_t, const void *, uint32_t, uint32_t *, void *, uint32_t *); 
     108static wi_p7_message_t *                                        _wi_p7_socket_read_binary_message(wi_p7_socket_t *, wi_time_interval_t, uint32_t); 
     109static wi_p7_message_t *                                        _wi_p7_socket_read_xml_message(wi_p7_socket_t *, wi_time_interval_t, wi_string_t *); 
     110 
     111 
     112wi_p7_socket_password_provider_func_t           *wi_p7_socket_password_provider = NULL; 
     113 
     114static wi_runtime_id_t                                          _wi_p7_socket_runtime_id = WI_RUNTIME_ID_NULL; 
     115static wi_runtime_class_t                                       _wi_p7_socket_runtime_class = { 
    122116    "wi_p7_socket_t", 
    123117    _wi_p7_socket_dealloc, 
     
    179173         
    180174        wi_release(p7_socket->socket); 
    181          
    182         if(p7_socket->private_key) 
    183                 EVP_PKEY_free(p7_socket->private_key); 
    184          
    185         if(p7_socket->public_key) 
    186                 EVP_PKEY_free(p7_socket->public_key); 
    187          
    188         if(p7_socket->rsa_key) 
    189                 RSA_free(p7_socket->rsa_key); 
     175        wi_release(p7_socket->rsa); 
     176        wi_release(p7_socket->cipher); 
    190177} 
    191178 
     
    206193#pragma mark - 
    207194 
    208 void wi_p7_socket_set_private_key(wi_p7_socket_t *p7_socket, void *key) { 
    209         if(EVP_PKEY_type(((EVP_PKEY *) key)->type) != EVP_PKEY_RSA) 
    210                 wi_log_warn(WI_STR("private key must be RSA")); 
    211          
    212         p7_socket->private_key = key; 
    213         p7_socket->rsa_key = EVP_PKEY_get1_RSA(p7_socket->private_key); 
    214 
    215  
    216  
    217  
    218 void * wi_p7_socket_private_key(wi_p7_socket_t *p7_socket) { 
    219         return p7_socket->private_key; 
     195void wi_p7_socket_set_private_rsa(wi_p7_socket_t *p7_socket, wi_p7_rsa_t *p7_rsa) { 
     196        wi_retain(p7_rsa); 
     197        wi_release(p7_socket->rsa); 
     198         
     199        p7_socket->rsa = p7_rsa; 
     200
     201 
     202 
     203 
     204wi_p7_rsa_t * wi_p7_socket_private_rsa(wi_p7_socket_t *p7_socket) { 
     205        return p7_socket->rsa; 
    220206} 
    221207 
     
    256242        wi_p7_uint32_t          compression, encryption; 
    257243         
    258         p7_message = wi_autorelease(wi_p7_message_init_with_name(wi_p7_message_alloc(), WI_STR("p7.handshake"), p7_socket)); 
     244        p7_message = wi_p7_message_with_name(WI_STR("p7.handshake"), p7_socket); 
    259245         
    260246        if(!p7_message) 
     
    269255        wi_log_info(WI_STR("should set %u for encryption"), wi_p7_spec_enum_value(p7_socket->spec, 4, WI_STR("p7.handshake.encryption.rsa_aes_256"))); 
    270256         
    271         if(options & WI_P7_ENCRYPTION_RSA_AES
     257        if(options & WI_P7_ENCRYPTION_RSA_AES_256
    272258                wi_p7_message_set_enum_for_name(p7_message, 0, WI_STR("p7.handshake.encryption")); 
    273         else if(options & WI_P7_ENCRYPTION_RSA_BF
     259        else if(options & WI_P7_ENCRYPTION_RSA_BF_128
    274260                wi_p7_message_set_enum_for_name(p7_message, 1, WI_STR("p7.handshake.encryption")); 
    275261         
     
    316302        if(wi_p7_message_get_uint32_for_name(p7_message, &encryption, WI_STR("p7.handshake.encryption"))) { 
    317303                if(encryption == 0) 
    318                         p7_socket->options |= WI_P7_ENCRYPTION_RSA_AES
     304                        p7_socket->options |= WI_P7_ENCRYPTION_RSA_AES_256
    319305                else if(encryption == 1) 
    320                         p7_socket->options |= WI_P7_ENCRYPTION_RSA_BF
     306                        p7_socket->options |= WI_P7_ENCRYPTION_RSA_BF_128
    321307        } 
    322308         
     
    324310                p7_socket->remote_compatibility_check = false; 
    325311         
    326         p7_message = wi_autorelease(wi_p7_message_init_with_name(wi_p7_message_alloc(), WI_STR("p7.handshake.acknowledge"), p7_socket)); 
     312        p7_message = wi_p7_message_with_name(WI_STR("p7.handshake.acknowledge"), p7_socket); 
    327313         
    328314        if(!p7_message) 
     
    388374                wi_log_info(WI_STR("should get %@ for %u"), wi_p7_spec_enum_name(p7_socket->spec, 4, encryption), encryption); 
    389375 
    390                 if(encryption == 0 && options & WI_P7_ENCRYPTION_RSA_AES
    391                         p7_socket->options |= WI_P7_ENCRYPTION_RSA_AES
    392                 else if(encryption == 1 && options & WI_P7_ENCRYPTION_RSA_BF
    393                         p7_socket->options |= WI_P7_ENCRYPTION_RSA_BF
    394         } 
    395          
    396         p7_message = wi_autorelease(wi_p7_message_init_with_name(wi_p7_message_alloc(), WI_STR("p7.handshake.reply"), p7_socket)); 
     376                if(encryption == 0 && options & WI_P7_ENCRYPTION_RSA_AES_256
     377                        p7_socket->options |= WI_P7_ENCRYPTION_RSA_AES_256
     378                else if(encryption == 1 && options & WI_P7_ENCRYPTION_RSA_BF_128
     379                        p7_socket->options |= WI_P7_ENCRYPTION_RSA_BF_128
     380        } 
     381         
     382        p7_message = wi_p7_message_with_name(WI_STR("p7.handshake.reply"), p7_socket); 
    397383 
    398384        if(!p7_message) 
     
    405391           wi_p7_message_set_uint32_for_name(p7_message, 0, WI_STR("p7.handshake.compression")); 
    406392         
    407         if(p7_socket->options & WI_P7_ENCRYPTION_RSA_AES
     393        if(p7_socket->options & WI_P7_ENCRYPTION_RSA_AES_256
    408394                wi_p7_message_set_uint32_for_name(p7_message, 0, WI_STR("p7.handshake.encryption")); 
    409         else if(p7_socket->options & WI_P7_ENCRYPTION_RSA_BF
     395        else if(p7_socket->options & WI_P7_ENCRYPTION_RSA_BF_128
    410396                wi_p7_message_set_uint32_for_name(p7_message, 1, WI_STR("p7.handshake.encryption")); 
    411397         
     
    441427 
    442428 
    443 static wi_boolean_t _wi_p7_socket_connect_key_exchange(wi_p7_socket_t *p7_socket, wi_time_interval_t timeout, wi_string_t *username_string, wi_string_t *password_string) { 
     429static wi_boolean_t _wi_p7_socket_connect_key_exchange(wi_p7_socket_t *p7_socket, wi_time_interval_t timeout, wi_string_t *username, wi_string_t *password) { 
    444430        wi_p7_message_t         *p7_message; 
    445431        wi_data_t                       *data, *rsa_data; 
    446         wi_string_t                     *client_password_string1, *client_password_string2, *server_password_string; 
    447         unsigned char           *rsa_buffer, *cipher_key, *iv; 
    448         void                            *username, *server_password, *client_password; 
    449         uint32_t                        username_length, client_password_length, server_password_length; 
    450         int32_t                         iv_length; 
    451         int                                     cipher_key_length; 
    452          
    453         username = server_password = client_password = cipher_key = iv = NULL; 
    454          
     432        wi_string_t                     *client_password1, *client_password2, *server_password; 
     433 
    455434        p7_message = wi_p7_socket_read_message(p7_socket, timeout); 
    456435         
    457436        if(!p7_message) 
    458                 goto end
     437                return false
    459438         
    460439        if(!wi_is_equal(p7_message->name, WI_STR("p7.encryption"))) { 
    461440                wi_log_warn(WI_STR("wrong message")); 
    462441                 
    463                 goto end
     442                return false
    464443        } 
    465444         
     
    469448                wi_log_warn(WI_STR("no certificate")); 
    470449                 
    471                 goto end; 
    472         } 
    473          
    474         rsa_buffer = (unsigned char *) wi_data_bytes(rsa_data); 
    475         p7_socket->rsa_key = d2i_RSA_PUBKEY(NULL, &rsa_buffer, wi_data_length(rsa_data)); 
    476  
    477         if(!p7_socket->rsa_key) { 
    478                 ERR_print_errors_fp(stderr); 
    479                  
    480                 goto end; 
    481         } 
    482          
    483         p7_socket->public_key = EVP_PKEY_new(); 
    484         EVP_PKEY_assign_RSA(p7_socket->public_key, p7_socket->rsa_key); 
    485          
    486         p7_socket->cipher       = _wi_p7_socket_cipher(p7_socket); 
    487         cipher_key                      = wi_malloc(EVP_PKEY_size(p7_socket->public_key)); 
    488         iv_length                       = EVP_CIPHER_iv_length(p7_socket->cipher); 
    489         iv                                      = wi_malloc(iv_length); 
    490          
    491         if(EVP_SealInit(&p7_socket->cipher_ctx, p7_socket->cipher, NULL, NULL, NULL, NULL, 1) != 1) { 
    492                 ERR_print_errors_fp(stderr); 
    493                  
    494                 goto end; 
    495         } 
    496  
    497         _wi_p7_socket_configure_cipher_ctx(p7_socket); 
    498  
    499         if(EVP_SealInit(&p7_socket->cipher_ctx, 
    500                                         NULL, 
    501                                         &cipher_key, 
    502                                         &cipher_key_length, 
    503                                         iv, 
    504                                         &p7_socket->public_key, 
    505                                         1) != 1) { 
    506                 ERR_print_errors_fp(stderr); 
    507                  
    508                 goto end; 
    509         } 
    510          
    511         if(!_wi_p7_socket_public_encrypt_buffer(p7_socket, 
    512                                                                                         wi_string_cstring(username_string), 
    513                                                                                         wi_string_length(username_string), 
    514                                                                                         &username, 
    515                                                                                         &username_length)) 
    516                 goto end; 
    517          
    518         client_password_string1 = wi_data_sha1(wi_data_by_appending_data(wi_string_data(wi_string_sha1(password_string)), rsa_data)); 
    519         client_password_string2 = wi_data_sha1(wi_data_by_appending_data(rsa_data, wi_string_data(wi_string_sha1(password_string)))); 
    520          
    521         if(!_wi_p7_socket_public_encrypt_buffer(p7_socket, 
    522                                                                                         wi_string_cstring(client_password_string1), 
    523                                                                                         wi_string_length(client_password_string1), 
    524                                                                                         &client_password, 
    525                                                                                         &client_password_length)) 
    526                 goto end; 
    527          
    528         p7_message = wi_autorelease(wi_p7_message_init_with_name(wi_p7_message_alloc(), WI_STR("p7.encryption.reply"), p7_socket)); 
     450                return false; 
     451        } 
     452         
     453        p7_socket->rsa = wi_p7_rsa_init_with_public_key_data(wi_p7_rsa_alloc(), rsa_data); 
     454         
     455        if(!p7_socket->rsa) { 
     456                wi_log_info(WI_STR("could not create rsa")); 
     457                 
     458                return false; 
     459        } 
     460         
     461        p7_socket->cipher = wi_p7_cipher_init_with_public_rsa(wi_p7_cipher_alloc(), 
     462                                                                                                                  _wi_p7_socket_cipher(p7_socket), 
     463                                                                    &nbs