Changeset 4639

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

Refactor encryption interface, don't use envelope methods, do it ourselves instead

Fix reading of XML messages with no fields

Rename p7.encryption.cipher_key_iv to p7.encryption.cipher_iv

Various refactorings

Files:

Legend:

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

    r4629 r4639  
    3838 
    3939#include <openssl/pem.h> 
     40#include <openssl/rand.h> 
    4041#include <openssl/rsa.h> 
    4142#include <openssl/x509.h> 
     
    4546         
    4647        RSA                                                                     *rsa; 
    47         wi_data_t                                                       *public_key_data
     48        wi_data_t                                                       *public_key
    4849}; 
    4950 
     
    5152static wi_string_t *                                    _wi_p7_rsa_description(wi_runtime_instance_t *); 
    5253 
    53 static wi_data_t *                                              _wi_p7_rsa_public_key_data(wi_p7_rsa_t *); 
     54static wi_data_t *                                              _wi_p7_rsa_public_key(wi_p7_rsa_t *); 
    5455 
    5556static wi_runtime_id_t                                  _wi_p7_rsa_runtime_id = WI_RUNTIME_ID_NULL; 
     
    6869        wi_runtime_base_t                                       base; 
    6970         
    70         EVP_PKEY                                                       *aes
     71        wi_p7_cipher_type_t                                    type
    7172        const EVP_CIPHER                                        *cipher; 
    72         EVP_CIPHER_CTX                                          cipher_ctx; 
    73         wi_data_t                                                       *key_data; 
    74         wi_data_t                                                       *iv_data; 
     73        EVP_CIPHER_CTX                                          encrypt_ctx; 
     74        EVP_CIPHER_CTX                                          decrypt_ctx; 
     75        wi_data_t                                                       *key; 
     76        wi_data_t                                                       *iv; 
    7577}; 
    7678 
     
    7880static wi_string_t *                                    _wi_p7_cipher_description(wi_runtime_instance_t *); 
    7981 
    80 static const EVP_CIPHER *                               _wi_p7_cipher_cipher_for_type(wi_p7_cipher_t *, wi_p7_cipher_type_t); 
    81 static void                                                             _wi_p7_cipher_configure_cipher_for_type(wi_p7_cipher_t *, wi_p7_cipher_type_t); 
     82static const EVP_CIPHER *                               _wi_p7_cipher_cipher(wi_p7_cipher_t *); 
     83static void                                                             _wi_p7_cipher_configure_cipher(wi_p7_cipher_t *); 
    8284 
    8385static wi_runtime_id_t                                  _wi_p7_cipher_runtime_id = WI_RUNTIME_ID_NULL; 
     
    130132        } 
    131133         
    132         p7_rsa->public_key_data = wi_retain(_wi_p7_rsa_public_key_data(p7_rsa)); 
    133          
    134         if(!p7_rsa->public_key_data) { 
     134        p7_rsa->public_key = wi_retain(_wi_p7_rsa_public_key(p7_rsa)); 
     135         
     136        if(!p7_rsa->public_key) { 
    135137                wi_release(p7_rsa); 
    136138 
     
    168170        } 
    169171         
    170         p7_rsa->public_key_data = wi_retain(_wi_p7_rsa_public_key_data(p7_rsa)); 
    171          
    172         if(!p7_rsa->public_key_data) { 
     172        p7_rsa->public_key = wi_retain(_wi_p7_rsa_public_key(p7_rsa)); 
     173         
     174        if(!p7_rsa->public_key) { 
    173175                wi_release(p7_rsa); 
    174176 
     
    181183 
    182184 
    183 wi_p7_rsa_t * wi_p7_rsa_init_with_public_key_data(wi_p7_rsa_t *p7_rsa, wi_data_t *data) { 
     185wi_p7_rsa_t * wi_p7_rsa_init_with_public_key(wi_p7_rsa_t *p7_rsa, wi_data_t *data) { 
    184186        unsigned char   *buffer; 
    185187        long                    length; 
     
    198200        } 
    199201         
    200         p7_rsa->public_key_data = wi_retain(data); 
     202        p7_rsa->public_key = wi_retain(data); 
    201203         
    202204        return p7_rsa; 
     
    209211         
    210212        RSA_free(p7_rsa->rsa); 
    211         wi_release(p7_rsa->public_key_data); 
     213        wi_release(p7_rsa->public_key); 
    212214} 
    213215 
     
    228230#pragma mark - 
    229231 
    230 static wi_data_t * _wi_p7_rsa_public_key_data(wi_p7_rsa_t *p7_rsa) { 
     232static wi_data_t * _wi_p7_rsa_public_key(wi_p7_rsa_t *p7_rsa) { 
    231233        wi_data_t               *data; 
    232234        unsigned char   *buffer; 
     
    253255#pragma mark - 
    254256 
    255 wi_data_t * wi_p7_rsa_public_key_data(wi_p7_rsa_t *p7_rsa) { 
    256         return p7_rsa->public_key_data; 
    257 
    258  
    259  
    260  
    261 #pragma mark - 
    262  
    263 wi_boolean_t wi_p7_rsa_public_encrypt_bytes(wi_p7_rsa_t *p7_rsa, const void *decrypted_buffer, wi_uinteger_t decrypted_length, void **out_buffer, wi_uinteger_t *out_length) { 
    264         void            *encrypted_buffer; 
    265         int32_t         encrypted_length; 
    266  
    267         encrypted_buffer = wi_malloc(RSA_size(p7_rsa->rsa)); 
    268         encrypted_length = RSA_public_encrypt(decrypted_length, decrypted_buffer, encrypted_buffer, p7_rsa->rsa, RSA_PKCS1_PADDING); 
    269          
    270         if(encrypted_length == -1) { 
    271                 wi_error_set_openssl_error(); 
    272                  
    273                 wi_free(encrypted_buffer); 
    274                  
    275                 return false; 
    276         } 
    277          
    278         *out_buffer = encrypted_buffer; 
    279         *out_length = encrypted_length; 
    280  
    281         return true; 
    282 
    283  
    284  
    285  
    286 wi_data_t * wi_p7_rsa_public_encrypt_data(wi_p7_rsa_t *p7_rsa, wi_data_t *decrypted_data) { 
     257wi_data_t * wi_p7_rsa_public_key(wi_p7_rsa_t *p7_rsa) { 
     258        return p7_rsa->public_key; 
     259
     260 
     261 
     262 
     263#pragma mark - 
     264 
     265wi_data_t * wi_p7_rsa_encrypt(wi_p7_rsa_t *p7_rsa, wi_data_t *decrypted_data) { 
    287266        const void              *decrypted_buffer; 
    288267        void                    *encrypted_buffer; 
     
    292271        decrypted_length = wi_data_length(decrypted_data); 
    293272         
    294         if(!wi_p7_rsa_public_encrypt_bytes(p7_rsa, decrypted_buffer, decrypted_length, &encrypted_buffer, &encrypted_length)) 
     273        if(!wi_p7_rsa_encrypt_bytes(p7_rsa, decrypted_buffer, decrypted_length, &encrypted_buffer, &encrypted_length)) 
    295274                return NULL; 
    296275         
     
    300279 
    301280 
    302 wi_boolean_t wi_p7_rsa_private_decrypt_bytes(wi_p7_rsa_t *p7_rsa, const void *encrypted_buffer, wi_uinteger_t encrypted_length, void **out_buffer, wi_uinteger_t *out_length) { 
    303         void            *decrypted_buffer; 
    304         int32_t         decrypted_length; 
    305          
    306         decrypted_buffer = wi_malloc(RSA_size(p7_rsa->rsa)); 
    307         decrypted_length = RSA_private_decrypt(encrypted_length, encrypted_buffer, decrypted_buffer, p7_rsa->rsa, RSA_PKCS1_PADDING); 
    308  
    309         if(decrypted_length == -1) { 
    310                 wi_error_set_openssl_error(); 
    311                  
    312                 wi_free(decrypted_buffer); 
    313  
     281wi_boolean_t wi_p7_rsa_encrypt_bytes(wi_p7_rsa_t *p7_rsa, const void *decrypted_buffer, wi_uinteger_t decrypted_length, void **out_buffer, wi_uinteger_t *out_length) { 
     282        void            *encrypted_buffer; 
     283        int32_t         encrypted_length; 
     284 
     285        encrypted_buffer = wi_malloc(RSA_size(p7_rsa->rsa)); 
     286        encrypted_length = RSA_public_encrypt(decrypted_length, decrypted_buffer, encrypted_buffer, p7_rsa->rsa, RSA_PKCS1_PADDING); 
     287         
     288        if(encrypted_length == -1) { 
     289                wi_error_set_openssl_error(); 
     290                 
     291                wi_free(encrypted_buffer); 
     292                 
    314293                return false; 
    315294        } 
    316295         
    317         *out_buffer = decrypted_buffer; 
    318         *out_length = decrypted_length; 
     296        *out_buffer = encrypted_buffer; 
     297        *out_length = encrypted_length; 
    319298 
    320299        return true; 
     
    323302 
    324303 
    325 wi_data_t * wi_p7_rsa_private_decrypt_data(wi_p7_rsa_t *p7_rsa, wi_data_t *encrypted_data) { 
     304wi_data_t * wi_p7_rsa_decrypt(wi_p7_rsa_t *p7_rsa, wi_data_t *encrypted_data) { 
    326305        const void              *encrypted_buffer; 
    327306        void                    *decrypted_buffer; 
     
    331310        encrypted_length = wi_data_length(encrypted_data); 
    332311         
    333         if(!wi_p7_rsa_private_decrypt_bytes(p7_rsa, encrypted_buffer, encrypted_length, &decrypted_buffer, &decrypted_length)) 
     312        if(!wi_p7_rsa_decrypt_bytes(p7_rsa, encrypted_buffer, encrypted_length, &decrypted_buffer, &decrypted_length)) 
    334313                return NULL; 
    335314         
    336315        return wi_data_with_bytes_no_copy(decrypted_buffer, decrypted_length, true); 
     316} 
     317 
     318 
     319 
     320wi_boolean_t wi_p7_rsa_decrypt_bytes(wi_p7_rsa_t *p7_rsa, const void *encrypted_buffer, wi_uinteger_t encrypted_length, void **out_buffer, wi_uinteger_t *out_length) { 
     321        void            *decrypted_buffer; 
     322        int32_t         decrypted_length; 
     323         
     324        decrypted_buffer = wi_malloc(RSA_size(p7_rsa->rsa)); 
     325        decrypted_length = RSA_private_decrypt(encrypted_length, encrypted_buffer, decrypted_buffer, p7_rsa->rsa, RSA_PKCS1_PADDING); 
     326 
     327        if(decrypted_length == -1) { 
     328                wi_error_set_openssl_error(); 
     329                 
     330                wi_free(decrypted_buffer); 
     331 
     332                return false; 
     333        } 
     334         
     335        *out_buffer = decrypted_buffer; 
     336        *out_length = decrypted_length; 
     337 
     338        return true; 
    337339} 
    338340 
     
    354356 
    355357 
    356 wi_p7_cipher_t * wi_p7_cipher_init_with_public_rsa(wi_p7_cipher_t *p7_cipher, wi_p7_cipher_type_t type, wi_p7_rsa_t *p7_rsa) { 
    357         EVP_PKEY                        *private_cipher; 
     358 
     359wi_p7_cipher_t * wi_p7_cipher_init_with_key(wi_p7_cipher_t *p7_cipher, wi_p7_cipher_type_t type, wi_data_t *key, wi_data_t *iv) { 
     360        unsigned char           *key_buffer, *iv_buffer; 
     361         
     362        p7_cipher->type         = type; 
     363        p7_cipher->cipher       = _wi_p7_cipher_cipher(p7_cipher); 
     364         
     365        key_buffer                      = (unsigned char *) wi_data_bytes(key); 
     366        iv_buffer                       = iv ? (unsigned char *) wi_data_bytes(iv) : NULL; 
     367         
     368        if(EVP_EncryptInit(&p7_cipher->encrypt_ctx, p7_cipher->cipher, NULL, NULL) != 1) { 
     369                wi_error_set_openssl_error(); 
     370                 
     371                wi_release(p7_cipher); 
     372                 
     373                return NULL; 
     374        } 
     375         
     376        if(EVP_DecryptInit(&p7_cipher->decrypt_ctx, p7_cipher->cipher, NULL, NULL) != 1) { 
     377                wi_error_set_openssl_error(); 
     378                 
     379                wi_release(p7_cipher); 
     380                 
     381                return NULL; 
     382        } 
     383         
     384        _wi_p7_cipher_configure_cipher(p7_cipher); 
     385 
     386        if(EVP_EncryptInit(&p7_cipher->encrypt_ctx, p7_cipher->cipher, key_buffer, iv_buffer) != 1) { 
     387                wi_error_set_openssl_error(); 
     388                 
     389                wi_release(p7_cipher); 
     390                 
     391                return NULL; 
     392        } 
     393 
     394        if(EVP_DecryptInit(&p7_cipher->decrypt_ctx, p7_cipher->cipher, key_buffer, iv_buffer) != 1) { 
     395                wi_error_set_openssl_error(); 
     396                 
     397                wi_release(p7_cipher); 
     398                 
     399                return NULL; 
     400        } 
     401         
     402        p7_cipher->key = wi_retain(key); 
     403        p7_cipher->iv = wi_retain(iv); 
     404 
     405        return p7_cipher; 
     406
     407 
     408 
     409 
     410wi_p7_cipher_t * wi_p7_cipher_init_with_random_key(wi_p7_cipher_t *p7_cipher, wi_p7_cipher_type_t type) { 
    358411        unsigned char           *key_buffer, *iv_buffer; 
    359412        int                                     key_length, iv_length; 
    360413         
    361         p7_cipher->cipher = _wi_p7_cipher_cipher_for_type(p7_cipher, type); 
    362          
    363         if(EVP_SealInit(&p7_cipher->cipher_ctx, p7_cipher->cipher, NULL, NULL, NULL, NULL, 1) != 1) { 
    364                 wi_error_set_openssl_error(); 
    365                  
    366                 wi_release(p7_cipher); 
    367                  
    368                 return NULL; 
    369         } 
    370          
    371         _wi_p7_cipher_configure_cipher_for_type(p7_cipher, type); 
    372  
    373         private_cipher          = EVP_PKEY_new(); 
    374          
    375         EVP_PKEY_set1_RSA(private_cipher, p7_rsa->rsa); 
    376          
    377         key_buffer                      = wi_malloc(EVP_PKEY_size(private_cipher)); 
     414        p7_cipher->type         = type; 
     415        p7_cipher->cipher       = _wi_p7_cipher_cipher(p7_cipher); 
     416         
     417        key_length                      = EVP_MAX_KEY_LENGTH; 
     418        key_buffer                      = wi_malloc(key_length); 
    378419        iv_length                       = EVP_CIPHER_iv_length(p7_cipher->cipher); 
    379         iv_buffer                       = wi_malloc(iv_length); 
    380          
    381         if(EVP_SealInit(&p7_cipher->cipher_ctx, NULL, &key_buffer, &key_length, iv_buffer, &private_cipher, 1) != 1) { 
    382                 wi_error_set_openssl_error(); 
    383                  
    384                 EVP_PKEY_free(private_cipher); 
    385                 wi_free(key_buffer); 
    386                 wi_free(iv_buffer); 
    387                 wi_release(p7_cipher); 
    388  
    389                 return NULL; 
    390         } 
    391          
    392         p7_cipher->key_data = wi_data_init_with_bytes_no_copy(wi_data_alloc(), key_buffer, key_length, true); 
    393         p7_cipher->iv_data = wi_data_init_with_bytes_no_copy(wi_data_alloc(), iv_buffer, iv_length, true); 
    394          
    395         EVP_PKEY_free(private_cipher); 
    396  
    397         return p7_cipher; 
    398 
    399  
    400  
    401  
    402 wi_p7_cipher_t * wi_p7_cipher_init_with_private_rsa(wi_p7_cipher_t *p7_cipher, wi_p7_cipher_type_t type, wi_p7_rsa_t *p7_rsa, wi_data_t *key_data, wi_data_t *iv_data) { 
    403         EVP_PKEY                        *private_rsa; 
    404         unsigned char           *key_buffer, *iv_buffer; 
    405         int                                     key_length; 
    406          
    407         p7_cipher->cipher = _wi_p7_cipher_cipher_for_type(p7_cipher, type); 
    408          
    409         if(EVP_OpenInit(&p7_cipher->cipher_ctx, p7_cipher->cipher, NULL, 0, NULL, NULL) != 1) { 
    410                 wi_error_set_openssl_error(); 
    411                  
    412                 wi_release(p7_cipher); 
    413                  
    414                 return NULL; 
    415         } 
    416          
    417         _wi_p7_cipher_configure_cipher_for_type(p7_cipher, type); 
    418  
    419         private_rsa                     = EVP_PKEY_new(); 
    420          
    421         EVP_PKEY_set1_RSA(private_rsa, p7_rsa->rsa); 
    422          
    423         key_buffer                      = (unsigned char *) wi_data_bytes(key_data); 
    424         key_length                      = wi_data_length(key_data); 
    425         iv_buffer                       = iv_data ? (unsigned char *) wi_data_bytes(iv_data) : NULL; 
    426          
    427         if(EVP_OpenInit(&p7_cipher->cipher_ctx, NULL, key_buffer, key_length, iv_buffer, private_rsa) != 1) { 
    428                 wi_error_set_openssl_error(); 
    429                  
    430                 EVP_PKEY_free(private_rsa); 
    431                 wi_release(p7_cipher); 
    432  
    433                 return NULL; 
    434         } 
    435          
    436         p7_cipher->key_data = wi_retain(key_data); 
    437         p7_cipher->iv_data = wi_retain(iv_data); 
    438          
    439         EVP_PKEY_free(private_rsa); 
     420        iv_buffer                       = (iv_length > 0) ? wi_malloc(iv_length) : NULL; 
     421         
     422        if(RAND_bytes(key_buffer, key_length) <= 0) { 
     423                wi_error_set_openssl_error(); 
     424                 
     425                wi_release(p7_cipher); 
     426                 
     427                return NULL; 
     428        } 
     429 
     430        if(EVP_EncryptInit(&p7_cipher->encrypt_ctx, p7_cipher->cipher, NULL, NULL) != 1) { 
     431                wi_error_set_openssl_error(); 
     432                 
     433                wi_release(p7_cipher); 
     434                 
     435                return NULL; 
     436        } 
     437         
     438        if(EVP_DecryptInit(&p7_cipher->decrypt_ctx, p7_cipher->cipher, NULL, NULL) != 1) { 
     439                wi_error_set_openssl_error(); 
     440                 
     441                wi_release(p7_cipher); 
     442                 
     443                return NULL; 
     444        } 
     445         
     446        _wi_p7_cipher_configure_cipher(p7_cipher); 
     447 
     448        if(EVP_EncryptInit(&p7_cipher->encrypt_ctx, p7_cipher->cipher, key_buffer, iv_buffer) != 1) { 
     449                wi_error_set_openssl_error(); 
     450                 
     451                wi_release(p7_cipher); 
     452                 
     453                return NULL; 
     454        } 
     455 
     456        if(EVP_DecryptInit(&p7_cipher->decrypt_ctx, p7_cipher->cipher, key_buffer, iv_buffer) != 1) { 
     457                wi_error_set_openssl_error(); 
     458                 
     459                wi_release(p7_cipher); 
     460                 
     461                return NULL; 
     462        } 
     463         
     464        p7_cipher->key = wi_data_init_with_bytes_no_copy(wi_data_alloc(), key_buffer, key_length, true); 
     465        p7_cipher->iv = (iv_length > 0) ? wi_data_init_with_bytes_no_copy(wi_data_alloc(), iv_buffer, iv_length, true) : NULL; 
    440466 
    441467        return p7_cipher; 
     
    447473        wi_p7_cipher_t          *p7_cipher = instance; 
    448474         
    449         wi_release(p7_cipher->key_data); 
    450         wi_release(p7_cipher->iv_data); 
     475        EVP_CIPHER_CTX_cleanup(&p7_cipher->encrypt_ctx); 
     476        EVP_CIPHER_CTX_cleanup(&p7_cipher->decrypt_ctx); 
     477         
     478        wi_release(p7_cipher->key); 
     479        wi_release(p7_cipher->iv); 
    451480} 
    452481 
     
    456485        wi_p7_cipher_t          *p7_cipher = instance; 
    457486         
    458         return wi_string_with_format(WI_STR("<%@ %p>{name = %@, bits = %u}"), 
     487        return wi_string_with_format(WI_STR("<%@ %p>{name = %@, bits = %u, iv = %@, key = %@}"), 
    459488        wi_runtime_class_name(p7_cipher), 
    460489                p7_cipher, 
    461490                wi_p7_cipher_name(p7_cipher), 
    462                 wi_p7_cipher_bits(p7_cipher)); 
    463 
    464  
    465  
    466  
    467 #pragma mark - 
    468  
    469 static const EVP_CIPHER * _wi_p7_cipher_cipher_for_type(wi_p7_cipher_t *p7_cipher, wi_p7_cipher_type_t type) { 
    470         switch(type) { 
    471                 case WI_P7_CIPHER_NULL:         return EVP_enc_null(); 
     491                wi_p7_cipher_bits(p7_cipher), 
     492                wi_p7_cipher_iv(p7_cipher), 
     493                wi_p7_cipher_key(p7_cipher)); 
     494
     495 
     496 
     497 
     498#pragma mark - 
     499 
     500static const EVP_CIPHER * _wi_p7_cipher_cipher(wi_p7_cipher_t *p7_cipher) { 
     501        switch(p7_cipher->type) { 
    472502                case WI_P7_CIPHER_AES_256:      return EVP_aes_256_cbc(); 
    473503                case WI_P7_CIPHER_BF_128:       return EVP_bf_cbc(); 
     
    479509 
    480510 
    481 static void _wi_p7_cipher_configure_cipher_for_type(wi_p7_cipher_t *p7_cipher, wi_p7_cipher_type_t type) { 
    482         if(type == WI_P7_CIPHER_BF_128) 
    483                 EVP_CIPHER_CTX_set_key_length(&p7_cipher->cipher_ctx, 16); 
    484 
    485  
    486  
    487  
    488 #pragma mark - 
    489  
    490 wi_data_t * wi_p7_cipher_key_data(wi_p7_cipher_t *p7_cipher) { 
    491         return p7_cipher->key_data; 
    492 
    493  
    494  
    495  
    496 wi_data_t * wi_p7_cipher_iv_data(wi_p7_cipher_t *p7_cipher) { 
    497         return p7_cipher->iv_data; 
     511static void _wi_p7_cipher_configure_cipher(wi_p7_cipher_t *p7_cipher) { 
     512        if(p7_cipher->type == WI_P7_CIPHER_BF_128) { 
     513                EVP_CIPHER_CTX_set_key_length(&p7_cipher->encrypt_ctx, 16); 
     514                EVP_CIPHER_CTX_set_key_length(&p7_cipher->decrypt_ctx, 16); 
     515        } 
     516
     517 
     518 
     519 
     520#pragma mark - 
     521 
     522wi_data_t * wi_p7_cipher_key(wi_p7_cipher_t *p7_cipher) { 
     523        return p7_cipher->key; 
     524
     525 
     526 
     527 
     528wi_data_t * wi_p7_cipher_iv(wi_p7_cipher_t *p7_cipher) { 
     529        return p7_cipher->iv; 
    498530} 
    499531 
     
    503535 
    504536wi_string_t * wi_p7_cipher_name(wi_p7_cipher_t *p7_cipher) { 
    505         return wi_string_with_cstring(EVP_CIPHER_name(p7_cipher->cipher)); 
     537        switch(p7_cipher->type) { 
     538                case WI_P7_CIPHER_AES_256:      return WI_STR("AES"); 
     539                case WI_P7_CIPHER_BF_128:       return WI_STR("Blowfish"); 
     540        } 
     541         
     542        return NULL; 
    506543} 
    507544 
     
    509546 
    510547wi_uinteger_t wi_p7_cipher_bits(wi_p7_cipher_t *p7_cipher) { 
    511         return EVP_CIPHER_key_length(&p7_cipher->cipher_ctx) * 8; 
    512 
    513  
    514  
    515  
    516 #pragma mark - 
    517  
    518 wi_boolean_t wi_p7_cipher_encrypt_bytes(wi_p7_cipher_t *p7_cipher, const void *decrypted_buffer, wi_uinteger_t decrypted_length, void **out_buffer, wi_uinteger_t *out_length) { 
    519         void                    *encrypted_buffer; 
    520         wi_uinteger_t   encrypted_length, padded_length; 
    521          
    522         encrypted_buffer = wi_malloc(decrypted_length + EVP_CIPHER_block_size(p7_cipher->cipher)); 
    523  
    524         if(EVP_SealUpdate(&p7_cipher->cipher_ctx, encrypted_buffer, (int *) &encrypted_length, decrypted_buffer, decrypted_length) != 1) { 
    525                 wi_error_set_openssl_error(); 
    526                  
    527                 wi_free(encrypted_buffer); 
    528                  
    529                 return false; 
    530         } 
    531          
    532         if(EVP_SealFinal(&p7_cipher->cipher_ctx, encrypted_buffer + encrypted_length, (int *) &padded_length) != 1) { 
    533                 wi_error_set_openssl_error(); 
    534                  
    535                 wi_free(encrypted_buffer); 
    536                  
    537                 return false; 
    538         } 
    539          
    540         *out_buffer = encrypted_buffer; 
    541         *out_length = encrypted_length + padded_length; 
    542          
    543         return true; 
    544 
    545  
    546  
    547  
    548 wi_data_t * wi_p7_cipher_encrypt_data(wi_p7_cipher_t *p7_cipher, wi_data_t *decrypted_data) { 
     548        return EVP_CIPHER_key_length(&p7_cipher->encrypt_ctx) * 8; 
     549
     550 
     551 
     552 
     553#pragma mark - 
     554 
     555wi_data_t * wi_p7_cipher_encrypt(wi_p7_cipher_t *p7_cipher, wi_data_t *decrypted_data) { 
    549556        const void              *decrypted_buffer; 
    550557        void                    *encrypted_buffer; 
     
    562569 
    563570 
    564 wi_boolean_t wi_p7_cipher_decrypt_bytes(wi_p7_cipher_t *p7_cipher, const void *encrypted_buffer, wi_uinteger_t encrypted_length, void **out_buffer, wi_uinteger_t *out_length) { 
    565         void                   *decrypted_buffer; 
    566         wi_uinteger_t  decrypted_length, padded_length; 
    567          
    568         decrypted_buffer = wi_malloc(encrypted_length + EVP_CIPHER_block_size(p7_cipher->cipher)); 
    569  
    570         if(EVP_OpenUpdate(&p7_cipher->cipher_ctx, decrypted_buffer, (int *) &decrypted_length, encrypted_buffer, encrypted_length) != 1) { 
    571                 wi_error_set_openssl_error(); 
    572                  
    573                 wi_free(decrypted_buffer); 
     571wi_boolean_t wi_p7_cipher_encrypt_bytes(wi_p7_cipher_t *p7_cipher, const void *decrypted_buffer, wi_uinteger_t decrypted_length, void **out_buffer, wi_uinteger_t *out_length) { 
     572        void            *encrypted_buffer; 
     573        int                    encrypted_length, padded_length; 
     574         
     575        encrypted_buffer = wi_malloc(decrypted_length + EVP_CIPHER_block_size(p7_cipher->cipher)); 
     576 
     577        if(EVP_EncryptUpdate(&p7_cipher->encrypt_ctx, encrypted_buffer, &encrypted_length, decrypted_buffer, decrypted_length) != 1) { 
     578                wi_error_set_openssl_error(); 
     579                 
     580                wi_free(encrypted_buffer); 
    574581                 
    575582                return false; 
    576583        } 
    577584         
    578         if(EVP_OpenFinal(&p7_cipher->cipher_ctx, decrypted_buffer + decrypted_length, (int *) &padded_length) != 1) { 
    579                 wi_error_set_openssl_error(); 
    580                  
    581                 wi_free(decrypted_buffer); 
     585        if(EVP_EncryptFinal(&p7_cipher->encrypt_ctx, encrypted_buffer + encrypted_length, &padded_length) != 1) { 
     586                wi_error_set_openssl_error(); 
     587                 
     588                wi_free(encrypted_buffer); 
    582589                 
    583590                return false; 
    584591        } 
    585          
    586         *out_buffer = decrypted_buffer; 
    587         *out_length = decrypted_length + padded_length; 
     592 
     593        *out_buffer = encrypted_buffer; 
     594        *out_length = encrypted_length + padded_length; 
    588595         
    589596        return true; 
     
    592599 
    593600 
    594 wi_data_t * wi_p7_cipher_decrypt_data(wi_p7_cipher_t *p7_cipher, wi_data_t *encrypted_data) { 
     601wi_data_t * wi_p7_cipher_decrypt(wi_p7_cipher_t *p7_cipher, wi_data_t *encrypted_data) { 
    595602        const void              *encrypted_buffer; 
    596603        void                    *decrypted_buffer; 
     
    606613} 
    607614 
     615 
     616 
     617wi_boolean_t wi_p7_cipher_decrypt_bytes(wi_p7_cipher_t *p7_cipher, const void *encrypted_buffer, wi_uinteger_t encrypted_length, void **out_buffer, wi_uinteger_t *out_length) { 
     618        void            *decrypted_buffer; 
     619        int                     decrypted_length, padded_length; 
     620         
     621        decrypted_buffer = wi_malloc(encrypted_length + EVP_CIPHER_block_size(p7_cipher->cipher)); 
     622         
     623        if(EVP_DecryptUpdate(&p7_cipher->decrypt_ctx, decrypted_buffer, &decrypted_length, encrypted_buffer, encrypted_length) != 1) { 
     624                wi_error_set_openssl_error(); 
     625                 
     626                wi_free(decrypted_buffer); 
     627                 
     628                return false; 
     629        } 
     630         
     631        if(EVP_DecryptFinal(&p7_cipher->decrypt_ctx, decrypted_buffer + decrypted_length, &padded_length) != 1) { 
     632                wi_error_set_openssl_error(); 
     633                 
     634                wi_free(decrypted_buffer); 
     635                 
     636                return false; 
     637        } 
     638         
     639        *out_buffer = decrypted_buffer; 
     640        *out_length = decrypted_length + padded_length; 
     641         
     642        return true; 
     643} 
     644 
    608645#endif 
  • libwired/trunk/libwired/p7/wi-p7-crypto.h

    r4629 r4639  
    3535 
    3636enum _wi_p7_cipher_type { 
    37         WI_P7_CIPHER_NULL, 
    3837        WI_P7_CIPHER_AES_256, 
    3938        WI_P7_CIPHER_BF_128 
     
    5150WI_EXPORT wi_p7_rsa_t *                                 wi_p7_rsa_init_with_bits(wi_p7_rsa_t *, wi_uinteger_t); 
    5251WI_EXPORT wi_p7_rsa_t *                                 wi_p7_rsa_init_with_pem_file(wi_p7_rsa_t *, wi_string_t *); 
    53 WI_EXPORT wi_p7_rsa_t *                                 wi_p7_rsa_init_with_public_key_data(wi_p7_rsa_t *, wi_data_t *); 
     52WI_EXPORT wi_p7_rsa_t *                                 wi_p7_rsa_init_with_public_key(wi_p7_rsa_t *, wi_data_t *); 
    5453 
    55 WI_EXPORT wi_data_t *                                   wi_p7_rsa_public_key_data(wi_p7_rsa_t *); 
     54WI_EXPORT wi_data_t *                                   wi_p7_rsa_public_key(wi_p7_rsa_t *); 
    5655 
    57 WI_EXPORT wi_boolean_t                                 wi_p7_rsa_public_encrypt_bytes(wi_p7_rsa_t *, const void *, wi_uinteger_t, void **, wi_uinteger_t *); 
    58 WI_EXPORT wi_data_t *                                  wi_p7_rsa_public_encrypt_data(wi_p7_rsa_t *, wi_data_t *); 
    59 WI_EXPORT wi_boolean_t                                 wi_p7_rsa_private_decrypt_bytes(wi_p7_rsa_t *, const void *, wi_uinteger_t, void **, wi_uinteger_t *); 
    60 WI_EXPORT wi_data_t *                                  wi_p7_rsa_private_decrypt_data(wi_p7_rsa_t *, wi_data_t *); 
     56WI_EXPORT wi_data_t *                                  wi_p7_rsa_encrypt(wi_p7_rsa_t *, wi_data_t *); 
     57WI_EXPORT wi_boolean_t                                 wi_p7_rsa_encrypt_bytes(wi_p7_rsa_t *, const void *, wi_uinteger_t, void **, wi_uinteger_t *); 
     58WI_EXPORT wi_data_t *                                  wi_p7_rsa_decrypt(wi_p7_rsa_t *, wi_data_t *); 
     59WI_EXPORT wi_boolean_t                                 wi_p7_rsa_decrypt_bytes(wi_p7_rsa_t *, const void *, wi_uinteger_t, void **, wi_uinteger_t *); 
    6160 
    6261 
     
    6463 
    6564WI_EXPORT wi_p7_cipher_t *                              wi_p7_cipher_alloc(void); 
    66 WI_EXPORT wi_p7_cipher_t *                              wi_p7_cipher_init_with_public_rsa(wi_p7_cipher_t *, wi_p7_cipher_type_t, wi_p7_rsa_t *); 
    67 WI_EXPORT wi_p7_cipher_t *                              wi_p7_cipher_init_with_private_rsa(wi_p7_cipher_t *, wi_p7_cipher_type_t, wi_p7_rsa_t *, wi_data_t *, wi_data_t *); 
     65WI_EXPORT wi_p7_cipher_t *                              wi_p7_cipher_init_with_key(wi_p7_cipher_t *, wi_p7_cipher_type_t, wi_data_t *, wi_data_t *); 
     66WI_EXPORT wi_p7_cipher_t *                              wi_p7_cipher_init_with_random_key(wi_p7_cipher_t *, wi_p7_cipher_type_t); 
    6867 
    69 WI_EXPORT wi_data_t *                                   wi_p7_cipher_key_data(wi_p7_cipher_t *); 
    70 WI_EXPORT wi_data_t *                                   wi_p7_cipher_iv_data(wi_p7_cipher_t *); 
     68WI_EXPORT wi_data_t *                                   wi_p7_cipher_key(wi_p7_cipher_t *); 
     69WI_EXPORT wi_data_t *                                   wi_p7_cipher_iv(wi_p7_cipher_t *); 
    7170 
    7271WI_EXPORT wi_string_t *                                 wi_p7_cipher_name(wi_p7_cipher_t *); 
    7372WI_EXPORT wi_uinteger_t                                 wi_p7_cipher_bits(wi_p7_cipher_t *); 
    7473 
     74WI_EXPORT wi_data_t *                                   wi_p7_cipher_encrypt(wi_p7_cipher_t *, wi_data_t *); 
    7575WI_EXPORT wi_boolean_t                                  wi_p7_cipher_encrypt_bytes(wi_p7_cipher_t *, const void *, wi_uinteger_t, void **, wi_uinteger_t *); 
    76 WI_EXPORT wi_data_t *                                   wi_p7_cipher_encrypt_data(wi_p7_cipher_t *, wi_data_t *); 
     76WI_EXPORT wi_data_t *                                   wi_p7_cipher_decrypt(wi_p7_cipher_t *, wi_data_t *); 
    7777WI_EXPORT wi_boolean_t                                  wi_p7_cipher_decrypt_bytes(wi_p7_cipher_t *, const void *, wi_uinteger_t, void **, wi_uinteger_t *); 
    78 WI_EXPORT wi_data_t *                                   wi_p7_cipher_decrypt_data(wi_p7_cipher_t *, wi_data_t *); 
    7978 
    8079#endif /* WI_P7_CRYPTO_H */ 
  • libwired/trunk/libwired/p7/wi-p7-message.c

    r4623 r4639  
    121121 
    122122        if(p7_message->serialization == WI_P7_BINARY) { 
    123                 p7_message->binary_buffer_size        = _WI_P7_MESSAGE_BINARY_BUFFER_INITIAL_SIZE; 
    124                 p7_message->binary_buffer              = wi_malloc(p7_message->binary_buffer_size); 
    125                 p7_message->binary_length              = _WI_P7_MESSAGE_BINARY_HEADER_SIZE; 
    126         } else { 
    127                 p7_message->xml_doc                            = xmlNewDoc((xmlChar *) "1.0"); 
    128                 p7_message->xml_root_node              = xmlNewNode(NULL, (xmlChar *) "message"); 
     123                p7_message->binary_size               = _WI_P7_MESSAGE_BINARY_BUFFER_INITIAL_SIZE; 
     124                p7_message->binary_buffer       = wi_malloc(p7_message->binary_size); 
     125                p7_message->binary_length       = _WI_P7_MESSAGE_BINARY_HEADER_SIZE; 
     126        } else { 
     127                p7_message->xml_doc                     = xmlNewDoc((xmlChar *) "1.0"); 
     128                p7_message->xml_root_node       = xmlNewNode(NULL, (xmlChar *) "message"); 
    129129 
    130130                xmlDocSetRootElement(p7_message->xml_doc, p7_message->xml_root_node); 
     
    173173        p7_message, 
    174174                p7_message->name, 
    175                 p7_message->serialization == WI_P7_BINARY ? WI_STR("binary") : WI_STR("XML")); 
     175                p7_message->serialization == WI_P7_BINARY ? WI_STR("binary") : WI_STR("xml")); 
    176176         
    177177        if(p7_message->serialization == WI_P7_BINARY) { 
     
    550550                new_length += sizeof(length); 
    551551 
    552         if(p7_message->binary_length + new_length > p7_message->binary_buffer_size) { 
    553                 p7_message->binary_buffer_size        *= 2; 
    554                 p7_message->binary_buffer              = wi_realloc(p7_message->binary_buffer, p7_message->binary_buffer_size); 
     552        if(p7_message->binary_length + new_length > p7_message->binary_size) { 
     553                p7_message->binary_size               *= 2; 
     554                p7_message->binary_buffer       = wi_realloc(p7_message->binary_buffer, p7_message->binary_size); 
    555555        } 
    556556 
  • libwired/trunk/libwired/p7/wi-p7-private.h

    r4629 r4639  
    4646 
    4747        unsigned char                                   *binary_buffer; 
    48         uint32_t                                                binary_buffer_size; 
     48        uint32_t                                                binary_size; 
    4949        uint32_t                                                binary_length; 
    5050        uint32_t                                                binary_id; 
  • libwired/trunk/libwired/p7/wi-p7-socket.c

    r4629 r4639  
    255255        wi_p7_message_set_uuid_for_name(p7_message, wi_p7_spec_id(p7_socket->spec), WI_STR("p7.handshake.protocol")); 
    256256         
    257         if(options & WI_P7_COMPRESSION_DEFLATE) 
    258            wi_p7_message_set_enum_for_name(p7_message, 0, WI_STR("p7.handshake.compression")); 
    259          
    260         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"))); 
    261          
    262         if(options & WI_P7_ENCRYPTION_RSA_AES_256) 
    263                 wi_p7_message_set_enum_for_name(p7_message, 0, WI_STR("p7.handshake.encryption")); 
    264         else if(options & WI_P7_ENCRYPTION_RSA_BF_128) 
    265                wi_p7_message_set_enum_for_name(p7_message, 1, WI_STR("p7.handshake.encryption")); 
     257        if(p7_socket->serialization == WI_P7_BINARY) { 
     258               if(options & WI_P7_COMPRESSION_DEFLATE) 
     259                  wi_p7_message_set_enum_for_name(p7_message, 0, WI_STR("p7.handshake.compression")); 
     260                
     261               if(options & WI_P7_ENCRYPTION_RSA_AES_256) 
     262                       wi_p7_message_set_enum_for_name(p7_message, 0, WI_STR("p7.handshake.encryption")); 
     263                else if(options & WI_P7_ENCRYPTION_RSA_BF_128) 
     264                       wi_p7_message_set_enum_for_name(p7_message, 1, WI_STR("p7.handshake.encryption")); 
     265        } 
    266266         
    267267        if(!wi_p7_socket_write_message(p7_socket, timeout, p7_message)) { 
     
    374374        p7_socket->local_compatibility_check = !wi_p7_spec_is_compatible_with_id(p7_socket->spec, protocol); 
    375375 
    376         if(wi_p7_message_get_enum_for_name(p7_message, &compression, WI_STR("p7.handshake.compression"))) { 
    377                 if(compression == 0 && options & WI_P7_COMPRESSION_DEFLATE) 
    378                         p7_socket->options |= WI_P7_COMPRESSION_DEFLATE; 
    379         } 
    380          
    381         if(wi_p7_message_get_enum_for_name(p7_message, &encryption, WI_STR("p7.handshake.encryption"))) { 
    382                 wi_log_info(WI_STR("should get %@ for %u"), wi_p7_spec_enum_name(p7_socket->spec, 4, encryption), encryption); 
    383  
    384                 if(encryption == 0 && options & WI_P7_ENCRYPTION_RSA_AES_256) 
    385                         p7_socket->options |= WI_P7_ENCRYPTION_RSA_AES_256; 
    386                 else if(encryption == 1 && options & WI_P7_ENCRYPTION_RSA_BF_128) 
    387                        p7_socket->options |= WI_P7_ENCRYPTION_RSA_BF_128; 
     376        if(p7_socket->serialization == WI_P7_BINARY) { 
     377                if(wi_p7_message_get_enum_for_name(p7_message, &compression, WI_STR("p7.handshake.compression"))) { 
     378                        if(compression == 0 && options & WI_P7_COMPRESSION_DEFLATE) 
     379                               p7_socket->options |= WI_P7_COMPRESSION_DEFLATE; 
     380               } 
     381                
     382                if(wi_p7_message_get_enum_for_name(p7_message, &encryption, WI_STR("p7.handshake.encryption"))) { 
     383                        if(encryption == 0 && options & WI_P7_ENCRYPTION_RSA_AES_256) 
     384                               p7_socket->options |= WI_P7_ENCRYPTION_RSA_AES_256; 
     385                        else if(encryption == 1 && options & WI_P7_ENCRYPTION_RSA_BF_128) 
     386                               p7_socket->options |= WI_P7_ENCRYPTION_RSA_BF_128; 
     387                } 
    388388        } 
    389389         
     
    440440static 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) { 
    441441        wi_p7_message_t         *p7_message; 
    442         wi_data_t                       *data, *rsa_data
     442        wi_data_t                       *data, *rsa
    443443        wi_string_t                     *client_password1, *client_password2, *server_password; 
    444444 
     
    457457        } 
    458458         
    459         rsa_data = wi_p7_message_data_for_name(p7_message, WI_STR("p7.encryption.envelope_key")); 
    460          
    461         if(!rsa_data) { 
     459        rsa = wi_p7_message_data_for_name(p7_message, WI_STR("p7.encryption.envelope_key")); 
     460         
     461        if(!rsa) { 
    462462                wi_log_warn(WI_STR("no certificate")); 
    463463                 
     
    465465        } 
    466466         
    467         p7_socket->rsa = wi_p7_rsa_init_with_public_key_data(wi_p7_rsa_alloc(), rsa_data); 
     467        p7_socket->rsa = wi_p7_rsa_init_with_public_key(wi_p7_rsa_alloc(), rsa); 
    468468         
    469469        if(!p7_socket->rsa) { 
     
    473473        } 
    474474         
    475         p7_socket->cipher = wi_p7_cipher_init_with_public_rsa(wi_p7_cipher_alloc(), 
    476                                                                                                                   _wi_p7_socket_cipher(p7_socket), 
    477                                                                                                                   p7_socket->rsa); 
     475        p7_socket->cipher = wi_p7_cipher_init_with_random_key(wi_p7_cipher_alloc(), _wi_p7_socket_cipher(p7_socket)); 
    478476         
    479477        if(!p7_socket->cipher) { 
     
    491489        } 
    492490         
    493         wi_p7_message_set_data_for_name(p7_message, wi_p7_cipher_key_data(p7_socket->cipher), WI_STR("p7.encryption.cipher_key")); 
    494         wi_p7_message_set_data_for_name(p7_message, wi_p7_cipher_iv_data(p7_socket->cipher), WI_STR("p7.encryption.cipher_key_iv"));