Alexandre Lision | 51140e1 | 2013-12-02 10:54:09 -0500 | [diff] [blame] | 1 | /* |
Alexandre Lision | ddd731e | 2014-01-31 11:50:08 -0500 | [diff] [blame] | 2 | Copyright (C) 2006-2007 Werner Dittmann |
Alexandre Lision | 51140e1 | 2013-12-02 10:54:09 -0500 | [diff] [blame] | 3 | |
| 4 | This program is free software: you can redistribute it and/or modify |
Alexandre Lision | ddd731e | 2014-01-31 11:50:08 -0500 | [diff] [blame] | 5 | it under the terms of the GNU General Public License as published by |
Alexandre Lision | 51140e1 | 2013-12-02 10:54:09 -0500 | [diff] [blame] | 6 | 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 | |
| 24 | ZrtpPacketConfirm::ZrtpPacketConfirm() { |
| 25 | DEBUGOUT((fprintf(stdout, "Creating Confirm packet without data, no sl data\n"))); |
| 26 | initialize(); |
| 27 | setSignatureLength(0); |
| 28 | } |
| 29 | |
| 30 | ZrtpPacketConfirm::ZrtpPacketConfirm(uint32_t sl) { |
| 31 | DEBUGOUT((fprintf(stdout, "Creating Confirm packet without data\n"))); |
| 32 | initialize(); |
| 33 | setSignatureLength(sl); |
| 34 | } |
| 35 | |
| 36 | void 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 | |
| 46 | bool 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 | |
| 59 | bool 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 Lision | 51140e1 | 2013-12-02 10:54:09 -0500 | [diff] [blame] | 69 | int32_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 | |
| 77 | ZrtpPacketConfirm::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 | |
| 85 | ZrtpPacketConfirm::~ZrtpPacketConfirm() { |
| 86 | DEBUGOUT((fprintf(stdout, "Deleting Confirm packet: alloc: %x\n", allocated))); |
| 87 | } |