mirror of
https://github.com/ossrs/srs.git
synced 2025-03-09 15:49:59 +00:00
add HLS encryption feature
This commit is contained in:
parent
0d78b908a7
commit
52596a0b04
8 changed files with 378 additions and 11 deletions
|
@ -2607,6 +2607,65 @@ SrsVideoCodecId SrsTsContextWriter::video_codec()
|
|||
return vcodec;
|
||||
}
|
||||
|
||||
srs_error_t SrsEncFileWriter::write(void* buf, size_t count, ssize_t* pnwrite)
|
||||
{
|
||||
|
||||
srs_assert(count == SRS_TS_PACKET_SIZE);
|
||||
srs_error_t err = srs_success;
|
||||
|
||||
if(buflength != HLS_AES_ENCRYPT_BLOCK_LENGTH)
|
||||
{
|
||||
memcpy(tmpbuf+buflength,(char*)buf,SRS_TS_PACKET_SIZE);
|
||||
buflength += SRS_TS_PACKET_SIZE;
|
||||
}
|
||||
if(buflength == HLS_AES_ENCRYPT_BLOCK_LENGTH)
|
||||
{
|
||||
unsigned char encryptedbuf[HLS_AES_ENCRYPT_BLOCK_LENGTH];
|
||||
memset(encryptedbuf,0,HLS_AES_ENCRYPT_BLOCK_LENGTH);
|
||||
AES_cbc_encrypt((unsigned char *)tmpbuf, (unsigned char *)encryptedbuf, HLS_AES_ENCRYPT_BLOCK_LENGTH, &key, iv, AES_ENCRYPT);
|
||||
buflength = 0;
|
||||
memset(tmpbuf,0,HLS_AES_ENCRYPT_BLOCK_LENGTH);
|
||||
return SrsFileWriter::write(encryptedbuf,HLS_AES_ENCRYPT_BLOCK_LENGTH,pnwrite);
|
||||
}
|
||||
else
|
||||
{
|
||||
return err;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
srs_error_t SrsEncFileWriter::SetEncCfg(unsigned char* keyval,unsigned char *ivval)
|
||||
{
|
||||
|
||||
srs_error_t err = srs_success;
|
||||
|
||||
if (AES_set_encrypt_key(keyval, 16*8, &key))
|
||||
{
|
||||
return srs_error_new(ERROR_SYSTEM_FILE_WRITE, "set aes key failed");
|
||||
}
|
||||
|
||||
memcpy(iv,ivval,16);
|
||||
return err;
|
||||
}
|
||||
|
||||
void SrsEncFileWriter::close()
|
||||
{
|
||||
if(buflength > 0)
|
||||
{
|
||||
int addBytes = 16 - buflength % 16;
|
||||
memset(tmpbuf + buflength, addBytes, addBytes);
|
||||
unsigned char encryptedbuf[buflength+addBytes];
|
||||
memset(encryptedbuf,0,buflength+addBytes);
|
||||
AES_cbc_encrypt((unsigned char *)tmpbuf, (unsigned char *)encryptedbuf, buflength+addBytes, &key, iv, AES_ENCRYPT);
|
||||
SrsFileWriter::write(encryptedbuf,buflength+addBytes,NULL);
|
||||
|
||||
buflength = 0;
|
||||
memset(tmpbuf,0,HLS_AES_ENCRYPT_BLOCK_LENGTH);
|
||||
}
|
||||
SrsFileWriter::close();
|
||||
}
|
||||
|
||||
|
||||
SrsTsMessageCache::SrsTsMessageCache()
|
||||
{
|
||||
audio = NULL;
|
||||
|
|
|
@ -31,8 +31,11 @@
|
|||
#include <string>
|
||||
#include <map>
|
||||
#include <vector>
|
||||
#include <openssl/aes.h>
|
||||
|
||||
#include <srs_kernel_codec.hpp>
|
||||
#include <srs_kernel_file.hpp>
|
||||
|
||||
|
||||
class SrsBuffer;
|
||||
class SrsTsMessageCache;
|
||||
|
@ -1571,6 +1574,36 @@ public:
|
|||
virtual SrsVideoCodecId video_codec();
|
||||
};
|
||||
|
||||
/*
|
||||
* Used for HLS Encryption
|
||||
*/
|
||||
|
||||
#define HLS_AES_ENCRYPT_BLOCK_LENGTH 188*4
|
||||
|
||||
static char tmpbuf[HLS_AES_ENCRYPT_BLOCK_LENGTH] = {0};
|
||||
static int buflength = 0;
|
||||
|
||||
|
||||
class SrsEncFileWriter: public SrsFileWriter
|
||||
{
|
||||
public:
|
||||
SrsEncFileWriter()
|
||||
{
|
||||
memset(iv,0,16);
|
||||
}
|
||||
virtual ~SrsEncFileWriter(){}
|
||||
|
||||
virtual srs_error_t write(void* buf, size_t count, ssize_t* pnwrite);
|
||||
|
||||
srs_error_t SetEncCfg(unsigned char* key,unsigned char *iv);
|
||||
|
||||
virtual void close();
|
||||
|
||||
private:
|
||||
AES_KEY key;
|
||||
unsigned char iv[16];
|
||||
};
|
||||
|
||||
/**
|
||||
* TS messages cache, to group frames to TS message,
|
||||
* for example, we may write multiple AAC RAW frames to a TS message.
|
||||
|
|
|
@ -1050,6 +1050,27 @@ int srs_do_create_dir_recursively(string dir)
|
|||
}
|
||||
return c;
|
||||
}
|
||||
|
||||
char *ff_data_to_hex(char *buff, const uint8_t *src, int s, int lowercase)
|
||||
{
|
||||
int i;
|
||||
static const char hex_table_uc[16] = { '0', '1', '2', '3',
|
||||
'4', '5', '6', '7',
|
||||
'8', '9', 'A', 'B',
|
||||
'C', 'D', 'E', 'F' };
|
||||
static const char hex_table_lc[16] = { '0', '1', '2', '3',
|
||||
'4', '5', '6', '7',
|
||||
'8', '9', 'a', 'b',
|
||||
'c', 'd', 'e', 'f' };
|
||||
const char *hex_table = lowercase ? hex_table_lc : hex_table_uc;
|
||||
|
||||
for (i = 0; i < s; i++) {
|
||||
buff[i * 2] = hex_table[src[i] >> 4];
|
||||
buff[i * 2 + 1] = hex_table[src[i] & 0xF];
|
||||
}
|
||||
|
||||
return buff;
|
||||
}
|
||||
|
||||
int ff_hex_to_data(uint8_t* data, const char* p)
|
||||
{
|
||||
|
|
|
@ -181,6 +181,10 @@ extern char* srs_av_base64_encode(char* out, int out_size, const uint8_t* in, in
|
|||
* output hex to data={0x13, 0x90, 0x56, 0xe5, 0xa0}
|
||||
*/
|
||||
extern int ff_hex_to_data(uint8_t* data, const char* p);
|
||||
/**
|
||||
* convert data string to hex.
|
||||
*/
|
||||
extern char *ff_data_to_hex(char *buff, const uint8_t *src, int s, int lowercase);
|
||||
|
||||
/**
|
||||
* generate the c0 chunk header for msg.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue