HuaWei rtp

This commit is contained in:
AlanRen 2023-03-14 04:30:54 -06:00
parent 1be373198a
commit f1f584cf55
45 changed files with 4516 additions and 411 deletions

63
.vscode/settings.json vendored
View File

@ -1,5 +1,66 @@
{
"files.associations": {
"*.tcc": "cpp"
"*.tcc": "cpp",
"functional": "cpp",
"fstream": "cpp",
"thread": "cpp",
"cctype": "cpp",
"clocale": "cpp",
"cmath": "cpp",
"cstdarg": "cpp",
"cstddef": "cpp",
"cstdio": "cpp",
"cstdlib": "cpp",
"cstring": "cpp",
"ctime": "cpp",
"cwchar": "cpp",
"cwctype": "cpp",
"array": "cpp",
"atomic": "cpp",
"strstream": "cpp",
"bitset": "cpp",
"chrono": "cpp",
"complex": "cpp",
"condition_variable": "cpp",
"cstdint": "cpp",
"deque": "cpp",
"list": "cpp",
"unordered_map": "cpp",
"vector": "cpp",
"exception": "cpp",
"algorithm": "cpp",
"iterator": "cpp",
"map": "cpp",
"memory": "cpp",
"memory_resource": "cpp",
"numeric": "cpp",
"optional": "cpp",
"random": "cpp",
"ratio": "cpp",
"set": "cpp",
"string": "cpp",
"string_view": "cpp",
"system_error": "cpp",
"tuple": "cpp",
"type_traits": "cpp",
"utility": "cpp",
"initializer_list": "cpp",
"iomanip": "cpp",
"iosfwd": "cpp",
"iostream": "cpp",
"istream": "cpp",
"limits": "cpp",
"mutex": "cpp",
"new": "cpp",
"ostream": "cpp",
"sstream": "cpp",
"stdexcept": "cpp",
"streambuf": "cpp",
"cfenv": "cpp",
"cinttypes": "cpp",
"typeindex": "cpp",
"typeinfo": "cpp",
"variant": "cpp",
"bit": "cpp"
}
}

View File

