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 methods 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.
14 * Copyright (C) 2007-2014 Markus Grüneis, Othmar Gsenger, Erwin Nindl,
15 * Christian Pointner <satp@wirdorange.org>
17 * This file is part of Anytun.
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
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.
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/>.
32 * In addition, as a special exception, the copyright holders give
33 * permission to link the code of portions of this program with the
34 * OpenSSL library under certain conditions as described in each
35 * individual source file, and distribute linked combinations
37 * You must obey the GNU General Public License in all respects
38 * for all of the code used other than OpenSSL. If you modify
39 * file(s) with this exception, you may extend this exception to your
40 * version of the file(s), but you are not obligated to do so. If you
41 * do not wish to do so, delete this exception statement from your
42 * version. If you delete this exception statement from all source
43 * files in the program, then also delete it here.
48 #include <cstdio> // for std::memcpy
50 #include "encryptedPacket.h"
52 #include "datatypes.h"
54 #include "anytunError.h"
56 EncryptedPacket::EncryptedPacket(uint32_t payload_length, uint32_t auth_tag_length, bool allow_realloc)
57 : Buffer(payload_length + sizeof(struct HeaderStruct), allow_realloc), auth_tag_length_(auth_tag_length)
59 header_ = reinterpret_cast<struct HeaderStruct*>(buf_);
60 payload_ = buf_ + sizeof(struct HeaderStruct);
64 header_->sender_id = 0;
69 uint32_t EncryptedPacket::getHeaderLength()
71 return sizeof(struct HeaderStruct);
74 seq_nr_t EncryptedPacket::getSeqNr() const
77 return SEQ_NR_T_NTOH(header_->seq_nr);
83 sender_id_t EncryptedPacket::getSenderId() const
86 return SENDER_ID_T_NTOH(header_->sender_id);
92 mux_t EncryptedPacket::getMux() const
95 return MUX_T_NTOH(header_->mux);
101 void EncryptedPacket::setSeqNr(seq_nr_t seq_nr)
104 header_->seq_nr = SEQ_NR_T_HTON(seq_nr);
108 void EncryptedPacket::setSenderId(sender_id_t sender_id)
111 header_->sender_id = SENDER_ID_T_HTON(sender_id);
115 void EncryptedPacket::setMux(mux_t mux)
118 header_->mux = MUX_T_HTON(mux);
122 void EncryptedPacket::setHeader(seq_nr_t seq_nr, sender_id_t sender_id, mux_t mux)
128 header_->seq_nr = SEQ_NR_T_HTON(seq_nr);
129 header_->sender_id = SENDER_ID_T_HTON(sender_id);
130 header_->mux = MUX_T_HTON(mux);
133 uint32_t EncryptedPacket::getPayloadLength() const
140 return (length_ > sizeof(struct HeaderStruct)) ? (length_ - sizeof(struct HeaderStruct)) : 0;
143 return (length_ > (sizeof(struct HeaderStruct) + auth_tag_length_)) ? (length_ - sizeof(struct HeaderStruct) - auth_tag_length_) : 0;
146 void EncryptedPacket::setPayloadLength(uint32_t payload_length)
148 Buffer::setLength(payload_length + sizeof(struct HeaderStruct));
149 // depending on allow_realloc buf_ may point to another address
150 // therefore in this case reinit() gets called by Buffer::setLength()
153 void EncryptedPacket::reinit()
155 header_ = reinterpret_cast<struct HeaderStruct*>(buf_);
156 payload_ = buf_ + sizeof(struct HeaderStruct);
158 if(length_ <= (sizeof(struct HeaderStruct))) {
162 if(length_ < (sizeof(struct HeaderStruct))) {
164 AnytunError::throwErr() << "encrypted packet can't be initialized, buffer is too small";
168 if(length_ < (sizeof(struct HeaderStruct) + auth_tag_length_)) {
170 AnytunError::throwErr() << "auth-tag can't be enabled, buffer is too small";
172 auth_tag_ = buf_ + length_ - auth_tag_length_;
176 uint8_t* EncryptedPacket::getPayload()
181 uint8_t* EncryptedPacket::getAuthenticatedPortion()
186 uint32_t EncryptedPacket::getAuthenticatedPortionLength()
196 return (length_ > auth_tag_length_) ? (length_ - auth_tag_length_) : 0;
199 void EncryptedPacket::withAuthTag(bool b)
201 if((b && auth_tag_) || (!b && !auth_tag_)) {
206 if(length_ < (sizeof(struct HeaderStruct) + auth_tag_length_)) {
207 AnytunError::throwErr() << "auth-tag can't be enabled, buffer is too small";
210 auth_tag_ = buf_ + length_ - auth_tag_length_;
216 void EncryptedPacket::addAuthTag()
222 auth_tag_ = buf_; // will be set to the correct value @ reinit
223 setLength(length_ + auth_tag_length_);
224 if(auth_tag_ == buf_) { // reinit was not called by setLength
229 void EncryptedPacket::removeAuthTag()
236 setLength(length_ - auth_tag_length_);
239 uint8_t* EncryptedPacket::getAuthTag()
244 uint32_t EncryptedPacket::getAuthTagLength()
247 return auth_tag_length_;