build an encrypted data or alert message for output
| 333 | |
| 334 | // build an encrypted data or alert message for output |
| 335 | void buildMessage(SSL& ssl, output_buffer& output, const Message& msg) |
| 336 | { |
| 337 | uint digestSz = ssl.getCrypto().get_digest().get_digestSize(); |
| 338 | uint sz = RECORD_HEADER + msg.get_length() + digestSz; |
| 339 | uint pad = 0; |
| 340 | uint blockSz = ssl.getCrypto().get_cipher().get_blockSize(); |
| 341 | |
| 342 | if (ssl.getSecurity().get_parms().cipher_type_ == block) { |
| 343 | if (ssl.isTLSv1_1()) // IV |
| 344 | sz += blockSz; |
| 345 | sz += 1; // pad byte |
| 346 | pad = (sz - RECORD_HEADER) % blockSz; |
| 347 | pad = blockSz - pad; |
| 348 | sz += pad; |
| 349 | } |
| 350 | |
| 351 | RecordLayerHeader rlHeader; |
| 352 | buildHeader(ssl, rlHeader, msg); |
| 353 | rlHeader.length_ = sz - RECORD_HEADER; // record header includes mac |
| 354 | // and pad, hanshake doesn't |
| 355 | input_buffer iv; |
| 356 | if (ssl.isTLSv1_1() && ssl.getSecurity().get_parms().cipher_type_== block){ |
| 357 | iv.allocate(blockSz); |
| 358 | ssl.getCrypto().get_random().Fill(iv.get_buffer(), blockSz); |
| 359 | iv.add_size(blockSz); |
| 360 | } |
| 361 | |
| 362 | uint ivSz = iv.get_size(); |
| 363 | output.allocate(sz); |
| 364 | output << rlHeader << iv << msg; |
| 365 | |
| 366 | opaque digest[SHA_LEN]; // max size |
| 367 | if (ssl.isTLS()) |
| 368 | TLS_hmac(ssl, digest, output.get_buffer() + RECORD_HEADER + ivSz, |
| 369 | output.get_size() - RECORD_HEADER - ivSz, msg.get_type()); |
| 370 | else |
| 371 | hmac(ssl, digest, output.get_buffer() + RECORD_HEADER, |
| 372 | output.get_size() - RECORD_HEADER, msg.get_type()); |
| 373 | output.write(digest, digestSz); |
| 374 | |
| 375 | if (ssl.getSecurity().get_parms().cipher_type_ == block) |
| 376 | for (uint i = 0; i <= pad; i++) output[AUTO] = pad; // pad byte gets |
| 377 | // pad value too |
| 378 | input_buffer cipher(rlHeader.length_); |
| 379 | ssl.useCrypto().use_cipher().encrypt(cipher.get_buffer(), |
| 380 | output.get_buffer() + RECORD_HEADER, output.get_size() - RECORD_HEADER); |
| 381 | output.set_current(RECORD_HEADER); |
| 382 | output.write(cipher.get_buffer(), cipher.get_capacity()); |
| 383 | } |
| 384 | |
| 385 | |
| 386 | // build alert message |
no test coverage detected