Changeset 5400

Show
Ignore:
Timestamp:
03/14/08 18:38:31 (6 months ago)
Author:
morris
Message:

Add wi_x509_init_with_common_name()

Files:

Legend:

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

    r5393 r5400  
    425425 
    426426 
     427 
     428wi_x509_t * wi_x509_init_with_common_name(wi_x509_t *x509, wi_rsa_t *rsa, wi_string_t *common_name) { 
     429        X509_REQ                *req; 
     430        EVP_PKEY                *pkey = NULL; 
     431        X509_NAME               *name = NULL; 
     432        BIGNUM                  *bn = NULL; 
     433         
     434        req = X509_REQ_new(); 
     435         
     436        if(!req) 
     437                goto err; 
     438 
     439        if(X509_REQ_set_version(req, 0) != 1) 
     440                goto err; 
     441         
     442        name = X509_NAME_new(); 
     443         
     444        if(X509_NAME_add_entry_by_NID(name, NID_commonName, MBSTRING_ASC, wi_string_cstring(common_name), -1, -1, 0) != 1) 
     445                goto err; 
     446 
     447        if(X509_REQ_set_subject_name(req, name) != 1) 
     448                goto err; 
     449 
     450        pkey = EVP_PKEY_new(); 
     451        EVP_PKEY_set1_RSA(pkey, rsa->rsa); 
     452         
     453        if(X509_REQ_set_pubkey(req, pkey) != 1) 
     454                goto err; 
     455         
     456        x509->x509 = X509_new(); 
     457         
     458        if(!x509->x509) 
     459                goto err; 
     460         
     461        bn = BN_new(); 
     462         
     463        if(!bn) 
     464                goto err; 
     465         
     466        if(BN_pseudo_rand(bn, 64, 0, 0) != 1) 
     467                goto err; 
     468         
     469        if(!BN_to_ASN1_INTEGER(bn, X509_get_serialNumber(x509->x509))) 
     470                goto err; 
     471         
     472        if(X509_set_issuer_name(x509->x509, X509_REQ_get_subject_name(req)) != 1) 
     473                goto err; 
     474 
     475        if(!X509_gmtime_adj(X509_get_notBefore(x509->x509), 0)) 
     476                goto err; 
     477 
     478        if(!X509_gmtime_adj(X509_get_notAfter(x509->x509), 3600 * 24 * 365)) 
     479                goto err; 
     480 
     481        if(X509_set_subject_name(x509->x509, X509_REQ_get_subject_name(req)) != 1) 
     482                goto end; 
     483 
     484        if(X509_set_pubkey(x509->x509, pkey) != 1) 
     485                goto err; 
     486         
     487        if(X509_sign(x509->x509, pkey, EVP_sha1()) == 0) 
     488                goto err; 
     489 
     490        goto end; 
     491         
     492err: 
     493        wi_error_set_openssl_error(); 
     494 
     495        wi_release(x509); 
     496 
     497        x509 = NULL; 
     498         
     499end: 
     500        if(req) 
     501                X509_REQ_free(req); 
     502         
     503        if(pkey) 
     504                EVP_PKEY_free(pkey); 
     505         
     506        if(name) 
     507                X509_NAME_free(name); 
     508 
     509        if(bn) 
     510                BN_free(bn); 
     511         
     512        return x509; 
     513} 
     514 
     515 
     516 
    427517wi_x509_t * wi_x509_init_with_pem_file(wi_x509_t *x509, wi_string_t *path) { 
    428518        FILE            *fp; 
  • libwired/trunk/libwired/misc/wi-crypto.h

    r5393 r5400  
    7272 
    7373WI_EXPORT wi_x509_t *                                   wi_x509_alloc(void); 
     74WI_EXPORT wi_x509_t *                                   wi_x509_init_with_common_name(wi_x509_t *, wi_rsa_t *, wi_string_t *); 
    7475WI_EXPORT wi_x509_t *                                   wi_x509_init_with_pem_file(wi_x509_t *, wi_string_t *); 
    7576