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 #include <stdint.h>
23 #include "bzrtp/bzrtp.h"
24 
25 /* header of ZRTP packet is 12 bytes : Preambule/Sequence Number + ZRTP Magic Cookie + SSRC */
26 #define ZRTP_PACKET_HEADER_LENGTH 12
27 #define ZRTP_PACKET_CRC_LENGTH 4
28 #define ZRTP_PACKET_OVERHEAD 16
29 
30 #define BZRTP_PARSER_ERROR_INVALIDCRC 0xa001
31 #define BZRTP_PARSER_ERROR_INVALIDPACKET 0xa002
32 #define BZRTP_PARSER_ERROR_OUTOFORDER 0xa004
33 #define BZRTP_PARSER_ERROR_INVALIDMESSAGE 0xa008
34 #define BZRTP_PARSER_ERROR_INVALIDCONTEXT 0xa010
35 #define BZRTP_PARSER_ERROR_UNMATCHINGCONFIRMMAC 0xa020
36 #define BZRTP_PARSER_ERROR_UNMATCHINGSSRC 0xa040
37 #define BZRTP_PARSER_ERROR_UNMATCHINGHASHCHAIN 0xa080
38 #define BZRTP_PARSER_ERROR_UNMATCHINGMAC 0xa100
39 #define BZRTP_PARSER_ERROR_UNEXPECTEDMESSAGE 0xa200
40 #define BZRTP_PARSER_ERROR_UNMATCHINGHVI 0xa400
41 
42 #define BZRTP_BUILDER_ERROR_INVALIDPACKET 0x5001
43 #define BZRTP_BUILDER_ERROR_INVALIDMESSAGE 0x5002
44 #define BZRTP_BUILDER_ERROR_INVALIDMESSAGETYPE 0x5004
45 #define BZRTP_BUILDER_ERROR_UNKNOWN 0x5008
46 #define BZRTP_BUILDER_ERROR_INVALIDCONTEXT 0x5010
47 
48 #define BZRTP_CREATE_ERROR_INVALIDMESSAGETYPE 0x0a01
49 #define BZRTP_CREATE_ERROR_UNABLETOCREATECRYPTOCONTEXT 0x0a02
50 #define BZRTP_CREATE_ERROR_INVALIDCONTEXT 0x0a04
51 
52 /* map all message type to an uint8_t value */
53 #define MSGTYPE_INVALID 0x00
54 #define MSGTYPE_HELLO 0x01
55 #define MSGTYPE_HELLOACK 0x02
56 #define MSGTYPE_COMMIT 0x03
57 #define MSGTYPE_DHPART1 0x04
58 #define MSGTYPE_DHPART2 0x05
59 #define MSGTYPE_CONFIRM1 0x06
60 #define MSGTYPE_CONFIRM2 0x07
61 #define MSGTYPE_CONF2ACK 0x08
62 #define MSGTYPE_ERROR 0x10
63 #define MSGTYPE_ERRORACK 0x11
64 #define MSGTYPE_GOCLEAR 0x12
65 #define MSGTYPE_CLEARACK 0x13
66 #define MSGTYPE_SASRELAY 0x14
67 #define MSGTYPE_RELAYACK 0x15
68 #define MSGTYPE_PING 0x16
69 #define MSGTYPE_PINGACK 0x17
70 
75 typedef struct bzrtpPacket_struct {
80  void *messageData;
83 
92 typedef struct bzrtpHelloMessage_struct {
93  uint8_t version[4];
94  uint8_t clientIdentifier[17];
95  uint8_t H3[32];
96  uint8_t ZID[12];
101  uint8_t supportedHash[7];
103  uint8_t supportedCipher[7];
105  uint8_t supportedAuthTag[7];
107  uint8_t supportedKeyAgreement[7];
109  uint8_t supportedSas[7];
110  uint8_t MAC[8];
112 
126  uint8_t H2[32];
127  uint8_t ZID[12];
133  uint8_t hvi[32];
134  uint8_t nonce[16];
135  uint8_t keyID[8];
136  uint8_t MAC[8];
138 
139 
147  uint8_t H1[32];
148  uint8_t rs1ID[8];
149  uint8_t rs2ID[8];
150  uint8_t auxsecretID[8];
151  uint8_t pbxsecretID[8];
152  uint8_t *pv; /* Key exchange public value (length depends on key agreement type) */
153  uint8_t MAC[8];
155 
164  uint8_t confirm_mac[8];
165  uint8_t CFBIV[16];
166  uint8_t H0[32];
173  uint8_t signatureBlockType[4];
177 
202  uint8_t clear_mac[8];
204 
219  uint8_t MAC[8];
220  uint8_t CFBIV[16];
225  uint8_t renderingScheme[4];
226  uint8_t relayedSasHash[32];
231 
242 typedef struct bzrtpPingMessage_struct {
243  uint8_t version[4];
244  uint8_t endpointHash[8];
246 
253  uint8_t version[4];
254  uint8_t endpointHash[8];
255  uint8_t endpointHashReceived[8];
258 
259 
275 BZRTP_EXPORT bzrtpPacket_t *bzrtp_packetCheck(const uint8_t * input, uint16_t inputLength, uint16_t lastValidSequenceNumber, int *exitCode);
276 
277 
289 BZRTP_EXPORT int bzrtp_packetParser(bzrtpContext_t *zrtpContext, bzrtpChannelContext_t *zrtpChannelContext, const uint8_t * input, uint16_t inputLength, bzrtpPacket_t *zrtpPacket);
290 
291 
303 
304 
317 BZRTP_EXPORT int bzrtp_packetBuild(bzrtpContext_t *zrtpContext, bzrtpChannelContext_t *zrtpChannelContext, bzrtpPacket_t *zrtpPacket, uint16_t sequenceNumber);
318 
319 
327 
339 #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:106
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:104
#define BZRTP_EXPORT
Definition: bzrtp.h:41
uint8_t keyAgreementAlgo
Definition: packetParser.h:131
uint8_t * signatureBlock
Definition: packetParser.h:228
BZRTP_EXPORT int bzrtp_packetBuild(bzrtpContext_t *zrtpContext, bzrtpChannelContext_t *zrtpChannelContext, bzrtpPacket_t *zrtpPacket, uint16_t sequenceNumber)
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.
Definition: packetParser.c:735
uint8_t hc
Definition: packetParser.h:100
struct bzrtpHelloMessage_struct bzrtpHelloMessage_t
Hello Message rfc 5.2.
uint32_t cacheExpirationInterval
Definition: packetParser.h:172
Hello ACK Message rfc 5.3 This message contains no data but only a length and message type which are ...
Definition: packetParser.h:125
void * messageData
Definition: packetParser.h:80
uint8_t * packetString
Definition: packetParser.h:81
uint16_t sig_len
Definition: packetParser.h:221
Confirm Message rfc 5.7 Confirm1 and Confirm2 messages have the same structure Confirm1 is generated ...
Definition: packetParser.h:163
unsigned short uint16_t
Definition: stdint.h:79
BZRTP_EXPORT void bzrtp_freeZrtpPacket(bzrtpPacket_t *zrtpPacket)
Deallocate zrtp Packet.
Definition: packetParser.c:1404
Store all zrtpPacket informations according to type a specific structure type is mapped to the void *...
Definition: packetParser.h:75
uint8_t cipherAlgo
Definition: packetParser.h:129
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:152
uint8_t sasAlgo
Definition: packetParser.h:132
uint8_t hashAlgo
Definition: packetParser.h:128
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:224
Relay ACK Message rfc 5.14 This message contains no data but only a length and message type which are...
Definition: packetParser.h:242
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:222
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:1123
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:76
BZRTP_EXPORT bzrtpPacket_t * bzrtp_packetCheck(const uint8_t *input, uint16_t inputLength, uint16_t lastValidSequenceNumber, int *exitCode)
Parse a string which shall be a valid ZRTP packet Check validity and allocate the bzrtpPacket structu...
Definition: packetParser.c:101
uint8_t S
Definition: packetParser.h:97
structure of the ZRTP engine context Store current state, timers, HMAC and encryption keys ...
Definition: typedef.h:202
unsigned int uint32_t
Definition: stdint.h:80
uint8_t A
Definition: packetParser.h:223
PingAck Message The Ping and PingACK messages are unrelated to the rest of the ZRTP protocol...
Definition: packetParser.h:252
BZRTP_EXPORT int bzrtp_packetUpdateSequenceNumber(bzrtpPacket_t *zrtpPacket, uint16_t sequenceNumber)
Modify the current sequence number of the packet in the packetString and sequenceNumber fields The CR...
Definition: packetParser.c:1445
uint8_t D
Definition: packetParser.h:171
uint8_t sc
Definition: packetParser.h:108
uint32_t sourceIdentifier
Definition: packetParser.h:77
uint8_t M
Definition: packetParser.h:98
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:256
Clear ACK Message rfc 5.12 This message contains no data but only a length and message type which are...
Definition: packetParser.h:218
uint8_t A
Definition: packetParser.h:170
Hello Message rfc 5.2.
Definition: packetParser.h:92
uint8_t authTagAlgo
Definition: packetParser.h:130
uint8_t * signatureBlock
Definition: packetParser.h:174
uint8_t P
Definition: packetParser.h:99
uint16_t messageLength
Definition: packetParser.h:79
DHPart Message rfc 5.5 and rfc 5.6 DHPart1 and DHPart2 message have the same structure DHPart1 is gen...
Definition: packetParser.h:146
uint8_t V
Definition: packetParser.h:169
Conf2 ACK Message rfc 5.8 This message contains no data but only a length and message type which are ...
Definition: packetParser.h:201
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:195
The zrtp context of a channel.
Definition: typedef.h:132
uint8_t messageType
Definition: packetParser.h:78
uint16_t sig_len
Definition: packetParser.h:167
uint8_t E
Definition: packetParser.h:168
uint8_t cc
Definition: packetParser.h:102
uint8_t signatureBlockType
Definition: packetParser.h:227