22 #include "rdm_rsa_signature_verify.h"
24 #if defined(DEBUG_ENABLED)
25 static time_t timebuffer;
43 #if defined(DEBUG_ENABLED)
47 struct tm *tm = localtime(&timebuffer);
49 static int filecount=0;
51 memset(s,0,
sizeof(s));
52 strftime(s,
sizeof(s),
"%T-", tm);
54 sdebug_print(s+strlen(s),
"%d-",filecount);
56 strncat(s,name,
sizeof(s)-strlen(name)-1);
57 strncat(s,
".bin",
sizeof(s)-strlen(
".bin")-1);
61 for(i = 0;i < buffer_size;++i) {
62 debug_print(
"buffer[%d]=(%c) [%2.2x]\n",i, ((
char *)buffer)[i],((
unsigned char *)buffer)[i]);
65 FILE *binout = fopen(s,
"wb");
66 if ( binout == NULL )
return;
67 fwrite(buffer,buffer_size,1,binout);
87 int asciihex_to_bin(
const char *asciihex,
size_t asciihex_length,
unsigned char *bin,
size_t *bin_length )
89 if ( asciihex == NULL || bin == NULL || bin_length == NULL || (asciihex_length & 1) ) {
92 if ( *bin_length < asciihex_length/2 ) {
93 *bin_length = asciihex_length/2;
97 while ( asciihex_length > 0 ) {
98 unsigned char uc = (*asciihex++);
99 if ( uc >
'9' ) { uc &= ~0x20; uc -= (
'A'-10); }
else { uc -=
'0'; }
102 if ( uc >
'9' ) { uc &= ~0x20; uc -= (
'A'-10); }
else { uc -=
'0'; }
104 asciihex_length -= 2;
128 int bin_to_asciihex(
const unsigned char *bin,
size_t bin_length,
char *asciihex,
size_t *asciihex_length )
130 if ( bin == NULL || asciihex == NULL || asciihex_length == NULL ) {
133 if ( *asciihex_length < bin_length * 2 ) {
134 *asciihex_length = bin_length * 2;
138 for( i=0; i < bin_length; i++, bin++ ) {
139 unsigned char c = (*bin >> 4) +
'0';
144 c = (*bin & 0x0f) +
'0';
153 static int prepare_sig_file(
char *sig_file) {
154 char buffer[512] = {0};
155 int read = 0, skip = 0, len = 0;
156 char *mv_command = NULL;
158 FILE *file_in = fopen(sig_file,
"r");
159 FILE *file_out = fopen(RDM_TMP_SIGFILE,
"w+");
161 if (NULL == file_in || NULL == file_out)
164 while((read = fread(buffer,
sizeof(
char),
sizeof(buffer), file_in)) > 0)
166 if(0 == skip && read > 6) {
168 fwrite(&buffer[6],
sizeof(
char), read-6, file_out);
171 fwrite(buffer,
sizeof(
char), read, file_out);
178 len = strlen(
"/bin/mv") + strlen(RDM_TMP_SIGFILE) + strlen(sig_file) + 3;
179 mv_command = (
char*) calloc(len,
sizeof(
char));
180 sprintf(mv_command,
"/bin/mv %s %s", RDM_TMP_SIGFILE, sig_file);
188 static int prepare_app_manifest(
char *etc_manifest_file,
char *cache_manifest_file,
char* padding_file,
char *prefix) {
191 char *new_line = NULL;
193 FILE *file_in = fopen(etc_manifest_file,
"r");
194 FILE *file_out = fopen(cache_manifest_file,
"w+");
196 if(file_in == NULL || file_out == NULL) {
200 while(getline(&line, &len, file_in) != -1) {
201 if (NULL == new_line)
202 new_line = (
char *) malloc(
sizeof(
char) * (strlen(line) + strlen(prefix) + 1));
204 new_line = (
char *) realloc(new_line,
sizeof(
char) * (strlen(line) + strlen(prefix) + 1));
205 sprintf(new_line,
"%s%s", prefix, line);
206 fwrite(new_line,
sizeof(
char), strlen(new_line), file_out);
210 fprintf(file_out,
"%s\n", padding_file);
223 static int prepare_kms_pubkey() {
225 ret = system(
"/usr/bin/configparamgen jx /etc/rdm/vjyrepbsb.ijv /tmp/vstuvwx.file");
226 return WEXITSTATUS(ret);
239 char *app_cache_dir = NULL;
240 char *app_home_dir = NULL;
241 char *path_to_sig_file = NULL;
242 char *cache_app_manifest = NULL;
243 char *etc_app_manifest = NULL;
244 char *path_to_padding_file = NULL;
247 len = strlen(cache_dir) + strlen(RDM_DOWNLOADS_DIR) + strlen(app_name) + 2;
248 app_cache_dir = (
char*) calloc(len,
sizeof(
char));
249 sprintf(app_cache_dir,
"%s%s%s/", cache_dir, RDM_DOWNLOADS_DIR, app_name);
251 len = strlen(cache_dir) + strlen(app_name) + 3;
252 app_home_dir = (
char*) calloc(len,
sizeof(
char));
253 sprintf(app_home_dir,
"%s/%s/", cache_dir, app_name);
255 len = strlen(app_cache_dir) + strlen(app_name) + strlen(RDM_SIGFILE_SUFFIX) + 1;
256 path_to_sig_file = (
char*) calloc(len,
sizeof(
char));
257 sprintf(path_to_sig_file,
"%s%s%s", app_cache_dir, app_name, RDM_SIGFILE_SUFFIX);
259 ret = prepare_sig_file(path_to_sig_file);
261 free(path_to_sig_file);
269 len = strlen(RDM_MANIFEST_DIR) + strlen(app_name) + strlen(RDM_MANIFEST_SUFFIX) + 1;
270 etc_app_manifest = (
char*) calloc(len,
sizeof(
char));
271 sprintf(etc_app_manifest,
"%s%s%s", RDM_MANIFEST_DIR, app_name, RDM_MANIFEST_SUFFIX);
273 len = strlen(app_home_dir) + strlen(app_name) + strlen(RDM_MANIFEST_SUFFIX) + 1;
274 cache_app_manifest = (
char*) calloc(len,
sizeof(
char));
275 sprintf(cache_app_manifest,
"%s%s%s", app_home_dir, app_name, RDM_MANIFEST_SUFFIX);
277 len = strlen(app_cache_dir) + strlen(RDM_KMS_PADDING_FILE) + 1;
278 path_to_padding_file = (
char*) calloc(len,
sizeof(
char));
279 sprintf(path_to_padding_file,
"%s%s", app_cache_dir, RDM_KMS_PADDING_FILE);
281 ret = prepare_app_manifest(etc_app_manifest, cache_app_manifest, path_to_padding_file, app_home_dir);
283 free(path_to_padding_file);
286 free(cache_app_manifest);
287 free(etc_app_manifest);
293 ret = prepare_kms_pubkey();
305 int status = 1, len = 0;
308 char *dataFile=NULL, *sigFile=NULL;
310 if (NULL == cache_dir || NULL == app_name)
313 if (1 == prepare_files) {
315 printf(
"prepare_rdm_files_for_sign_verification failed\n");
319 if (0 != prepare_kms_pubkey()) {
320 printf(
"prepare_kms_pubkey failed\n");
328 len = strlen(cache_dir) + 2 * strlen(app_name) + strlen(RDM_MANIFEST_SUFFIX) + 3;
329 dataFile = (
char*) calloc(len,
sizeof(
char));
330 sprintf(dataFile,
"%s/%s/%s%s", cache_dir, app_name, app_name, RDM_MANIFEST_SUFFIX);
332 len = strlen(cache_dir) + strlen(RDM_DOWNLOADS_DIR) + 2 * strlen(app_name) + strlen(RDM_SIGFILE_SUFFIX) + 2;
333 sigFile = (
char*) calloc(len,
sizeof(
char));
334 sprintf(sigFile,
"%s%s%s/%s%s", cache_dir, RDM_DOWNLOADS_DIR, app_name, app_name, RDM_SIGFILE_SUFFIX);
338 printf(
"RSA Signature Validation Success\n");
341 printf(
"RSA Signature Verification Failed\n");
347 unlink(RDM_KMS_PUB_KEY);
358 static int ssl_init=0;
360 if ( ssl_init ) {
return;}
362 ERR_load_crypto_strings();
365 OpenSSL_add_all_algorithms();
368 OPENSSL_config(NULL);
370 #if defined(DEBUG_ENABLED)
371 timebuffer = time(NULL);
392 char buf[RSA2048_ASCII_SIGNATURE_LEN];
395 static unsigned char sig[RSA2048_SIGNATURE_LEN];
397 if ( sig_file == NULL || sig_buffer == NULL || sig_size == NULL ) {
406 sig_fh = fopen( sig_file,
"r" );
407 if ( sig_fh == NULL ) {
408 debug_print(
"read_signature_file file open error\n");
412 size_t nread = fread( buf, 1, RSA2048_ASCII_SIGNATURE_LEN, sig_fh );
413 if ( nread != (RSA2048_ASCII_SIGNATURE_LEN) || ferror( sig_fh ) ) {
415 debug_print(
"read_signature_file file read error\n");
420 #if defined(DEBUG_ENABLED)
421 char buf2[RSA2048_ASCII_SIGNATURE_LEN + 1];
422 memcpy(buf2, buf ,RSA2048_ASCII_SIGNATURE_LEN);
423 buf2[RSA2048_ASCII_SIGNATURE_LEN] = 0;
426 size_t sig_len =
sizeof( sig );
427 if (
asciihex_to_bin( buf, RSA2048_ASCII_SIGNATURE_LEN, sig, &sig_len ) != 0 ) {
431 *sig_size = RSA2048_SIGNATURE_LEN;
442 int manifest_file_size(
const char *data_file,
int *buffer_size)
446 data_fh = fopen( data_file,
"r" );
447 if ( data_fh == NULL )
449 debug_print(
"manifest_file_size(): datafile open error\n");
453 fseek(data_fh, 0, SEEK_END);
455 *buffer_size = ftell(data_fh) + 1;
457 if ( fseek( data_fh, 0, SEEK_END ) != 0 ) {
462 if ( ferror( data_fh ) ) {
468 if ( data_fh != NULL ) fclose( data_fh );
496 if ( manifest_file_size(data_file, &BUFSIZE) != 0 )
498 EVP_MD_CTX *mdctx=NULL;
500 unsigned char* buffer= (
unsigned char*)calloc(
sizeof(
unsigned char), BUFSIZE );
503 debug_print(
"rdm_openssl_file_hash_sha256() Entry\n");
505 if ( data_file == NULL || hash_buffer == NULL || buffer_len == NULL ) {
506 debug_print(
"rdm_openssl_file_hash_sha256(): Invalid param error\n");
510 if ( *buffer_len < SHA256_DIGEST_LENGTH ) {
511 *buffer_len = SHA256_DIGEST_LENGTH;
512 debug_print(
"rdm_openssl_file_hash_sha256(): Wrong param error\n");
519 data_fh = fopen( data_file,
"r" );
520 if ( data_fh == NULL ) {
521 debug_print(
"rdm_openssl_file_hash_sha256(): datafile open error\n");
529 if((mdctx = EVP_MD_CTX_create()) == NULL) {
530 debug_print(
"rdm_openssl_file_hash_sha256(): Digest Context Initialize Failed\n");
534 if(1 != EVP_DigestInit_ex(mdctx, EVP_sha256(), NULL)) {
535 debug_print(
"rdm_openssl_file_hash_sha256(): Digest Context Type Setup Failed\n");
540 if ( file_len == (
size_t)-1 ) {
541 if ( fseek( data_fh, 0, SEEK_END ) != 0 ) {
544 file_len = (size_t)ftell( data_fh );
545 if ( fseek( data_fh, 0, SEEK_SET ) != 0 ) {
552 size_t bytes_to_read = ( file_len <
sizeof(buffer) ? file_len :
sizeof(buffer) );
553 bytesread = fread(buffer, 1, bytes_to_read, data_fh );
554 if ( bytesread > 0 ) {
555 if(1 != EVP_DigestUpdate(mdctx, buffer, bytesread)) {
559 file_len -= bytes_to_read;
560 }
while ( file_len > 0 );
562 if ( ferror( data_fh ) ) {
568 if( 1 != EVP_DigestFinal_ex(mdctx, hash_buffer, &hashval_len) ) {
571 if ( hashval_len != SHA256_DIGEST_LENGTH ) {
578 if ( data_fh != NULL ) fclose( data_fh );
579 if ( mdctx != NULL ) EVP_MD_CTX_destroy( mdctx );
580 if ( buffer != NULL ) free( buffer );
602 if ( manifest_file_size(data_file, &BUFSIZE) != 0 )
604 EVP_MD_CTX *mdctx=NULL;
605 FILE *manifest_fh=NULL;
607 unsigned char* buffer= (
unsigned char*)calloc(
sizeof(
unsigned char), BUFSIZE );
609 char *path_buff=NULL;
613 debug_print(
"rdm_openssl_file_hash_sha256_pkg_components() Entry\n");
615 if ( data_file == NULL || hash_buffer == NULL || buffer_len == NULL ) {
616 debug_print(
"rdm_openssl_file_hash_sha256_pkg_components(): Invalid param error\n");
620 if ( *buffer_len < SHA256_DIGEST_LENGTH ) {
621 *buffer_len = SHA256_DIGEST_LENGTH;
622 debug_print(
"rdm_openssl_file_hash_sha256_pkg_components(): Wrong param error\n");
629 manifest_fh = fopen( data_file,
"r" );
630 if ( manifest_fh == NULL ) {
631 debug_print(
"rdm_openssl_file_hash_sha256_pkg_components(): manifest file open error\n");
639 if((mdctx = EVP_MD_CTX_create()) == NULL) {
640 debug_print(
"rdm_openssl_file_hash_sha256_pkg_components(): Digest Context Initialize Failed\n");
644 if(1 != EVP_DigestInit_ex(mdctx, EVP_sha256(), NULL)) {
645 debug_print(
"rdm_openssl_file_hash_sha256_pkg_components(): Digest Context Type Setup Failed\n");
650 manifest = calloc(
sizeof(
char), BUFSIZE);
651 if ( NULL == manifest ) {
652 debug_print(
"rdm_openssl_file_hash_sha256_pkg_components(): memory allocation failed\n");
655 fread(manifest, BUFSIZE, 1, manifest_fh);
657 path_buff = strtok(manifest,
"\t\r\n");
658 while ( path_buff != NULL )
660 data_fh = fopen( path_buff,
"r" );
661 if( strstr(path_buff,
"tmp") || strstr(path_buff,
"media") || strstr(path_buff,
"padding") || strstr(path_buff,
"json") ) {
662 if ( data_fh == NULL ) {
663 printf(
"rdm_openssl_file_hash_sha256_pkg_components: datafile open error\n");
668 bytesread = fread(buffer, 1, BUFSIZE, data_fh );
669 if ( bytesread > 0 ) {
670 if( 1 != EVP_DigestUpdate(mdctx, buffer, bytesread) ) {
674 if ( bytesread != BUFSIZE)
677 if ( ferror( data_fh ) ) {
681 path_buff = strtok(NULL,
"\t\r\n");
682 if ( data_fh != NULL ) fclose( data_fh );
687 if( 1 != EVP_DigestFinal_ex(mdctx, hash_buffer, &hashval_len) ) {
690 if ( hashval_len != SHA256_DIGEST_LENGTH ) {
697 if ( data_fh != NULL ) fclose( data_fh );
698 if ( mdctx != NULL ) EVP_MD_CTX_destroy( mdctx );
699 if ( manifest != NULL) free( manifest );
700 if ( buffer != NULL ) free( buffer );
718 int openssl_verify_signature(
const unsigned char *hashval,
int hashval_len,
const char *sig_file,
const char *vkey_file,
char *reply_msg,
int *reply_msg_len)
721 EVP_MD_CTX *mdctx=NULL;
724 char hash_ascii[SHA256_ASCII_DIGEST_LENGTH + 1];
731 if ( hashval == NULL ) {
743 #if defined(DEBUG_ENABLED)
746 size_t hashval_ascii_len = SHA256_ASCII_DIGEST_LENGTH;
747 if (
bin_to_asciihex( hashval, hashval_len, hash_ascii, &hashval_ascii_len ) != 0 ) {
754 hash_ascii[
sizeof(hash_ascii)-1] = 0;
755 debug_print(
"HASH ASCII (signed message):\n%s\n",hash_ascii);
763 FILE *pub_fh = fopen( vkey_file,
"rb" );
764 if ( pub_fh == NULL ) {
766 retval = retcode_keyfile_err;
770 pkey = PEM_read_PUBKEY( pub_fh, NULL, NULL, NULL );
771 if ( pkey == NULL ) {
777 EVP_MD_CTX_destroy( mdctx );
778 if((mdctx = EVP_MD_CTX_create()) == NULL) {
784 if(1 != EVP_DigestVerifyInit(mdctx, NULL, EVP_sha256(), NULL, pkey)) {
790 if( 1 != EVP_DigestVerifyUpdate( mdctx, hash_ascii, SHA256_ASCII_DIGEST_LENGTH ) ) {
796 if(1 == EVP_DigestVerifyFinal( mdctx, sig, sig_len) )
807 if ( sig_fh != NULL ) fclose( sig_fh );
808 if ( pub_fh != NULL ) fclose( pub_fh );
809 if ( mdctx != NULL ) EVP_MD_CTX_destroy( mdctx );
810 if ( pkey != NULL ) EVP_PKEY_free( pkey );
814 snprintf( reply_msg, (
size_t)
REPLY_MSG_LEN,
"c_l_s_v performance status: %x", retval );
840 unsigned char hashval[SHA256_DIGEST_LENGTH];
841 int hashval_len=SHA256_DIGEST_LENGTH;
843 debug_print(
"Entry: rdm_openssl_rsa_file_signature_verify\n");
844 if ( data_file == NULL ||
848 reply_msg_len == NULL ) {
849 debug_print(
"rdm_openssl_rsa_file_signature_verify(): Input Args parameter error\n");
855 debug_print(
"rdm_openssl_rsa_file_signature_verify(): Output Buffer Len parameter error\n");
859 if ( NULL == strstr(data_file,
"cpemanifest") ) {
864 printf(
"rdm_openssl_rsa_file_signature_verify():Initiating signature validation of individual package components\n");
868 debug_print(
"rdm_openssl_rsa_file_signature_verify(): rdm_openssl_file_hash_sha256 returns err %x\n",retval);