/*! \brief Add DRM information to MPEG2 program stream. IMPEG2IPMPXDRMEncoder will use the functionality of the IMPEG2IPMPXPStreamEncoder to add DRM information. \param psEncoder input, program stream encoder. \returns Boolean indicating success or failure. */ bool MPEG2IPMPXDRMEncoder::AddDRMInfo(IMPEG2IPMPXPStreamEncoder* psEncoder) { ByteSeq cInfos; std::vector<MPEG2IPMPXDRMEncryptor*>::iterator iter = encryptors.begin(); // First we create all the IPMP control infos of all encryptors. while (iter != encryptors.end()) { ByteSeq cInfo; if ((*iter)->CreateControlInfo(cInfo) == false) { return false; } cInfos += cInfo; iter++; } // Now we must create correctly sized control info parts. /* First we must calculate the size of signature/certificate part, if we want to sign control info classes. Size is given by the following formula: size = 1 (signed flag) + signature size + (certificate type + certificate size of all certificates) + 16 (verifying tool ID). */ int sigCertSize = 0; if (false) { // We're signing control info classes. sigCertSize = 1 + 128 + 1024 + 16; } else { // We're not signing control info classes. sigCertSize = 0; } /* Now we need to calculate size of the control info part size without control info classes and signature/certificate part. Size is given by the following formula: cInfoSize = 3 (packet start code prefix) + 1 (stream id) + 2 (packet length) + 2 (constant + PES extension flag) + 1 (PES header data length) + 1 (constant + PES extension flag 2) + 1 (constant) + 1 (stream id extension flag + stream id extension) + 1 (IPMP control info version + current next indicator + reserved) + 1 (IPMP control info packet number) + 1 (last packet number) + 2 (control info classes length in a part) + 4 (CRC size). */ int cInfoSize = 21; // Maximal part size. int maxSize = ((1 << 16) - 1); /* Since we need to set last part number, we must save all the created parts pointers, cause we don't know the last part number till we're done with packing. We shall also add CRCs then, cause we cannot calculate them without last part number. */ std::vector<IInterPStreamIPMPControlInfoPart*> interParts; ByteT partNumber = 0; UInt16T totalLen = (UInt16T)cInfos.GetLength(); while ((maxSize - sigCertSize - cInfoSize - (long int)cInfos.GetLength()) < 0) { // Add new intermediate part. IInterPStreamIPMPControlInfoPart* interPart; if (psEncoder->CreateInterIPMPControlInfoPart(&interPart) == false) { return false; } if (interPart->SetPacketStartCodePrefix(0x000001) == false) { return false; } if (interPart->SetStreamID(0xfd) == false) { return false; } if (interPart->SetPESExtensionFlag(true) == false) { return false; } if (interPart->SetPESExtensionFlag2(true) == false) { return false; } if (interPart->SetStreamIDExtensionFlag(true) == false) { return false; } // Check this! if (interPart->SetStreamIDExtension(0x0) == false) { return false; } if (interPart->SetIPMPControlInfoVersion(0x0) == false) { return false; } if (interPart->SetCurrentNextIndicator(true) == false) { return false; } if (interPart->SetPartNumber(partNumber++) == false) { return false; } ByteSeq tmp = cInfos.GetBytes(maxSize - cInfoSize); if (interPart->SetIPMPControlInfos(tmp) == false) { return false; } } /* Now we only have to add last part, so we can set last part number for all intermediate parts, and calculate CRCs. */ std::vector<IInterPStreamIPMPControlInfoPart*>::iterator interIter = interParts.begin(); while (interIter != interParts.end()) { if ((*interIter)->SetLastPartNumber(partNumber) == false) { return false; } ByteSeq encoded; if ((*interIter)->MPEG2Encode(encoded) == false) { return false; } CRC32Calculator calc; UInt32T crc = calc.GetCRC(encoded.GetFirst(), encoded.GetLength()); if ((*interIter)->SetCRC32Code(crc) == false) { return false; } interIter++; } // Add last part. ILastPStreamIPMPControlInfoPart* lastPart; if (psEncoder->CreateLastIPMPControlInfoPart(&lastPart) == false) { return false; } if (lastPart->SetPacketStartCodePrefix(0x000001) == false) { return false; } if (lastPart->SetStreamID(0xfd) == false) { return false; } if (lastPart->SetPESExtensionFlag(true) == false) { return false; } if (lastPart->SetPESExtensionFlag2(true) == false) { return false; } if (lastPart->SetStreamIDExtensionFlag(true) == false) { return false; } // Check this! if (lastPart->SetStreamIDExtension(0x0) == false) { return false; } if (lastPart->SetIPMPControlInfoVersion(0x0) == false) { return false; } if (lastPart->SetCurrentNextIndicator(true) == false) { return false; } if (lastPart->SetPartNumber(partNumber) == false) { return false; } if (lastPart->SetIPMPControlInfos(cInfos) == false) { return false; } // Add CRC code. ByteSeq encoded; if (lastPart->MPEG2Encode(encoded) == false) { return false; } CRC32Calculator calc; UInt32T crc = calc.GetCRC(encoded.GetFirst(), encoded.GetLength()); if (lastPart->SetCRC32Code(crc) == false) { return false; } return true; }
/*! \brief Parse key descriptor. \warning Encoded data is modified during parsing. \warning In case of an error, throws either ByteSeqException or IPMPDataException. \param encoded input, encoded data. \returns Key descriptor. */ KeyDescriptor IPMPDataParser::ParseKeyDescriptor(ByteSeq& encoded) { ByteSeq keyBody = encoded.GetBytes((UInt32T)(encoded.GetSizeOfInstance(0))); return KeyDescriptor(keyBody); }
/*! \brief Add DRM information to MPEG2 transport stream. IMPEG2IPMPXDRMEncoder will use the functionality of the IMPEG2IPMPXTStreamEncoder to add DRM information. \param tsEncoder input, transport stream encoder. \returns Boolean indicating success or failure. */ bool MPEG2IPMPXDRMEncoder::AddDRMInfo(IMPEG2IPMPXTStreamEncoder* tsEncoder) { ByteSeq cInfos; std::vector<MPEG2IPMPXDRMEncryptor*>::iterator iter = encryptors.begin(); // First we create all the IPMP control infos of all encryptors. while (iter != encryptors.end()) { ByteSeq cInfo; if ((*iter)->CreateControlInfo(cInfo) == false) { return false; } cInfos += cInfo; iter++; } /* Check if 16-bit value is enough for holding the control infos. If not, then it's an error. */ if (cInfos.GetLength() >= (1 << 16)) { return false; } // Now we must create correctly sized control info parts. /* First we must calculate the size of signature/certificate part, if we want to sign control info classes. Size is given by the following formula: size = 1 (signed flag) + signature size + (certificate type + certificate size of all certificates) + 16 (verifying tool ID). */ int sigCertSize = 0; if (false) { // We're signing control info classes. sigCertSize = 1 + 128 + 1024 + 16; } else { // We're not signing control info classes. sigCertSize = 0; } /* Now we need to calculate size of the control info part size without control info classes and signature/certificate part. Size is given by the following formula: cInfoSize = 1 (table ID) + 2 (section syntax indicator + 0 + reserved + section length) + 1 (reserved + control info version + current next indicator) + 1 (section number) + 1 + (last section number) + 2 (control info classes total length) + 2 (control info classes length in a part) + 4 (CRC size). */ int cInfoSize = 14; // Maximal part size. int maxSize = 4093; /* Since we need to set last part number, we must save all the created parts pointers, cause we don't know the last part number till we're done with packing. We shall also add CRCs then, cause we cannot calculate them without last part number. */ std::vector<IInterTStreamIPMPControlInfoPart*> interParts; ByteT partNumber = 0; UInt16T totalLen = (UInt16T)cInfos.GetLength(); while ((maxSize - sigCertSize - cInfoSize - (long int)cInfos.GetLength()) < 0) { // Add new intermediate part. IInterTStreamIPMPControlInfoPart* interPart; if (tsEncoder->CreateInterIPMPControlInfoPart(&interPart) == false) { return false; } if (interPart->SetTableID(0x07) == false) { return false; } if (interPart->SetSectionSyntaxIndicator(true) == false) { return false; } if (interPart->SetIPMPControlInfoVersion(0x0) == false) { return false; } if (interPart->SetCurrentNextIndicator(true) == false) { return false; } if (interPart->SetPartNumber(partNumber++) == false) { return false; } if (interPart->SetTotalControlInfoClassesLen(totalLen) == false) { return false; } ByteSeq tmp = cInfos.GetBytes(maxSize - cInfoSize); if (interPart->SetIPMPControlInfos(tmp) == false) { return false; } } /* Now we only have to add last part, so we can set last part number for all intermediate parts, and calculate CRCs. */ std::vector<IInterTStreamIPMPControlInfoPart*>::iterator interIter = interParts.begin(); while (interIter != interParts.end()) { if ((*interIter)->SetLastPartNumber(partNumber) == false) { return false; } ByteSeq encoded; if ((*interIter)->MPEG2Encode(encoded) == false) { return false; } CRC32Calculator calc; UInt32T crc = calc.GetCRC(encoded.GetFirst(), encoded.GetLength()); if ((*interIter)->SetCRC32Code(crc) == false) { return false; } interIter++; } // Add last part. ILastTStreamIPMPControlInfoPart* lastPart; if (tsEncoder->CreateLastIPMPControlInfoPart(&lastPart) == false) { return false; } if (lastPart->SetTableID(0x07) == false) { return false; } if (lastPart->SetSectionSyntaxIndicator(true) == false) { return false; } if (lastPart->SetIPMPControlInfoVersion(0x0) == false) { return false; } if (lastPart->SetCurrentNextIndicator(true) == false) { return false; } if (lastPart->SetPartNumber(partNumber) == false) { return false; } if (lastPart->SetTotalControlInfoClassesLen(totalLen) == false) { return false; } if (lastPart->SetIPMPControlInfos(cInfos) == false) { return false; } // Add CRC code. ByteSeq encoded; if (lastPart->MPEG2Encode(encoded) == false) { return false; } CRC32Calculator calc; UInt32T crc = calc.GetCRC(encoded.GetFirst(), encoded.GetLength()); if (lastPart->SetCRC32Code(crc) == false) { return false; } return true; }
/*! \brief Parse IPMP data message, call handling function and return it's response. \warning Encoded data is modified during parsing. \warning Response can be empty, depending on whether any response is needed and whether an error occured during the processing. \param sender input, sender's code. \param encoded input, encoded message. \param agent input, IPMP agent which will handle parsed message. \param resp output, response message data. \returns Boolean indicating success or failure. */ bool IPMPDataParser::ParseMessage(const Bit32T& sender, ByteSeq& encoded, IPMPAgent* agent, ByteSeq& resp) { try { ByteT tag = encoded.GetByte(); ByteSeq data(encoded.GetBytes((UInt32T)(encoded.GetSizeOfInstance(0xfffffff)))); switch (tag) { case 0x01: // Opaque data. if (agent->HandleOpaqueIPMPData(sender, ParseOpaqueData(data), resp) == false) { return false; } break; case 0x08: // Rights data. if (agent->HandleRightsIPMPData(sender, ParseRightsData(data), resp) == false) { return false; } break; case 0x09: // Secure container. if (agent->HandleSecureContainerIPMPData(sender, ParseSecureContainer(data), resp) == false) { return false; } break; case 0x0c: // Init authentication. if (agent->HandleInitAuthenticationIPMPData(sender, ParseInitAuthentication(data), resp) == false) { return false; } break; case 0x0d: // Mutual authentication. if (agent->HandleMutualAuthenticationIPMPData(sender, ParseMutualAuthentication(data), resp) == false) { return false; } break; case 0x17: // Can process. if (agent->HandleCanProcessIPMPData(sender, ParseCanProcess(data), resp) == false) { return false; } break; case 0x0e: // User query. if (agent->HandleUserQueryIPMPData(sender, ParseUserQueryData(data), resp) == false) { return false; } break; case 0x0f: // User response. if (agent->HandleUserQueryResponseIPMPData(sender, ParseUserQueryResponseData(data), resp) == false) { return false; } break; default: return false; } return true; } catch (ByteSeqException) { return false; } catch (IPMPDataException) { return false; } catch (IPMPDataParserException) { return false; } // catch (...) { // return false; // } }