Alexandre Lision | 51140e1 | 2013-12-02 10:54:09 -0500 | [diff] [blame] | 1 | /* |
| 2 | Copyright (C) 2006-2009 Werner Dittmann |
| 3 | |
| 4 | This program is free software: you can redistribute it and/or modify |
| 5 | it under the terms of the GNU General Public License as published by |
| 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 <string> |
| 23 | #include <stdio.h> |
| 24 | |
Alexandre Lision | 7fd5d3d | 2013-12-04 13:06:40 -0500 | [diff] [blame] | 25 | #include <ZrtpQueue.h> |
| 26 | #include <libzrtpcpp/ZIDCache.h> |
Alexandre Lision | 51140e1 | 2013-12-02 10:54:09 -0500 | [diff] [blame] | 27 | #include <libzrtpcpp/ZRtp.h> |
| 28 | #include <libzrtpcpp/ZrtpStateClass.h> |
| 29 | #include <libzrtpcpp/ZrtpUserCallback.h> |
| 30 | |
| 31 | static TimeoutProvider<std::string, ost::ZrtpQueue*>* staticTimeoutProvider = NULL; |
| 32 | |
| 33 | NAMESPACE_COMMONCPP |
| 34 | using namespace GnuZrtpCodes; |
| 35 | |
| 36 | ZrtpQueue::ZrtpQueue(uint32 size, RTPApplication& app) : |
| 37 | AVPQueue(size,app) |
| 38 | { |
| 39 | init(); |
| 40 | } |
| 41 | |
| 42 | ZrtpQueue::ZrtpQueue(uint32 ssrc, uint32 size, RTPApplication& app) : |
| 43 | AVPQueue(ssrc,size,app) |
| 44 | { |
| 45 | init(); |
| 46 | } |
| 47 | |
| 48 | void ZrtpQueue::init() |
| 49 | { |
| 50 | zrtpUserCallback = NULL; |
| 51 | enableZrtp = false; |
| 52 | started = false; |
| 53 | mitmMode = false; |
| 54 | enableParanoidMode = false; |
| 55 | zrtpEngine = NULL; |
| 56 | senderZrtpSeqNo = 1; |
| 57 | |
| 58 | clientIdString = clientId; |
| 59 | peerSSRC = 0; |
| 60 | } |
| 61 | |
| 62 | ZrtpQueue::~ZrtpQueue() { |
| 63 | |
| 64 | endQueue(); |
| 65 | stopZrtp(); |
| 66 | |
| 67 | if (zrtpUserCallback != NULL) { |
| 68 | delete zrtpUserCallback; |
| 69 | zrtpUserCallback = NULL; |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | int32_t |
| 74 | ZrtpQueue::initialize(const char *zidFilename, bool autoEnable, ZrtpConfigure* config) |
| 75 | { |
| 76 | int32_t ret = 1; |
| 77 | |
| 78 | synchEnter(); |
| 79 | |
| 80 | ZrtpConfigure* configOwn = NULL; |
| 81 | if (config == NULL) { |
| 82 | config = configOwn = new ZrtpConfigure(); |
| 83 | config->setStandardConfig(); |
| 84 | } |
| 85 | enableZrtp = autoEnable; |
| 86 | |
| 87 | config->setParanoidMode(enableParanoidMode); |
| 88 | |
| 89 | if (staticTimeoutProvider == NULL) { |
| 90 | staticTimeoutProvider = new TimeoutProvider<std::string, ZrtpQueue*>(); |
| 91 | staticTimeoutProvider->start(); |
| 92 | } |
Alexandre Lision | 7fd5d3d | 2013-12-04 13:06:40 -0500 | [diff] [blame] | 93 | ZIDCache* zf = getZidCacheInstance(); |
Alexandre Lision | 51140e1 | 2013-12-02 10:54:09 -0500 | [diff] [blame] | 94 | if (!zf->isOpen()) { |
| 95 | std::string fname; |
| 96 | if (zidFilename == NULL) { |
| 97 | char *home = getenv("HOME"); |
| 98 | std::string baseDir = (home != NULL) ? (std::string(home) + std::string("/.")) |
| 99 | : std::string("."); |
| 100 | fname = baseDir + std::string("GNUZRTP.zid"); |
| 101 | zidFilename = fname.c_str(); |
| 102 | } |
| 103 | if (zf->open((char *)zidFilename) < 0) { |
| 104 | enableZrtp = false; |
| 105 | ret = -1; |
| 106 | } |
| 107 | } |
| 108 | if (ret > 0) { |
| 109 | const uint8_t* ownZid = zf->getZid(); |
| 110 | zrtpEngine = new ZRtp((uint8_t*)ownZid, (ZrtpCallback*)this, clientIdString, config, mitmMode, signSas); |
| 111 | } |
| 112 | if (configOwn != NULL) { |
| 113 | delete configOwn; |
| 114 | } |
| 115 | synchLeave(); |
| 116 | return ret; |
| 117 | } |
| 118 | |
| 119 | void ZrtpQueue::startZrtp() { |
| 120 | if (zrtpEngine != NULL) { |
| 121 | zrtpEngine->startZrtpEngine(); |
Alexandre Lision | 7fd5d3d | 2013-12-04 13:06:40 -0500 | [diff] [blame] | 122 | zrtpUnprotect = 0; |
Alexandre Lision | 51140e1 | 2013-12-02 10:54:09 -0500 | [diff] [blame] | 123 | started = true; |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | void ZrtpQueue::stopZrtp() { |
| 128 | if (zrtpEngine != NULL) { |
Alexandre Lision | 7fd5d3d | 2013-12-04 13:06:40 -0500 | [diff] [blame] | 129 | if (zrtpUnprotect < 50 && !zrtpEngine->isMultiStream()) |
| 130 | zrtpEngine->setRs2Valid(); |
Alexandre Lision | 51140e1 | 2013-12-02 10:54:09 -0500 | [diff] [blame] | 131 | delete zrtpEngine; |
| 132 | zrtpEngine = NULL; |
| 133 | started = false; |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | /* |
| 138 | * The takeInDataPacket implementation for ZRTPQueue. |
| 139 | */ |
| 140 | size_t |
| 141 | ZrtpQueue::takeInDataPacket(void) |
| 142 | { |
| 143 | InetHostAddress network_address; |
| 144 | tpport_t transport_port; |
| 145 | |
| 146 | uint32 nextSize = (uint32)getNextDataPacketSize(); |
| 147 | unsigned char* buffer = new unsigned char[nextSize]; |
| 148 | int32 rtn = (int32)recvData(buffer, nextSize, network_address, transport_port); |
| 149 | if ( (rtn < 0) || ((uint32)rtn > getMaxRecvPacketSize()) ){ |
| 150 | delete buffer; |
| 151 | return 0; |
| 152 | } |
| 153 | |
| 154 | IncomingZRTPPkt* packet = NULL; |
| 155 | // check if this could be a real RTP/SRTP packet. |
| 156 | if ((*buffer & 0xf0) != 0x10) { |
| 157 | return (rtpDataPacket(buffer, rtn, network_address, transport_port)); |
| 158 | } |
| 159 | |
| 160 | // We assume all other packets are ZRTP packets here. Process |
| 161 | // if ZRTP processing is enabled. Because valid RTP packets are |
| 162 | // already handled we delete any packets here after processing. |
| 163 | if (enableZrtp && zrtpEngine != NULL) { |
| 164 | // Fixed header length + smallest ZRTP packet (includes CRC) |
Alexandre Lision | 7fd5d3d | 2013-12-04 13:06:40 -0500 | [diff] [blame] | 165 | if (rtn < (int32)(12 + sizeof(HelloAckPacket_t))) // data too small, dismiss |
Alexandre Lision | 51140e1 | 2013-12-02 10:54:09 -0500 | [diff] [blame] | 166 | return 0; |
| 167 | |
| 168 | // Get CRC value into crc (see above how to compute the offset) |
| 169 | uint16_t temp = rtn - CRC_SIZE; |
| 170 | uint32_t crc = *(uint32_t*)(buffer + temp); |
| 171 | crc = ntohl(crc); |
| 172 | |
| 173 | if (!zrtpCheckCksum(buffer, temp, crc)) { |
| 174 | delete buffer; |
| 175 | if (zrtpUserCallback != NULL) |
| 176 | zrtpUserCallback->showMessage(Warning, WarningCRCmismatch); |
| 177 | return 0; |
| 178 | } |
| 179 | |
| 180 | packet = new IncomingZRTPPkt(buffer,rtn); |
| 181 | |
| 182 | uint32 magic = packet->getZrtpMagic(); |
| 183 | |
| 184 | // Check if it is really a ZRTP packet, if not delete it and return 0 |
| 185 | if (magic != ZRTP_MAGIC || zrtpEngine == NULL) { |
| 186 | delete packet; |
| 187 | return 0; |
| 188 | } |
| 189 | // cover the case if the other party sends _only_ ZRTP packets at the |
| 190 | // beginning of a session. Start ZRTP in this case as well. |
| 191 | if (!started) { |
| 192 | startZrtp(); |
| 193 | } |
| 194 | // this now points beyond the undefined and length field. |
| 195 | // We need them, thus adjust |
| 196 | unsigned char* extHeader = |
| 197 | const_cast<unsigned char*>(packet->getHdrExtContent()); |
| 198 | extHeader -= 4; |
| 199 | |
| 200 | // store peer's SSRC, used when creating the CryptoContext |
| 201 | peerSSRC = packet->getSSRC(); |
Alexandre Lision | 7fd5d3d | 2013-12-04 13:06:40 -0500 | [diff] [blame] | 202 | zrtpEngine->processZrtpMessage(extHeader, peerSSRC, rtn); |
Alexandre Lision | 51140e1 | 2013-12-02 10:54:09 -0500 | [diff] [blame] | 203 | } |
| 204 | delete packet; |
| 205 | return 0; |
| 206 | } |
| 207 | |
| 208 | size_t |
| 209 | ZrtpQueue::rtpDataPacket(unsigned char* buffer, int32 rtn, InetHostAddress network_address, tpport_t transport_port) |
| 210 | { |
| 211 | // Special handling of padding to take care of encrypted content. |
| 212 | // In case of SRTP the padding length field is also encrypted, thus |
| 213 | // it gives a wrong length. Check and clear padding bit before |
| 214 | // creating the RTPPacket. Will be set and re-computed after a possible |
| 215 | // SRTP decryption. |
| 216 | uint8 padSet = (*buffer & 0x20); |
| 217 | if (padSet) { |
| 218 | *buffer = *buffer & ~0x20; // clear padding bit |
| 219 | } |
| 220 | // build a packet. It will link itself to its source |
| 221 | IncomingRTPPkt* packet = |
| 222 | new IncomingRTPPkt(buffer,rtn); |
| 223 | |
| 224 | // Generic header validity check. |
| 225 | if ( !packet->isHeaderValid() ) { |
| 226 | delete packet; |
| 227 | return 0; |
| 228 | } |
| 229 | |
| 230 | // Look for a CryptoContext for this packet's SSRC |
| 231 | CryptoContext* pcc = getInQueueCryptoContext(packet->getSSRC()); |
| 232 | |
| 233 | // If no crypto context is available for this SSRC but we are already in |
| 234 | // Secure state then create a CryptoContext for this SSRC. |
| 235 | // Assumption: every SSRC stream sent via this connection is secured |
| 236 | // _and_ uses the same crypto parameters. |
| 237 | if (pcc == NULL) { |
| 238 | pcc = getInQueueCryptoContext(0); |
| 239 | if (pcc != NULL) { |
| 240 | pcc = pcc->newCryptoContextForSSRC(packet->getSSRC(), 0, 0L); |
| 241 | if (pcc != NULL) { |
| 242 | pcc->deriveSrtpKeys(0); |
| 243 | setInQueueCryptoContext(pcc); |
| 244 | } |
| 245 | } |
| 246 | } |
| 247 | // If no crypto context: then either ZRTP is off or in early state |
| 248 | // If crypto context is available then unprotect data here. If an error |
| 249 | // occurs report the error and discard the packet. |
| 250 | if (pcc != NULL) { |
| 251 | int32 ret; |
| 252 | if ((ret = packet->unprotect(pcc)) < 0) { |
| 253 | if (!onSRTPPacketError(*packet, ret)) { |
| 254 | delete packet; |
| 255 | return 0; |
| 256 | } |
| 257 | } |
| 258 | if (started && zrtpEngine->inState(WaitConfAck)) { |
| 259 | zrtpEngine->conf2AckSecure(); |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | // virtual for profile-specific validation and processing. |
| 264 | if (!onRTPPacketRecv(*packet) ) { |
| 265 | delete packet; |
| 266 | return 0; |
| 267 | } |
| 268 | if (padSet) { |
| 269 | packet->reComputePayLength(true); |
| 270 | } |
| 271 | // get time of arrival |
| 272 | struct timeval recvtime; |
| 273 | gettimeofday(&recvtime,NULL); |
| 274 | |
| 275 | bool source_created; |
| 276 | SyncSourceLink* sourceLink = |
| 277 | getSourceBySSRC(packet->getSSRC(),source_created); |
| 278 | SyncSource* s = sourceLink->getSource(); |
| 279 | if ( source_created ) { |
| 280 | // Set data transport address. |
| 281 | setDataTransportPort(*s,transport_port); |
| 282 | // Network address is assumed to be the same as the control one |
| 283 | setNetworkAddress(*s,network_address); |
| 284 | sourceLink->initStats(); |
| 285 | // First packet arrival time. |
| 286 | sourceLink->setInitialDataTime(recvtime); |
| 287 | sourceLink->setProbation(getMinValidPacketSequence()); |
| 288 | if ( sourceLink->getHello() ) |
| 289 | onNewSyncSource(*s); |
| 290 | } |
| 291 | else if ( 0 == s->getDataTransportPort() ) { |
| 292 | // Test if RTCP packets had been received but this is the |
| 293 | // first data packet from this source. |
| 294 | setDataTransportPort(*s,transport_port); |
| 295 | } |
| 296 | |
| 297 | // Before inserting in the queue, |
| 298 | // 1) check for collisions and loops. If the packet cannot be |
| 299 | // assigned to a source, it will be rejected. |
| 300 | // 2) check the source is a sufficiently well known source |
| 301 | // TODO: also check CSRC identifiers. |
| 302 | if (checkSSRCInIncomingRTPPkt(*sourceLink, source_created, |
| 303 | network_address, transport_port) && |
| 304 | recordReception(*sourceLink,*packet,recvtime) ) { |
| 305 | // now the packet link is linked in the queues |
| 306 | IncomingRTPPktLink* packetLink = new IncomingRTPPktLink(packet, sourceLink, recvtime, |
| 307 | packet->getTimestamp() - sourceLink->getInitialDataTimestamp(), |
| 308 | NULL,NULL,NULL,NULL); |
| 309 | insertRecvPacket(packetLink); |
| 310 | } else { |
| 311 | // must be discarded due to collision or loop or |
| 312 | // invalid source |
| 313 | delete packet; |
| 314 | return 0; |
| 315 | } |
| 316 | // Start the ZRTP engine after we got a at least one RTP packet and |
| 317 | // sent some as well or we are in multi-stream mode. |
| 318 | if (!started && enableZrtp) { |
| 319 | startZrtp(); |
| 320 | } |
| 321 | return rtn; |
| 322 | } |
| 323 | |
| 324 | bool |
| 325 | ZrtpQueue::onSRTPPacketError(IncomingRTPPkt& pkt, int32 errorCode) |
| 326 | { |
| 327 | if (errorCode == -1) { |
| 328 | sendInfo(Warning, WarningSRTPauthError); |
| 329 | } |
| 330 | else { |
| 331 | sendInfo(Warning, WarningSRTPreplayError); |
| 332 | } |
| 333 | return false; |
| 334 | } |
| 335 | |
| 336 | |
| 337 | void |
| 338 | ZrtpQueue::putData(uint32 stamp, const unsigned char* data, size_t len) |
| 339 | { |
| 340 | OutgoingDataQueue::putData(stamp, data, len); |
| 341 | } |
| 342 | |
| 343 | |
| 344 | void |
| 345 | ZrtpQueue::sendImmediate(uint32 stamp, const unsigned char* data, size_t len) |
| 346 | { |
| 347 | OutgoingDataQueue::sendImmediate(stamp, data, len); |
| 348 | } |
| 349 | |
| 350 | |
| 351 | /* |
| 352 | * Here the callback methods required by the ZRTP implementation |
| 353 | */ |
| 354 | int32_t ZrtpQueue::sendDataZRTP(const unsigned char *data, int32_t length) { |
| 355 | |
| 356 | OutgoingZRTPPkt* packet = new OutgoingZRTPPkt(data, length); |
| 357 | |
| 358 | packet->setSSRC(getLocalSSRC()); |
| 359 | |
| 360 | packet->setSeqNum(senderZrtpSeqNo++); |
| 361 | |
| 362 | /* |
| 363 | * Compute the ZRTP CRC over the full ZRTP packet. Thus include |
| 364 | * the fixed packet header into the calculation. |
| 365 | */ |
| 366 | uint16_t temp = packet->getRawPacketSize() - CRC_SIZE; |
| 367 | uint8_t* pt = (uint8_t*)packet->getRawPacket(); |
| 368 | uint32_t crc = zrtpGenerateCksum(pt, temp); |
| 369 | // convert and store CRC in crc field of ZRTP packet. |
| 370 | crc = zrtpEndCksum(crc); |
| 371 | |
| 372 | // advance pointer to CRC storage |
| 373 | pt += temp; |
| 374 | *(uint32_t*)pt = htonl(crc); |
| 375 | |
| 376 | dispatchImmediate(packet); |
| 377 | delete packet; |
| 378 | |
| 379 | return 1; |
| 380 | } |
| 381 | |
| 382 | bool ZrtpQueue::srtpSecretsReady(SrtpSecret_t* secrets, EnableSecurity part) |
| 383 | { |
| 384 | CryptoContext* recvCryptoContext; |
| 385 | CryptoContext* senderCryptoContext; |
| 386 | CryptoContextCtrl* recvCryptoContextCtrl; |
| 387 | CryptoContextCtrl* senderCryptoContextCtrl; |
| 388 | |
| 389 | int cipher; |
| 390 | int authn; |
| 391 | int authKeyLen; |
| 392 | |
| 393 | if (secrets->authAlgorithm == Sha1) { |
| 394 | authn = SrtpAuthenticationSha1Hmac; |
| 395 | authKeyLen = 20; |
| 396 | } |
| 397 | |
| 398 | if (secrets->authAlgorithm == Skein) { |
| 399 | authn = SrtpAuthenticationSkeinHmac; |
| 400 | authKeyLen = 32; |
| 401 | } |
| 402 | |
| 403 | if (secrets->symEncAlgorithm == Aes) |
| 404 | cipher = SrtpEncryptionAESCM; |
| 405 | |
| 406 | if (secrets->symEncAlgorithm == TwoFish) |
| 407 | cipher = SrtpEncryptionTWOCM; |
| 408 | |
| 409 | if (part == ForSender) { |
| 410 | // To encrypt packets: intiator uses initiator keys, |
| 411 | // responder uses responder keys |
| 412 | // Create a "half baked" crypto context first and store it. This is |
| 413 | // the main crypto context for the sending part of the connection. |
| 414 | if (secrets->role == Initiator) { |
| 415 | senderCryptoContext = new CryptoContext( |
| 416 | 0, |
| 417 | 0, |
| 418 | 0L, // keyderivation << 48, |
| 419 | cipher, // encryption algo |
| 420 | authn, // authtentication algo |
| 421 | (unsigned char*)secrets->keyInitiator, // Master Key |
| 422 | secrets->initKeyLen / 8, // Master Key length |
| 423 | (unsigned char*)secrets->saltInitiator, // Master Salt |
| 424 | secrets->initSaltLen / 8, // Master Salt length |
| 425 | secrets->initKeyLen / 8, // encryption keyl |
| 426 | authKeyLen, // authentication key len |
| 427 | secrets->initSaltLen / 8, // session salt len |
| 428 | secrets->srtpAuthTagLen / 8); // authentication tag lenA |
| 429 | senderCryptoContextCtrl = new CryptoContextCtrl(0, |
| 430 | cipher, // encryption algo |
| 431 | authn, // authtication algo |
| 432 | (unsigned char*)secrets->keyInitiator, // Master Key |
| 433 | secrets->initKeyLen / 8, // Master Key length |
| 434 | (unsigned char*)secrets->saltInitiator, // Master Salt |
| 435 | secrets->initSaltLen / 8, // Master Salt length |
| 436 | secrets->initKeyLen / 8, // encryption keyl |
| 437 | authKeyLen, // authentication key len |
| 438 | secrets->initSaltLen / 8, // session salt len |
| 439 | secrets->srtpAuthTagLen / 8); // authentication tag len |
| 440 | } |
| 441 | else { |
| 442 | senderCryptoContext = new CryptoContext( |
| 443 | 0, |
| 444 | 0, |
| 445 | 0L, // keyderivation << 48, |
| 446 | cipher, // encryption algo |
| 447 | authn, // authtentication algo |
| 448 | (unsigned char*)secrets->keyResponder, // Master Key |
| 449 | secrets->respKeyLen / 8, // Master Key length |
| 450 | (unsigned char*)secrets->saltResponder, // Master Salt |
| 451 | secrets->respSaltLen / 8, // Master Salt length |
| 452 | secrets->respKeyLen / 8, // encryption keyl |
| 453 | authKeyLen, // authentication key len |
| 454 | secrets->respSaltLen / 8, // session salt len |
| 455 | secrets->srtpAuthTagLen / 8); // authentication tag len |
| 456 | senderCryptoContextCtrl = new CryptoContextCtrl(0, |
| 457 | cipher, // encryption algo |
| 458 | authn, // authtication algo |
| 459 | (unsigned char*)secrets->keyResponder, // Master Key |
| 460 | secrets->respKeyLen / 8, // Master Key length |
| 461 | (unsigned char*)secrets->saltResponder, // Master Salt |
| 462 | secrets->respSaltLen / 8, // Master Salt length |
| 463 | secrets->respKeyLen / 8, // encryption keyl |
| 464 | authKeyLen, // authentication key len |
| 465 | secrets->respSaltLen / 8, // session salt len |
| 466 | secrets->srtpAuthTagLen / 8); // authentication tag len |
| 467 | } |
| 468 | if (senderCryptoContext == NULL) { |
| 469 | return false; |
| 470 | } |
| 471 | // Insert the Crypto templates (SSRC == 0) into the queue. When we send |
| 472 | // the first RTP or RTCP packet the real crypto context will be created. |
| 473 | // Refer to putData(), sendImmediate() in ccrtp's outqueue.cpp and |
| 474 | // takeinControlPacket() in ccrtp's control.cpp. |
| 475 | // |
| 476 | setOutQueueCryptoContext(senderCryptoContext); |
| 477 | setOutQueueCryptoContextCtrl(senderCryptoContextCtrl); |
| 478 | } |
| 479 | if (part == ForReceiver) { |
| 480 | // To decrypt packets: intiator uses responder keys, |
| 481 | // responder initiator keys |
| 482 | // See comment above. |
| 483 | if (secrets->role == Initiator) { |
| 484 | recvCryptoContext = new CryptoContext( |
| 485 | 0, |
| 486 | 0, |
| 487 | 0L, // keyderivation << 48, |
| 488 | cipher, // encryption algo |
| 489 | authn, // authtentication algo |
| 490 | (unsigned char*)secrets->keyResponder, // Master Key |
| 491 | secrets->respKeyLen / 8, // Master Key length |
| 492 | (unsigned char*)secrets->saltResponder, // Master Salt |
| 493 | secrets->respSaltLen / 8, // Master Salt length |
| 494 | secrets->respKeyLen / 8, // encryption keyl |
| 495 | authKeyLen, // authentication key len |
| 496 | secrets->respSaltLen / 8, // session salt len |
| 497 | secrets->srtpAuthTagLen / 8); // authentication tag len |
| 498 | recvCryptoContextCtrl = new CryptoContextCtrl(0, |
| 499 | cipher, // encryption algo |
| 500 | authn, // authtication algo |
| 501 | (unsigned char*)secrets->keyResponder, // Master Key |
| 502 | secrets->respKeyLen / 8, // Master Key length |
| 503 | (unsigned char*)secrets->saltResponder, // Master Salt |
| 504 | secrets->respSaltLen / 8, // Master Salt length |
| 505 | secrets->respKeyLen / 8, // encryption keyl |
| 506 | authKeyLen, // authentication key len |
| 507 | secrets->respSaltLen / 8, // session salt len |
| 508 | secrets->srtpAuthTagLen / 8); // authentication tag len |
| 509 | |
| 510 | } |
| 511 | else { |
| 512 | recvCryptoContext = new CryptoContext( |
| 513 | 0, |
| 514 | 0, |
| 515 | 0L, // keyderivation << 48, |
| 516 | cipher, // encryption algo |
| 517 | authn, // authtentication algo |
| 518 | (unsigned char*)secrets->keyInitiator, // Master Key |
| 519 | secrets->initKeyLen / 8, // Master Key length |
| 520 | (unsigned char*)secrets->saltInitiator, // Master Salt |
| 521 | secrets->initSaltLen / 8, // Master Salt length |
| 522 | secrets->initKeyLen / 8, // encryption keyl |
| 523 | authKeyLen, // authentication key len |
| 524 | secrets->initSaltLen / 8, // session salt len |
| 525 | secrets->srtpAuthTagLen / 8); // authentication tag len |
| 526 | recvCryptoContextCtrl = new CryptoContextCtrl(0, |
| 527 | cipher, // encryption algo |
| 528 | authn, // authtication algo |
| 529 | (unsigned char*)secrets->keyInitiator, // Master Key |
| 530 | secrets->initKeyLen / 8, // Master Key length |
| 531 | (unsigned char*)secrets->saltInitiator, // Master Salt |
| 532 | secrets->initSaltLen / 8, // Master Salt length |
| 533 | secrets->initKeyLen / 8, // encryption keyl |
| 534 | authKeyLen, // authentication key len |
| 535 | secrets->initSaltLen / 8, // session salt len |
| 536 | secrets->srtpAuthTagLen / 8); // authentication tag len |
| 537 | } |
| 538 | if (recvCryptoContext == NULL) { |
| 539 | return false; |
| 540 | } |
| 541 | // Insert the Crypto templates (SSRC == 0) into the queue. When we receive |
| 542 | // the first RTP or RTCP packet the real crypto context will be created. |
| 543 | // Refer to rtpDataPacket() above and takeinControlPacket in ccrtp's control.cpp. |
| 544 | // |
| 545 | setInQueueCryptoContext(recvCryptoContext); |
| 546 | setInQueueCryptoContextCtrl(recvCryptoContextCtrl); |
| 547 | } |
| 548 | return true; |
| 549 | } |
| 550 | |
| 551 | void ZrtpQueue::srtpSecretsOn(std::string c, std::string s, bool verified) |
| 552 | { |
| 553 | |
| 554 | if (zrtpUserCallback != NULL) { |
| 555 | zrtpUserCallback->secureOn(c); |
| 556 | if (!s.empty()) { |
| 557 | zrtpUserCallback->showSAS(s, verified); |
| 558 | } |
| 559 | } |
| 560 | } |
| 561 | |
| 562 | void ZrtpQueue::srtpSecretsOff(EnableSecurity part) { |
| 563 | if (part == ForSender) { |
| 564 | removeOutQueueCryptoContext(NULL); |
| 565 | removeOutQueueCryptoContextCtrl(NULL); |
| 566 | } |
| 567 | if (part == ForReceiver) { |
| 568 | removeInQueueCryptoContext(NULL); |
| 569 | removeInQueueCryptoContextCtrl(NULL); |
| 570 | } |
| 571 | if (zrtpUserCallback != NULL) { |
| 572 | zrtpUserCallback->secureOff(); |
| 573 | } |
| 574 | } |
| 575 | |
| 576 | int32_t ZrtpQueue::activateTimer(int32_t time) { |
| 577 | std::string s("ZRTP"); |
| 578 | if (staticTimeoutProvider != NULL) { |
| 579 | staticTimeoutProvider->requestTimeout(time, this, s); |
| 580 | } |
| 581 | return 1; |
| 582 | } |
| 583 | |
| 584 | int32_t ZrtpQueue::cancelTimer() { |
| 585 | std::string s("ZRTP"); |
| 586 | if (staticTimeoutProvider != NULL) { |
| 587 | staticTimeoutProvider->cancelRequest(this, s); |
| 588 | } |
| 589 | return 1; |
| 590 | } |
| 591 | |
| 592 | void ZrtpQueue::handleTimeout(const std::string &c) { |
| 593 | if (zrtpEngine != NULL) { |
| 594 | zrtpEngine->processTimeout(); |
| 595 | } |
| 596 | } |
| 597 | |
| 598 | void ZrtpQueue::handleGoClear() |
| 599 | { |
| 600 | fprintf(stderr, "Need to process a GoClear message!"); |
| 601 | } |
| 602 | |
| 603 | void ZrtpQueue::sendInfo(MessageSeverity severity, int32_t subCode) { |
| 604 | if (zrtpUserCallback != NULL) { |
| 605 | zrtpUserCallback->showMessage(severity, subCode); |
| 606 | } |
| 607 | } |
| 608 | |
| 609 | void ZrtpQueue::zrtpNegotiationFailed(MessageSeverity severity, int32_t subCode) { |
| 610 | if (zrtpUserCallback != NULL) { |
| 611 | zrtpUserCallback->zrtpNegotiationFailed(severity, subCode); |
| 612 | } |
| 613 | } |
| 614 | |
| 615 | void ZrtpQueue::zrtpNotSuppOther() { |
| 616 | if (zrtpUserCallback != NULL) { |
| 617 | zrtpUserCallback->zrtpNotSuppOther(); |
| 618 | } |
| 619 | } |
| 620 | |
| 621 | void ZrtpQueue::synchEnter() { |
| 622 | synchLock.enter(); |
| 623 | } |
| 624 | |
| 625 | void ZrtpQueue::synchLeave() { |
| 626 | synchLock.leave(); |
| 627 | } |
| 628 | |
| 629 | void ZrtpQueue::zrtpAskEnrollment(GnuZrtpCodes::InfoEnrollment info) { |
| 630 | if (zrtpUserCallback != NULL) { |
| 631 | zrtpUserCallback->zrtpAskEnrollment(info); |
| 632 | } |
| 633 | } |
| 634 | |
| 635 | void ZrtpQueue::zrtpInformEnrollment(GnuZrtpCodes::InfoEnrollment info) { |
| 636 | if (zrtpUserCallback != NULL) { |
| 637 | zrtpUserCallback->zrtpInformEnrollment(info); |
| 638 | } |
| 639 | } |
| 640 | |
| 641 | void ZrtpQueue::signSAS(uint8_t* sasHash) { |
| 642 | if (zrtpUserCallback != NULL) { |
| 643 | zrtpUserCallback->signSAS(sasHash); |
| 644 | } |
| 645 | } |
| 646 | |
| 647 | bool ZrtpQueue::checkSASSignature(uint8_t* sasHash) { |
| 648 | if (zrtpUserCallback != NULL) { |
| 649 | return zrtpUserCallback->checkSASSignature(sasHash); |
| 650 | } |
| 651 | return false; |
| 652 | } |
| 653 | |
| 654 | void ZrtpQueue::setEnableZrtp(bool onOff) { |
| 655 | enableZrtp = onOff; |
| 656 | } |
| 657 | |
| 658 | bool ZrtpQueue::isEnableZrtp() { |
| 659 | return enableZrtp; |
| 660 | } |
| 661 | |
| 662 | void ZrtpQueue::SASVerified() { |
| 663 | if (zrtpEngine != NULL) |
| 664 | zrtpEngine->SASVerified(); |
| 665 | } |
| 666 | |
| 667 | void ZrtpQueue::resetSASVerified() { |
| 668 | if (zrtpEngine != NULL) |
| 669 | zrtpEngine->resetSASVerified(); |
| 670 | } |
| 671 | |
| 672 | void ZrtpQueue::goClearOk() { } |
| 673 | |
| 674 | void ZrtpQueue::requestGoClear() { } |
| 675 | |
| 676 | void ZrtpQueue::setAuxSecret(uint8* data, int32_t length) { |
| 677 | if (zrtpEngine != NULL) |
| 678 | zrtpEngine->setAuxSecret(data, length); |
| 679 | } |
| 680 | |
| 681 | void ZrtpQueue::setUserCallback(ZrtpUserCallback* ucb) { |
| 682 | zrtpUserCallback = ucb; |
| 683 | } |
| 684 | |
| 685 | void ZrtpQueue::setClientId(std::string id) { |
| 686 | clientIdString = id; |
| 687 | } |
| 688 | |
Alexandre Lision | 7fd5d3d | 2013-12-04 13:06:40 -0500 | [diff] [blame] | 689 | std::string ZrtpQueue::getHelloHash(int32_t index) { |
Alexandre Lision | 51140e1 | 2013-12-02 10:54:09 -0500 | [diff] [blame] | 690 | if (zrtpEngine != NULL) |
Alexandre Lision | 7fd5d3d | 2013-12-04 13:06:40 -0500 | [diff] [blame] | 691 | return zrtpEngine->getHelloHash(index); |
Alexandre Lision | 51140e1 | 2013-12-02 10:54:09 -0500 | [diff] [blame] | 692 | else |
| 693 | return std::string(); |
| 694 | } |
| 695 | |
| 696 | std::string ZrtpQueue::getPeerHelloHash() { |
| 697 | if (zrtpEngine != NULL) |
| 698 | return zrtpEngine->getPeerHelloHash(); |
| 699 | else |
| 700 | return std::string(); |
| 701 | } |
| 702 | |
| 703 | std::string ZrtpQueue::getMultiStrParams() { |
| 704 | if (zrtpEngine != NULL) |
| 705 | return zrtpEngine->getMultiStrParams(); |
| 706 | else |
| 707 | return std::string(); |
| 708 | } |
| 709 | |
| 710 | void ZrtpQueue::setMultiStrParams(std::string parameters) { |
| 711 | if (zrtpEngine != NULL) |
| 712 | zrtpEngine->setMultiStrParams(parameters); |
| 713 | } |
| 714 | |
| 715 | bool ZrtpQueue::isMultiStream() { |
| 716 | if (zrtpEngine != NULL) |
| 717 | return zrtpEngine->isMultiStream(); |
| 718 | return false; |
| 719 | } |
| 720 | |
| 721 | bool ZrtpQueue::isMultiStreamAvailable() { |
| 722 | if (zrtpEngine != NULL) |
| 723 | return zrtpEngine->isMultiStreamAvailable(); |
| 724 | return false; |
| 725 | } |
| 726 | |
| 727 | void ZrtpQueue::acceptEnrollment(bool accepted) { |
| 728 | if (zrtpEngine != NULL) |
| 729 | zrtpEngine->acceptEnrollment(accepted); |
| 730 | } |
| 731 | |
| 732 | std::string ZrtpQueue::getSasType() { |
| 733 | if (zrtpEngine != NULL) |
| 734 | return zrtpEngine->getSasType(); |
| 735 | else |
| 736 | return NULL; |
| 737 | } |
| 738 | |
| 739 | uint8_t* ZrtpQueue::getSasHash() { |
| 740 | if (zrtpEngine != NULL) |
| 741 | return zrtpEngine->getSasHash(); |
| 742 | else |
| 743 | return NULL; |
| 744 | } |
| 745 | |
| 746 | bool ZrtpQueue::sendSASRelayPacket(uint8_t* sh, std::string render) { |
| 747 | |
| 748 | if (zrtpEngine != NULL) |
| 749 | return zrtpEngine->sendSASRelayPacket(sh, render); |
| 750 | else |
| 751 | return false; |
| 752 | } |
| 753 | |
| 754 | bool ZrtpQueue::isMitmMode() { |
| 755 | return mitmMode; |
| 756 | } |
| 757 | |
| 758 | void ZrtpQueue::setMitmMode(bool mitmMode) { |
| 759 | this->mitmMode = mitmMode; |
| 760 | } |
| 761 | |
| 762 | bool ZrtpQueue::isEnrollmentMode() { |
| 763 | if (zrtpEngine != NULL) |
| 764 | return zrtpEngine->isEnrollmentMode(); |
| 765 | else |
| 766 | return false; |
| 767 | } |
| 768 | |
| 769 | void ZrtpQueue::setEnrollmentMode(bool enrollmentMode) { |
| 770 | if (zrtpEngine != NULL) |
| 771 | zrtpEngine->setEnrollmentMode(enrollmentMode); |
| 772 | } |
| 773 | |
| 774 | void ZrtpQueue::setParanoidMode(bool yesNo) { |
| 775 | enableParanoidMode = yesNo; |
| 776 | } |
| 777 | |
| 778 | bool ZrtpQueue::isParanoidMode() { |
| 779 | return enableParanoidMode; |
| 780 | } |
| 781 | |
| 782 | bool ZrtpQueue::isPeerEnrolled() { |
| 783 | if (zrtpEngine != NULL) |
| 784 | return zrtpEngine->isPeerEnrolled(); |
| 785 | else |
| 786 | return false; |
| 787 | } |
| 788 | |
| 789 | void ZrtpQueue::setSignSas(bool sasSignMode) { |
| 790 | signSas = sasSignMode; |
| 791 | } |
| 792 | |
| 793 | bool ZrtpQueue::setSignatureData(uint8* data, int32 length) { |
| 794 | if (zrtpEngine != NULL) |
| 795 | return zrtpEngine->setSignatureData(data, length); |
| 796 | return 0; |
| 797 | } |
| 798 | |
| 799 | const uint8* ZrtpQueue::getSignatureData() { |
| 800 | if (zrtpEngine != NULL) |
| 801 | return zrtpEngine->getSignatureData(); |
| 802 | return 0; |
| 803 | } |
| 804 | |
| 805 | int32 ZrtpQueue::getSignatureLength() { |
| 806 | if (zrtpEngine != NULL) |
| 807 | return zrtpEngine->getSignatureLength(); |
| 808 | return 0; |
| 809 | } |
| 810 | |
| 811 | int32 ZrtpQueue::getPeerZid(uint8* data) { |
| 812 | if (data == NULL) |
| 813 | return 0; |
| 814 | |
| 815 | if (zrtpEngine != NULL) |
| 816 | return zrtpEngine->getPeerZid(data); |
| 817 | |
| 818 | return 0; |
| 819 | } |
| 820 | |
Alexandre Lision | 7fd5d3d | 2013-12-04 13:06:40 -0500 | [diff] [blame] | 821 | int32_t ZrtpQueue::getNumberSupportedVersions() { |
| 822 | if (zrtpEngine != NULL) |
| 823 | return zrtpEngine->getNumberSupportedVersions(); |
| 824 | |
| 825 | return 0; |
| 826 | } |
| 827 | |
| 828 | int32_t ZrtpQueue::getCurrentProtocolVersion() { |
| 829 | if (zrtpEngine != NULL) |
| 830 | return zrtpEngine->getCurrentProtocolVersion(); |
| 831 | |
| 832 | return 0; |
| 833 | } |
| 834 | |
| 835 | |
Alexandre Lision | 51140e1 | 2013-12-02 10:54:09 -0500 | [diff] [blame] | 836 | IncomingZRTPPkt::IncomingZRTPPkt(const unsigned char* const block, size_t len) : |
| 837 | IncomingRTPPkt(block,len) { |
| 838 | } |
| 839 | |
| 840 | uint32 IncomingZRTPPkt::getZrtpMagic() const { |
| 841 | return ntohl(getHeader()->timestamp); |
| 842 | } |
| 843 | |
| 844 | uint32 IncomingZRTPPkt::getSSRC() const { |
| 845 | return ntohl(getHeader()->sources[0]); |
| 846 | } |
| 847 | |
| 848 | OutgoingZRTPPkt::OutgoingZRTPPkt( |
| 849 | const unsigned char* const hdrext, uint32 hdrextlen) : |
| 850 | OutgoingRTPPkt(NULL, 0, hdrext, hdrextlen, NULL ,0, 0, NULL) |
| 851 | { |
| 852 | getHeader()->version = 0; |
| 853 | getHeader()->timestamp = htonl(ZRTP_MAGIC); |
| 854 | } |
| 855 | |
| 856 | END_NAMESPACE |
| 857 | |
| 858 | /** EMACS ** |
| 859 | * Local variables: |
| 860 | * mode: c++ |
| 861 | * c-default-style: ellemtel |
| 862 | * c-basic-offset: 4 |
| 863 | * End: |
| 864 | */ |
| 865 | |