blob: 6f13caebf4c0c2c126a45fa6809347def49834cd [file] [log] [blame]
Alexandre Lision51140e12013-12-02 10:54:09 -05001/*
Alexandre Lision7fd5d3d2013-12-04 13:06:40 -05002 Copyright (C) 2006-2013 Werner Dittmann
Alexandre Lision51140e12013-12-02 10:54:09 -05003
4 This program is free software: you can redistribute it and/or modify
Alexandre Lision7fd5d3d2013-12-04 13:06:40 -05005 it under the terms of the GNU Lesser 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 Lision7fd5d3d2013-12-04 13:06:40 -050069bool ZrtpPacketConfirm::isSignatureLengthOk() {
70 int32_t actualLen = getLength();
71 int32_t expectedLen = 19; // Confirm packet fixed part is 19 ZRTP words
72 int32_t sigLen = getSignatureLength();
73
74 if (sigLen > 0) { // We have a signature
75 expectedLen += sigLen + 1; // +1 for the signature length field
76 }
77 return (expectedLen == actualLen);
78}
79
Alexandre Lision51140e12013-12-02 10:54:09 -050080int32_t ZrtpPacketConfirm::getSignatureLength() {
81 int32_t sl = confirmHeader->sigLength & 0xff;
82 if (confirmHeader->filler[1] == 1) { // do we have a 9th bit
83 sl |= 0x100;
84 }
85 return sl;
86}
87
88ZrtpPacketConfirm::ZrtpPacketConfirm(uint8_t* data) {
89 DEBUGOUT((fprintf(stdout, "Creating Confirm packet from data\n")));
90
91 allocated = NULL;
92 zrtpHeader = (zrtpPacketHeader_t *)&((ConfirmPacket_t *)data)->hdr; // the standard header
93 confirmHeader = (Confirm_t *)&((ConfirmPacket_t *)data)->confirm;
94}
95
96ZrtpPacketConfirm::~ZrtpPacketConfirm() {
97 DEBUGOUT((fprintf(stdout, "Deleting Confirm packet: alloc: %x\n", allocated)));
98}