RDK Documentation (Open Sourced RDK Components)
rdm_rsa_signature_verify.h
1 /*
2  * If not stated otherwise in this file or this component's Licenses.txt file the
3  * following copyright and licenses apply:
4  *
5  * Copyright 2018 RDK Management
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18 */
19 
20 /**
21  * @defgroup RDM RDM(RDK Download Manager)
22  *
23  * - RDM is used for the management of downloadable modules.
24  * - RDK download manager enforces https connections for downloading modules (does not allow non-https connections).
25  *
26  * @defgroup RDM_API RDM Public APIs
27  * @ingroup RDM
28  *
29  * @defgroup RDM_TYPES RDM Data Types
30  * @ingroup RDM
31  */
32 
33 #include <stdio.h>
34 #include <string.h>
35 #include <unistd.h>
36 #include <sys/wait.h>
37 #include <openssl/conf.h>
38 #include <openssl/evp.h>
39 #include <openssl/err.h>
40 #include <openssl/pem.h>
41 
42 #define RDM_DOWNLOADS_DIR "/rdm/downloads/"
43 #define RDM_MANIFEST_DIR "/etc/rdm/"
44 #define RDM_TMP_SIGFILE "/tmp/sig.truncated"
45 #define RDM_KMS_PUB_KEY "/tmp/vstuvwx.file"
46 #define RDM_KMS_PADDING_FILE "pkg_padding"
47 #define RDM_SIGFILE_SUFFIX "-pkg.sig"
48 #define RDM_MANIFEST_SUFFIX "_cpemanifest"
49 #define ENABLE_DEBUG_FLAG "/tmp/debug_rdmopenssl"
50 
51 /**
52  * @addtogroup RDM_TYPES
53  * @{
54  */
55 
56 
57 /**
58  * Obfuscated error return values.
59  * Inital status returns to splunk will consist of the string:
60  * "performance status bla bla: " followed by the hex ascii of one of
61  * the following values.
62  */
63 #define retcode_param_error 0x5165C860 /*!< -1 */
64 #define retcode_success 0x15245EAD /*!< 0 */
65 #define retcode_datafile_err 0x3560800C /*!< 1 */
66 #define retcode_sigfile_err 0x59A67B29 /*!< 2 */
67 #define retcode_ssl_err 0x716A311F /*!< 3 */
68 #define retcode_verify_fail 0x151358C6 /*!< 4 */
69 #define retcode_keyfile_err 0x389CD6A0
70 /**
71  * debug stuff
72  */
73 #ifdef DEBUG_ENABLED
74 #define debug_print(fmt,args...) printf(fmt,##args)
75 #else
76 #define debug_print(fmt,args...) if(access(ENABLE_DEBUG_FLAG, F_OK) != -1) printf(fmt,##args);
77 #endif
78 
79 /**< Minimum bufferlength for reply strings */
80 #define REPLY_MSG_LEN 40
81 
82 /**< buffer sizes */
83 #define SHA256_DIGEST_LENGTH 32
84 #define SHA256_ASCII_DIGEST_LENGTH (SHA256_DIGEST_LENGTH * 2)
85 #define RSA2048_SIGNATURE_LEN 256
86 #define RSA2048_ASCII_SIGNATURE_LEN ( RSA2048_SIGNATURE_LEN * 2 )
87 
88 /** @} */ //END OF GROUP RDM_TYPES
89 
90 /**
91  * @addtogroup RDM_API
92  * @{
93  */
94 
95  /**
96  *
97  * @brief This function is used to verify the signature of rdm package
98  *
99  * @param[in] *cache_dir - Mount point where rdm packages are extracted (Eg - /media/apps, /tmp)
100  * @param[in] *app_name - Name of the app
101  * @param[in] *prepare_files - 1 - prepare files and then verify signature. 0 - just verify signature
102  *
103  * @return The status of the operation.
104  *
105  * @reval 0 - Signature verification success
106  * @retval 1 - Signature verification failed
107  */
108  int rdm_signature_verify(char *cache_dir, char *app_name, int prepare_files);
109 
110  /**
111  * @brief This function is used to verify the signature file locally.
112  *
113  * @param[in] *data_file - The file that has been signed
114  * @param[in] file_len - The length of the file. PASS (size_t)-1 for "don't know, use eof"
115  * @param[in] *sig_file - Contains the KMS ASCII hex signature ALL UPPER CASE as created by signing process
116  * @param[in] *vkey_file - PEM format public key exported from KMS
117  * @param[out] *reply_msg - Buffer to receive message to send to logging system
118  * @param[out] *reply_msg_len - Pointer to int containing size of buffer. Must be at least 65 bytes.
119  *
120  * @return The status of the operation.
121  *
122  * @reval -1 - reply_msg NULL or *reply_msg_len too small, no check done, required size in *reply_msg_len.
123  * @retval 0 - Signature verifies, reply_msg buffer size ok, reply_msg has response.
124  * @retval 1 - Failed reading data_file, no sig check done, reply_msg has response.
125  * @retval 2 - Failed reading sig_file, no sig check done, reply_msg has response.
126  * @retval 3 - Openssl operational error, no sig check done, reply_msg has response.
127  * @retval 4 - Signature does not match! reply_msg has response.
128  *
129  * @note -1 can also be returned for internal invalid lengths in buffer size variables. the logic is not fully implemented
130  * to check for buffer length updates and retry. Left as an exercise.
131  */
132  int cpe_local_verify_file_signature(const char *data_file, size_t file_len, const char *sig_file, const char *vkey_file, char *reply_msg, int *reply_msg_len);
133 
134 /** @} */ //END OF GROUP RDM_API
rdm_signature_verify
int rdm_signature_verify(char *cache_dir, char *app_name, int prepare_files)
This function prepares the rdm files for signature verification and invokes the kms openssl verificat...
Definition: rdm_rsa_signature_verify.c:303
cpe_local_verify_file_signature
int cpe_local_verify_file_signature(const char *data_file, size_t file_len, const char *sig_file, const char *vkey_file, char *reply_msg, int *reply_msg_len)
This function is used to verify the signature file locally.