blob: f5587592a37da56889a004ea66bc2632c6b607c0 [file] [log] [blame]
Alexandre Lision51140e12013-12-02 10:54:09 -05001/*
Alexandre Lisione24852d2014-02-04 13:13:02 -05002 Copyright (C) 2006-2007 Werner Dittmann
Alexandre Lision51140e12013-12-02 10:54:09 -05003
4 This program is free software: you can redistribute it and/or modify
Alexandre Lisione24852d2014-02-04 13:13:02 -05005 it under the terms of the GNU General Public License as published by
Alexandre Lision51140e12013-12-02 10:54:09 -05006 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
16*/
17
18/*
19 * Authors: Werner Dittmann <Werner.Dittmann@t-online.de>
20 */
21
22#include <libzrtpcpp/ZrtpPacketConfirm.h>
23
24ZrtpPacketConfirm::ZrtpPacketConfirm() {
25 DEBUGOUT((fprintf(stdout, "Creating Confirm packet without data, no sl data\n")));
26 initialize();
27 setSignatureLength(0);
28}
29
30ZrtpPacketConfirm::ZrtpPacketConfirm(uint32_t sl) {
31 DEBUGOUT((fprintf(stdout, "Creating Confirm packet without data\n")));
32 initialize();
33 setSignatureLength(sl);
34}
35
36void ZrtpPacketConfirm::initialize() {
37 void* allocated = &data;
38 memset(allocated, 0, sizeof(data));
39
40 zrtpHeader = (zrtpPacketHeader_t *)&((ConfirmPacket_t *)allocated)->hdr; // the standard header
41 confirmHeader = (Confirm_t *)&((ConfirmPacket_t *)allocated)->confirm;
42
43 setZrtpId();
44}
45
46bool ZrtpPacketConfirm::setSignatureLength(uint32_t sl) {
47 if (sl > 512)
48 return false;
49
50 int32_t length = sizeof(ConfirmPacket_t) + (sl * ZRTP_WORD_SIZE);
51 confirmHeader->sigLength = sl; // sigLength is a uint byte
52 if (sl & 0x100) { // check the 9th bit
53 confirmHeader->filler[1] = 1; // and set it if necessary
54 }
55 setLength(length / 4);
56 return true;
57}
58
59bool ZrtpPacketConfirm::setSignatureData(uint8_t* data, int32_t length) {
60 int32_t l = getSignatureLength() * 4;
61 if (length > l || (length % 4) != 0)
62 return false;
63
64 uint8_t* p = ((uint8_t*)&confirmHeader->expTime) + 4; // point to signature block
65 memcpy(p, data, length);
66 return true;
67}
68
Alexandre Lision51140e12013-12-02 10:54:09 -050069int32_t ZrtpPacketConfirm::getSignatureLength() {
70 int32_t sl = confirmHeader->sigLength & 0xff;
71 if (confirmHeader->filler[1] == 1) { // do we have a 9th bit
72 sl |= 0x100;
73 }
74 return sl;
75}
76
77ZrtpPacketConfirm::ZrtpPacketConfirm(uint8_t* data) {
78 DEBUGOUT((fprintf(stdout, "Creating Confirm packet from data\n")));
79
80 allocated = NULL;
81 zrtpHeader = (zrtpPacketHeader_t *)&((ConfirmPacket_t *)data)->hdr; // the standard header
82 confirmHeader = (Confirm_t *)&((ConfirmPacket_t *)data)->confirm;
83}
84
85ZrtpPacketConfirm::~ZrtpPacketConfirm() {
86 DEBUGOUT((fprintf(stdout, "Deleting Confirm packet: alloc: %x\n", allocated)));
87}