revise logging and coding style
[platform/core/connectivity/nfc-manager-neard.git] / common / net_nfc_util_sign_record.c
index 1815d9e..4fef23d 100644 (file)
 
 #define __NEXT_SUB_FIELD(__dst) ((__dst)->value + (__dst)->length)
 
-bool _get_records_data_buffer(ndef_record_s *begin_record, ndef_record_s *end_record, uint8_t **buffer, uint32_t *length)
+bool _get_records_data_buffer(ndef_record_s *begin_record,
+       ndef_record_s *end_record, uint8_t **buffer, uint32_t *length)
 {
-       bool result = false;
        uint32_t len = 0;
+       bool result = false;
        ndef_record_s *current_record = NULL;
 
-       if (begin_record == NULL || begin_record == end_record)
-               return result;
+       RETV_IF(NULL == begin_record, result);
+       RETV_IF(begin_record == end_record, result);
 
        /* count total buffer length */
        current_record = begin_record;
@@ -63,8 +64,11 @@ bool _get_records_data_buffer(ndef_record_s *begin_record, ndef_record_s *end_re
                        len += current_record->id_s.length;
 
                /* payload length */
-               if (current_record->payload_s.buffer != NULL && current_record->payload_s.length > 0)
+               if (current_record->payload_s.buffer != NULL &&
+                               current_record->payload_s.length > 0)
+               {
                        len += current_record->payload_s.length;
+               }
 
                current_record = current_record->next;
        }
@@ -83,23 +87,28 @@ bool _get_records_data_buffer(ndef_record_s *begin_record, ndef_record_s *end_re
                        while (offset < len && current_record != NULL && current_record != end_record)
                        {
                                /* type length */
-                               if (current_record->type_s.buffer != NULL && current_record->type_s.length > 0)
+                               if (current_record->type_s.buffer != NULL &&
+                                       current_record->type_s.length > 0)
                                {
-                                       memcpy(buf + offset, current_record->type_s.buffer, MIN(current_record->type_s.length, len - offset));
+                                       memcpy(buf + offset, current_record->type_s.buffer,
+                                               MIN(current_record->type_s.length, len - offset));
                                        offset += MIN(current_record->type_s.length, len - offset);
                                }
 
                                /* ID length */
                                if (current_record->id_s.buffer != NULL && current_record->id_s.length > 0)
                                {
-                                       memcpy(buf + offset, current_record->id_s.buffer, MIN(current_record->id_s.length, len - offset));
+                                       memcpy(buf + offset, current_record->id_s.buffer,
+                                               MIN(current_record->id_s.length, len - offset));
                                        offset += MIN(current_record->id_s.length, len - offset);
                                }
 
                                /* payload length */
-                               if (current_record->payload_s.buffer != NULL && current_record->payload_s.length > 0)
+                               if (current_record->payload_s.buffer != NULL &&
+                                       current_record->payload_s.length > 0)
                                {
-                                       memcpy(buf + offset, current_record->payload_s.buffer, MIN(current_record->payload_s.length, len - offset));
+                                       memcpy(buf + offset, current_record->payload_s.buffer,
+                                               MIN(current_record->payload_s.length, len - offset));
                                        offset += MIN(current_record->payload_s.length, len - offset);
                                }
 
@@ -116,64 +125,70 @@ bool _get_records_data_buffer(ndef_record_s *begin_record, ndef_record_s *end_re
        return result;
 }
 
-net_nfc_error_e net_nfc_util_verify_signature_records(ndef_record_s *begin_record, ndef_record_s *sign_record)
+net_nfc_error_e net_nfc_util_verify_signature_records(ndef_record_s *begin_record,
+       ndef_record_s *sign_record)
 {
-       net_nfc_error_e result = NET_NFC_UNKNOWN_ERROR;
-       uint8_t *buffer = NULL;
+       int ret;
        uint32_t length = 0;
+       uint8_t *buffer = NULL;
+       net_nfc_error_e result = NET_NFC_UNKNOWN_ERROR;
 
-       if (begin_record == NULL || sign_record == NULL || begin_record == sign_record)
-               return NET_NFC_INVALID_PARAM;
+       RETV_IF(NULL == sign_record, NET_NFC_INVALID_PARAM);
+       RETV_IF(NULL == begin_record, NET_NFC_INVALID_PARAM);
+       RETV_IF(begin_record == sign_record, NET_NFC_INVALID_PARAM);
 
        /* get signed data */
        if (_get_records_data_buffer(begin_record, sign_record, &buffer, &length) == true)
        {
-               uint8_t *signature = NULL;
-               uint32_t sign_len = 0;
                net_nfc_signature_record_s *sign_info = NULL;
                net_nfc_certificate_chain_s *chain_info = NULL;
 
                /* parse signature info */
                sign_info = (net_nfc_signature_record_s *)sign_record->payload_s.buffer;
 
-               DEBUG_MSG("record version : %d", sign_info->version);
-               DEBUG_MSG("signature URI present? : %s", sign_info->uri_present ? "true" : "false");
-               DEBUG_MSG("signature type : %d", sign_info->sign_type);
-               DEBUG_MSG("signature length : %d", sign_info->signature.length);
+               NFC_DBG("record version : %d", sign_info->version);
+               NFC_DBG("signature URI present? : %s", sign_info->uri_present ? "true" : "false");
+               NFC_DBG("signature type : %d", sign_info->sign_type);
+               NFC_DBG("signature length : %d", sign_info->signature.length);
 
-               if (sign_info->uri_present == true)
+               if (true == sign_info->uri_present)
                {
                        /* TODO */
                        /* receive the signature data directed by uri */
-                       DEBUG_ERR_MSG("NOT IMPLEMENTED (sign_info->uri_present == true)");
+                       NFC_ERR("NOT IMPLEMENTED (sign_info->uri_present == true)");
                        _net_nfc_util_free_mem(buffer);
                        return result;
                }
-               else
-               {
-                       signature = sign_info->signature.value;
-                       sign_len = sign_info->signature.length;
-               }
 
                /* parse certificate chain info */
-               chain_info = (net_nfc_certificate_chain_s *)__NEXT_SUB_FIELD(&(sign_info->signature));
+               chain_info =
+                       (net_nfc_certificate_chain_s *)__NEXT_SUB_FIELD(&(sign_info->signature));
 
-               SECURE_LOGD("certificate URI present? : %s", chain_info->uri_present ? "true" : "false");
-               DEBUG_MSG("certificate format : %d", chain_info->cert_format);
-               DEBUG_MSG("number of certificates : %d", chain_info->num_of_certs);
+               SECURE_LOGD("certificate URI present? : %s",
+                       chain_info->uri_present ? "true" : "false");
+               NFC_DBG("certificate format : %d", chain_info->cert_format);
+               NFC_DBG("number of certificates : %d", chain_info->num_of_certs);
 
                if (chain_info->num_of_certs > 0)
                {
                        net_nfc_sub_field_s *data_info = NULL;
 
                        data_info = (net_nfc_sub_field_s *)chain_info->cert_store;
-                       DEBUG_MSG("certficate length : %d", data_info->length);
+                       NFC_DBG("certficate length : %d", data_info->length);
 
                        //              DEBUG_MSG_PRINT_BUFFER(data_info->value, data_info->length);
 
                        /* the first certificate is signer's one
                         * verify signature of content */
-                       if (net_nfc_util_openssl_verify_signature(sign_info->sign_type, buffer, length, data_info->value, data_info->length, sign_info->signature.value, sign_info->signature.length) == true)
+                       ret = net_nfc_util_openssl_verify_signature(sign_info->sign_type,
+                                               buffer,
+                                               length,
+                                               data_info->value,
+                                               data_info->length,
+                                               sign_info->signature.value,
+                                               sign_info->signature.length);
+
+                       if (true == ret)
                        {
                                if (chain_info->num_of_certs > 1)
                                {
@@ -184,29 +199,31 @@ net_nfc_error_e net_nfc_util_verify_signature_records(ndef_record_s *begin_recor
                                        context = net_nfc_util_openssl_init_verify_certificate();
 
                                        /* add signer's certificate */
-                                       net_nfc_util_openssl_add_certificate_of_signer(context, data_info->value, data_info->length);
+                                       net_nfc_util_openssl_add_certificate_of_signer(context, data_info->value,
+                                               data_info->length);
 
                                        /* verify certificate using certificate chain */
                                        for (i = 1, data_info = (net_nfc_sub_field_s *)__NEXT_SUB_FIELD(data_info);
                                                        i < chain_info->num_of_certs;
                                                        i++, data_info = (net_nfc_sub_field_s *)__NEXT_SUB_FIELD(data_info))
                                        {
-                                               DEBUG_MSG("certficate length : %d", data_info->length);
+                                               NFC_DBG("certficate length : %d", data_info->length);
                                                //DEBUG_MSG_PRINT_BUFFER(data_info->value, data_info->length);
 
-                                               net_nfc_util_openssl_add_certificate_of_ca(context, data_info->value, data_info->length);
+                                               net_nfc_util_openssl_add_certificate_of_ca(context, data_info->value,
+                                                       data_info->length);
                                        }
 
                                        /* if the CA_Uri is present, continue adding certificate from uri */
-                                       if (chain_info->uri_present == true)
+                                       if (true == chain_info->uri_present)
                                        {
                                                /* TODO : Need to implement */
-                                               DEBUG_ERR_MSG("NOT IMPLEMENTED (found_root == false && chain_info->uri_present == true)");
+                                               NFC_ERR("NOT IMPLEMENTED (found_root == false && chain_info->uri_present == true)");
                                                net_nfc_util_openssl_release_verify_certificate(context);
                                                _net_nfc_util_free_mem(buffer);
                                                return result;
 
-                                               //DEBUG_MSG("certficate length : %d", data_info->length);
+                                               //NFC_DBG("certficate length : %d", data_info->length);
                                                //DEBUG_MSG_PRINT_BUFFER(data_info->value, data_info->length);
                                        }
 
@@ -222,16 +239,16 @@ net_nfc_error_e net_nfc_util_verify_signature_records(ndef_record_s *begin_recor
                                        result = NET_NFC_OK;
                                }
 
-                               DEBUG_MSG("verifying signature %d", result);
+                               NFC_DBG("verifying signature %d", result);
                        }
                        else
                        {
-                               DEBUG_ERR_MSG("verifying signature failed");
+                               NFC_ERR("verifying signature failed");
                        }
                }
                else
                {
-                       DEBUG_ERR_MSG("certificate not found");
+                       NFC_ERR("certificate not found");
                }
 
                _net_nfc_util_free_mem(buffer);
@@ -239,10 +256,9 @@ net_nfc_error_e net_nfc_util_verify_signature_records(ndef_record_s *begin_recor
        else
        {
                if(buffer != NULL)
-               {
                        _net_nfc_util_free_mem(buffer);
-               }
-               DEBUG_ERR_MSG("_get_records_data_buffer failed");
+
+               NFC_ERR("_get_records_data_buffer failed");
        }
 
        return result;
@@ -250,19 +266,18 @@ net_nfc_error_e net_nfc_util_verify_signature_records(ndef_record_s *begin_recor
 
 net_nfc_error_e net_nfc_util_verify_signature_ndef_message(ndef_message_s *msg)
 {
-       net_nfc_error_e result = NET_NFC_UNKNOWN_ERROR;
        ndef_record_s *begin_record = NULL;
        ndef_record_s *current_record = NULL;
+       net_nfc_error_e result = NET_NFC_UNKNOWN_ERROR;
+
 
        begin_record = msg->records;
        current_record = msg->records;
 
        while (current_record != NULL)
        {
-               if (begin_record == NULL)
-               {
+               if (NULL == begin_record)
                        begin_record = current_record;
-               }
 
                if (IS_EMPTY_RECORD(current_record))
                {
@@ -284,40 +299,47 @@ net_nfc_error_e net_nfc_util_verify_signature_ndef_message(ndef_message_s *msg)
 /*
  * sign method
  */
-net_nfc_error_e net_nfc_util_sign_records(ndef_message_s *msg, int begin_index, int end_index, char *cert_file, char *password)
+net_nfc_error_e net_nfc_util_sign_records(ndef_message_s *msg, int begin_index,
+       int end_index, char *cert_file, char *password)
 {
-       net_nfc_error_e result = NET_NFC_UNKNOWN_ERROR;
-       ndef_record_s *begin_record = NULL, *end_record = NULL, *record = NULL;
-       data_s payload = { NULL, 0 };
-       uint8_t *data_buffer = NULL;
+       uint32_t cert_len = 0;
        uint32_t data_len = 0;
+       uint32_t cert_count = 0;
+       uint8_t *data_buffer = NULL;
+       uint8_t *cert_buffer = NULL;
+       data_s payload = { NULL, 0 };
        uint8_t signature[1024] = { 0, };
        uint32_t sign_len = sizeof(signature);
-       uint8_t *cert_buffer = NULL;
-       uint32_t cert_len = 0;
-       uint32_t cert_count = 0;
+       net_nfc_error_e result = NET_NFC_UNKNOWN_ERROR;
+       ndef_record_s *begin_record = NULL, *end_record = NULL, *record = NULL;
+
 
        net_nfc_util_get_record_by_index(msg, begin_index, &begin_record);
        net_nfc_util_get_record_by_index(msg, end_index, &end_record);
 
-       DEBUG_MSG("total record count : %d, begin_index : %d, end_index : %d", msg->recordCount, begin_index, end_index);
+       NFC_DBG("total record count : %d, begin_index : %d, end_index : %d", msg->recordCount,
+               begin_index, end_index);
 
        /* get target data */
        _get_records_data_buffer(begin_record, end_record->next, &data_buffer, &data_len);
 
        DEBUG_MSG_PRINT_BUFFER(data_buffer, data_len);
 
-       net_nfc_util_openssl_sign_buffer(NET_NFC_SIGN_TYPE_PKCS_1, data_buffer, data_len, cert_file, password, signature, &sign_len);
+       net_nfc_util_openssl_sign_buffer(NET_NFC_SIGN_TYPE_PKCS_1, data_buffer, data_len,
+               cert_file, password, signature, &sign_len);
 
        /* get cert chain */
-       net_nfc_util_get_cert_list_from_file(cert_file, password, &cert_buffer, &cert_len, &cert_count);
+       net_nfc_util_get_cert_list_from_file(cert_file, password, &cert_buffer, &cert_len,
+               &cert_count);
 
        /* create payload */
-       payload.length = sizeof(net_nfc_signature_record_s) + sign_len + sizeof(net_nfc_certificate_chain_s) + cert_len;
+       payload.length = sizeof(net_nfc_signature_record_s) + sign_len +
+               sizeof(net_nfc_certificate_chain_s) + cert_len;
 
        _net_nfc_util_alloc_mem(payload.buffer, payload.length);
 
-       net_nfc_signature_record_s *sign_record = (net_nfc_signature_record_s *)payload.buffer;
+       net_nfc_signature_record_s *sign_record =
+               (net_nfc_signature_record_s *)payload.buffer;
        sign_record->version = 1;
        sign_record->uri_present = 0;
        sign_record->sign_type = NET_NFC_SIGN_TYPE_PKCS_1;
@@ -331,15 +353,13 @@ net_nfc_error_e net_nfc_util_sign_records(ndef_message_s *msg, int begin_index,
                __FILL_SUB_FIELD(&(sign_record->signature), signature, sign_len);
        }
 
-       net_nfc_certificate_chain_s *chain = (net_nfc_certificate_chain_s *)__NEXT_SUB_FIELD(&(sign_record->signature));
+       net_nfc_certificate_chain_s *chain =
+               (net_nfc_certificate_chain_s *)__NEXT_SUB_FIELD(&(sign_record->signature));
+
        if (cert_count < 16)
-       {
                chain->uri_present = 0;
-       }
        else
-       {
                chain->uri_present = 1;
-       }
 
        chain->cert_format = NET_NFC_CERT_FORMAT_X_509;
        chain->num_of_certs = cert_count;
@@ -348,13 +368,14 @@ net_nfc_error_e net_nfc_util_sign_records(ndef_message_s *msg, int begin_index,
        if (chain->uri_present)
        {
                /* TODO */
-               DEBUG_ERR_MSG("num_of_certs is greater than 15 [%d]", cert_count);
+               NFC_ERR("num_of_certs is greater than 15 [%d]", cert_count);
        }
 
        /* create record */
        data_s type = { (uint8_t *)"Sig", 3 };
 
-       net_nfc_util_create_record(NET_NFC_RECORD_WELL_KNOWN_TYPE, &type, NULL, &payload, &record);
+       net_nfc_util_create_record(NET_NFC_RECORD_WELL_KNOWN_TYPE, &type, NULL,
+               &payload, &record);
 
        /* get last record index */
        net_nfc_util_append_record_by_index(msg, end_index + 1, record);
@@ -366,7 +387,8 @@ net_nfc_error_e net_nfc_util_sign_records(ndef_message_s *msg, int begin_index,
        return result;
 }
 
-net_nfc_error_e net_nfc_util_sign_ndef_message(ndef_message_s *msg, char *cert_file, char *password)
+net_nfc_error_e net_nfc_util_sign_ndef_message(ndef_message_s *msg,
+       char *cert_file, char *password)
 {
        net_nfc_error_e result = NET_NFC_UNKNOWN_ERROR;