@ -24,37 +24,55 @@ set(CMAKE_C_VISIBILITY_PRESET hidden)
set(CMAKE_CXX_VISIBILITY_PRESET hidden)
set(CMAKE_C_FLAGS$ "${CMAKE_C_FLAGS} -fvisibility = hidden")
set(CMAKE_CXX_FLAGS$ "${CMAKE_CXX_FLAGS} -fvisibility = hidden")
set(SecMedia_Root ${CMAKE_CURRENT_SOURCE_DIR}/src)
add_compile_options(-fPIC)
add_compile_options(-fvisibility=hidden)
#add_compile_options(-std=c++11)
add_compile_options(-mcpu=cortex-a7 -mfloat-abi=softfp -mfpu=neon-vfpv4 -mno-unaligned-access -fno-aggressive-loop-optimizations -Wcast-align)
#add_compile_options(-mcpu=cortex-a7 -mfloat-abi=softfp -mfpu=neon-vfpv4 -mno-unaligned-access -fno-aggressive-loop-optimizations -Wcast-align)
include(GNUInstallDirs)
include(GenerateExportHeader)
include_directories(${SecMedia_Root}/SVAC/src/svac_src)
#svac
aux_source_directory(${SecMedia_Root}/SVAC/src/sm2sm3 src_DEC)
aux_source_directory(${SecMedia_Root}/SVAC/src/svac_src src_DEC)
add_library(SVAC_DEC STATIC ${src_DEC})
#svac
#include_directories(${DEC_ENC_Algorithm}/SVAC/svac_enc)
#file(GLOB src_DEC_ENC ${DEC_ENC_Algorithm}/SVAC/svac_enc/*/*.c ${DEC_ENC_Algorithm}/SVAC/svac_enc/*/*.h)
aux_source_directory(${SecMedia_Root}/SVAC/src/sm2sm3_enc src_ENC)
aux_source_directory(${SecMedia_Root}/SVAC/src/svac_src src_ENC)
add_library(SVAC_ENC STATIC ${src_ENC})
list(APPEND LINK_LIB_SVAC_LIST SVAC_DEC)
list(APPEND LINK_LIB_SVAC_LIST SVAC_ENC)
include_directories(include src)
# add_definitions(-DGENERATE_EXPORT)
set(SecMedia_Root ${CMAKE_CURRENT_SOURCE_DIR}/src)
# file(GLOB SecMedia_src_list ${SecMedia_Root}/SVAC/./*.c ${SecMedia_Root}/SVAC/./*.h)
# # file(GLOB SecMedia_src_list ${SecMedia_Root}/*/*.cpp ${SecMedia_Root}/*/*.h ${SecMedia_Root}/*/*.c ${SecMedia_Root}/*/*/*.cpp ${SecMedia_Root}/*/*/*.h ${SecMedia_Root}/*/*/*.c)
# file(GLOB SecMedia_api_list ${CMAKE_CURRENT_SOURCE_DIR}/include/common.h)
# # # target_compile_options(${PROJECT_NAME} PRIVATE -fvisibility=hidden)
#file(GLOB SecMedia_src_list ${SecMedia_Root}/SVAC/./*.c ${SecMedia_Root}/SVAC/./*.h)
file(GLOB SecMedia_src_list ${SecMedia_Root}/*/*.cpp ${SecMedia_Root}/*/*.h ${SecMedia_Root}/*/*.c ${SecMedia_Root}/*/*/*.cpp ${SecMedia_Root}/*/*/*.h ${SecMedia_Root}/*/*/*.c)
file(GLOB SecMedia_api_list ${CMAKE_CURRENT_SOURCE_DIR}/include/common.h)
# add_library(${PROJECT_NAME} SHARED ${SecMedia_src_list})
# #add_library(${PROJECT_NAME} STATIC ${SecMedia_src_list})
# target_include_directories(${PROJECT_NAME} PRIVATE ${SecMedia_Root}/.)
# # target_compile_options(${PROJECT_NAME} PRIVATE -fvisibility=hidden)
# set_target_properties(${PROJECT_NAME} PROPERTIES
# VERSION ${PROJECT_VERSION}
# SOVERSION 1
# PUBLIC_HEADER ${SecMedia_api_list}
# )
# # CXX_VISIBILITY_PRESET hidden
# # CMAKE_C_FLAGS hidden)
#add_library(${PROJECT_NAME} SHARED ${SecMedia_src_list})
add_library(${PROJECT_NAME} SHARED ${SecMedia_src_list})
target_link_libraries(${PROJECT_NAME} ${LINK_LIB_SVAC_LIST})
target_include_directories(${PROJECT_NAME} PRIVATE ${SecMedia_Root}/.)
set_target_properties(${PROJECT_NAME} PROPERTIES
VERSION ${PROJECT_VERSION}
SOVERSION 1
PUBLIC_HEADER ${SecMedia_api_list}
)
# CXX_VISIBILITY_PRESET hidden
# CMAKE_C_FLAGS hidden)
# set(LINK_LIB_LIST ${PROJECT_NAME} )
list(APPEND LINK_LIB_LIST ${PROJECT_NAME})
#set(LINK_LIB_LIST )
#MESSAGE(STATUS ${SecMedia_api_list})
#configure_file(SecMedia.pc.in SecMedia.pc @ONLY)
@ -66,19 +84,6 @@ set(SecMedia_Root ${CMAKE_CURRENT_SOURCE_DIR}/src)
include_directories(${SecMedia_Root}/SVAC/svac_src)
#svac
aux_source_directory(${SecMedia_Root}/SVAC/sm2sm3 src_DEC)
aux_source_directory(${SecMedia_Root}/SVAC/svac_src src_DEC)
add_library(SVAC_DEC STATIC ${src_DEC})
#svac
#include_directories(${DEC_ENC_Algorithm}/SVAC/svac_enc)
#file(GLOB src_DEC_ENC ${DEC_ENC_Algorithm}/SVAC/svac_enc/*/*.c ${DEC_ENC_Algorithm}/SVAC/svac_enc/*/*.h)
aux_source_directory(${SecMedia_Root}/SVAC/sm2sm3_enc src_ENC)
aux_source_directory(${SecMedia_Root}/SVAC/svac_src src_ENC)
add_library(SVAC_ENC STATIC ${src_ENC})
list(APPEND LINK_LIB_LIST SVAC_DEC)
list(APPEND LINK_LIB_LIST SVAC_ENC)
add_subdirectory(test)

BIN
sign.pcap Normal file

Binary file not shown.

BIN
sign.pcapng Normal file

Binary file not shown.

View File

@ -3,6 +3,85 @@
namespace SecMedia
{
int GeneHWSecritySEI(uint8_t* dst, CodecId code_type, uint8_t SecrityLevel,uint32_t vid_len, uint8_t sign_len,uint8_t* sign,char* rtp_head,uint16_t rtp_len)
{
char SEIdata[1024] = { 0x00,0x00,0x00,0x00,0x00 };
// const char uuid[] = { "U0VDIFZJRCBQUk9U" };
const uint8_t uuid[] = { 0x86,0xb1 ,0x16 ,0x6e ,0xad ,0xef ,0x67 ,0x3f ,0x70 ,0xbc ,0xe7 ,0xc7 ,0xe6 ,0x95 ,0xc6 ,0x09 };
char* ptr = SEIdata;
char* len_ptr;
memcpy(ptr,rtp_head,rtp_len);
ptr += rtp_len;
switch (code_type)
{
case CodecH264:
{
*ptr = 0x06; //nalu_type 6
ptr++;
*ptr = 0x05; //payload type 5
ptr++;
break;
}
case CodecH265:
{
*ptr = 0x28<< 1;//nalu_type 40
ptr++;
*ptr = 0x01; // TID
ptr++;
*ptr = 0x05; //payload type 5
ptr++;
break;
}
case CodecSVAC:
{
*ptr = 0x06 << 2 | 0xc0; //nalu_type
ptr++;
*ptr = 0x0A; //payload type 10
ptr++;
break;
}
default:
// WarnL << "暂不支持该CodecId:" << frame->getCodecId();
return -1;
}
len_ptr=ptr;// *ptr = 16 + 1; // payload size = 16(uuid)+ 1(sec bit)
ptr++;
*ptr = SecrityLevel << 6; // payload for sec
ptr++;
memcpy(ptr, uuid, 16); //base 64 ("SEC VID PROT")
ptr += 16;
//vkek version (reserved)
ptr += 4;
//enc vek (reserved)
ptr += 16;
//raw video data len
*(ptr++)=vid_len>>24;
*(ptr++)=vid_len>>16;
*(ptr++)=vid_len>>8;
*(ptr++)=vid_len;
//sign len
*(ptr++)=sign_len;
//sign
if(sign_len!=0 && sign){
memcpy(ptr, sign, sign_len);
ptr += sign_len;
}
*len_ptr=ptr-len_ptr-1; //figure out payload len
*ptr = 0x80; //rbsp trailing bits
ptr++;
auto len=int(ptr - SEIdata);
memcpy(dst,SEIdata,len);
return len;
}
int GeneSecritySEI(uint8_t* dst, CodecId code_type, uint8_t SecrityLevel)
{
char SEIdata[26] = { 0x00,0x00,0x00,0x01,0x00 };

View File

@ -4,7 +4,17 @@
namespace SecMedia
{
enum class DecryptType{
NONE=15,
SM1=0,
SM4=1,
RSA=2,
AES=3,
SVACEnc=4,
SVACDec=5,
SM3,
SM2_auth
};
struct NALUdecodeInfo
{
@ -75,6 +85,78 @@ namespace SecMedia
int GeneSecritySEI(uint8_t* dst, CodecId code_type, uint8_t SecrityLevel);
int DecSecuritySEI(uint8_t* frame,size_t len,uint8_t prefix,CodecId code);
int GeneHWSecritySEI(uint8_t* dst, CodecId code_type, uint8_t SecrityLevel,uint32_t vid_len, uint8_t sign_len,uint8_t* sign,char* rtp_head,uint16_t rtp_len);
enum class Hi_Packed_type_e{
Reserved=0,
FUA=1,
SINGLE=2
};
enum class Hi_Encrypt_type_e{
Reserved=0,
Full_encrypt=3,
};
//decrypt descriptor
struct Hi_Dec_Des_Sub_t{
uint8_t def_version[2];
#ifdef BIG_ENDIAN
uint8_t encrypt_arithmetic:4;
uint8_t packed_type:4;
uint8_t key_length:4;
uint8_t encrypt_round:4;
#else
uint8_t packed_type:4;
uint8_t encrypt_arithmetic:4;
uint8_t encrypt_round:4;
uint8_t key_length:4;
#endif
uint8_t encrypt_type;
uint8_t reserved=0x01;
};
struct Hi_Dec_Des_t{
uint8_t descriptor_tag;
uint8_t descriptor_length=sizeof(Hi_Dec_Des_Sub_t);
Hi_Dec_Des_Sub_t data;
};
struct Hi_Sec_t
{
uint8_t head[4]; //0x00 00 00 01
#ifdef BIG_ENDIAN
uint8_t sign_flag:4;
uint8_t encrypt_flag:4;
#else
uint8_t encrypt_flag:4;
uint8_t sign_flag:4;
#endif
uint8_t sec_frame_len[3];//视频安全编码数据的总长度
uint8_t raw_nal_head; //NAL 禁止位1bit NAL 优先级2bit NAL类型5bit
uint8_t reserve[3];
};
struct Hi_Enc_t
{
uint8_t device_id[32];
uint8_t vkek_version[20];
uint8_t vek[32];
uint8_t encrypt_type[2];
uint8_t shift_bit;
uint8_t reserver;
uint8_t IV[16];
};
struct Hi_Sec_Enc_t
{
Hi_Sec_t info;
Hi_Enc_t Enc;
};
struct Hi_Sec_Enc_Sign_t
{
Hi_Sec_t info;
Hi_Enc_t Enc;
uint8_t sign_data[64];
};
}

223
src/DecEnc/base64.cpp Normal file
View File

@ -0,0 +1,223 @@
/*
* Copyright (c) 2006 Ryan Martell. (rdm4@martellventures.com)
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
* @file
* @brief Base64 encode/decode
* @author Ryan Martell <rdm4@martellventures.com> (with lots of Michael)
*/
//#include "common.h"
#include "stdio.h"
#include "base64.h"
#include <memory>
#include <limits.h>
/* ---------------- private code */
static const uint8_t map2[] =
{
0x3e, 0xff, 0xff, 0xff, 0x3f, 0x34, 0x35, 0x36,
0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x01,
0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11,
0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x1a, 0x1b,
0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23,
0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b,
0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33
};
int av_base64_decode(uint8_t *out, const char *in, int out_size)
{
int i, v;
uint8_t *dst = out;
uint8_t shift=0;
v = 0;
for (i = 0; in[i] && in[i] != '='; i++) {
unsigned int index= in[i+shift]-43;
// if(in[i+shift]=='%'){
// if (in[i+2+shift]=='B')
// {
// shift+=2;
// index='+'-43;;
// }else if (in[i+2+shift]=='F')
// {
// shift+=2;
// index='/'-43;
// }else
// {
// return -1;
// }
// }
if (index>=FF_ARRAY_ELEMS(map2) || map2[index] == 0xff)
return -1;
v = (v << 6) + map2[index];
if (i & 3) {
if (dst - out < out_size) {
*dst++ = v >> (6 - 2 * (i & 3));
}
}
}
return dst - out;
}
/*****************************************************************************
* b64_encode: Stolen from VLC's http.c.
* Simplified by Michael.
* Fixed edge cases and made it work from data (vs. strings) by Ryan.
*****************************************************************************/
char *av_base64_encode(char *out, int out_size, const uint8_t *in, int in_size)
{
static const char b64[] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
char *ret, *dst,tmp;
unsigned i_bits = 0;
int i_shift = 0;
int bytes_remaining = in_size;
if (in_size >= UINT_MAX / 4 || out_size < AV_BASE64_SIZE(in_size))
{
return NULL;
}
ret = dst = out;
while (bytes_remaining) {
i_bits = (i_bits << 8) + *in++;
bytes_remaining--;
i_shift += 8;
do {
tmp=b64[(i_bits << 6 >> i_shift) & 0x3f];
// printf("%c",tmp);
// if (tmp=='+')
// {
// *dst++='%';
// *dst++='2';
// *dst++='B';
// }else if (tmp=='/')
// {
// *dst++='%';
// *dst++='2';
// *dst++='F';
// }else
// {
*dst++ = tmp;
// }
i_shift -= 6;
} while (i_shift > 6 || (bytes_remaining == 0 && i_shift > 0));
}
while ((dst - ret) & 3)
*dst++ = '=';
*dst = '\0';
return ret;
}
string encodeBase64(const string &txt){
if(txt.empty()){
return "";
}
int size = AV_BASE64_SIZE(txt.size()) + 10;
std::shared_ptr<char> txt_enc(new char[size],[](char *ptr){delete [] ptr;});
auto ret = av_base64_encode(txt_enc.get(),size,(uint8_t *)txt.data(),txt.size());
if(!ret ){
return "";
}
return ret;
}
string decodeBase64(const string &txt){
if(txt.empty()){
return "";
}
int size = txt.size() * 3 / 4 +10;
std::shared_ptr<char> txt_dec(new char[size],[](char *ptr){delete [] ptr;});
size = av_base64_decode((uint8_t *)txt_dec.get(),txt.data(),size);
if(size <= 0){
return "";
}
return string(txt_dec.get(),size);
}
#ifdef TEST
#undef printf
#define MAX_DATA_SIZE 1024
#define MAX_ENCODED_SIZE 2048
static int test_encode_decode(const uint8_t *data, unsigned int data_size,
const char *encoded_ref)
{
char encoded[MAX_ENCODED_SIZE];
uint8_t data2[MAX_DATA_SIZE];
int data2_size, max_data2_size = MAX_DATA_SIZE;
if (!av_base64_encode(encoded, MAX_ENCODED_SIZE, data, data_size)) {
printf("Failed: cannot encode the input data\n");
return 1;
}
if (encoded_ref && strcmp(encoded, encoded_ref)) {
printf("Failed: encoded string differs from reference\n"
"Encoded:\n%s\nReference:\n%s\n", encoded, encoded_ref);
return 1;
}
if ((data2_size = av_base64_decode(data2, encoded, max_data2_size)) < 0) {
printf("Failed: cannot decode the encoded string\n"
"Encoded:\n%s\n", encoded);
return 1;
}
if (memcmp(data2, data, data_size)) {
printf("Failed: encoded/decoded data differs from original data\n");
return 1;
}
printf("Passed!\n");
return 0;
}
int main(void)
{
int i, error_count = 0;
struct test {
const uint8_t *data;
const char *encoded_ref;
} tests[] = {
{ "", ""},
{ "1", "MQ=="},
{ "22", "MjI="},
{ "333", "MzMz"},
{ "4444", "NDQ0NA=="},
{ "55555", "NTU1NTU="},
{ "666666", "NjY2NjY2"},
{ "abc:def", "YWJjOmRlZg=="},
};
printf("Encoding/decoding tests\n");
for (i = 0; i < FF_ARRAY_ELEMS(tests); i++)
error_count += test_encode_decode(tests[i].data, strlen(tests[i].data), tests[i].encoded_ref);
return error_count;
}
#endif

72
src/DecEnc/base64.h Normal file
View File

@ -0,0 +1,72 @@
/*
* Copyright (c) 2006 Ryan Martell. (rdm4@martellventures.com)
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#define FF_ARRAY_ELEMS(a) (sizeof(a) / sizeof((a)[0]))
#ifndef AVUTIL_BASE64_H
#define AVUTIL_BASE64_H
#include <stdint.h>
#include <string>
using namespace std;
/**
* Decode a base64-encoded string.
*
* @param out buffer for decoded data
* @param in null-terminated input string
* @param out_size size in bytes of the out buffer, must be at
* least 3/4 of the length of in
* @return number of bytes written, or a negative value in case of
* invalid input
*/
int av_base64_decode(uint8_t *out, const char *in, int out_size);
/**
* Encode data to base64 and null-terminate.
*
* @param out buffer for encoded data
* @param out_size size in bytes of the output buffer, must be at
* least AV_BASE64_SIZE(in_size)
* @param in_size size in bytes of the 'in' buffer
* @return 'out' or NULL in case of error
*/
char *av_base64_encode(char *out, int out_size, const uint8_t *in, int in_size);
/**
* Calculate the output size needed to base64-encode x bytes.
*/
#define AV_BASE64_SIZE(x) (((x)+2) / 3 * 4 + 1)
/**
* base64
* @param txt
* @return
*/
string encodeBase64(const string &txt);
/**
* base64
* @param txt
* @return
*/
string decodeBase64(const string &txt);
#endif /* AVUTIL_BASE64_H */

View File

@ -10,6 +10,7 @@
#include "common.h"
#include "DecEnc/aes/AesOfb.h"
#include "DecEnc/NALUdecode.h"
using namespace std;
#define random(a,b) (rand()%(b-a)+a)
namespace SecMedia

317
src/HuaWei/HWsign.cpp Normal file
View File

@ -0,0 +1,317 @@
#include <arpa/inet.h>
#include <list>
#include <memory>
#include "common.h"
#include "HuaWei/HWsign.h"
#include "SVAC/src/svac_src/svac_dec.h"
#include "SVAC/src/svac_src/SM2_SM3.h"
#include "SVAC/src/sm2sm3/sm2.h"
#include "DecEnc/NALUdecode.h"
#include "DecEnc/base64.h"
#include "HuaWei/queue.h"
using namespace std;
struct SM2Config{
char prikey_size;
char pubkey_size;
uint8_t prikey[64];
uint8_t pubkey[128];
};
struct HWsign{
uint32_t seq_accu;
uint32_t stamp;
string* buff;
uint8_t buff_offest;
list<pair<shared_ptr<string>,void *>>* rtp_buff;
sm3_ctx * sm3_hd;
SM2Config * sm2_hd;
char sei_rtp_head[16];
void * sei_param;
function<void(const char * rtp_ptr, const uint32_t rtp_len,void * param)> rtp_cb ;
ThreadsafeQueue<pair<shared_ptr<string>,void *>>* buff_que;
uint8_t tcp;
uint32_t in;
uint32_t out;
int32_t I_seq;
};
typedef struct {
unsigned S :1;
unsigned E :1;
unsigned R :1;
unsigned type :5;
} FU;
void print_data2(const char * buf, uint32_t len,uint8_t offest){
printf("\n --%d-- \n",len);
for (int num = 0; num < offest; num++)
{
printf("%02X ", (uint8_t)buf[num]);
}
printf(", ");
for (int num = offest; num < 20; num++)
{
printf("%02X ", (uint8_t)buf[num]);
}
printf("\n ---- \n");
}
void print_rtp2(const char * buf, uint32_t len,uint8_t offest){
uint8_t mask=0;
uint16_t seq=0;
uint32_t stamp=0;
mask=(uint8_t)*(buf+1)>>7;
memcpy(&seq, buf + 2, 2);
seq= ntohs(seq);
memcpy(&stamp, buf + 4, 4);
stamp= ntohl(stamp);
printf("seq %d stamp %d mask %d",seq,stamp,mask);
print_data2(buf,len,offest);
}
static bool MakeFU(uint8_t in, FU &fu) {
fu.S = in >> 7;
fu.E = (in >> 6) & 0x01;
fu.R = (in >> 5) & 0x01;
fu.type = in & 0x1f;
if (fu.R != 0) {
return false;
}
return true;
}
// void * HWSign_init(const function<void(const char * rtp_ptr, const uint32_t rtp_len)> rtp_callback){
void * HWSign_init(){
HWsign* HWSign_hd= new HWsign();
HWSign_hd->I_seq=-1;
HWSign_hd->in=0;
HWSign_hd->out=0;
HWSign_hd->seq_accu=0;
HWSign_hd->stamp=0;
HWSign_hd->buff_offest=0;
HWSign_hd->sm3_hd=new sm3_ctx();
HWSign_hd->sm2_hd=new SM2Config();
HWSign_hd->buff=new string();
HWSign_hd->rtp_buff = new list<pair<shared_ptr<string>,void *>>();
HWSign_hd->buff_que =new ThreadsafeQueue<pair<shared_ptr<string>,void *>>();
// HWSign_hd->rtp_cb=rtp_callback;
HWSign_hd->rtp_cb=[HWSign_hd](const char * rtp_ptr, const uint32_t rtp_len,void * param){
printf("\n========================= output ====== ");
print_rtp2(rtp_ptr,rtp_len,12);
auto str_ptr=make_shared<string>(rtp_ptr,rtp_len);
HWSign_hd->buff_que->push(make_pair(str_ptr,param));
};
sm3_init(HWSign_hd->sm3_hd);
SM2Config sm2fig={
32,
64,
{
0x24,0x88,0xc8,0xdc,0x7f,0xd7,0xe0,0x91,0x30,0x1b,0x5c,0x58,0x2f,0xe7,0x44,0x7d,
0x2f,0x43,0xe4,0xee,0xc8,0x7d,0xc0,0xfb,0xa4,0xb8,0x7d,0x4b,0x8a,0x69,0x7c,0x4e
},
{
0xaa,0xb1,0x3f,0xd7,0x66,0xe2,0x75,0x97,0xc0,0x03,0xe6,0xe4,0x1d,0x77,0x54,0x78,
0xc8,0x29,0xb2,0x0b,0x9e,0xd1,0xff,0xa3,0x6a,0x6f,0xd2,0x7f,0xd6,0x2d,0xaa,0x3f,
0xc9,0x24,0xec,0x6c,0x96,0x0a,0x7b,0x73,0xf6,0xe6,0xfc,0xda,0x3a,0x08,0xfd,0x92,
0xfc,0x00,0x08,0x97,0x78,0x2c,0x71,0x6b,0xe1,0x26,0xf5,0x1e,0xba,0x31,0xf5,0xb2,
}
};
HWSign_hd->sm2_hd->prikey_size=32;
HWSign_hd->sm2_hd->pubkey_size=64;
memcpy(HWSign_hd->sm2_hd->prikey,sm2fig.prikey,sizeof(sm2fig.prikey));
memcpy(HWSign_hd->sm2_hd->pubkey,sm2fig.pubkey,sizeof(sm2fig.pubkey));
return (void*)HWSign_hd;
}
void HWSign_release(void* Handle){
HWsign* HWSign_hd=(HWsign*) Handle;
delete HWSign_hd->sm3_hd;
delete HWSign_hd->sm2_hd;
delete HWSign_hd->buff;
delete HWSign_hd->rtp_buff;
HWSign_hd->rtp_cb=nullptr;
}
uint16_t get_sequence(const char * rtp ,int tcp){
uint16_t seq;
if(tcp){
memcpy(&seq, rtp + 2 + 4, 2);
}else{
memcpy(&seq, rtp + 2 + 0, 2);
}
return htons(seq);
}
void send_rtp(HWsign* Handle,const char * buf, const uint32_t len ,int tcp,void * param){
char * send_buff=(char *)malloc(len);
uint8_t offset,offset2;
uint16_t seq;
memcpy(send_buff,buf,len);
if (tcp)
{
offset2=16;
offset=4;
send_buff[2] = (len-4) >> 8;
send_buff[3] = (len-4) & 0x00FF;
}else
{
offset2=12;
offset=0;
}
memcpy(&seq, buf + 2 + offset, 2);
seq=ntohs(seq)+Handle->seq_accu;
seq=htons(seq);
memcpy(send_buff+offset+2, &seq, 2);
// printf("\n@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ modify @@@@@@@@@ id %d ",id);
// print_rtp2(send_buff,len,offset2);
Handle->rtp_cb(send_buff,len,param);
free(send_buff);
}
void sign_data(HWsign* Handle,const char * buf, const uint32_t len,uint8_t offset,int tcp,void * param,const char * sei_head){
uint8_t sha[128],sign[128],sei[1024];
sm3_update(Handle->sm3_hd,(uint8_t*)buf+offset,len-offset);
sm3_final(Handle->sm3_hd,sha);
do_sm2_sign((char*)Handle->sm2_hd->prikey,(char*)Handle->sm2_hd->pubkey,(char *) sha, 32, (char *)sign);
Handle->sei_param=nullptr;
auto rtp_len=SecMedia::GeneHWSecritySEI(sei,CodecId::CodecH264,0,len,64,sign,(char *)sei_head,offset);
printf("Warning!!!!!!!!!! sei package\n");
send_rtp(Handle,(char *) sei,rtp_len,tcp,param);
// Handle->seq_accu++;
}
void none_sign_data(HWsign* Handle,uint8_t offset,int tcp,void * param,const char * sei_head){
uint8_t sei[1024];
auto rtp_len=SecMedia::GeneHWSecritySEI(sei,CodecId::CodecH264,0,0,64,nullptr,(char *)sei_head,offset);
Handle->sei_param=nullptr;
printf("Warning!!!!!!!!!! none sei package\n");
send_rtp(Handle,(char *) sei,rtp_len,tcp,param);
}
int HWSign_rtp_out(void* Handle, char * buf, uint32_t * len, void ** param){
HWsign* HWSign_hd=(HWsign*) Handle;
if (HWSign_hd->buff_que->empty()) return -1;
auto element= HWSign_hd->buff_que->pop();
*len=(uint32_t) element.first->size();
memcpy(buf,element.first->data(),*len);
*param=element.second;
HWSign_hd->out++;
printf("HWSign: in %d out %d \n",HWSign_hd->in,HWSign_hd->out);
return 1;
}
void flush_all(HWsign* HWSign_hd){
auto rtp_list=*(HWSign_hd->rtp_buff);
for(auto& rtp_sig : rtp_list){
HWSign_hd->rtp_cb(rtp_sig.first->data(),rtp_sig.first->size(),rtp_sig.second);
// send_rtp(HWSign_hd,rtp_sig.first->data(),rtp_sig.first->size(),HWSign_hd->tcp,rtp_sig.second);
}
HWSign_hd->buff->clear();
HWSign_hd->rtp_buff->clear();
printf("Warning!!!!!!!!!! flush all\n");
}
int HWSign_rtp_input(void* Handle, const char * buf, const uint32_t len,int tcp, void * param){
HWsign* HWSign_hd=(HWsign*) Handle;
if(!HWSign_hd || !HWSign_hd->rtp_cb || !HWSign_hd->sm3_hd || !HWSign_hd->sm2_hd || !HWSign_hd->buff) {
throw runtime_error("HWSign_rtp initial error");
return -1;
}
HWSign_hd->in++;
printf("input: in %d out %d \n",HWSign_hd->in,HWSign_hd->out);
tcp=0;
HWSign_hd->tcp=tcp;
uint8_t offset;
if (tcp) offset=16;
else offset=12;
if (len<=offset) {
HWSign_hd->rtp_cb(buf,len,param);
return -1;
}
const char * rtp=buf+offset;
int length = len- offset;
int nal_type = *rtp & 0x1F;
int nal_suffix = *rtp & (~0x1F);
uint16_t now_seq= get_sequence(buf,tcp);
printf("\n###### input ##########################");
print_rtp2(buf,len,offset);
// send_rtp(HWSign_hd,buf,len,tcp,id);
// return 1;
// HWSign_hd->rtp_cb(buf,len,id);
// return 1;
if (nal_type == 5 || nal_type == 7 || nal_type == 8 ) {
//a full frame
// send_rtp(HWSign_hd,buf,len,tcp,id);
HWSign_hd->rtp_cb(buf,len,param);
return 1;
}
switch (nal_type){
case 6:{
memcpy(HWSign_hd->sei_rtp_head,buf,offset);
HWSign_hd->sei_param=param;
return 1;
}
case 28:{
FU fu;
MakeFU((uint8_t)rtp[1], fu);
if (fu.type!=5)
{
HWSign_hd->rtp_cb(buf,len,param);
// send_rtp(HWSign_hd,buf,len,tcp,param);
return 1;
}
if (fu.S) { //第一个rtp包
if(!HWSign_hd->buff->empty()){
printf("Warning!!!!!!!!!! missing package\n");
if (HWSign_hd->sei_param!=nullptr)
{
none_sign_data(HWSign_hd,offset,tcp, HWSign_hd->sei_param,HWSign_hd->sei_rtp_head);
}
flush_all(HWSign_hd);
}
printf("!!!!!!!!!! I head\n");
HWSign_hd->I_seq=now_seq;
auto rtp_str=make_shared<string>(buf,len);
HWSign_hd->rtp_buff->push_back(make_pair(rtp_str,param));
HWSign_hd->buff->assign(buf,len);
return 1;
}
if(HWSign_hd->I_seq>=0){
auto rtp_str=make_shared<string>(buf,len);
HWSign_hd->rtp_buff->push_back(make_pair(rtp_str,param));
if (!fu.E) { //中间rtp包
HWSign_hd->buff->append(rtp,length);
return 1;
}
HWSign_hd->buff->append(rtp,length);
if (HWSign_hd->sei_param!=nullptr)
{
sign_data(HWSign_hd,HWSign_hd->buff->data(),HWSign_hd->buff->size(),offset, tcp,HWSign_hd->sei_param,HWSign_hd->sei_rtp_head);
}
flush_all(HWSign_hd);
HWSign_hd->I_seq=-1;
return 1;
}else{
HWSign_hd->rtp_cb(buf,len,param);
return 1;
}
}
default:{
HWSign_hd->rtp_cb(buf,len,param);
return 1;
}
}
}

15
src/HuaWei/HWsign.h Normal file
View File

@ -0,0 +1,15 @@
#ifndef _HWSIGN_H
#define _HWSIGN_H
#include<stdlib.h>
#include<stdio.h>
#include <functional>
#define API_EXPORT __attribute__((visibility("default")))
// void * HWSign_init(const std::function<void(const char * rtp_ptr, const uint32_t rtp_len)> rtp_callback);
API_EXPORT void * HWSign_init();
API_EXPORT void HWSign_release(void* Handle);
API_EXPORT int HWSign_rtp_input(void* Handle, const char * buf, const uint32_t len,int tcp, void * param);
API_EXPORT int HWSign_rtp_out(void* Handle, char * buf, uint32_t * len, void ** param);
#endif /* _DECRYPT_H */

52
src/HuaWei/queue.h Normal file
View File

@ -0,0 +1,52 @@
#include <queue>
#include <thread>
#include <mutex>
#include <condition_variable>
template<class T>
class ThreadsafeQueue {
std::queue<T> queue_;
mutable std::mutex mutex_;
// Moved out of public interface to prevent races between this
// and pop().
bool empty() const {
return queue_.empty();
}
public:
ThreadsafeQueue() = default;
ThreadsafeQueue(const ThreadsafeQueue<T> &) = delete ;
ThreadsafeQueue& operator=(const ThreadsafeQueue<T> &) = delete ;
ThreadsafeQueue(ThreadsafeQueue<T>&& other) {
std::lock_guard<std::mutex> lock(mutex_);
queue_ = std::move(other.queue_);
}
virtual ~ThreadsafeQueue() { }
unsigned long size() const {
std::lock_guard<std::mutex> lock(mutex_);
return queue_.size();
}
T pop() {
std::lock_guard<std::mutex> lock(mutex_);
if (queue_.empty()) {
return {};
}
T tmp = queue_.front();
queue_.pop();
return tmp;
}
void push(const T &item) {
std::lock_guard<std::mutex> lock(mutex_);
queue_.push(item);
}
bool empty(){
std::lock_guard<std::mutex> lock(mutex_);
return queue_.empty();
}
};

View File

@ -3,7 +3,7 @@
#include <malloc.h>
#include <assert.h>
#include "svac_enc.h"
// #include "svac_enc.h"
#include "sm3.h"
#include "sm2.h"
@ -55,28 +55,28 @@ struct vkek_info
struct sec_param
{
/* 安全参数集部分 */
int encrypt_flag; //是否加密
int encrypt_type; //加密算法
int auth_flag; //是否签名
int auth_type; //签名算法
int hash_type; //散列算法
int vek_flag; //是否包含evek
int vek_encrypt_type; //vek加密算法
/* <EFBFBD><EFBFBD>ȫ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
int encrypt_flag; //<EFBFBD>Ƿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
int encrypt_type; //<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
int auth_flag; //<EFBFBD>Ƿ<EFBFBD>ǩ<EFBFBD><EFBFBD>
int auth_type; //ǩ<EFBFBD><EFBFBD><EFBFBD>
int hash_type; //ɢ<EFBFBD><EFBFBD><EFBFBD>
int vek_flag; //<EFBFBD>Ƿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>evek
int vek_encrypt_type; //vek<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
char evek[32]; //evek
int vek_len; //vek长度
int vek_len; //vek<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
char vkek_version[128]; //vkek version
int version_len; //vkek version长度
int iv_flag; //是否包含iv
int version_len; //vkek version<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
int iv_flag; //<EFBFBD>Ƿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>iv
char iv[32]; //iv
int iv_len; //iv长度
int iv_len; //iv<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
int only_IDR; // sign only for idr
int hash_period; // sign period frame
};
struct hash_cache
{
/* 一个gop最多256帧 */
/* һ<EFBFBD><EFBFBD>gop<EFBFBD><EFBFBD><EFBFBD>256֡ */
char hash[32];
int frame_num;
};
@ -85,9 +85,9 @@ struct svac_handle
{
int magic;
/* sm2 公钥 */
/* sm2 <EFBFBD><EFBFBD>Կ */
char sm2_pubkey[64];
/* sm2 公钥是否设置 */
/* sm2 <EFBFBD><EFBFBD>Կ<EFBFBD>Ƿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
int sm_pubkey_flag;
char sm2_prikey[32];
@ -108,16 +108,16 @@ struct svac_handle
/* current frame is IDR frame */
int idr_flag;
/* current frame 包含 sec nalu */
/* current frame <EFBFBD><EFBFBD><EFBFBD><EFBFBD> sec nalu */
int sec_nalu_flag;
/* current frame 包含 auth nalu */
/* current frame <EFBFBD><EFBFBD><EFBFBD><EFBFBD> auth nalu */
int auth_nalu_flag;
/* 当前使用的安全参数 */
/* <EFBFBD><EFBFBD>ǰʹ<EFBFBD>õİ<EFBFBD>ȫ<EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
struct sec_param curr_param;
/* 新收到的安全参数 */
/* <EFBFBD><EFBFBD><EFBFBD>յ<EFBFBD><EFBFBD>İ<EFBFBD>ȫ<EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
struct sec_param new_param;
/* 安全参数更新标识新的GOP开始更新 */
/* <EFBFBD><EFBFBD>ȫ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>±<EFBFBD>ʶ<EFBFBD><EFBFBD><EFBFBD>µ<EFBFBD>GOP<EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
int sec_param_update_flag;
int vkek_start;
@ -128,8 +128,8 @@ struct svac_handle
struct hash_cache cache[HASH_CACHE_NUM];
int cache_idx; // 0 ~ HASH_CACHE_NUM - 1
char camera_id[64]; //国标id
char camera_idc[19]; //证书id
char camera_id[64]; //<EFBFBD><EFBFBD><EFBFBD><EFBFBD>id
char camera_idc[19]; //֤<EFBFBD><EFBFBD>id
};
#define CHECK_HANDLE(h) \
@ -182,7 +182,7 @@ static unsigned int write_long_data_bits(unsigned int data, unsigned int write_l
int loop;
unsigned int first_write_len = write_len & 7;
/* 不满一字节的当first_write_len处理*/
/* <EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD>ֽڵĵ<EFBFBD>first_write_len<EFBFBD><EFBFBD><EFBFBD><EFBFBD>*/
if (first_write_len)
offset = write_bits((unsigned char)((data >> (write_len - first_write_len)) & ((1 <<first_write_len) - 1)), first_write_len, addr, offset);
@ -289,7 +289,7 @@ static int svac_parse_nalu(struct svac_handle *h, char *frame_data, int frame_le
h->auth_nalu_flag = 1;
else if (SVAC_PPS_SLICE == h->nalu[h->nalu_num].type)
{
/* pps的第一个字节是帧号 svac2.0 p23 */
/* pps<EFBFBD>ĵ<EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD>ֽ<EFBFBD><EFBFBD><EFBFBD>֡<EFBFBD><EFBFBD> svac2.0 p23 */
h->frame_num = (unsigned char)start[5];
//PRINT_INFO("frame %d\n", h->frame_num);
}
@ -303,7 +303,7 @@ static int svac_parse_nalu(struct svac_handle *h, char *frame_data, int frame_le
return 0;
}
/* 增加 svac 的竞争码 */
/* <EFBFBD><EFBFBD><EFBFBD><EFBFBD> svac <20>ľ<EFBFBD><C4BE><EFBFBD><EFBFBD><EFBFBD> */
static int do_add_racing_Code(char *ptr, int *len, int offset)
{
int i, find = 0, start = offset;
@ -353,7 +353,7 @@ static int do_add_racing_Code(char *ptr, int *len, int offset)
}
/* 去掉 svac 的竞争码 */
/* ȥ<EFBFBD><EFBFBD> svac <20>ľ<EFBFBD><C4BE><EFBFBD><EFBFBD><EFBFBD> */
static int do_remove_racing_code(char *ptr, int *len, int offset)
{
int i, find = 0, start;
@ -420,7 +420,7 @@ static int calc_nalu_hash(struct svac_handle *h)
break;
}
/* 没有签名的nalu */
/* û<EFBFBD><EFBFBD>ǩ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>nalu */
if (i >= h->nalu_num)
return 0;
@ -920,7 +920,7 @@ int SvacEncProcess(void *handle, char *in_buff, int in_len, char *out_buff, int
if (h->idr_flag && h->sec_param_update_flag)
update_new_enc_param(h);
/* 如果是a级码流 bypass */
/* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>a<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> bypass */
if (!h->curr_param.auth_flag && !h->curr_param.encrypt_flag)
return make_bypass_frame(h, out_buff, out_len);

View File

@ -25,12 +25,12 @@ struct sm3_ctx {
u8 opad[64]; /*!< HMAC: outer padding */
};
/* */
EXT_API int do_sm2_sign(char *prikey, char *pubkey, char *data_addr, int data_len, char *sign_addr);
EXT_API int do_sm2_verify(char *pubkey, char *data_addr, int data_len, char *sign_addr);
int do_sm2_sign(char *prikey, char *pubkey, char *data_addr, int data_len, char *sign_addr);
int do_sm2_verify(char *pubkey, char *data_addr, int data_len, char *sign_addr);
EXT_API int sm3_init(struct sm3_ctx *ctx);
EXT_API int sm3_update(struct sm3_ctx *ctx, const u8 *input, u32 ilen);
EXT_API int sm3_final(struct sm3_ctx *ctx, u8 *output);
int sm3_init(struct sm3_ctx *ctx);
int sm3_update(struct sm3_ctx *ctx, const u8 *input, u32 ilen);
int sm3_final(struct sm3_ctx *ctx, u8 *output);
#ifdef __cplusplus
}
#endif

View File

@ -1,6 +1,6 @@
include_directories(../include)
file(GLOB test_src_list ./*.cpp ./*.h)
file(GLOB test_src_list ./*.cpp ./*.h ../include/*.h)
MESSAGE(STATUS ${test_src_list})

2806
test/data.h Normal file

File diff suppressed because it is too large Load Diff

View File

@ -1,354 +1,240 @@
#include<stdlib.h>
#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#include <unistd.h>
#include<string>
#include<functional>
#include "common.h"
#include "SVAC/svac_src/svac_dec.h"
#include "SVAC/svac_src/SM2_SM3.h"
#include "SVAC/sm2sm3/sm2.h"
#include "HuaWei/HWsign.h"
#include "data.h"
#include<netinet/in.h>
#include <arpa/inet.h>
#include<sys/socket.h>
using namespace std;
class UdpSocket{
private:
int _sockfd;
public:
UdpSocket():_sockfd(-1){}
~UdpSocket()
{
if(_sockfd != -1)
{
close(_sockfd);
}
}
bool Socket(){
_sockfd = socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP);
if(_sockfd < 0)
{
perror("socket error !");
return false;
}
return true;
}
bool Bind(const string &ip,uint16_t port){
struct sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
addr.sin_addr.s_addr = inet_addr(ip.c_str());
socklen_t len = sizeof(struct sockaddr_in);
int ret;
ret = bind(_sockfd,(struct sockaddr*)&addr,len);
if(ret < 0)
{
perror("bind error !");
return false;
}
return true;
}
bool Send(char* data,const uint32_t &dlen, const string &ip,uint16_t port){
struct sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
addr.sin_addr.s_addr = inet_addr(ip.c_str());
socklen_t len = sizeof(struct sockaddr_in);
int ret;
ret = sendto(_sockfd,(void*)data,dlen,0,(struct sockaddr*) &addr,len);
if(ret < 0)
{
perror("sendto error");
return false;
}
return true;
}
bool Recv(string *buf, string *ip = NULL,uint16_t *port = NULL)
{
struct sockaddr_in addr;
socklen_t len = sizeof(struct sockaddr_in);
char temp [4096] = {0};
int ret;
ret = recvfrom(_sockfd,temp,4096,0,(struct sockaddr *)&addr,&len);
if(ret < 0){
perror ("recvfrom error !");
return false;
}
if(ip != NULL)
{
*ip = inet_ntoa(addr.sin_addr);
}
if(port != NULL){
*port = ntohs(addr.sin_port);
}
*buf = temp;
return true;
}
bool Close()
{
if(_sockfd != -1)
{
close(_sockfd);
}
return true;
}
};
int main(){
uint8_t privi[]={
0x24,0x88,0xc8,0xdc,0x7f,0xd7,0xe0,0x91,0x30,0x1b,0x5c,0x58,0x2f,0xe7,0x44,0x7d,
0x2f,0x43,0xe4,0xee,0xc8,0x7d,0xc0,0xfb,0xa4,0xb8,0x7d,0x4b,0x8a,0x69,0x7c,0x4e
};
uint8_t pub[]={
0xaa,0xb1,0x3f,0xd7,0x66,0xe2,0x75,0x97,0xc0,0x03,0xe6,0xe4,0x1d,0x77,0x54,0x78,
0xc8,0x29,0xb2,0x0b,0x9e,0xd1,0xff,0xa3,0x6a,0x6f,0xd2,0x7f,0xd6,0x2d,0xaa,0x3f,
0xc9,0x24,0xec,0x6c,0x96,0x0a,0x7b,0x73,0xf6,0xe6,0xfc,0xda,0x3a,0x08,0xfd,0x92,
0xfc,0x00,0x08,0x97,0x78,0x2c,0x71,0x6b,0xe1,0x26,0xf5,0x1e,0xba,0x31,0xf5,0xb2,
};
char outdata[300];
do_sm2_verify((char*)pub ,(char*)privi,sizeof(privi),outdata);
// u64 rt,data=0xFFF;
// rt=be64_to_cpu(data);
// printf("%ld\n",rt);
return 0;
void print_data(const char * buf, uint32_t len){
printf("\n --%d-- \n",len);
for (int num = 0; num < 12; num++)
{
printf("%02X ", (uint8_t)buf[num]);
}
printf(", ");
for (int num = 12; num < 20; num++)
{
printf("%02X ", (uint8_t)buf[num]);
}
printf("\n ---- \n");
}
void print_rtp(const char * buf, uint32_t len){
uint8_t mask=0;
uint16_t seq=0;
uint32_t stamp=0;
mask=(uint8_t)*(buf+1)>>7;
memcpy(&seq, buf + 2, 2);
seq= ntohs(seq);
memcpy(&stamp, buf + 4, 4);
stamp= ntohl(stamp);
printf("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ modify @@@@@@@@@ seq %d stamp %d mask %d",seq,stamp,mask);
print_data(buf,len);
}
// mask=*(pkt##x+offset+1)>>7; \
// memcpy(&seq, pkt##x+offset + 2, 2); \
// seq= ntohs(seq); \
// memcpy(&stamp, pkt##x+offset + 4, 4); \
// stamp= ntohl(stamp); \
// printf("raw seq %d stamp %d mask %d",seq,stamp,mask); \
// print_data((const char*)pkt##x+offset,sizeof(pkt##x)-offset); \
#define ARRAY(x,offset) { \
id=(int *)malloc(sizeof(int)); \
*id=x; \
auto ret =HWSign_rtp_input(sign_handle,(const char*)pkt##x+offset,sizeof(pkt##x)-offset,0,(void*)id); \
}
// #include "Decrypt.h"
// #include "Encrypt.h"
// #define H264_TYPE(v) ((uint8_t)(v) & 0x1F)
// #define FRAME_SIZE 6
// #define FRAME_LEN 10
// const uint8_t rawframe[FRAME_SIZE][FRAME_LEN]={
// {0x00,0x00,0x00,0x01,0x61,0x01,0x02,0x03,0x04,0x05},
// {0x00,0x00,0x00,0x01,0x65,0x01,0x02,0x03,0x04,0x05},
// {0x00,0x00,0x00,0x01,0x67,0x01,0x02,0x03,0x04,0x05},
// {0x00,0x00,0x00,0x01,0x61,0xf1,0xf2,0xf3,0xf4,0xf5},
// {0x00,0x00,0x00,0x01,0x61,0x01,0x02,0x03,0x04,0x05},
// {0x00,0x00,0x00,0x01,0x61,0x01,0x02,0x03,0x04,0x05},
// };
// struct packet_t
// {
// uint8_t sid;
// uint8_t codecid;
// int flags;
// int64_t pts;
// int64_t dts;
// uint8_t *data;
// size_t size;
// size_t capacity;
// int vcl; // h.264/h.265 only
// };
// struct pes_t
// {
// uint16_t pn; // TS program number(0-ps)
// uint16_t pid; // PES PID : 13
// uint8_t sid; // PES stream_id : 8
// uint8_t codecid; // PMT/PSM stream_type : 8
// uint8_t cc; // continuity_counter : 4;
// uint8_t* esinfo; // es_info
// uint16_t esinfo_len;// es_info_length : 12
// uint32_t len; // PES_packet_length : 16;
// uint32_t reserved10 : 2;
// uint32_t PES_scrambling_control : 2;
// uint32_t PES_priority : 1;
// uint32_t data_alignment_indicator : 1;
// uint32_t copyright : 1;
// uint32_t original_or_copy : 1;
// uint32_t PTS_DTS_flags : 2;
// uint32_t ESCR_flag : 1;
// uint32_t ES_rate_flag : 1;
// uint32_t DSM_trick_mode_flag : 1;
// uint32_t additional_copy_info_flag : 1;
// uint32_t PES_CRC_flag : 1;
// uint32_t PES_extension_flag : 1;
// uint32_t PES_header_data_length : 8;
// int64_t pts;
// int64_t dts;
// int64_t ESCR_base;
// uint32_t ESCR_extension;
// uint32_t ES_rate;
// //uint8_t trick_mode;
// //uint32_t trick_mode_control : 3;
// //uint32_t field_id : 2;
// //uint32_t intra_slice_refresh : 1;
// //uint32_t frequency_truncation : 2;
// //uint8_t additional_copy_info;
// //int16_t previous_PES_packet_CRC;
// //uint32_t PES_private_data_flag : 1;
// //uint32_t pack_header_field_flag : 1;
// //uint32_t program_packet_sequence_counter_flag : 1;
// //uint32_t P_STD_buffer_flag : 1;
// //uint32_t reserved_ : 3;
// //uint32_t PES_extension_flag_2 : 1;
// //uint32_t PES_private_data_flag2 : 1;
// //uint8_t PES_private_data[128/8];
// //uint32_t pack_field_length : 8;
// struct packet_t pkt;
// };
// struct psm_t
// {
// uint32_t ver : 5; // version_number : 5;
// struct pes_t streams[16];
// size_t stream_count;
// };
// typedef struct _avc_timing_hrd_descriptor_t
// {
// uint32_t hrd_management_valid_flag : 1;
// uint32_t picture_and_timing_info_present : 1;
// uint32_t _90kHZ_flag : 1;
// uint32_t fixed_frame_rate_flag : 1;
// uint32_t temporal_poc_flag : 1;
// uint32_t picture_to_display_conversion_flag : 1;
// uint32_t N;
// uint32_t K;
// uint32_t num_unit_in_tick;
// } avc_timing_hrd_descriptor_t;
// size_t avc_timing_hrd_descriptor(const uint8_t* data, size_t bytes)
// {
// // 2.6.66 AVC timing and HRD descriptor(p112)
// size_t i;
// avc_timing_hrd_descriptor_t desc;
// // uint8_t descriptor_tag = data[0];
// size_t descriptor_len = data[1];
// assert(descriptor_len+2 <= bytes);
// i = 2;
// memset(&desc, 0, sizeof(desc));
// desc.hrd_management_valid_flag = (data[i] >> 7) & 0x01;
// desc.picture_and_timing_info_present = (data[i] >> 0) & 0x01;
// ++i;
// if(desc.picture_and_timing_info_present)
// {
// desc._90kHZ_flag = (data[i] >> 7) & 0x01;
// if(0 == desc._90kHZ_flag)
// {
// desc.N = (data[i+1] << 24) | (data[i+2] << 16) | (data[i+3] << 8) | data[i+4];
// desc.K = (data[i+5] << 24) | (data[i+6] << 16) | (data[i+7] << 8) | data[i+8];
// i += 8;
// }
// desc.num_unit_in_tick = (data[i+1] << 24) | (data[i+2] << 16) | (data[i+3] << 8) | data[i+4];
// i += 5;
// }
// desc.fixed_frame_rate_flag = (data[i] >> 7) & 0x01;
// desc.temporal_poc_flag = (data[i] >> 6) & 0x01;
// desc.picture_to_display_conversion_flag = (data[i] >> 5) & 0x01;
// return descriptor_len+2;
// }
// size_t mpeg_elment_descriptor(const uint8_t* data, size_t bytes)
// {
// uint8_t descriptor_tag = data[0];
// uint8_t descriptor_len = data[1];
// if ((size_t)descriptor_len + 2 > bytes)
// return bytes;
// size_t rt=0;
// rt=avc_timing_hrd_descriptor(data,bytes);
// printf("%d,%d,%d\n",descriptor_tag,descriptor_len,rt);
// return descriptor_len+2;
// }
// size_t psm_read(struct psm_t *psm, const uint8_t* data, size_t bytes)
// {
// size_t i, j, k;
// uint8_t current_next_indicator;
// uint8_t single_extension_stream_flag;
// uint16_t program_stream_map_length;
// uint16_t program_stream_info_length;
// uint16_t element_stream_map_length;
// uint16_t element_stream_info_length;
// // Table 2-41 - Program stream map(p79)
// assert(0x00==data[0] && 0x00==data[1] && 0x01==data[2] && 0xBC==data[3]);
// program_stream_map_length = (data[4] << 8) | data[5];
// if (program_stream_map_length < 3 || bytes < (size_t)program_stream_map_length + 6)
// return 0; // invalid data length
// //assert((0x20 & data[6]) == 0x00); // 'xx0xxxxx'
// current_next_indicator = (data[6] >> 7) & 0x01;
// single_extension_stream_flag = (data[6] >> 6) & 0x01;
// psm->ver = data[6] & 0x1F;
// //assert(data[7] == 0x01); // '00000001'
// // program stream descriptor
// program_stream_info_length = (data[8] << 8) | data[9];
// if ((size_t)program_stream_info_length + 4 + 2 /*element_stream_map_length*/ > (size_t)program_stream_map_length)
// return 0; // TODO: error
// // TODO: parse descriptor
// // program element stream
// i = 10 + program_stream_info_length;
// element_stream_map_length = (data[i] << 8) | data[i+1];
// /* Ignore es_map_length, trust psm_length */
// element_stream_map_length = program_stream_map_length - program_stream_info_length - 10;
// i += 2;
// psm->stream_count = 0;
// for(j = i; j + 4/*element_stream_info_length*/ <= i+element_stream_map_length && psm->stream_count < sizeof(psm->streams)/sizeof(psm->streams[0]); j += 4 + element_stream_info_length)
// {
// psm->streams[psm->stream_count].codecid = data[j];
// psm->streams[psm->stream_count].sid = data[j+1];
// psm->streams[psm->stream_count].pid = psm->streams[psm->stream_count].sid; // for ts PID
// element_stream_info_length = (data[j+2] << 8) | data[j+3];
// if (j + 4 + element_stream_info_length > i+element_stream_map_length)
// return 0; // TODO: error
// k = j + 4;
// if(0xFD == psm->streams[psm->stream_count].sid && 0 == single_extension_stream_flag)
// {
// if(element_stream_info_length < 3)
// return 0; // TODO: error
// // uint8_t pseudo_descriptor_tag = data[k];
// // uint8_t pseudo_descriptor_length = data[k+1];
// // uint8_t element_stream_id_extension = data[k+2] & 0x7F;
// assert((0x80 & data[k+2]) == 0x80); // '1xxxxxxx'
// k += 3;
// }
// while(k + 2 <= j + 4 + element_stream_info_length)
// {
// // descriptor()
// k += mpeg_elment_descriptor(data+k, j + 4 + element_stream_info_length - k);
// }
// ++psm->stream_count;
// assert(k - j - 4 == element_stream_info_length);
// }
// // assert(j+4 == program_stream_map_length+6);
// // assert(0 == mpeg_crc32(0xffffffff, data, program_stream_map_length+6));
// return program_stream_map_length+6;
// }
// int main(int argc, char *argv[]){
// psm_t psm_obj;
// uint8_t data1[]={0x00,0x00,0x01,0xbc,0x00,0x1e,0xe1,0xff,
// 0x00,0x00,0x00,0x14,0x80,0xe0,0x00,0x0c,0x2a,0x0a,0x7f,0xff,0x00,0x00,0x05,0xdc,
// 0x1f,0xfe,0xf0,0x87,0x90,0xc0,0x00,0x00,0x00,0x00,0x00,0x00};
// size_t rt=0;
// rt=psm_read(&psm_obj,(const uint8_t*)&data1,sizeof(data1));
// printf("%d\n",rt);
// }
// int main(int argc, char *argv[]){
// dec_media dec_media_obj;
// dec_media_func_t dec_fun={
// //alloc
// [](void* param, size_t bytes){
// return (void*)malloc(sizeof(uint8_t)*bytes);
// },
// //free
// [](void* param, void* packet){
// free(packet);
// },
// //write
// [](void* param, void* packet, size_t bytes,uint64_t timestamp,uint8_t frametype){
// printf("\n");
// printf("type: %d timestamp:%lld lens:%zd\n", frametype, timestamp, bytes);
// uint8_t * ptr=(uint8_t*)packet;
// for (size_t i = 0; i < bytes; i++)
// {
// printf("%02X ", *(ptr++));
// }
// printf("\n");
// printf("-----------Decrypted data----------------- ");
// }
// };
int main(){
// EXPAND(DEFER(A)());
void * sign_handle=nullptr;
// dec_media_obj=dec_media_create(&dec_fun,NULL);
auto udpsocket=new UdpSocket();
udpsocket->Socket();
// udpsocket.Send()
// [udpsocket](const char * rtp_ptr, const uint32_t rtp_len){
// udpsocket->Send(rtp_ptr,rtp_len,string("192.168.123.1"),30000);
// // print_rtp(rtp_ptr,rtp_len);
// }
uint8_t mask;
uint16_t seq=0;
uint32_t stamp=0;
int* id;
int ret,count;
sign_handle=HWSign_init();
ARRAY(1,42)
ARRAY(2,42)
ARRAY(3,42)
ARRAY(4,42)
ARRAY(5,42) //I
ARRAY(6,42)
ARRAY(7,42)
ARRAY(8,42)
ARRAY(9,42)
ARRAY(10,42)
ARRAY(11,42)
ARRAY(12,42)
ARRAY(13,42)
ARRAY(14,42)
ARRAY(15,42)
ARRAY(16,42)
ARRAY(17,42)
ARRAY(18,42)
ARRAY(19,42)
ARRAY(20,42)
ARRAY(21,42)
ARRAY(1,42)
ARRAY(2,42)
ARRAY(3,42)
ARRAY(4,42)
ARRAY(5,42) //I
ARRAY(6,42)
ARRAY(7,42)
ARRAY(8,42)
ARRAY(9,42)
ARRAY(10,42)
ARRAY(11,42)
ARRAY(12,42)
ARRAY(13,42)
ARRAY(14,42)
ARRAY(15,42)
ARRAY(16,42)
ARRAY(17,42)
ARRAY(18,42)
ARRAY(19,42)
ARRAY(20,42)
ARRAY(21,42)
ret=1;count=0;
while (ret==1)
{
char buf_h[2048];
uint32_t len;
int* id;
count++;
ret=HWSign_rtp_out(sign_handle,buf_h,&len,(void **)&id);
if (ret==-1) break;
udpsocket->Send((char*)buf_h,len,string("192.168.123.1"),30000);
printf("%d %d %d\n",count,*id,len);
free(id);
// printf(" modify @@@@@@@@@ id %d seq %d stamp %d mask %d",id,seq,stamp,mask);
// print_data(buf_h,len);
// sleep(0.1);
/* code */
}
// printf("raw");
// print_data((const char*)pkt2+42,sizeof(pkt2)-42);
// HWSign_rtp_input(sign_handle,(const char*)pkt2+42,sizeof(pkt2)-42,0);
// printf("raw");
// print_data((const char*)pkt3+42,sizeof(pkt3)-42);
// HWSign_rtp_input(sign_handle,(const char*)pkt3+42,sizeof(pkt3)-42,0);
// enc_media enc_media_obj;
// enc_media_func_t enc_fun;
// enc_fun={
// //alloc
// [](void* param, size_t bytes){
// return (void*)malloc(sizeof(uint8_t)*bytes);
// },
// //free
// [](void* param, void* packet){
// free(packet);
// },
// //write
// [](void* param, void* packet, size_t bytes,uint64_t timestamp,uint8_t frametype){
// printf("\n");
// printf("type: %d timestamp:%lld lens:%zd\n", frametype, timestamp, bytes);
// uint8_t * ptr=(uint8_t*)packet;
// for (size_t i = 0; i < bytes; i++)
// {
// printf("%02X ", *(ptr++));
// }
// printf("\n");
// int ret=dec_security_SEI((uint8_t*)packet,bytes,4,0);
// if (ret>0)
// {
// printf("\nSecurity level: %d\n",ret);
// }
// printf("############Encrypted data############ ");
// dec_media_decoded((dec_media) param,(uint8_t*)packet,bytes,timestamp,frametype,4);
// }
// };
// enc_media_obj=enc_media_create(&enc_fun,(void*)dec_media_obj);
// uint32_t timestamp=0;
// for (size_t i = 0; i < FRAME_SIZE; i++)
// {
// uint8_t * ptr=(uint8_t*)rawframe[i];
// printf("\n");
// printf("type: %d timestamp:%d lens:%d\n", H264_TYPE(*(rawframe[i]+4)), timestamp, FRAME_LEN);
// for (size_t i = 0; i < FRAME_LEN; i++)
// {
// printf("%02X ", *(ptr++));
// }
// printf("\n");
// printf("//////////////////Raw data//////////////////// ");
// enc_media_decoded(enc_media_obj,(uint8_t*)rawframe[i],FRAME_LEN,timestamp,H264_TYPE(*(rawframe[i]+4)),4);
// timestamp+=100;
// }
// dec_media_release(dec_media_obj);
// enc_media_release(enc_media_obj);
// return 0;
// }
HWSign_release(sign_handle);
}

506
test/main.cppbak Normal file
View File

@ -0,0 +1,506 @@
#include <asm/byteorder.h>
#include<stdlib.h>
#include<stdio.h>
#include "common.h"
#include "SVAC/src/svac_src/svac_dec.h"
#include "SVAC/src/svac_src/SM2_SM3.h"
#include "SVAC/src/sm2sm3/sm2.h"
#include "DecEnc/NALUdecode.h"
#include "DecEnc/base64.h"
// int main(){
// uint8_t data[sizeof(Hi_Dec_Des_t)];
// Hi_Dec_Des_t * set=(Hi_Dec_Des_t *)data;
// memset(data,0x00,sizeof(Hi_Dec_Des_t));
// set->data.def_version[0]=0x00;
// set->data.def_version[1]=0x01;
// set->data.packed_type=(uint8_t)Hi_Packed_type_e::SINGLE;
// set->data.encrypt_arithmetic=0x01;
// sizeof(Hi_Sec_t);
// sizeof(Hi_Enc_t);
// sizeof(Hi_Sec_Enc_t);
// sizeof(Hi_Sec_Enc_Sign_t);
// return 0;
// }
struct SM2Config{
char prikey_size;
char pubkey_size;
uint8_t prikey[64];
uint8_t pubkey[128];
};
SecMedia::NALUdecodeInfo set_enc_config(){
//生成安全参数集
uint8_t camera_id[21] = "34020000001320000004";
//uint8_t evek[128] = {1}; //加密秘钥的秘钥
uint8_t vkek[128] ={0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,0x00};
uint8_t evektmp2[128] = {0};
uint8_t vkek_version[32] = "2021-07-06T17:27:19.000";
uint8_t iv[64] = {0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,0x00};
uint8_t camera_idc[20] = {4,1,2,3,4,5,6,7,8,9};
int eveki = 0;
SecMedia::NALUdecodeInfo nalu;
nalu.Flag.encryption_flag = 0;
nalu.Flag.authnetication_flag = 1;
nalu.Flag.vek_flag = 1;
nalu.Flag.iv_flag = 1;
nalu.Flag.hash_discard_p_picture = 1;
nalu.Flag.reserved_flag = 0b001;
nalu.Type.signature_type = (uint8_t) SecMedia::DecryptType::SM2_auth;
nalu.Type.hash_type =(uint8_t) SecMedia::DecryptType::SM3;
nalu.Type.encryption_type =(uint8_t) SecMedia::DecryptType::AES;
nalu.Type.vek_encryption_type = (uint8_t) SecMedia::DecryptType::SM4;
memset(nalu.camera_id,0,sizeof(nalu.camera_id));
memcpy(nalu.camera_id, camera_id, 20);
/// <变为加密数据集>
/// ///////////////////////////////////
/// </summary>
nalu.evek_length_minus1 = 31; // @@在加密复用中为vkek密钥长度
memcpy(nalu.evek, vkek, 128); // @@在加密复用中为vkek密钥
nalu.vkek_version_length_minus1 = 31; //@@在加密复用中:为视频密钥长度
////////////////////////////////////////////
memcpy(nalu.vkek_version, vkek_version, 32);
nalu.iv_length_minus1 = 31;
memcpy(nalu.iv, iv, 64);
nalu.successive_hash_pictures_minus1 = 25;
memcpy(nalu.camera_idc, camera_idc, 20);
nalu.end_flag = 0x80;
return nalu;
}
SM2Config set_auth(){
SM2Config sm2fig={
32,
64,
{
0x24,0x88,0xc8,0xdc,0x7f,0xd7,0xe0,0x91,0x30,0x1b,0x5c,0x58,0x2f,0xe7,0x44,0x7d,
0x2f,0x43,0xe4,0xee,0xc8,0x7d,0xc0,0xfb,0xa4,0xb8,0x7d,0x4b,0x8a,0x69,0x7c,0x4e
},
{
0xaa,0xb1,0x3f,0xd7,0x66,0xe2,0x75,0x97,0xc0,0x03,0xe6,0xe4,0x1d,0x77,0x54,0x78,
0xc8,0x29,0xb2,0x0b,0x9e,0xd1,0xff,0xa3,0x6a,0x6f,0xd2,0x7f,0xd6,0x2d,0xaa,0x3f,
0xc9,0x24,0xec,0x6c,0x96,0x0a,0x7b,0x73,0xf6,0xe6,0xfc,0xda,0x3a,0x08,0xfd,0x92,
0xfc,0x00,0x08,0x97,0x78,0x2c,0x71,0x6b,0xe1,0x26,0xf5,0x1e,0xba,0x31,0xf5,0xb2,
}
};
return sm2fig;
}
int base64Turl(const char* indata,int in_len,char * out){
char tmp,*p=out;
for (int i = 0; i < in_len; i++)
{
tmp=indata[i];
if (tmp=='+')
{
*out++='%';
*out++='2';
*out++='B';
}else if (tmp=='/')
{
*out++='%';
*out++='2';
*out++='F';
}else
{
*out++ = tmp;
}
}
return out-p;
}
#define ENCODE_SIZE sizeof(SecMedia::NALUdecodeInfo)*10
int gen_sec_set_bases64_url(){
auto retsrc=set_enc_config();
uint8_t* sec=(uint8_t *)&retsrc;
char encoded[ENCODE_SIZE],encoded_url[ENCODE_SIZE];
uint8_t dec[ sizeof(SecMedia::NALUdecodeInfo)];
memset(encoded,0x00,ENCODE_SIZE);
memset(encoded_url,0x00,ENCODE_SIZE);
memset(dec,0x00,ENCODE_SIZE);
auto ret=av_base64_encode(encoded,ENCODE_SIZE,sec,sizeof(SecMedia::NALUdecodeInfo));
if (!ret)
{
return -1;
}
// printf("\n");
// // printf("%s\n",encoded);
// printf("\n");
base64Turl(encoded,strlen(encoded),encoded_url);
// printf("%s\n",encoded_url);
printf("\n");
// ////test
// int len=av_base64_decode(dec,encoded,sizeof(dec));
// // printf("\n");
// printf("%d",memcmp(sec,dec,sizeof(SecMedia::NALUdecodeInfo)));
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// http://127.0.0.1/index/api/SetAuthHashCig?deviceID=(cameraID)&KeyVersion=123&DecryptType=2&ConfigData=base64(Authinfo)
char auth[ENCODE_SIZE],auth_base[ENCODE_SIZE];
auto auth_s=set_auth();
memset(auth,0x00,ENCODE_SIZE);
memset(auth_base,0x00,ENCODE_SIZE);
auto ret_auth=av_base64_encode(auth_base,ENCODE_SIZE,(uint8_t *)&auth_s,sizeof(SM2Config));
if (!ret_auth)
{
return -1;
}
base64Turl(auth_base,strlen(auth_base),auth);
char auth_url[]="http://127.0.0.1%s/index/api/SetAuthHashCig?deviceID=%s&KeyVersion=%s&DecryptType=%d&ConfigData=%s";
//printf("%s",auth);
uint8_t camera_id[21] = "34020000001320000003";
memset(camera_id,0,sizeof(camera_id));
memcpy(camera_id, retsrc.camera_id, 20);
printf(auth_url,":4080",camera_id,retsrc.vkek_version,retsrc.Type.signature_type, auth);
printf("\n\n\n");
char enc_url[]="http://127.0.0.1%s/index/api/SetEncryptConfig?StreamId=%s&ConfigData=%s";
printf(enc_url,":4080","rtp_encoded/100001003",encoded_url);
printf("\n");
}
int main(){
}
// #include "Decrypt.h"
// #include "Encrypt.h"
// #define H264_TYPE(v) ((uint8_t)(v) & 0x1F)
// #define FRAME_SIZE 6
// #define FRAME_LEN 10
// const uint8_t rawframe[FRAME_SIZE][FRAME_LEN]={
// {0x00,0x00,0x00,0x01,0x61,0x01,0x02,0x03,0x04,0x05},
// {0x00,0x00,0x00,0x01,0x65,0x01,0x02,0x03,0x04,0x05},
// {0x00,0x00,0x00,0x01,0x67,0x01,0x02,0x03,0x04,0x05},
// {0x00,0x00,0x00,0x01,0x61,0xf1,0xf2,0xf3,0xf4,0xf5},
// {0x00,0x00,0x00,0x01,0x61,0x01,0x02,0x03,0x04,0x05},
// {0x00,0x00,0x00,0x01,0x61,0x01,0x02,0x03,0x04,0x05},
// };
// struct packet_t
// {
// uint8_t sid;
// uint8_t codecid;
// int flags;
// int64_t pts;
// int64_t dts;
// uint8_t *data;
// size_t size;
// size_t capacity;
// int vcl; // h.264/h.265 only
// };
// struct pes_t
// {
// uint16_t pn; // TS program number(0-ps)
// uint16_t pid; // PES PID : 13
// uint8_t sid; // PES stream_id : 8
// uint8_t codecid; // PMT/PSM stream_type : 8
// uint8_t cc; // continuity_counter : 4;
// uint8_t* esinfo; // es_info
// uint16_t esinfo_len;// es_info_length : 12
// uint32_t len; // PES_packet_length : 16;
// uint32_t reserved10 : 2;
// uint32_t PES_scrambling_control : 2;
// uint32_t PES_priority : 1;
// uint32_t data_alignment_indicator : 1;
// uint32_t copyright : 1;
// uint32_t original_or_copy : 1;
// uint32_t PTS_DTS_flags : 2;
// uint32_t ESCR_flag : 1;
// uint32_t ES_rate_flag : 1;
// uint32_t DSM_trick_mode_flag : 1;
// uint32_t additional_copy_info_flag : 1;
// uint32_t PES_CRC_flag : 1;
// uint32_t PES_extension_flag : 1;
// uint32_t PES_header_data_length : 8;
// int64_t pts;
// int64_t dts;
// int64_t ESCR_base;
// uint32_t ESCR_extension;
// uint32_t ES_rate;
// //uint8_t trick_mode;
// //uint32_t trick_mode_control : 3;
// //uint32_t field_id : 2;
// //uint32_t intra_slice_refresh : 1;
// //uint32_t frequency_truncation : 2;
// //uint8_t additional_copy_info;
// //int16_t previous_PES_packet_CRC;
// //uint32_t PES_private_data_flag : 1;
// //uint32_t pack_header_field_flag : 1;
// //uint32_t program_packet_sequence_counter_flag : 1;
// //uint32_t P_STD_buffer_flag : 1;
// //uint32_t reserved_ : 3;
// //uint32_t PES_extension_flag_2 : 1;
// //uint32_t PES_private_data_flag2 : 1;
// //uint8_t PES_private_data[128/8];
// //uint32_t pack_field_length : 8;
// struct packet_t pkt;
// };
// struct psm_t
// {
// uint32_t ver : 5; // version_number : 5;
// struct pes_t streams[16];
// size_t stream_count;
// };
// typedef struct _avc_timing_hrd_descriptor_t
// {
// uint32_t hrd_management_valid_flag : 1;
// uint32_t picture_and_timing_info_present : 1;
// uint32_t _90kHZ_flag : 1;
// uint32_t fixed_frame_rate_flag : 1;
// uint32_t temporal_poc_flag : 1;
// uint32_t picture_to_display_conversion_flag : 1;
// uint32_t N;
// uint32_t K;
// uint32_t num_unit_in_tick;
// } avc_timing_hrd_descriptor_t;
// size_t avc_timing_hrd_descriptor(const uint8_t* data, size_t bytes)
// {
// // 2.6.66 AVC timing and HRD descriptor(p112)
// size_t i;
// avc_timing_hrd_descriptor_t desc;
// // uint8_t descriptor_tag = data[0];
// size_t descriptor_len = data[1];
// assert(descriptor_len+2 <= bytes);
// i = 2;
// memset(&desc, 0, sizeof(desc));
// desc.hrd_management_valid_flag = (data[i] >> 7) & 0x01;
// desc.picture_and_timing_info_present = (data[i] >> 0) & 0x01;
// ++i;
// if(desc.picture_and_timing_info_present)
// {
// desc._90kHZ_flag = (data[i] >> 7) & 0x01;
// if(0 == desc._90kHZ_flag)
// {
// desc.N = (data[i+1] << 24) | (data[i+2] << 16) | (data[i+3] << 8) | data[i+4];
// desc.K = (data[i+5] << 24) | (data[i+6] << 16) | (data[i+7] << 8) | data[i+8];
// i += 8;
// }
// desc.num_unit_in_tick = (data[i+1] << 24) | (data[i+2] << 16) | (data[i+3] << 8) | data[i+4];
// i += 5;
// }
// desc.fixed_frame_rate_flag = (data[i] >> 7) & 0x01;
// desc.temporal_poc_flag = (data[i] >> 6) & 0x01;
// desc.picture_to_display_conversion_flag = (data[i] >> 5) & 0x01;
// return descriptor_len+2;
// }
// size_t mpeg_elment_descriptor(const uint8_t* data, size_t bytes)
// {
// uint8_t descriptor_tag = data[0];
// uint8_t descriptor_len = data[1];
// if ((size_t)descriptor_len + 2 > bytes)
// return bytes;
// size_t rt=0;
// rt=avc_timing_hrd_descriptor(data,bytes);
// printf("%d,%d,%d\n",descriptor_tag,descriptor_len,rt);
// return descriptor_len+2;
// }
// size_t psm_read(struct psm_t *psm, const uint8_t* data, size_t bytes)
// {
// size_t i, j, k;
// uint8_t current_next_indicator;
// uint8_t single_extension_stream_flag;
// uint16_t program_stream_map_length;
// uint16_t program_stream_info_length;
// uint16_t element_stream_map_length;
// uint16_t element_stream_info_length;
// // Table 2-41 - Program stream map(p79)
// assert(0x00==data[0] && 0x00==data[1] && 0x01==data[2] && 0xBC==data[3]);
// program_stream_map_length = (data[4] << 8) | data[5];
// if (program_stream_map_length < 3 || bytes < (size_t)program_stream_map_length + 6)
// return 0; // invalid data length
// //assert((0x20 & data[6]) == 0x00); // 'xx0xxxxx'
// current_next_indicator = (data[6] >> 7) & 0x01;
// single_extension_stream_flag = (data[6] >> 6) & 0x01;
// psm->ver = data[6] & 0x1F;
// //assert(data[7] == 0x01); // '00000001'
// // program stream descriptor
// program_stream_info_length = (data[8] << 8) | data[9];
// if ((size_t)program_stream_info_length + 4 + 2 /*element_stream_map_length*/ > (size_t)program_stream_map_length)
// return 0; // TODO: error
// // TODO: parse descriptor
// // program element stream
// i = 10 + program_stream_info_length;
// element_stream_map_length = (data[i] << 8) | data[i+1];
// /* Ignore es_map_length, trust psm_length */
// element_stream_map_length = program_stream_map_length - program_stream_info_length - 10;
// i += 2;
// psm->stream_count = 0;
// for(j = i; j + 4/*element_stream_info_length*/ <= i+element_stream_map_length && psm->stream_count < sizeof(psm->streams)/sizeof(psm->streams[0]); j += 4 + element_stream_info_length)
// {
// psm->streams[psm->stream_count].codecid = data[j];
// psm->streams[psm->stream_count].sid = data[j+1];
// psm->streams[psm->stream_count].pid = psm->streams[psm->stream_count].sid; // for ts PID
// element_stream_info_length = (data[j+2] << 8) | data[j+3];
// if (j + 4 + element_stream_info_length > i+element_stream_map_length)
// return 0; // TODO: error
// k = j + 4;
// if(0xFD == psm->streams[psm->stream_count].sid && 0 == single_extension_stream_flag)
// {
// if(element_stream_info_length < 3)
// return 0; // TODO: error
// // uint8_t pseudo_descriptor_tag = data[k];
// // uint8_t pseudo_descriptor_length = data[k+1];
// // uint8_t element_stream_id_extension = data[k+2] & 0x7F;
// assert((0x80 & data[k+2]) == 0x80); // '1xxxxxxx'
// k += 3;
// }
// while(k + 2 <= j + 4 + element_stream_info_length)
// {
// // descriptor()
// k += mpeg_elment_descriptor(data+k, j + 4 + element_stream_info_length - k);
// }
// ++psm->stream_count;
// assert(k - j - 4 == element_stream_info_length);
// }
// // assert(j+4 == program_stream_map_length+6);
// // assert(0 == mpeg_crc32(0xffffffff, data, program_stream_map_length+6));
// return program_stream_map_length+6;
// }
// int main(int argc, char *argv[]){
// psm_t psm_obj;
// uint8_t data1[]={0x00,0x00,0x01,0xbc,0x00,0x1e,0xe1,0xff,
// 0x00,0x00,0x00,0x14,0x80,0xe0,0x00,0x0c,0x2a,0x0a,0x7f,0xff,0x00,0x00,0x05,0xdc,
// 0x1f,0xfe,0xf0,0x87,0x90,0xc0,0x00,0x00,0x00,0x00,0x00,0x00};
// size_t rt=0;
// rt=psm_read(&psm_obj,(const uint8_t*)&data1,sizeof(data1));
// printf("%d\n",rt);
// }
// int main(int argc, char *argv[]){
// dec_media dec_media_obj;
// dec_media_func_t dec_fun={
// //alloc
// [](void* param, size_t bytes){
// return (void*)malloc(sizeof(uint8_t)*bytes);
// },
// //free
// [](void* param, void* packet){
// free(packet);
// },
// //write
// [](void* param, void* packet, size_t bytes,uint64_t timestamp,uint8_t frametype){
// printf("\n");
// printf("type: %d timestamp:%lld lens:%zd\n", frametype, timestamp, bytes);
// uint8_t * ptr=(uint8_t*)packet;
// for (size_t i = 0; i < bytes; i++)
// {
// printf("%02X ", *(ptr++));
// }
// printf("\n");
// printf("-----------Decrypted data----------------- ");
// }
// };
// dec_media_obj=dec_media_create(&dec_fun,NULL);
// enc_media enc_media_obj;
// enc_media_func_t enc_fun;
// enc_fun={
// //alloc
// [](void* param, size_t bytes){
// return (void*)malloc(sizeof(uint8_t)*bytes);
// },
// //free
// [](void* param, void* packet){
// free(packet);
// },
// //write
// [](void* param, void* packet, size_t bytes,uint64_t timestamp,uint8_t frametype){
// printf("\n");
// printf("type: %d timestamp:%lld lens:%zd\n", frametype, timestamp, bytes);
// uint8_t * ptr=(uint8_t*)packet;
// for (size_t i = 0; i < bytes; i++)
// {
// printf("%02X ", *(ptr++));
// }
// printf("\n");
// int ret=dec_security_SEI((uint8_t*)packet,bytes,4,0);
// if (ret>0)
// {
// printf("\nSecurity level: %d\n",ret);
// }
// printf("############Encrypted data############ ");
// dec_media_decoded((dec_media) param,(uint8_t*)packet,bytes,timestamp,frametype,4);
// }
// };
// enc_media_obj=enc_media_create(&enc_fun,(void*)dec_media_obj);
// uint32_t timestamp=0;
// for (size_t i = 0; i < FRAME_SIZE; i++)
// {
// uint8_t * ptr=(uint8_t*)rawframe[i];
// printf("\n");
// printf("type: %d timestamp:%d lens:%d\n", H264_TYPE(*(rawframe[i]+4)), timestamp, FRAME_LEN);
// for (size_t i = 0; i < FRAME_LEN; i++)
// {
// printf("%02X ", *(ptr++));
// }
// printf("\n");
// printf("//////////////////Raw data//////////////////// ");
// enc_media_decoded(enc_media_obj,(uint8_t*)rawframe[i],FRAME_LEN,timestamp,H264_TYPE(*(rawframe[i]+4)),4);
// timestamp+=100;
// }
// dec_media_release(dec_media_obj);
// enc_media_release(enc_media_obj);
// return 0;
// }