Changeset 5385

Show
Ignore:
Timestamp:
03/14/08 11:10:54 (4 months ago)
Author:
morris
Message:

Add wi_x509_t, simple class to load X509 certificates

Files:

Legend:

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

    r5308 r5385  
    6565 
    6666 
     67struct _wi_x509 { 
     68        wi_runtime_base_t                                       base; 
     69         
     70        X509                                                            *x509; 
     71}; 
     72 
     73static void                                                             _wi_x509_dealloc(wi_runtime_instance_t *); 
     74static wi_string_t *                                    _wi_x509_description(wi_runtime_instance_t *); 
     75 
     76static wi_runtime_id_t                                  _wi_x509_runtime_id = WI_RUNTIME_ID_NULL; 
     77static wi_runtime_class_t                               _wi_x509_runtime_class = { 
     78        "wi_x509_t", 
     79        _wi_x509_dealloc, 
     80        NULL, 
     81        NULL, 
     82        _wi_x509_description, 
     83        NULL 
     84}; 
     85 
     86 
     87 
    6788struct _wi_cipher { 
    6889        wi_runtime_base_t                                       base; 
     
    96117void wi_crypto_register(void) { 
    97118        _wi_rsa_runtime_id = wi_runtime_register_class(&_wi_rsa_runtime_class); 
     119        _wi_x509_runtime_id = wi_runtime_register_class(&_wi_x509_runtime_class); 
    98120        _wi_cipher_runtime_id = wi_runtime_register_class(&_wi_cipher_runtime_class); 
    99121} 
     
    239261#pragma mark - 
    240262 
     263void * wi_rsa_rsa(wi_rsa_t *rsa) { 
     264        return rsa->rsa; 
     265} 
     266 
     267 
     268 
    241269wi_data_t * wi_rsa_public_key(wi_rsa_t *rsa) { 
    242270        unsigned char   *buffer; 
     
    369397 
    370398        return true; 
     399} 
     400 
     401 
     402 
     403#pragma mark - 
     404 
     405wi_runtime_id_t wi_x509_runtime_id(void) { 
     406        return _wi_x509_runtime_id; 
     407} 
     408 
     409 
     410 
     411#pragma mark - 
     412 
     413wi_x509_t * wi_x509_alloc(void) { 
     414        return wi_runtime_create_instance(_wi_x509_runtime_id, sizeof(wi_x509_t)); 
     415} 
     416 
     417 
     418 
     419wi_x509_t * wi_x509_init_with_pem_file(wi_x509_t *x509, wi_string_t *path) { 
     420        FILE            *fp; 
     421         
     422        fp = fopen(wi_string_cstring(path), "r"); 
     423         
     424        if(!fp) { 
     425                wi_error_set_errno(errno); 
     426                 
     427                wi_release(x509); 
     428                 
     429                return NULL; 
     430        } 
     431         
     432        x509->x509 = PEM_read_X509(fp, NULL, NULL, NULL); 
     433         
     434        fclose(fp); 
     435         
     436        if(!x509->x509) { 
     437                wi_error_set_openssl_error(); 
     438                 
     439                wi_release(x509); 
     440                 
     441                return NULL; 
     442        } 
     443         
     444        return x509; 
     445} 
     446 
     447 
     448 
     449static void _wi_x509_dealloc(wi_runtime_instance_t *instance) { 
     450        wi_x509_t               *x509 = instance; 
     451         
     452        X509_free(x509->x509); 
     453} 
     454 
     455 
     456 
     457static wi_string_t * _wi_x509_description(wi_runtime_instance_t *instance) { 
     458        wi_x509_t               *x509 = instance; 
     459         
     460        return wi_string_with_format(WI_STR("<%@ %p>{x509 = %p}"), 
     461        wi_runtime_class_name(x509), 
     462                x509, 
     463                x509->x509); 
     464} 
     465 
     466 
     467 
     468#pragma mark - 
     469 
     470void * wi_x509_x509(wi_x509_t *x509) { 
     471        return x509->x509; 
    371472} 
    372473 
  • libwired/trunk/libwired/misc/wi-crypto.h

    r5308 r5385  
    4545 
    4646typedef struct _wi_rsa                                  wi_rsa_t; 
     47typedef struct _wi_x509                                 wi_x509_t; 
    4748typedef struct _wi_cipher                               wi_cipher_t; 
    4849 
     
    5657WI_EXPORT wi_rsa_t *                                    wi_rsa_init_with_public_key(wi_rsa_t *, wi_data_t *); 
    5758 
     59WI_EXPORT void *                                                wi_rsa_rsa(wi_rsa_t *); 
    5860WI_EXPORT wi_data_t *                                   wi_rsa_public_key(wi_rsa_t *); 
    5961WI_EXPORT wi_data_t *                                   wi_rsa_private_key(wi_rsa_t *); 
     
    6466WI_EXPORT wi_data_t *                                   wi_rsa_decrypt(wi_rsa_t *, wi_data_t *); 
    6567WI_EXPORT wi_boolean_t                                  wi_rsa_decrypt_bytes(wi_rsa_t *, const void *, wi_uinteger_t, void **, wi_uinteger_t *); 
     68 
     69 
     70WI_EXPORT wi_runtime_id_t                               wi_x509_runtime_id(void); 
     71 
     72WI_EXPORT wi_x509_t *                                   wi_x509_alloc(void); 
     73WI_EXPORT wi_x509_t *                                   wi_x509_init_with_pem_file(wi_x509_t *, wi_string_t *); 
     74 
     75WI_EXPORT void *                                                wi_x509_x509(wi_x509_t *); 
    6676 
    6777