Skip to content

Commit dcb5805

Browse files
authored
Issue 65: Bring patches from amlogic and realtek into secapi2-adapter (rdkcentral#66)
Reason for change: Bring patches to secapi2-adapter, including adding SecCipher_ProcessOpaqueWithMapAndHandle. Risks: None Priority: P1
1 parent 6dfd27f commit dcb5805

File tree

4 files changed

+146
-21
lines changed

4 files changed

+146
-21
lines changed

include/sec_security.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -997,6 +997,9 @@ Sec_Result SecCipher_ProcessOpaqueWithMapAndPattern(Sec_CipherHandle* cipherHand
997997
SEC_SIZE inputSize, SEC_BOOL lastInput, SEC_MAP* map, SEC_SIZE mapLength, SEC_SIZE numEncryptedBlocks,
998998
SEC_SIZE numClearBlocks, Sec_OpaqueBufferHandle** opaqueBufferHandle, SEC_SIZE* bytesWritten);
999999

1000+
Sec_Result SecCipher_ProcessOpaqueWithMapAndHandle(Sec_CipherHandle* cipherHandle, SEC_BYTE* iv, uint32_t secureBufferHandle,
1001+
SEC_BYTE* input, SEC_SIZE inputSize, SEC_BOOL lastInput, SEC_MAP* map, SEC_SIZE mapLength, SEC_SIZE* bytesWritten);
1002+
10001003
#ifdef __cplusplus
10011004
}
10021005
#endif

include/sec_security_datatype.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,13 @@ typedef enum {
444444
SEC_KEYUSAGE_NUM
445445
} Sec_KeyUsage;
446446

