Imported Upstream version 0.3.4
[anytun.git] / src / encryptedPacket.cpp
1 /*
2  *  anytun
3  *
4  *  The secure anycast tunneling protocol (satp) defines a protocol used
5  *  for communication between any combination of unicast and anycast
6  *  tunnel endpoints.  It has less protocol overhead than IPSec in Tunnel
7  *  mode and allows tunneling of every ETHER TYPE protocol (e.g.
8  *  ethernet, ip, arp ...). satp directly includes cryptography and
9  *  message authentication based on the methodes used by SRTP.  It is
10  *  intended to deliver a generic, scaleable and secure solution for
11  *  tunneling and relaying of packets of any protocol.
12  *
13  *
14  *  Copyright (C) 2007-2009 Othmar Gsenger, Erwin Nindl,
15  *                          Christian Pointner <satp@wirdorange.org>
16  *
17  *  This file is part of Anytun.
18  *
19  *  Anytun is free software: you can redistribute it and/or modify
20  *  it under the terms of the GNU General Public License as published by
21  *  the Free Software Foundation, either version 3 of the License, or
22  *  any later version.
23  *
24  *  Anytun is distributed in the hope that it will be useful,
25  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
26  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27  *  GNU General Public License for more details.
28  *
29  *  You should have received a copy of the GNU General Public License
30  *  along with anytun.  If not, see <http://www.gnu.org/licenses/>.
31  */
32
33 #include <stdexcept>
34 #include <iostream>
35 #include <cstdio>       // for std::memcpy
36
37 #include "encryptedPacket.h"
38 #include "endian.h"
39 #include "datatypes.h"
40 #include "log.h"
41 #include "anytunError.h"
42
43 EncryptedPacket::EncryptedPacket(uint32_t payload_length, uint32_t auth_tag_length, bool allow_realloc)
44   : Buffer(payload_length + sizeof(struct HeaderStruct), allow_realloc), auth_tag_length_(auth_tag_length)
45 {
46   header_ = reinterpret_cast<struct HeaderStruct*>(buf_);
47   payload_ = buf_ + sizeof(struct HeaderStruct);
48   auth_tag_ = NULL;
49   if(header_) {
50     header_->seq_nr = 0;
51     header_->sender_id = 0;
52     header_->mux = 0;
53   }
54 }
55
56 uint32_t EncryptedPacket::getHeaderLength()
57 {
58   return sizeof(struct HeaderStruct);
59 }
60
61 seq_nr_t EncryptedPacket::getSeqNr() const
62 {
63   if(header_) {
64     return SEQ_NR_T_NTOH(header_->seq_nr);
65   }
66
67   return 0;
68 }
69
70 sender_id_t EncryptedPacket::getSenderId() const
71 {
72   if(header_) {
73     return SENDER_ID_T_NTOH(header_->sender_id);
74   }
75
76   return 0;
77 }
78
79 mux_t EncryptedPacket::getMux() const
80 {
81   if(header_) {
82     return MUX_T_NTOH(header_->mux);
83   }
84
85   return 0;
86 }
87
88 void EncryptedPacket::setSeqNr(seq_nr_t seq_nr)
89 {
90   if(header_) {
91     header_->seq_nr = SEQ_NR_T_HTON(seq_nr);
92   }
93 }
94
95 void EncryptedPacket::setSenderId(sender_id_t sender_id)
96 {
97   if(header_) {
98     header_->sender_id = SENDER_ID_T_HTON(sender_id);
99   }
100 }
101
102 void EncryptedPacket::setMux(mux_t mux)
103 {
104   if(header_) {
105     header_->mux = MUX_T_HTON(mux);
106   }
107 }
108
109 void EncryptedPacket::setHeader(seq_nr_t seq_nr, sender_id_t sender_id, mux_t mux)
110 {
111   if(!header_) {
112     return;
113   }
114
115   header_->seq_nr = SEQ_NR_T_HTON(seq_nr);
116   header_->sender_id = SENDER_ID_T_HTON(sender_id);
117   header_->mux = MUX_T_HTON(mux);
118 }
119
120 uint32_t EncryptedPacket::getPayloadLength() const
121 {
122   if(!payload_) {
123     return 0;
124   }
125
126   if(!auth_tag_) {
127     return (length_ > sizeof(struct HeaderStruct)) ? (length_ - sizeof(struct HeaderStruct)) : 0;
128   }
129
130   return (length_ > (sizeof(struct HeaderStruct) + auth_tag_length_)) ? (length_ - sizeof(struct HeaderStruct) - auth_tag_length_) : 0;
131 }
132
133 void EncryptedPacket::setPayloadLength(uint32_t payload_length)
134 {
135   Buffer::setLength(payload_length + sizeof(struct HeaderStruct));
136   // depending on allow_realloc buf_ may point to another address
137   // therefore in this case reinit() gets called by Buffer::setLength()
138 }
139
140 void EncryptedPacket::reinit()
141 {
142   header_ = reinterpret_cast<struct HeaderStruct*>(buf_);
143   payload_ = buf_ + sizeof(struct HeaderStruct);
144
145   if(length_ <= (sizeof(struct HeaderStruct))) {
146     payload_ = NULL;
147   }
148
149   if(length_ < (sizeof(struct HeaderStruct))) {
150     header_ = NULL;
151     AnytunError::throwErr() << "encrypted packet can't be initialized, buffer is too small";
152   }
153
154   if(auth_tag_) {
155     if(length_ < (sizeof(struct HeaderStruct) + auth_tag_length_)) {
156       auth_tag_ = NULL;
157       AnytunError::throwErr() << "auth-tag can't be enabled, buffer is too small";
158     }
159     auth_tag_ = buf_ + length_ - auth_tag_length_;
160   }
161 }
162
163 uint8_t* EncryptedPacket::getPayload()
164 {
165   return payload_;
166 }
167
168 uint8_t* EncryptedPacket::getAuthenticatedPortion()
169 {
170   return buf_;
171 }
172
173 uint32_t EncryptedPacket::getAuthenticatedPortionLength()
174 {
175   if(!buf_) {
176     return 0;
177   }
178
179   if(!auth_tag_) {
180     return length_;
181   }
182
183   return (length_ > auth_tag_length_) ? (length_ - auth_tag_length_) : 0;
184 }
185
186 void EncryptedPacket::withAuthTag(bool b)
187 {
188   if((b && auth_tag_) || (!b && !auth_tag_)) {
189     return;
190   }
191
192   if(b) {
193     if(length_ < (sizeof(struct HeaderStruct) + auth_tag_length_)) {
194       AnytunError::throwErr() << "auth-tag can't be enabled, buffer is too small";
195     }
196
197     auth_tag_ = buf_ + length_ - auth_tag_length_;
198   } else {
199     auth_tag_ = NULL;
200   }
201 }
202
203 void EncryptedPacket::addAuthTag()
204 {
205   if(auth_tag_) {
206     return;
207   }
208
209   auth_tag_ = buf_; // will be set to the correct value @ reinit
210   setLength(length_ + auth_tag_length_);
211   if(auth_tag_ == buf_) { // reinit was not called by setLength
212     reinit();
213   }
214 }
215
216 void EncryptedPacket::removeAuthTag()
217 {
218   if(!auth_tag_) {
219     return;
220   }
221
222   auth_tag_ = NULL;
223   setLength(length_ - auth_tag_length_);
224 }
225
226 uint8_t* EncryptedPacket::getAuthTag()
227 {
228   return auth_tag_;
229 }
230
231 uint32_t EncryptedPacket::getAuthTagLength()
232 {
233   if(auth_tag_) {
234     return auth_tag_length_;
235   }
236
237   return 0;
238 }