Imported Upstream version 0.3.2
[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(u_int32_t payload_length, u_int32_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   {
51     header_->seq_nr = 0;
52     header_->sender_id = 0;
53     header_->mux = 0;
54   }
55 }
56
57 u_int32_t EncryptedPacket::getHeaderLength()
58 {
59   return sizeof(struct HeaderStruct);
60 }
61
62 seq_nr_t EncryptedPacket::getSeqNr() const
63 {
64   if(header_)
65     return SEQ_NR_T_NTOH(header_->seq_nr);
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   return 0;
76 }
77
78 mux_t EncryptedPacket::getMux() const
79 {
80   if(header_)
81     return MUX_T_NTOH(header_->mux);
82
83   return 0;
84 }
85
86 void EncryptedPacket::setSeqNr(seq_nr_t seq_nr)
87 {
88   if(header_)
89     header_->seq_nr = SEQ_NR_T_HTON(seq_nr);
90 }
91
92 void EncryptedPacket::setSenderId(sender_id_t sender_id)
93 {
94   if(header_)
95     header_->sender_id = SENDER_ID_T_HTON(sender_id);
96 }
97
98 void EncryptedPacket::setMux(mux_t mux)
99 {
100   if(header_)
101     header_->mux = MUX_T_HTON(mux);
102 }
103
104 void EncryptedPacket::setHeader(seq_nr_t seq_nr, sender_id_t sender_id, mux_t mux)
105 {
106   if(!header_)
107     return;
108
109   header_->seq_nr = SEQ_NR_T_HTON(seq_nr);
110   header_->sender_id = SENDER_ID_T_HTON(sender_id);
111   header_->mux = MUX_T_HTON(mux);
112 }
113
114 u_int32_t EncryptedPacket::getPayloadLength() const
115 {
116   if(!payload_) 
117     return 0;
118
119   if(!auth_tag_)
120     return (length_ > sizeof(struct HeaderStruct)) ? (length_ - sizeof(struct HeaderStruct)) : 0;
121   
122   return (length_ > (sizeof(struct HeaderStruct) + auth_tag_length_)) ? (length_ - sizeof(struct HeaderStruct) - auth_tag_length_) : 0;
123 }
124
125 void EncryptedPacket::setPayloadLength(u_int32_t payload_length)
126 {
127   Buffer::setLength(payload_length + sizeof(struct HeaderStruct));
128       // depending on allow_realloc buf_ may point to another address
129       // therefore in this case reinit() gets called by Buffer::setLength()
130 }
131
132 void EncryptedPacket::reinit()
133 {
134   header_ = reinterpret_cast<struct HeaderStruct*>(buf_);
135   payload_ = buf_ + sizeof(struct HeaderStruct);
136   
137   if(length_ <= (sizeof(struct HeaderStruct)))
138     payload_ = NULL;
139   
140   if(length_ < (sizeof(struct HeaderStruct))) {
141     header_ = NULL;
142     AnytunError::throwErr() << "encrypted packet can't be initialized, buffer is too small"; 
143   }  
144   
145   if(auth_tag_)
146   {
147     if(length_ < (sizeof(struct HeaderStruct) + auth_tag_length_)) {
148       auth_tag_ = NULL;
149       AnytunError::throwErr() << "auth-tag can't be enabled, buffer is too small"; 
150     }
151     auth_tag_ = buf_ + length_ - auth_tag_length_;
152   }  
153 }
154
155 u_int8_t* EncryptedPacket::getPayload()
156 {
157   return payload_;
158 }
159
160 u_int8_t* EncryptedPacket::getAuthenticatedPortion()
161 {
162   return buf_;
163 }
164
165 u_int32_t EncryptedPacket::getAuthenticatedPortionLength()
166 {
167   if(!buf_)
168     return 0;
169
170   if(!auth_tag_)
171     return length_;
172   
173   return (length_ > auth_tag_length_) ? (length_ - auth_tag_length_) : 0;
174 }
175
176 void EncryptedPacket::withAuthTag(bool b)
177 {
178   if((b && auth_tag_) || (!b && !auth_tag_))
179     return;
180   
181   if(b)
182   {
183     if(length_ < (sizeof(struct HeaderStruct) + auth_tag_length_))
184       AnytunError::throwErr() << "auth-tag can't be enabled, buffer is too small";
185     
186     auth_tag_ = buf_ + length_ - auth_tag_length_;
187   }
188   else
189     auth_tag_ = NULL;
190 }
191
192 void EncryptedPacket::addAuthTag()
193 {
194   if(auth_tag_)
195     return;
196
197   auth_tag_ = buf_; // will be set to the correct value @ reinit
198   setLength(length_ + auth_tag_length_);
199   if(auth_tag_ == buf_) // reinit was not called by setLength
200     reinit();
201 }
202
203 void EncryptedPacket::removeAuthTag()
204 {
205   if(!auth_tag_)
206     return;
207
208   auth_tag_ = NULL;
209   setLength(length_ - auth_tag_length_);
210 }
211
212 u_int8_t* EncryptedPacket::getAuthTag()
213 {
214   return auth_tag_;
215 }
216
217 u_int32_t EncryptedPacket::getAuthTagLength()
218 {
219   if(auth_tag_)
220     return auth_tag_length_;
221
222   return 0;
223 }