Alexandre Lision | 51140e1 | 2013-12-02 10:54:09 -0500 | [diff] [blame] | 1 | /* |
Alexandre Lision | e24852d | 2014-02-04 13:13:02 -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 | e24852d | 2014-02-04 13:13:02 -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/ZrtpPacketDHPart.h> |
| 23 | |
| 24 | ZrtpPacketDHPart::ZrtpPacketDHPart() { |
| 25 | DEBUGOUT((fprintf(stdout, "Creating DHPart packet without data and pkt type\n"))); |
| 26 | initialize(); |
| 27 | } |
| 28 | |
| 29 | ZrtpPacketDHPart::ZrtpPacketDHPart(const char* pkt) { |
| 30 | DEBUGOUT((fprintf(stdout, "Creating DHPart packet without data\n"))); |
| 31 | initialize(); |
| 32 | setPubKeyType(pkt); |
| 33 | } |
| 34 | |
| 35 | void ZrtpPacketDHPart::initialize() { |
| 36 | |
| 37 | void* allocated = &data; |
| 38 | memset(allocated, 0, sizeof(data)); |
| 39 | |
| 40 | zrtpHeader = (zrtpPacketHeader_t *)&((DHPartPacket_t *)allocated)->hdr; // the standard header |
| 41 | DHPartHeader = (DHPart_t *)&((DHPartPacket_t *)allocated)->dhPart; |
| 42 | pv = ((uint8_t*)allocated) + sizeof(DHPartPacket_t); // point to the public key value |
| 43 | |
| 44 | setZrtpId(); |
| 45 | } |
| 46 | |
| 47 | // The fixed numbers below are taken from ZRTP specification, chap 5.1.5 |
| 48 | void ZrtpPacketDHPart::setPubKeyType(const char* pkt) { |
| 49 | // Well - the algo type is only 4 char thus cast to int32 and compare |
| 50 | if (*(int32_t*)pkt == *(int32_t*)dh2k) { |
| 51 | dhLength = 256; |
| 52 | } |
| 53 | else if (*(int32_t*)pkt == *(int32_t*)dh3k) { |
| 54 | dhLength = 384; |
| 55 | } |
| 56 | else if (*(int32_t*)pkt == *(int32_t*)ec25) { |
| 57 | dhLength = 64; |
| 58 | } |
| 59 | else if (*(int32_t*)pkt == *(int32_t*)ec38) { |
| 60 | dhLength = 96; |
| 61 | } |
| 62 | else |
| 63 | return; |
| 64 | |
| 65 | int length = sizeof(DHPartPacket_t) + dhLength + (2 * ZRTP_WORD_SIZE); // HMAC field is 2*ZRTP_WORD_SIZE |
| 66 | setLength(length / ZRTP_WORD_SIZE); |
| 67 | } |
| 68 | |
| 69 | ZrtpPacketDHPart::ZrtpPacketDHPart(uint8_t *data) { |
| 70 | DEBUGOUT((fprintf(stdout, "Creating DHPart packet from data\n"))); |
| 71 | |
| 72 | zrtpHeader = (zrtpPacketHeader_t *)&((DHPartPacket_t *)data)->hdr; // the standard header |
| 73 | DHPartHeader = (DHPart_t *)&((DHPartPacket_t *)data)->dhPart; |
| 74 | |
| 75 | int16_t len = getLength(); |
| 76 | DEBUGOUT((fprintf(stdout, "DHPart length: %d\n", len))); |
Alexandre Lision | e24852d | 2014-02-04 13:13:02 -0500 | [diff] [blame] | 77 | if (len == 85) { |
Alexandre Lision | 51140e1 | 2013-12-02 10:54:09 -0500 | [diff] [blame] | 78 | dhLength = 256; |
| 79 | } |
Alexandre Lision | e24852d | 2014-02-04 13:13:02 -0500 | [diff] [blame] | 80 | else if (len == 117) { |
Alexandre Lision | 51140e1 | 2013-12-02 10:54:09 -0500 | [diff] [blame] | 81 | dhLength = 384; |
| 82 | } |
Alexandre Lision | e24852d | 2014-02-04 13:13:02 -0500 | [diff] [blame] | 83 | else if (len == 37) { |
Alexandre Lision | 51140e1 | 2013-12-02 10:54:09 -0500 | [diff] [blame] | 84 | dhLength = 64; |
| 85 | } |
Alexandre Lision | e24852d | 2014-02-04 13:13:02 -0500 | [diff] [blame] | 86 | else if (len == 45) { |
Alexandre Lision | 51140e1 | 2013-12-02 10:54:09 -0500 | [diff] [blame] | 87 | dhLength = 96; |
| 88 | } |
Alexandre Lision | 51140e1 | 2013-12-02 10:54:09 -0500 | [diff] [blame] | 89 | else { |
| 90 | pv = NULL; |
| 91 | return; |
| 92 | } |
| 93 | pv = data + sizeof(DHPartPacket_t); // point to the public key value |
| 94 | } |
| 95 | |
| 96 | ZrtpPacketDHPart::~ZrtpPacketDHPart() { |
| 97 | DEBUGOUT((fprintf(stdout, "Deleting DHPart packet: alloc: %x\n", allocated))); |
| 98 | } |