BadVPN – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 /**
2 * @file SPProtoDecoder.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 decodes packets according to SPProto.
32 */
33  
34 #ifndef BADVPN_CLIENT_SPPROTODECODER_H
35 #define BADVPN_CLIENT_SPPROTODECODER_H
36  
37 #include <stdint.h>
38  
39 #include <misc/debug.h>
40 #include <base/DebugObject.h>
41 #include <base/BLog.h>
42 #include <protocol/spproto.h>
43 #include <security/BEncryption.h>
44 #include <security/OTPChecker.h>
45 #include <flow/PacketPassInterface.h>
46  
47 /**
48 * Handler called when OTP generation for a new seed is finished.
49 *
50 * @param user as in {@link SPProtoDecoder_Init}
51 */
52 typedef void (*SPProtoDecoder_otp_handler) (void *user);
53  
54 /**
55 * Object which decodes packets according to SPProto.
56 * Input is with {@link PacketPassInterface}.
57 * Output is with {@link PacketPassInterface}.
58 */
59 typedef struct {
60 PacketPassInterface *output;
61 struct spproto_security_params sp_params;
62 BThreadWorkDispatcher *twd;
63 void *user;
64 BLog_logfunc logfunc;
65 int output_mtu;
66 int hash_size;
67 int enc_block_size;
68 int enc_key_size;
69 int input_mtu;
70 uint8_t *buf;
71 PacketPassInterface input;
72 OTPChecker otpchecker;
73 int have_encryption_key;
74 BEncryption encryptor;
75 uint8_t *in;
76 int in_len;
77 int tw_have;
78 BThreadWork tw;
79 uint16_t tw_out_seed_id;
80 otp_t tw_out_otp;
81 uint8_t *tw_out;
82 int tw_out_len;
83 DebugObject d_obj;
84 } SPProtoDecoder;
85  
86 /**
87 * Initializes the object.
88 * {@link BSecurity_GlobalInitThreadSafe} must have been done if
89 * {@link BThreadWorkDispatcher_UsingThreads}(twd) = 1.
90 *
91 * @param o the object
92 * @param output output interface. Its MTU must not be too large, i.e. this must hold:
93 * spproto_carrier_mtu_for_payload_mtu(sp_params, output MTU) >= 0
94 * @param sp_params SPProto parameters
95 * @param encryption_key if using encryption, the encryption key
96 * @param num_otp_seeds if using OTPs, how many OTP seeds to keep for checking
97 * receiving packets. Must be >=2 if using OTPs.
98 * @param pg pending group
99 * @param twd thread work dispatcher
100 * @param user argument to handlers
101 * @param logfunc function which prepends the log prefix using {@link BLog_Append}
102 * @return 1 on success, 0 on failure
103 */
104 int SPProtoDecoder_Init (SPProtoDecoder *o, PacketPassInterface *output, struct spproto_security_params sp_params, int num_otp_seeds, BPendingGroup *pg, BThreadWorkDispatcher *twd, void *user, BLog_logfunc logfunc) WARN_UNUSED;
105  
106 /**
107 * Frees the object.
108 *
109 * @param o the object
110 */
111 void SPProtoDecoder_Free (SPProtoDecoder *o);
112  
113 /**
114 * Returns the input interface.
115 * The MTU of the input interface will depend on the output MTU and security parameters,
116 * that is spproto_carrier_mtu_for_payload_mtu(sp_params, output MTU).
117 *
118 * @param o the object
119 * @return input interface
120 */
121 PacketPassInterface * SPProtoDecoder_GetInput (SPProtoDecoder *o);
122  
123 /**
124 * Sets an encryption key for decrypting packets.
125 * Encryption must be enabled.
126 *
127 * @param o the object
128 * @param encryption_key key to use
129 */
130 void SPProtoDecoder_SetEncryptionKey (SPProtoDecoder *o, uint8_t *encryption_key);
131  
132 /**
133 * Removes an encryption key if one is configured.
134 * Encryption must be enabled.
135 *
136 * @param o the object
137 */
138 void SPProtoDecoder_RemoveEncryptionKey (SPProtoDecoder *o);
139  
140 /**
141 * Starts generating OTPs for a seed to check received packets against.
142 * OTPs for this seed will not be recognized until the {@link SPProtoDecoder_otp_handler} handler
143 * is called.
144 * If OTPs are still being generated for the previous seed, it will be forgotten.
145 * OTPs must be enabled.
146 *
147 * @param o the object
148 * @param seed_id seed identifier
149 * @param key OTP encryption key
150 * @param iv OTP initialization vector
151 */
152 void SPProtoDecoder_AddOTPSeed (SPProtoDecoder *o, uint16_t seed_id, uint8_t *key, uint8_t *iv);
153  
154 /**
155 * Removes all OTP seeds for checking received packets against.
156 * OTPs must be enabled.
157 *
158 * @param o the object
159 */
160 void SPProtoDecoder_RemoveOTPSeeds (SPProtoDecoder *o);
161  
162 /**
163 * Sets handlers.
164 *
165 * @param o the object
166 * @param otp_handler handler called when OTP generation is finished
167 * @param user argument to handler
168 */
169 void SPProtoDecoder_SetHandlers (SPProtoDecoder *o, SPProtoDecoder_otp_handler otp_handler, void *user);
170  
171 #endif