236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
|
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
|
-
+
-
+
+
+
+
+
-
-
+
+
+
-
-
+
-
+
+
+
+
-
-
+
+
-
-
+
|
blob_reset(&ans);
}
X509_free(cert);
return 0;
}
/*
** Save certificate to global config.
** Save certificate to global certificate/key store.
*/
void ssl_save_certificate(X509 *cert){
BIO *mem;
char *zCert, *zHost;
char *zCert;
mem = BIO_new(BIO_s_mem());
PEM_write_bio_X509(mem, cert);
BIO_write(mem, "", 1); // null-terminate mem buffer
BIO_get_mem_data(mem, &zCert);
db_swap_connections();
create_cert_table_if_not_exist();
db_begin_transaction();
db_multi_exec("REPLACE INTO certs(name,type,filepath) "
zHost = mprintf("cert:%s", g.urlName);
db_set(zHost, zCert, 1);
"VALUES(%Q,'scert',%Q)", g.urlName, zCert);
db_end_transaction(0);
db_swap_connections();
free(zHost);
BIO_free(mem);
}
/*
** Get certificate for g.urlName from global config.
** Get certificate for g.urlName from global certificate/key store.
** Return NULL if no certificate found.
*/
X509 *ssl_get_certificate(void){
char *zHost, *zCert;
char *zCert;
BIO *mem;
X509 *cert;
db_swap_connections();
create_cert_table_if_not_exist();
zCert = db_text(0, "SELECT filepath FROM certs WHERE name=%Q"
zHost = mprintf("cert:%s", g.urlName);
zCert = db_get(zHost, NULL);
" AND type='scert'", g.urlName);
db_swap_connections();
free(zHost);
if ( zCert==NULL )
if( zCert==NULL )
return NULL;
mem = BIO_new(BIO_s_mem());
BIO_puts(mem, zCert);
cert = PEM_read_bio_X509(mem, NULL, 0, NULL);
free(zCert);
BIO_free(mem);
return cert;
|
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
|
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
|
-
+
|
zName = mprintf("certbundle:%s", g.urlName);
db_set(zName, g.urlCertBundle, 1);
free(zName);
zBundleName = strdup(g.urlCertBundle);
}else{
db_swap_connections();
zBundleName = db_text(0, "SELECT value FROM global_config"
" WHERE name='certbundle:%q'", g.urlName);
" WHERE name='certbundle:%q'", g.urlName);
db_swap_connections();
}
if( !zBundleName ){
/* No cert bundle specified on command line or found cached for URL */
return;
}
|
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
|
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
|
+
+
+
+
+
+
+
-
-
-
-
-
-
+
+
+
+
+
+
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
|
db_swap_connections();
keyfile = db_text(0, "SELECT filepath FROM certs WHERE name=%Q"
" AND type='ckey'", zBundleName);
certfile = db_text(0, "SELECT filepath FROM certs WHERE name=%Q"
" AND type='ccert'", zBundleName);
db_swap_connections();
if( certfile ){
/* If a client certificate is explicitly specified, but a key is not, then
** assume the key is in the same file as the certificate.
*/
if( !keyfile ){
keyfile = certfile;
}
if( SSL_CTX_use_certificate_file(sslCtx, certfile, SSL_FILETYPE_PEM)<=0 ){
fossil_fatal("SSL: Unable to open client certificate in %s.", certfile);
}
if( SSL_CTX_use_PrivateKey_file(sslCtx, keyfile, SSL_FILETYPE_PEM)<=0 ){
fossil_fatal("SSL: Unable to open client key in %s.", keyfile);
}
if( SSL_CTX_use_certificate_file(sslCtx, certfile, SSL_FILETYPE_PEM)<=0 ){
fossil_fatal("SSL: Unable to open client certificate in %s.", certfile);
}
if( SSL_CTX_use_PrivateKey_file(sslCtx, keyfile, SSL_FILETYPE_PEM)<=0 ){
fossil_fatal("SSL: Unable to open client key in %s.", keyfile);
}
if( !SSL_CTX_check_private_key(sslCtx) ){
fossil_fatal("SSL: Private key does not match the certificate public "
"key.");
}
free(keyfile);
if( certfile && keyfile && !SSL_CTX_check_private_key(sslCtx) ){
fossil_fatal("SSL: Private key does not match the certificate public "
"key.");
}
}
if( keyfile != certfile ){
free(keyfile);
}
free(certfile);
free(capath);
free(cafile);
}
#endif /* FOSSIL_ENABLE_SSL */
|