447+
/**
448+
* @brief Sample size
449+
*/
450+
typedef enum {
451+
SEC_SAMPLE_SIZE = 1
452+
} Sec_SampleSize;
453+
447454
typedef struct {
448455
char keyId[40];
449456
SEC_BYTE rights[SEC_KEYOUTPUTRIGHT_NUM];

src/sec_adapter_cipher.c

Lines changed: 108 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -988,9 +988,10 @@ Sec_Result SecCipher_ProcessOpaqueWithMap(Sec_CipherHandle* cipherHandle, SEC_BY
988988
sample.out = &out_buffer;
989989
sample.in = &in_buffer;
990990

991-
sa_status status = sa_invoke(cipherHandle->processorHandle, SA_PROCESS_COMMON_ENCRYPTION, (size_t) 1, &sample);
991+
sa_status status = sa_invoke(cipherHandle->processorHandle, SA_PROCESS_COMMON_ENCRYPTION, (size_t) SEC_SAMPLE_SIZE, &sample);
992992
SEC_FREE(subsample_lengths);
993993
if (status != SA_STATUS_OK) {
994+
SEC_LOG_ERROR("Failed process encryption");
994995
SecOpaqueBuffer_Free(*opaqueBufferHandle);
995996
*opaqueBufferHandle = NULL;
996997
*bytesWritten = 0;
@@ -1082,7 +1083,7 @@ Sec_Result SecCipher_ProcessOpaqueWithMapAndPattern(Sec_CipherHandle* cipherHand
10821083
sample.out = &out_buffer;
10831084
sample.in = &in_buffer;
10841085

1085-
sa_status status = sa_invoke(cipherHandle->processorHandle, SA_PROCESS_COMMON_ENCRYPTION, (size_t) 1, &sample);
1086+
sa_status status = sa_invoke(cipherHandle->processorHandle, SA_PROCESS_COMMON_ENCRYPTION, (size_t) SEC_SAMPLE_SIZE, &sample);
10861087
SEC_FREE(subsample_lengths);
10871088
if (status != SA_STATUS_OK) {
10881089
SecOpaqueBuffer_Free(*opaqueBufferHandle);
@@ -1095,6 +1096,111 @@ Sec_Result SecCipher_ProcessOpaqueWithMapAndPattern(Sec_CipherHandle* cipherHand
10951096
return SEC_RESULT_SUCCESS;
10961097
}
10971098

1099+
Sec_Result SecCipher_ProcessOpaqueWithMapAndHandle(Sec_CipherHandle* cipherHandle, SEC_BYTE* iv, uint32_t secureBufferHandle,
1100+
SEC_BYTE* input, SEC_SIZE inputSize, SEC_BOOL lastInput, SEC_MAP* map, SEC_SIZE mapLength, SEC_SIZE* bytesWritten) {
1101+
1102+
if (cipherHandle == NULL) {
1103+
SEC_LOG_ERROR("NULL cipherHandle");
1104+
return SEC_RESULT_FAILURE;
1105+
}
1106+
1107+
if (iv == NULL) {
1108+
SEC_LOG_ERROR("NULL iv");
1109+
return SEC_RESULT_FAILURE;
1110+
}
1111+
1112+
if (secureBufferHandle == 0) {
1113+
SEC_LOG_ERROR("Buffer Handle is 0");
1114+
return SEC_RESULT_FAILURE;
1115+
}
1116+
1117+
if (map == NULL) {
1118+
SEC_LOG_ERROR("NULL map");
1119+
return SEC_RESULT_FAILURE;
1120+
}
1121+
1122+
if (bytesWritten == NULL) {
1123+
SEC_LOG_ERROR("NULL bytesWritten");
1124+
return SEC_RESULT_FAILURE;
1125+
}
1126+
1127+
// wrap in a secapi2 adapter buffer
1128+
Sec_OpaqueBufferHandle* opaqueBufferHandle = NULL;
1129+
Sec_Result result = SecOpaqueBuffer_Create(&opaqueBufferHandle, (void*)(uintptr_t) secureBufferHandle, inputSize);
1130+
if (result != SEC_RESULT_SUCCESS) {
1131+
SEC_LOG_ERROR("SecOpaqueBuffer_Create failed with preallocated memory: %" PRIu32 "", secureBufferHandle);
1132+
return SEC_RESULT_FAILURE;
1133+
}
1134+
1135+
sa_subsample_length* subsample_lengths = malloc(mapLength * sizeof(sa_subsample_length));
1136+
for (size_t i = 0; i < mapLength; i++) {
1137+
subsample_lengths[i].bytes_of_clear_data = map[i].clear;
1138+
subsample_lengths[i].bytes_of_protected_data = map[i].encrypted;
1139+
}
1140+
1141+
sa_buffer out_buffer;
1142+
out_buffer.buffer_type = SA_BUFFER_TYPE_SVP;
1143+
out_buffer.context.svp.offset = 0;
1144+
out_buffer.context.svp.buffer = get_svp_buffer(cipherHandle->processorHandle, opaqueBufferHandle);
1145+
if (out_buffer.context.svp.buffer == INVALID_HANDLE) {
1146+
free(subsample_lengths);
1147+
SEC_LOG_ERROR("Failed create buffer from handle: %" PRIu32 "", secureBufferHandle);
1148+
1149+
// Memory is pre-allocated elsewhere so we don't wan't to free it, just
1150+
// release our handle to it.
1151+
Sec_ProtectedMemHandle* h = NULL;
1152+
SecOpaqueBuffer_Release(opaqueBufferHandle, &h);
1153+
opaqueBufferHandle = NULL;
1154+
*bytesWritten = 0;
1155+
return SEC_RESULT_FAILURE;
1156+
}
1157+
1158+
sa_buffer in_buffer;
1159+
in_buffer.buffer_type = SA_BUFFER_TYPE_CLEAR;
1160+
in_buffer.context.clear.buffer = input;
1161+
in_buffer.context.clear.length = inputSize;
1162+
in_buffer.context.clear.offset = 0;
1163+
1164+
sa_sample sample;
1165+
sample.iv = iv;
1166+
sample.iv_length = SEC_AES_BLOCK_SIZE;
1167+
sample.crypt_byte_block = 0;
1168+
sample.skip_byte_block = 0;
1169+
sample.subsample_count = mapLength;
1170+
sample.subsample_lengths = subsample_lengths;
1171+
sample.context = cipherHandle->cipher.context;
1172+
sample.out = &out_buffer;
1173+
sample.in = &in_buffer;
1174+
1175+
sa_status status = sa_invoke(cipherHandle->processorHandle, SA_PROCESS_COMMON_ENCRYPTION, (size_t) SEC_SAMPLE_SIZE, &sample);
1176+
free(subsample_lengths);
1177+
1178+
if (status != SA_STATUS_OK) {
1179+
SEC_LOG_ERROR("Failed process encryption");
1180+
// Memory is pre-allocated elsewhere so we don't wan't to free it, just
1181+
// release our handle to it.
1182+
Sec_ProtectedMemHandle* h = NULL;
1183+
SecOpaqueBuffer_Release(opaqueBufferHandle, &h);
1184+
1185+
*bytesWritten = 0;
1186+
CHECK_STATUS(status)
1187+
}
1188+
else
1189+
{
1190+
// Higher layers will use the pre-allocated memory directly, not our buffer.
1191+
Sec_ProtectedMemHandle* h = NULL;
1192+
const Sec_Result r = SecOpaqueBuffer_Release(opaqueBufferHandle, &h);
1193+
1194+
if (r != SEC_RESULT_SUCCESS)
1195+
{
1196+
SEC_LOG_ERROR("Failed to release buffer from pre-allocated memory");
1197+
}
1198+
}
1199+
1200+
*bytesWritten = out_buffer.context.svp.offset;
1201+
return SEC_RESULT_SUCCESS;
1202+
}
1203+
10981204
Sec_Result get_cipher_algorithm(const Sec_CipherAlgorithm algorithm, SEC_BOOL is_unwrap,
10991205
sa_cipher_algorithm* cipher_algorithm, void** parameters, void* iv, SEC_SIZE key_length, SEC_SIZE key_offset) {
11001206
*parameters = NULL;

src/sec_adapter_engine.c

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2020-2023 Comcast Cable Communications Management, LLC
2+
* Copyright 2020-2025 Comcast Cable Communications Management, LLC
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -29,6 +29,8 @@ static SEC_BOOL g_sec_openssl_inited = SEC_FALSE;
2929
static RSA_METHOD* rsa_method = NULL;
3030
#endif
3131

32+
static ENGINE* engine = NULL;
33+
3234
static void Sec_ShutdownOpenSSL() {
3335
#if OPENSSL_VERSION_NUMBER >= 0x10100000L
3436
if (rsa_method != NULL) {
@@ -37,11 +39,10 @@ static void Sec_ShutdownOpenSSL() {
3739
}
3840
#endif
3941

40-
ENGINE* engine = ENGINE_by_id(SECAPI_ENGINE_ID);
4142
if (engine != NULL) {
42-
ENGINE_remove(engine);
4343
ENGINE_finish(engine);
4444
ENGINE_free(engine);
45+
engine = NULL;
4546
}
4647
}
4748

@@ -62,7 +63,7 @@ static int Sec_OpenSSLPrivSign(int type, const unsigned char* m, unsigned int m_
6263
SEC_LOG_ERROR("Unknown type %d", type);
6364
return -1;
6465
}
65-
66+
SEC_PRINT("Calling Sec_OpenSSLPrivSign with %s", OPENSSL_VERSION_TEXT);
6667
keyHandle = (Sec_KeyHandle*) RSA_get_app_data(rsa);
6768
if (keyHandle == NULL) {
6869
SEC_LOG_ERROR("NULL keyHandle encountered");
@@ -96,6 +97,7 @@ static int Sec_OpenSSLPubVerify(int type, const unsigned char* m, unsigned int m
9697
return -1;
9798
}
9899

100+
SEC_PRINT("Calling Sec_OpenSSLPubVerify with %s", OPENSSL_VERSION_TEXT);
99101
keyHandle = (Sec_KeyHandle*) RSA_get_app_data(rsa);
100102
if (keyHandle == NULL) {
101103
SEC_LOG_ERROR("NULL keyHandle encountered");
@@ -129,6 +131,7 @@ static int Sec_OpenSSLPubEncrypt(int flen, const unsigned char* from, unsigned c
129131
return -1;
130132
}
131133

134+
SEC_PRINT("Calling Sec_OpenSSLPubEncrypt with %s", OPENSSL_VERSION_TEXT);
132135
keyHandle = (Sec_KeyHandle*) RSA_get_app_data(rsa);
133136
if (keyHandle == NULL) {
134137
SEC_LOG_ERROR("NULL keyHandle encountered");
@@ -162,6 +165,7 @@ static int Sec_OpenSSLPrivDecrypt(int flen, const unsigned char* from, unsigned
162165
return -1;
163166
}
164167

168+
SEC_PRINT("Calling Sec_OpenSSLPrivDecrypt with %s", OPENSSL_VERSION_TEXT);
165169
keyHandle = (Sec_KeyHandle*) RSA_get_app_data(rsa);
166170
if (keyHandle == NULL) {
167171
SEC_LOG_ERROR("NULL keyHandle encountered");
@@ -199,7 +203,8 @@ static RSA_METHOD g_sec_openssl_rsamethod = {
199203
#endif
200204

201205
static void ENGINE_load_securityapi(void) {
202-
ENGINE* engine = ENGINE_new();
206+
engine = ENGINE_new();
207+
SEC_PRINT("*****ENGINE_load_securityapi***** \n");
203208
if (engine == NULL) {
204209
SEC_LOG_ERROR("ENGINE_new failed");
205210
return;
@@ -208,28 +213,34 @@ static void ENGINE_load_securityapi(void) {
208213
if (!ENGINE_set_id(engine, SECAPI_ENGINE_ID)) {
209214
SEC_LOG_ERROR("ENGINE_set_id failed");
210215
ENGINE_free(engine);
216+
engine = NULL;
211217
return;
212218
}
213219
if (!ENGINE_set_name(engine, "SecurityApi engine")) {
214220
SEC_LOG_ERROR("ENGINE_set_name failed");
215221
ENGINE_free(engine);
222+
engine = NULL;
216223
return;
217224
}
218225

219226
if (!ENGINE_init(engine)) {
220227
SEC_LOG_ERROR("ENGINE_init failed");
221228
ENGINE_free(engine);
229+
engine = NULL;
222230
return;
223231
}
224232

225233
#if OPENSSL_VERSION_NUMBER < 0x10100000L
234+
SEC_PRINT("******Calling ENGINE_set_RSA 1 **** \n");
226235
if (!ENGINE_set_RSA(engine, &g_sec_openssl_rsamethod)) {
227236
#else
228237
if (rsa_method == NULL) {
238+
SEC_PRINT("*****Sec_InitOpenSSL creating RSA method****** \n");
229239
rsa_method = RSA_meth_new("securityapi RSA method", RSA_METHOD_FLAG_NO_CHECK | RSA_FLAG_EXT_PKEY);
230240
if (rsa_method == NULL) {
231241
SEC_LOG_ERROR("RSA_meth_new failed");
232242
ENGINE_free(engine);
243+
engine = NULL;
233244
return;
234245
}
235246

@@ -238,26 +249,27 @@ static void ENGINE_load_securityapi(void) {
238249
RSA_meth_set_sign(rsa_method, Sec_OpenSSLPrivSign);
239250
RSA_meth_set_verify(rsa_method, Sec_OpenSSLPubVerify);
240251
}
241-
252+
253+
SEC_PRINT("*****Calling ENGINE_set_RSA 2***** \n");
242254
if (!ENGINE_set_RSA(engine, rsa_method)) {
243255
#endif
244-
ENGINE_remove(engine);
245256
ENGINE_free(engine);
257+
engine = NULL;
246258
return;
247259
}
248260

249-
ENGINE_add(engine);
250-
ENGINE_free(engine);
251261
ERR_clear_error();
252262
}
253263

254264
void Sec_InitOpenSSL() {
255265
static pthread_mutex_t init_openssl_mutex = PTHREAD_MUTEX_INITIALIZER;
256266

257267
pthread_mutex_lock(&init_openssl_mutex);
268+
SEC_PRINT("***** Sec_InitOpenSSL****** \n");
258269

259270
if (g_sec_openssl_inited != SEC_TRUE) {
260271
#if OPENSSL_VERSION_NUMBER < 0x10100000L
272+
SEC_PRINT("*****Sec_InitOpenSSL OpenSSL < 1.1.0****** \n");
261273
ERR_load_crypto_strings();
262274
OpenSSL_add_all_algorithms();
263275
OpenSSL_add_all_ciphers();
@@ -276,9 +288,9 @@ void Sec_InitOpenSSL() {
276288

277289
ENGINE_set_default(engine, ENGINE_METHOD_ALL);
278290
ENGINE_free(engine);
291+
engine = NULL;
279292
}
280293

281-
ENGINE_load_securityapi();
282294

283295
if (atexit(Sec_ShutdownOpenSSL) != 0) {
284296
SEC_LOG_ERROR("atexit failed");
@@ -288,6 +300,10 @@ void Sec_InitOpenSSL() {
288300
g_sec_openssl_inited = SEC_TRUE;
289301
}
290302

303+
if (engine == NULL) {
304+
ENGINE_load_securityapi();
305+
}
306+
291307
pthread_mutex_unlock(&init_openssl_mutex);
292308
}
293309

@@ -300,21 +316,19 @@ RSA* SecKey_ToEngineRSA(Sec_KeyHandle* keyHandle) {
300316
Sec_RSARawPublicKey pubKey;
301317
RSA* rsa = NULL;
302318

303-
ENGINE* engine = ENGINE_by_id(SECAPI_ENGINE_ID);
304319
if (engine == NULL) {
305320
SEC_LOG_ERROR("ENGINE_by_id failed");
321+
SEC_LOG_ERROR("engine not initialized");
306322
return NULL;
307323
}
308324

309325
if (SEC_RESULT_SUCCESS != SecKey_ExtractRSAPublicKey(keyHandle, &pubKey)) {
310-
ENGINE_free(engine);
311326
SEC_LOG_ERROR("SecKey_ExtractRSAPublicKey failed");
312327
return NULL;
313328
}
314329

315330
rsa = RSA_new_method(engine);
316331
if (rsa == NULL) {
317-
ENGINE_free(engine);
318332
SEC_LOG_ERROR("RSA_new_method failed");
319333
return NULL;
320334
}
@@ -327,30 +341,27 @@ RSA* SecKey_ToEngineRSA(Sec_KeyHandle* keyHandle) {
327341
BN_bin2bn(pubKey.e, 4, NULL), NULL);
328342
#endif
329343

344+
SEC_PRINT("Calling SecKey_ToEngineRSA with %s", OPENSSL_VERSION_TEXT);
330345
RSA_set_app_data(rsa, keyHandle);
331-
ENGINE_free(engine);
332346
return rsa;
333347
}
334348

335349
RSA* SecKey_ToEngineRSAWithCert(Sec_KeyHandle* keyHandle, Sec_CertificateHandle* certificateHandle) {
336350
Sec_RSARawPublicKey pubKey;
337351
RSA* rsa = NULL;
338352

339-
ENGINE* engine = ENGINE_by_id(SECAPI_ENGINE_ID);
340353
if (engine == NULL) {
341354
SEC_LOG_ERROR("ENGINE_by_id failed");
342355
return NULL;
343356
}
344357

345358
if (SEC_RESULT_SUCCESS != SecCertificate_ExtractRSAPublicKey(certificateHandle, &pubKey)) {
346-
ENGINE_free(engine);
347359
SEC_LOG_ERROR("SecKey_ExtractRSAPublicKey failed");
348360
return NULL;
349361
}
350362

351363
rsa = RSA_new_method(engine);
352364
if (rsa == NULL) {
353-
ENGINE_free(engine);
354365
SEC_LOG_ERROR("RSA_new_method failed");
355366
return NULL;
356367
}
@@ -362,9 +373,7 @@ RSA* SecKey_ToEngineRSAWithCert(Sec_KeyHandle* keyHandle, Sec_CertificateHandle*
362373
RSA_set0_key(rsa, BN_bin2bn(pubKey.n, (int) Sec_BEBytesToUint32(pubKey.modulus_len_be), NULL),
363374
BN_bin2bn(pubKey.e, 4, NULL), NULL);
364375
#endif
365-
366376
RSA_set_app_data(rsa, keyHandle);
367-
ENGINE_free(engine);
368377
return rsa;
369378
}
370379

0 commit comments

Comments
 (0)