• source navigation  • diff markup  • identifier search  • freetext search  • 

Sources/uhttpd/tls.c

  1 /*
  2  * uhttpd - Tiny single-threaded httpd
  3  *
  4  *   Copyright (C) 2010-2013 Jo-Philipp Wich <xm@subsignal.org>
  5  *   Copyright (C) 2013 Felix Fietkau <nbd@openwrt.org>
  6  *
  7  * Permission to use, copy, modify, and/or distribute this software for any
  8  * purpose with or without fee is hereby granted, provided that the above
  9  * copyright notice and this permission notice appear in all copies.
 10  *
 11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 18  */
 19 
 20 #include <dlfcn.h>
 21 #include "uhttpd.h"
 22 #include "tls.h"
 23 
 24 #ifdef __APPLE__
 25 #define LIB_EXT "dylib"
 26 #else
 27 #define LIB_EXT "so"
 28 #endif
 29 
 30 static struct ustream_ssl_ops *ops;
 31 static void *dlh;
 32 static void *ctx;
 33 
 34 int uh_tls_init(const char *key, const char *crt, const char *ciphers)
 35 {
 36         static bool _init = false;
 37 
 38         if (_init)
 39                 return 0;
 40 
 41         _init = true;
 42         dlh = dlopen("libustream-ssl." LIB_EXT, RTLD_LAZY | RTLD_LOCAL);
 43         if (!dlh) {
 44                 fprintf(stderr, "Failed to load ustream-ssl library: %s\n", dlerror());
 45                 return -ENOENT;
 46         }
 47 
 48         ops = dlsym(dlh, "ustream_ssl_ops");
 49         if (!ops) {
 50                 fprintf(stderr, "Could not find required symbol 'ustream_ssl_ops' in ustream-ssl library\n");
 51                 return -ENOENT;
 52         }
 53 
 54         ctx = ops->context_new(true);
 55         if (!ctx) {
 56                 fprintf(stderr, "Failed to initialize ustream-ssl\n");
 57                 return -EINVAL;
 58         }
 59 
 60         if (ops->context_set_crt_file(ctx, crt) ||
 61             ops->context_set_key_file(ctx, key)) {
 62                 fprintf(stderr, "Failed to load certificate/key files\n");
 63                 return -EINVAL;
 64         }
 65 
 66         if (ciphers && ops->context_set_ciphers(ctx, ciphers)) {
 67                 fprintf(stderr, "No recognized ciphers in cipher list\n");
 68                 return -EINVAL;
 69         }
 70 
 71         return 0;
 72 }
 73 
 74 static void tls_ustream_read_cb(struct ustream *s, int bytes)
 75 {
 76         struct client *cl = container_of(s, struct client, ssl.stream);
 77 
 78         uh_client_read_cb(cl);
 79 }
 80 
 81 static void tls_ustream_write_cb(struct ustream *s, int bytes)
 82 {
 83         struct client *cl = container_of(s, struct client, ssl.stream);
 84 
 85         if (cl->dispatch.write_cb)
 86                 cl->dispatch.write_cb(cl);
 87 }
 88 
 89 static void tls_notify_state(struct ustream *s)
 90 {
 91         struct client *cl = container_of(s, struct client, ssl.stream);
 92 
 93         uh_client_notify_state(cl);
 94 }
 95 
 96 void uh_tls_client_attach(struct client *cl)
 97 {
 98         cl->us = &cl->ssl.stream;
 99         ops->init(&cl->ssl, &cl->sfd.stream, ctx, true);
100         cl->us->notify_read = tls_ustream_read_cb;
101         cl->us->notify_write = tls_ustream_write_cb;
102         cl->us->notify_state = tls_notify_state;
103 }
104 
105 void uh_tls_client_detach(struct client *cl)
106 {
107         ustream_free(&cl->ssl.stream);
108 }
109 

This page was automatically generated by LXR 0.3.1.  •  OpenWrt