Changeset 5589

Show
Ignore:
Timestamp:
06/09/08 14:53:52 (6 months ago)
Author:
morris
Message:

Auto-create certificate if not set

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • wired/trunk/wired/server.c

    r5540 r5589  
    175175void wd_server_apply_settings(void) { 
    176176        wi_data_t               *data; 
    177         wi_rsa_t                *private_key; 
    178         wi_x509_t               *certificate; 
     177        wi_rsa_t                *private_key = NULL; 
     178        wi_x509_t               *certificate = NULL; 
     179        wi_string_t             *hostname; 
    179180 
    180181        /* reload banner */ 
     
    218219        /* load SSL certificate */ 
    219220        if(wd_settings.certificate) { 
     221                private_key = wi_rsa_init_with_pem_file(wi_rsa_alloc(), wd_settings.certificate); 
     222                 
     223                if(!private_key) 
     224                        wi_log_warn(WI_STR("Could not find RSA key in %@, creating one..."), wd_settings.certificate); 
     225                 
    220226                certificate = wi_x509_init_with_pem_file(wi_x509_alloc(), wd_settings.certificate); 
    221227                 
    222                 if(certificate) { 
    223                         if(!wi_socket_tls_set_certificate(wd_control_socket_tls, certificate) || 
    224                            !wi_socket_tls_set_certificate(wd_transfer_socket_tls, certificate)) { 
    225                                 wi_log_err(WI_STR("Could not set TLS certificate: %m")); 
    226                         } 
    227                          
    228                         wi_release(certificate); 
    229                 } else { 
    230                         wi_log_err(WI_STR("Could not load certificate %@: %m"), 
    231                                 wd_settings.certificate); 
    232                 } 
    233  
    234                 private_key = wi_rsa_init_with_pem_file(wi_rsa_alloc(), wd_settings.certificate); 
    235                  
    236                 if(private_key) { 
    237                         if(!wi_socket_tls_set_private_key(wd_control_socket_tls, private_key) || 
    238                            !wi_socket_tls_set_private_key(wd_transfer_socket_tls, private_key)) { 
    239                                 wi_log_err(WI_STR("Could not set TLS private key: %m")); 
    240                         } 
    241                          
    242                         wi_release(private_key); 
    243                 } else { 
    244                         wi_log_err(WI_STR("Could not load private key %@: %m"), 
    245                                 wd_settings.certificate); 
    246                 } 
    247         } 
     228                if(!certificate) 
     229                        wi_log_warn(WI_STR("Could not find certificate in %@, creating one..."), wd_settings.certificate); 
     230        } 
     231         
     232        if(!private_key) { 
     233                private_key = wi_rsa_init_with_bits(wi_rsa_alloc(), 1024); 
     234                 
     235                if(private_key) 
     236                        wi_log_info(WI_STR("Created 1024-bit RSA key")); 
     237                else 
     238                        wi_log_err(WI_STR("Could not create RSA key: %m")); 
     239        } 
     240         
     241        if(!certificate) { 
     242                hostname = wi_process_hostname(wi_process()); 
     243                certificate = wi_x509_init_with_common_name(wi_x509_alloc(), private_key, hostname); 
     244                 
     245                if(certificate) 
     246                        wi_log_info(WI_STR("Created self-signed certificate for %@"), hostname); 
     247                else 
     248                        wi_log_err(WI_STR("Could not create self-signed certificate: %m")); 
     249        } 
     250         
     251        if(!wi_socket_tls_set_private_key(wd_control_socket_tls, private_key) || 
     252           !wi_socket_tls_set_private_key(wd_transfer_socket_tls, private_key)) { 
     253                wi_log_err(WI_STR("Could not set TLS private key: %m")); 
     254        } 
     255         
     256        if(!wi_socket_tls_set_certificate(wd_control_socket_tls, certificate) || 
     257           !wi_socket_tls_set_certificate(wd_transfer_socket_tls, certificate)) { 
     258                wi_log_err(WI_STR("Could not set TLS certificate: %m")); 
     259        } 
     260         
     261        wi_release(private_key); 
     262        wi_release(certificate); 
    248263} 
    249264