Brian Waters | 13d9601 | 2017-12-08 16:53:31 -0600 | [diff] [blame] | 1 | /***************************************************************************************************** |
| 2 | * Software License Agreement (BSD License) |
| 3 | * Author : Souheil Ben Ayed <souheil@tera.ics.keio.ac.jp> |
| 4 | * |
| 5 | * Copyright (c) 2009-2010, Souheil Ben Ayed, Teraoka Laboratory of Keio University, and the WIDE Project |
| 6 | * All rights reserved. |
| 7 | * |
| 8 | * Redistribution and use of this software in source and binary forms, with or without |
| 9 | * modification, are permitted provided that the following conditions are met: |
| 10 | * |
| 11 | * 1. Redistributions of source code must retain the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer. |
| 13 | * |
| 14 | * 2. Redistributions in binary form must reproduce the above copyright |
| 15 | * notice, this list of conditions and the following disclaimer in the |
| 16 | * documentation and/or other materials provided with the distribution. |
| 17 | * |
| 18 | * 3. All advertising materials mentioning features or use of this software |
| 19 | * must display the following acknowledgement: |
| 20 | * This product includes software developed by Souheil Ben Ayed <souheil@tera.ics.keio.ac.jp>. |
| 21 | * |
| 22 | * 4. Neither the name of Souheil Ben Ayed, Teraoka Laboratory of Keio University or the WIDE Project nor the |
| 23 | * names of its contributors may be used to endorse or promote products |
| 24 | * derived from this software without specific prior written permission. |
| 25 | * |
| 26 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ''AS IS'' AND ANY |
| 27 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 28 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 29 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY |
| 30 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 31 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 32 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 33 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 34 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 35 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 36 | *****************************************************************************************************/ |
| 37 | |
| 38 | |
| 39 | #ifndef DIAMEAP_TLS_H_ |
| 40 | #define DIAMEAP_TLS_H_ |
| 41 | |
| 42 | #if defined(__GNUC__) |
| 43 | # define GCC_DIAG_DO_PRAGMA(x) _Pragma (#x) |
| 44 | # define GCC_DIAG_PRAGMA(x) GCC_DIAG_DO_PRAGMA(GCC diagnostic x) |
| 45 | # if ((__GNUC__ * 100) + __GNUC_MINOR__) >= 406 /* 4.6.x */ |
| 46 | # define GCC_DIAG_OFF(x) GCC_DIAG_PRAGMA(push) \ |
| 47 | GCC_DIAG_PRAGMA(ignored x) |
| 48 | # define GCC_DIAG_ON(x) GCC_DIAG_PRAGMA(pop) |
| 49 | # else /* older */ |
| 50 | # define GCC_DIAG_OFF(x) GCC_DIAG_PRAGMA(ignored x) |
| 51 | # define GCC_DIAG_ON(x) GCC_DIAG_PRAGMA(warning x) |
| 52 | # endif |
| 53 | #else |
| 54 | # define GCC_DIAG_OFF(x) |
| 55 | # define GCC_DIAG_ON(x) |
| 56 | #endif |
| 57 | |
| 58 | |
| 59 | #include "diameap_defs.h" |
| 60 | #include <gnutls/gnutls.h> |
| 61 | GCC_DIAG_OFF("-Wdeprecated-declarations") |
| 62 | #include <gcrypt.h> |
| 63 | GCC_DIAG_ON("-Wdeprecated-declarations") |
| 64 | #include <errno.h> |
| 65 | #include <pthread.h> |
| 66 | |
| 67 | |
| 68 | |
| 69 | #define TLS_FLAG_LENGTH 0x80 |
| 70 | #define TLS_FLAG_MORE 0x40 |
| 71 | #define TLS_FLAG_START 0x20 |
| 72 | |
| 73 | struct tls_config{ |
| 74 | char * conffile; |
| 75 | //certificates |
| 76 | char * keyfile; |
| 77 | char * certfile; |
| 78 | char * cafile; |
| 79 | char * crlfile; |
| 80 | |
| 81 | //configuration parameters |
| 82 | boolean check_cert_cn_username; |
| 83 | |
| 84 | int max_size; |
| 85 | |
| 86 | gnutls_certificate_credentials_t cert_cred; |
| 87 | }; |
| 88 | |
| 89 | struct tls_msg{ |
| 90 | u8 flags; |
| 91 | u32 length; |
| 92 | u8 *data; |
| 93 | u32 datalength; |
| 94 | }; |
| 95 | |
| 96 | struct tls_data |
| 97 | { |
| 98 | gnutls_session_t session; |
| 99 | enum { START, CONTINUE, SUCCESS, FAILURE } state; |
| 100 | struct tls_msg tlsReq; |
| 101 | int p_length; /* length of buffer still not returned to handshake */ |
| 102 | struct tls_msg tlsResp; |
| 103 | int more_tosend_length; /* 0 if no more fragment to send. Otherwise the length of the buff not yet sent */ |
| 104 | boolean more_toreceive; /* TRUE if wait for more fragments. */ |
| 105 | boolean handshake; /* TRUE if handshake terminated successful. */ |
| 106 | }; |
| 107 | |
| 108 | int diameap_tls_init(struct tls_config * tls_conf ); |
| 109 | void diameap_tls_log(int lev, const char * text); |
| 110 | int diameap_tls_init_session(struct tls_config * tls_conf, struct tls_data * data); |
| 111 | int diameap_tls_new(struct tls_msg * tlsmsg); |
| 112 | |
| 113 | int diameap_tls_get_message_length(struct tls_msg tlsmsg, u32 * length); |
| 114 | int diameap_tls_set_message_length(struct tls_msg * tlsmsg, u32 length); |
| 115 | |
| 116 | int diameap_tls_get_flags(struct tls_msg tlsmsg, u8 * flags); |
| 117 | int diameap_tls_set_flags(struct tls_msg * tlsmsg, u8 flags); |
| 118 | |
| 119 | int diameap_tls_get_data(struct tls_msg tlsmsg, u8** tls_data, u32 * data_length); |
| 120 | int diameap_tls_set_data(struct tls_msg * tlsmsg, u8* tls_data, int data_length); |
| 121 | |
| 122 | int diameap_tls_parse(u8* data, int length, struct tls_msg * tlsmsg); |
| 123 | |
| 124 | int diameap_tls_new_tls_packet(u8** data, int * len, struct tls_msg tlsmsg); |
| 125 | |
| 126 | int diameap_set_tls(struct tls_msg * tlsmsg, u8 flags, u32 length,u8 *tls_data, int data_length); |
| 127 | |
| 128 | void diameap_tls_dump(struct tls_msg tlsmsg); |
| 129 | |
| 130 | int diameap_tls_initialize(struct tls_data * data); |
| 131 | |
| 132 | int diameap_tls_reassemble(struct tls_msg * to,struct tls_msg from); |
| 133 | |
| 134 | int diameap_tls_copy(struct tls_msg * to,struct tls_msg from); |
| 135 | |
| 136 | int diameap_tls_process_receive(struct tls_data * data); |
| 137 | |
| 138 | ssize_t diameap_tls_receive(gnutls_transport_ptr_t data , void *buffer, size_t length); |
| 139 | ssize_t diameap_tls_send(gnutls_transport_ptr_t data , const void *buffer, size_t length); |
| 140 | |
| 141 | #endif /* DIAMEAP_TLS_H_ */ |