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