Alexandre Lision | 51140e1 | 2013-12-02 10:54:09 -0500 | [diff] [blame] | 1 | /* |
Alexandre Lision | 907ed2e | 2014-02-04 10:33:09 -0500 | [diff] [blame] | 2 | Copyright (C) 2006-2013 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 | 907ed2e | 2014-02-04 10:33:09 -0500 | [diff] [blame] | 5 | it under the terms of the GNU Lesser 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 | #ifndef _ZRTPPACKETCONFIRM_H_ |
| 19 | #define _ZRTPPACKETCONFIRM_H_ |
| 20 | |
| 21 | /** |
| 22 | * @file ZrtpPacketConfirm.h |
| 23 | * @brief The ZRTP Confirm message |
| 24 | * |
| 25 | * @ingroup GNU_ZRTP |
| 26 | * @{ |
| 27 | */ |
| 28 | |
| 29 | #include <libzrtpcpp/ZrtpPacketBase.h> |
| 30 | |
| 31 | /** |
| 32 | * Implement the Confirm packet. |
| 33 | * |
| 34 | * The ZRTP message Confirm. The implementation sends this |
| 35 | * to confirm the switch to SRTP (encrypted) mode. The contents of |
| 36 | * the Confirm message are encrypted, thus the implementation |
| 37 | * can check if the secret keys work. |
| 38 | * |
| 39 | * @author Werner Dittmann <Werner.Dittmann@t-online.de> |
| 40 | */ |
| 41 | |
| 42 | class __EXPORT ZrtpPacketConfirm : public ZrtpPacketBase { |
| 43 | |
| 44 | private: |
| 45 | Confirm_t* confirmHeader; ///< Point to the Confirm message part |
| 46 | |
| 47 | public: |
| 48 | /// Creates a Confirm packet with default data |
| 49 | ZrtpPacketConfirm(); |
| 50 | |
| 51 | /// Creates a Confirm packet with default data and a given signature length |
| 52 | ZrtpPacketConfirm(uint32_t sl); |
| 53 | |
| 54 | /// Creates a Confirm packet from received data |
| 55 | ZrtpPacketConfirm(uint8_t* d); |
| 56 | |
| 57 | /// Normal destructor |
| 58 | virtual ~ZrtpPacketConfirm(); |
| 59 | |
| 60 | /// Check if SAS verify flag is set |
| 61 | const bool isSASFlag() { return confirmHeader->flags & 0x4; } |
| 62 | |
| 63 | /// Check if PBXEnrollment flag is set |
| 64 | const bool isPBXEnrollment() { return confirmHeader->flags & 0x8; } |
| 65 | |
| 66 | /// Get pointer to filler bytes (contains one bit of signature length) |
| 67 | const uint8_t* getFiller() { return confirmHeader->filler; } |
| 68 | |
| 69 | /// Get pointer to IV data, fixed byte array |
| 70 | const uint8_t* getIv() { return confirmHeader->iv; } |
| 71 | |
| 72 | /// Get pointer to MAC data, fixed byte array |
| 73 | const uint8_t* getHmac() { return confirmHeader->hmac; } |
| 74 | |
| 75 | /// Get Expiration time data |
Alexandre Lision | 7fd5d3d | 2013-12-04 13:06:40 -0500 | [diff] [blame] | 76 | const uint32_t getExpTime() { return zrtpNtohl(confirmHeader->expTime); } |
Alexandre Lision | 51140e1 | 2013-12-02 10:54:09 -0500 | [diff] [blame] | 77 | |
| 78 | /// Get pointer to initial hash chain (H0) data, fixed byte array |
| 79 | uint8_t* getHashH0() { return confirmHeader->hashH0; } |
| 80 | |
| 81 | /// Get pointer to signature data, variable length, refer to getSignatureLength() |
| 82 | const uint8_t* getSignatureData() { return ((uint8_t*)&confirmHeader->expTime) + 4; } |
| 83 | |
| 84 | /// get the signature length in words |
| 85 | int32_t getSignatureLength(); |
| 86 | |
Alexandre Lision | 907ed2e | 2014-02-04 10:33:09 -0500 | [diff] [blame] | 87 | /// Check if packet length makes sense. Confirm packets are 19 words at minumum |
| 88 | bool isLengthOk() {return (getLength() >= 19); } |
| 89 | |
| 90 | bool isSignatureLengthOk(); |
| 91 | |
Alexandre Lision | 51140e1 | 2013-12-02 10:54:09 -0500 | [diff] [blame] | 92 | /// set SAS verified flag |
| 93 | void setSASFlag() { confirmHeader->flags |= 0x4; } |
| 94 | |
| 95 | /// set setPBXEnrollment flag |
| 96 | void setPBXEnrollment() { confirmHeader->flags |= 0x8; } |
| 97 | |
| 98 | /// Set MAC data, fixed length byte array |
| 99 | void setHmac(uint8_t* text) { memcpy(confirmHeader->hmac, text, sizeof(confirmHeader->hmac)); } |
| 100 | |
| 101 | /// Set IV data, fixed length byte array |
| 102 | void setIv(uint8_t* text) { memcpy(confirmHeader->iv, text, sizeof(confirmHeader->iv)); } |
| 103 | |
| 104 | /// Set expiration time data |
Alexandre Lision | 7fd5d3d | 2013-12-04 13:06:40 -0500 | [diff] [blame] | 105 | void setExpTime(uint32_t t) { confirmHeader->expTime = zrtpHtonl(t); } |
Alexandre Lision | 51140e1 | 2013-12-02 10:54:09 -0500 | [diff] [blame] | 106 | |
| 107 | /// Set initial hash chain (H0) data, fixed length byte array |
| 108 | void setHashH0(uint8_t* t) { memcpy(confirmHeader->hashH0, t, sizeof(confirmHeader->hashH0)); } |
| 109 | |
| 110 | /// Set signature data, length of the signature data in bytes and must be a multiple of 4. |
| 111 | bool setSignatureData(uint8_t* data, int32_t length); |
| 112 | |
| 113 | /// Set signature length in words |
| 114 | bool setSignatureLength(uint32_t sl); |
| 115 | |
| 116 | private: |
| 117 | void initialize(); |
| 118 | // Confirm packet is of variable length. It maximum size is 524 words: |
| 119 | // - 11 words fixed size |
| 120 | // - up to 513 words variable part, depending if signature is present and its length. |
| 121 | // This leads to a maximum of 4*524=2096 bytes. |
| 122 | uint8_t data[2100]; // large enough to hold a full blown Confirm packet |
| 123 | |
| 124 | }; |
| 125 | |
| 126 | /** |
| 127 | * @} |
| 128 | */ |
| 129 | #endif // ZRTPPACKETCONFIRM |
| 130 | |