bZRTP
bZRTP is an opensource implementation of ZRTP keys exchange protocol
packetParser.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014-2019 Belledonne Communications SARL.
3  *
4  * This file is part of bzrtp.
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see <http://www.gnu.org/licenses/>.
18  */
19 #ifndef PACKETPARSER_H
20 #define PACKETPARSER_H
21 
22 
23 #include "bctoolbox/list.h"
24 
25 #include "bzrtp/bzrtp.h"
26 
27 #include <stdint.h>
28 
29 /* header of ZRTP packet is 12 bytes : Preambule/Sequence Number + ZRTP Magic Cookie + SSRC :
30  * 0 1 2 3
31  * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 *
32  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
33  * |0 0 0 1 0 0 0 0| Not used: 0 | Sequence Number |
34  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
35  * | Magic Cookie 'ZRTP' (0x5a525450) |
36  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
37  * | Source Identifier |
38  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
39  * | |
40  * | ZRTP Message (length depends on Message Type) |
41  * | . . . |
42  * | |
43  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
44  * | CRC (1 word) |
45  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
46 */
47 #define ZRTP_PACKET_HEADER_LENGTH 12
48 
49 /* when packet is fragmented, header is 20 bytes:
50  * 0 1 2 3
51  * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 *
52  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
53  * |0 0 0 1 0 0 0 1| Not used: 0 | Sequence Number |
54  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
55  * | Magic Cookie 'ZRTP' (0x5a525450) |
56  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
57  * | Source Identifier |
58  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
59  * | message Id | message total length |
60  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
61  * | offset | fragment length |
62  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
63  * | |
64  * | ZRTP Message fragment(length as indicated) |
65  * | . . . |
66  * | |
67  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
68  * | CRC (1 word) |
69  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
70  *
71  * with:
72  * - message Id: a unique Id for this message
73  * - message total length: size in 32bits words of the total message
74  * - offset : offset of this fragment (in 32 bits words)
75  * - fragment length: size of this fragment (in 32 bits words)
76  */
77 #define ZRTP_FRAGMENTEDPACKET_HEADER_LENGTH 20
78 #define ZRTP_PACKET_CRC_LENGTH 4
79 #define ZRTP_PACKET_OVERHEAD (ZRTP_PACKET_HEADER_LENGTH + ZRTP_PACKET_CRC_LENGTH)
80 #define ZRTP_FRAGMENTEDPACKET_OVERHEAD (ZRTP_FRAGMENTEDPACKET_HEADER_LENGTH + ZRTP_PACKET_CRC_LENGTH)
81 
82 #define BZRTP_PARSER_ERROR_INVALIDCRC 0xa001
83 #define BZRTP_PARSER_ERROR_INVALIDPACKET 0xa002
84 #define BZRTP_PARSER_ERROR_OUTOFORDER 0xa004
85 #define BZRTP_PARSER_ERROR_INVALIDMESSAGE 0xa008
86 #define BZRTP_PARSER_ERROR_INVALIDCONTEXT 0xa010
87 #define BZRTP_PARSER_ERROR_UNMATCHINGCONFIRMMAC 0xa020
88 #define BZRTP_PARSER_ERROR_UNMATCHINGSSRC 0xa040
89 #define BZRTP_PARSER_ERROR_UNMATCHINGHASHCHAIN 0xa080
90 #define BZRTP_PARSER_ERROR_UNMATCHINGMAC 0xa100
91 #define BZRTP_PARSER_ERROR_UNEXPECTEDMESSAGE 0xa200
92 #define BZRTP_PARSER_ERROR_UNMATCHINGHVI 0xa400
93 #define BZRTP_PARSER_INFO_PACKETFRAGMENT 0xa800
94 
95 #define BZRTP_BUILDER_ERROR_INVALIDPACKET 0x5001
96 #define BZRTP_BUILDER_ERROR_INVALIDMESSAGE 0x5002
97 #define BZRTP_BUILDER_ERROR_INVALIDMESSAGETYPE 0x5004
98 #define BZRTP_BUILDER_ERROR_UNKNOWN 0x5008
99 #define BZRTP_BUILDER_ERROR_INVALIDCONTEXT 0x5010
100 #define BZRTP_BUILDER_ERROR_UNABLETOFRAGMENT 0x5020
101 
102 #define BZRTP_CREATE_ERROR_INVALIDMESSAGETYPE 0x0a01
103 #define BZRTP_CREATE_ERROR_UNABLETOCREATECRYPTOCONTEXT 0x0a02
104 #define BZRTP_CREATE_ERROR_INVALIDCONTEXT 0x0a04
105 
106 /* map all message type to an uint8_t value */
107 #define MSGTYPE_INVALID 0x00
108 #define MSGTYPE_HELLO 0x01
109 #define MSGTYPE_HELLOACK 0x02
110 #define MSGTYPE_COMMIT 0x03
111 #define MSGTYPE_DHPART1 0x04
112 #define MSGTYPE_DHPART2 0x05
113 #define MSGTYPE_CONFIRM1 0x06
114 #define MSGTYPE_CONFIRM2 0x07
115 #define MSGTYPE_CONF2ACK 0x08
116 #define MSGTYPE_ERROR 0x10
117 #define MSGTYPE_ERRORACK 0x11
118 #define MSGTYPE_GOCLEAR 0x12
119 #define MSGTYPE_CLEARACK 0x13
120 #define MSGTYPE_SASRELAY 0x14
121 #define MSGTYPE_RELAYACK 0x15
122 #define MSGTYPE_PING 0x16
123 #define MSGTYPE_PINGACK 0x17
124 #define MSGTYPE_FRAGMENT 0xff
125 
126 #ifdef __cplusplus
127 extern "C"{
128 #endif
129 
134 typedef struct bzrtpPacket_struct {
139  void *messageData;
141  bctbx_list_t *fragments;
142 } bzrtpPacket_t;
143 
152 typedef struct bzrtpHelloMessage_struct {
153  uint8_t version[4];
154  uint8_t clientIdentifier[17];
155  uint8_t H3[32];
156  uint8_t ZID[12];
161  uint8_t supportedHash[7];
163  uint8_t supportedCipher[7];
165  uint8_t supportedAuthTag[7];
167  uint8_t supportedKeyAgreement[7];
169  uint8_t supportedSas[7];
170  uint8_t MAC[8];
172 
186  uint8_t H2[32];
187  uint8_t ZID[12];
193  uint8_t hvi[32];
194  uint8_t nonce[16];
195  uint8_t keyID[8];
197  uint8_t MAC[8];
199 
200 
201 /* The nonce used in KEMPart2 message is 16 bytes long */
202 #define ZRTP_KEMPART2_NONCE_SIZE 16
203 
210  uint8_t H1[32];
211  uint8_t rs1ID[8];
212  uint8_t rs2ID[8];
213  uint8_t auxsecretID[8];
214  uint8_t pbxsecretID[8];
216  uint8_t MAC[8];
218 
227  uint8_t confirm_mac[8];
228  uint8_t CFBIV[16];
229  uint8_t H0[32];
236  uint8_t signatureBlockType[4];
240 
265  uint8_t clear_mac[8];
267 
282  uint8_t MAC[8];
283  uint8_t CFBIV[16];
288  uint8_t renderingScheme[4];
289  uint8_t relayedSasHash[32];
294 
305 typedef struct bzrtpPingMessage_struct {
306  uint8_t version[4];
307  uint8_t endpointHash[8];
309 
316  uint8_t version[4];
317  uint8_t endpointHash[8];
318  uint8_t endpointHashReceived[8];
321 
322 
337 BZRTP_EXPORT bzrtpPacket_t *bzrtp_packetCheck(uint8_t ** inputPtr, uint16_t *inputLength, bzrtpChannelContext_t *zrtpChannelContext, int *exitCode);
338 
339 
351 BZRTP_EXPORT int bzrtp_packetParser(bzrtpContext_t *zrtpContext, bzrtpChannelContext_t *zrtpChannelContext, const uint8_t * input, uint16_t inputLength, bzrtpPacket_t *zrtpPacket);
352 
353 
365 
366 
379 BZRTP_EXPORT int bzrtp_packetBuild(bzrtpContext_t *zrtpContext, bzrtpChannelContext_t *zrtpChannelContext, bzrtpPacket_t *zrtpPacket);
380 
381 
389 
401 
402 #ifdef __cplusplus
403 }
404 #endif
405 
406 #endif /* PACKETPARSER_H */
struct bzrtpGoClearMessage_struct bzrtpGoClearMessage_t
Conf2 ACK Message rfc 5.8 This message contains no data but only a length and message type which are ...
uint8_t kc
Definition: packetParser.h:166
struct bzrtpPacket_struct bzrtpPacket_t
Store all zrtpPacket informations according to type a specific structure type is mapped to the void *...
uint8_t ac
Definition: packetParser.h:164
#define BZRTP_EXPORT
Definition: bzrtp.h:41
uint8_t keyAgreementAlgo
Definition: packetParser.h:191
uint8_t * signatureBlock
Definition: packetParser.h:291
uint8_t hc
Definition: packetParser.h:160
struct bzrtpHelloMessage_struct bzrtpHelloMessage_t
Hello Message rfc 5.2.
uint32_t cacheExpirationInterval
Definition: packetParser.h:235
Hello ACK Message rfc 5.3 This message contains no data but only a length and message type which are ...
Definition: packetParser.h:185
void * messageData
Definition: packetParser.h:139
uint8_t * packetString
Definition: packetParser.h:140
uint16_t sig_len
Definition: packetParser.h:284
Confirm Message rfc 5.7 Confirm1 and Confirm2 messages have the same structure Confirm1 is generated ...
Definition: packetParser.h:226
unsigned short uint16_t
Definition: stdint.h:79
BZRTP_EXPORT void bzrtp_freeZrtpPacket(bzrtpPacket_t *zrtpPacket)
Deallocate zrtp Packet.
Definition: packetParser.c:1653
Store all zrtpPacket informations according to type a specific structure type is mapped to the void *...
Definition: packetParser.h:134
uint8_t cipherAlgo
Definition: packetParser.h:189
unsigned char uint8_t
Definition: stdint.h:78
struct bzrtpPingAckMessage_struct bzrtpPingAckMessage_t
PingAck Message The Ping and PingACK messages are unrelated to the rest of the ZRTP protocol...
uint8_t * pv
Definition: packetParser.h:215
uint8_t sasAlgo
Definition: packetParser.h:192
uint8_t hashAlgo
Definition: packetParser.h:188
struct bzrtpPingMessage_struct bzrtpPingMessage_t
Relay ACK Message rfc 5.14 This message contains no data but only a length and message type which are...
uint8_t D
Definition: packetParser.h:287
Relay ACK Message rfc 5.14 This message contains no data but only a length and message type which are...
Definition: packetParser.h:305
struct bzrtpSASRelayMessage_struct bzrtpSASRelayMessage_t
Clear ACK Message rfc 5.12 This message contains no data but only a length and message type which are...
uint8_t V
Definition: packetParser.h:285
BZRTP_EXPORT bzrtpPacket_t * bzrtp_createZrtpPacket(bzrtpContext_t *zrtpContext, bzrtpChannelContext_t *zrtpChannelContext, uint32_t messageType, int *exitCode)
Create an empty packet and allocate the messageData according to requested packetType.
Definition: packetParser.c:1306
struct bzrtpConfirmMessage_struct bzrtpConfirmMessage_t
Confirm Message rfc 5.7 Confirm1 and Confirm2 messages have the same structure Confirm1 is generated ...
uint16_t sequenceNumber
Definition: packetParser.h:135
uint8_t S
Definition: packetParser.h:157
structure of the ZRTP engine context Store current state, timers, HMAC and encryption keys ...
Definition: typedef.h:235
unsigned int uint32_t
Definition: stdint.h:80
uint8_t A
Definition: packetParser.h:286
PingAck Message The Ping and PingACK messages are unrelated to the rest of the ZRTP protocol...
Definition: packetParser.h:315
uint8_t D
Definition: packetParser.h:234
uint8_t sc
Definition: packetParser.h:168
uint32_t sourceIdentifier
Definition: packetParser.h:136
uint8_t M
Definition: packetParser.h:158
bctbx_list_t * fragments
Definition: packetParser.h:141
struct bzrtpCommitMessage_struct bzrtpCommitMessage_t
Hello ACK Message rfc 5.3 This message contains no data but only a length and message type which are ...
struct bzrtpDHPartMessage_struct bzrtpDHPartMessage_t
DHPart Message rfc 5.5 and rfc 5.6 DHPart1 and DHPart2 message have the same structure DHPart1 is gen...
uint32_t SSRC
Definition: packetParser.h:319
Clear ACK Message rfc 5.12 This message contains no data but only a length and message type which are...
Definition: packetParser.h:281
uint8_t A
Definition: packetParser.h:233
Hello Message rfc 5.2.
Definition: packetParser.h:152
uint8_t authTagAlgo
Definition: packetParser.h:190
uint8_t * signatureBlock
Definition: packetParser.h:237
uint8_t P
Definition: packetParser.h:159
uint16_t messageLength
Definition: packetParser.h:138
DHPart Message rfc 5.5 and rfc 5.6 DHPart1 and DHPart2 message have the same structure DHPart1 is gen...
Definition: packetParser.h:209
uint8_t * pv
Definition: packetParser.h:196
uint8_t V
Definition: packetParser.h:232
BZRTP_EXPORT int bzrtp_packetBuild(bzrtpContext_t *zrtpContext, bzrtpChannelContext_t *zrtpChannelContext, bzrtpPacket_t *zrtpPacket)
Create a ZRTP packet string from the ZRTP packet values present in the structure messageType, messageData and sourceIdentifier in zrtpPacket must have been correctly set before calling this function The packet is not ready to be sent at that stage, sequenceNumber and CRC must be set using bzrtp_packetSetSequenceNumber.
Definition: packetParser.c:847
Conf2 ACK Message rfc 5.8 This message contains no data but only a length and message type which are ...
Definition: packetParser.h:264
BZRTP_EXPORT int bzrtp_packetParser(bzrtpContext_t *zrtpContext, bzrtpChannelContext_t *zrtpChannelContext, const uint8_t *input, uint16_t inputLength, bzrtpPacket_t *zrtpPacket)
Parse the packet to extract the message and allocate the matching message structure if needed...
Definition: packetParser.c:296
The zrtp context of a channel.
Definition: typedef.h:157
uint8_t messageType
Definition: packetParser.h:137
uint16_t sig_len
Definition: packetParser.h:230
BZRTP_EXPORT int bzrtp_packetSetSequenceNumber(bzrtpPacket_t *zrtpPacket, uint16_t sequenceNumber)
Set the current sequence number of the packet in the packetString and sequenceNumber fields The CRC a...
Definition: packetParser.c:1704
uint8_t E
Definition: packetParser.h:231
uint8_t cc
Definition: packetParser.h:162
uint8_t signatureBlockType
Definition: packetParser.h:290
BZRTP_EXPORT bzrtpPacket_t * bzrtp_packetCheck(uint8_t **inputPtr, uint16_t *inputLength, bzrtpChannelContext_t *zrtpChannelContext, int *exitCode)
Parse a string which shall be a valid ZRTP packet Check validity and allocate the bzrtpPacket structu...
Definition: packetParser.c:94