BadVPN – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 /**
2 * @file SPProtoEncoder.h
3 * @author Ambroz Bizjak <ambrop7@gmail.com>
4 *
5 * @section LICENSE
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. Neither the name of the author nor the
15 * names of its contributors may be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
22 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 *
29 * @section DESCRIPTION
30 *
31 * Object which encodes packets according to SPProto.
32 */
33  
34 #ifndef BADVPN_CLIENT_SPPROTOENCODER_H
35 #define BADVPN_CLIENT_SPPROTOENCODER_H
36  
37 #include <stdint.h>
38  
39 #include <misc/debug.h>
40 #include <protocol/spproto.h>
41 #include <base/DebugObject.h>
42 #include <security/BEncryption.h>
43 #include <security/OTPGenerator.h>
44 #include <flow/PacketRecvInterface.h>
45 #include <threadwork/BThreadWork.h>
46  
47 /**
48 * Event context handler called when the remaining number of
49 * OTPs equals the warning number after having encoded a packet.
50 *
51 * @param user as in {@link SPProtoEncoder_Init}
52 */
53 typedef void (*SPProtoEncoder_handler) (void *user);
54  
55 /**
56 * Object which encodes packets according to SPProto.
57 *
58 * Input is with {@link PacketRecvInterface}.
59 * Output is with {@link PacketRecvInterface}.
60 */
61 typedef struct {
62 PacketRecvInterface *input;
63 struct spproto_security_params sp_params;
64 int otp_warning_count;
65 SPProtoEncoder_handler handler;
66 BThreadWorkDispatcher *twd;
67 void *user;
68 int hash_size;
69 int enc_block_size;
70 int enc_key_size;
71 OTPGenerator otpgen;
72 uint16_t otpgen_seed_id;
73 uint16_t otpgen_pending_seed_id;
74 int have_encryption_key;
75 BEncryption encryptor;
76 int input_mtu;
77 int output_mtu;
78 int in_len;
79 PacketRecvInterface output;
80 int out_have;
81 uint8_t *out;
82 uint8_t *buf;
83 BPending handler_job;
84 int tw_have;
85 BThreadWork tw;
86 uint16_t tw_seed_id;
87 otp_t tw_otp;
88 int tw_out_len;
89 DebugObject d_obj;
90 } SPProtoEncoder;
91  
92 /**
93 * Initializes the object.
94 * The object is initialized in blocked state.
95 * {@link BSecurity_GlobalInitThreadSafe} must have been done if
96 * {@link BThreadWorkDispatcher_UsingThreads}(twd) = 1.
97 *
98 * @param o the object
99 * @param input input interface. Its MTU must not be too large, i.e. this must hold:
100 * spproto_carrier_mtu_for_payload_mtu(sp_params, input MTU) >= 0
101 * @param sp_params SPProto security parameters
102 * @param otp_warning_count If using OTPs, after how many encoded packets to call the handler.
103 * In this case, must be >0 and <=sp_params.otp_num.
104 * @param pg pending group
105 * @param twd thread work dispatcher
106 * @return 1 on success, 0 on failure
107 */
108 int SPProtoEncoder_Init (SPProtoEncoder *o, PacketRecvInterface *input, struct spproto_security_params sp_params, int otp_warning_count, BPendingGroup *pg, BThreadWorkDispatcher *twd) WARN_UNUSED;
109  
110 /**
111 * Frees the object.
112 *
113 * @param o the object
114 */
115 void SPProtoEncoder_Free (SPProtoEncoder *o);
116  
117 /**
118 * Returns the output interface.
119 * The MTU of the output interface will depend on the input MTU and security parameters,
120 * that is spproto_carrier_mtu_for_payload_mtu(sp_params, input MTU).
121 *
122 * @param o the object
123 * @return output interface
124 */
125 PacketRecvInterface * SPProtoEncoder_GetOutput (SPProtoEncoder *o);
126  
127 /**
128 * Sets an encryption key to use.
129 * Encryption must be enabled.
130 *
131 * @param o the object
132 * @param encryption_key key to use
133 */
134 void SPProtoEncoder_SetEncryptionKey (SPProtoEncoder *o, uint8_t *encryption_key);
135  
136 /**
137 * Removes an encryption key if one is configured.
138 * Encryption must be enabled.
139 *
140 * @param o the object
141 */
142 void SPProtoEncoder_RemoveEncryptionKey (SPProtoEncoder *o);
143  
144 /**
145 * Sets an OTP seed to use.
146 * OTPs must be enabled.
147 *
148 * @param o the object
149 * @param seed_id seed identifier
150 * @param key OTP encryption key
151 * @param iv OTP initialization vector
152 */
153 void SPProtoEncoder_SetOTPSeed (SPProtoEncoder *o, uint16_t seed_id, uint8_t *key, uint8_t *iv);
154  
155 /**
156 * Removes the OTP seed if one is configured.
157 * OTPs must be enabled.
158 *
159 * @param o the object
160 */
161 void SPProtoEncoder_RemoveOTPSeed (SPProtoEncoder *o);
162  
163 /**
164 * Sets handlers.
165 *
166 * @param o the object
167 * @param handler OTP warning handler
168 * @param user value to pass to handler
169 */
170 void SPProtoEncoder_SetHandlers (SPProtoEncoder *o, SPProtoEncoder_handler handler, void *user);
171  
172 #endif