Mayank Gupta / Mbed OS pelion-example-frdm

Dependencies:   FXAS21002 FXOS8700Q

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers arm_uc_certificate_raw_api.c Source File

arm_uc_certificate_raw_api.c

00001 // ----------------------------------------------------------------------------
00002 // Copyright 2016-2017 ARM Ltd.
00003 //
00004 // SPDX-License-Identifier: Apache-2.0
00005 //
00006 // Licensed under the Apache License, Version 2.0 (the "License");
00007 // you may not use this file except in compliance with the License.
00008 // You may obtain a copy of the License at
00009 //
00010 //     http://www.apache.org/licenses/LICENSE-2.0
00011 //
00012 // Unless required by applicable law or agreed to in writing, software
00013 // distributed under the License is distributed on an "AS IS" BASIS,
00014 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00015 // See the License for the specific language governing permissions and
00016 // limitations under the License.
00017 // ----------------------------------------------------------------------------
00018 
00019 #include "update-client-common/arm_uc_config.h"
00020 #if defined(ARM_UC_FEATURE_CERT_STORE_RAW) && (ARM_UC_FEATURE_CERT_STORE_RAW == 1)
00021 
00022 #include "update-client-control-center/arm_uc_certificate.h"
00023 #include "update-client-common/arm_uc_crypto.h"
00024 
00025 static const uint8_t *arm_uc_raw_fingerprint;
00026 static uint16_t arm_uc_raw_fingerprint_size;
00027 
00028 static const uint8_t *arm_uc_raw_certificate;
00029 static uint16_t arm_uc_raw_certificate_size;
00030 
00031 static arm_uc_error_t arm_uc_raw_cert_fetcher(arm_uc_buffer_t *certificate,
00032                                               const arm_uc_buffer_t *fingerprint,
00033                                               const arm_uc_buffer_t *DERCertificateList, // DERCertificateList - not used
00034                                               void (*callback)(arm_uc_error_t, const arm_uc_buffer_t *, const arm_uc_buffer_t *))
00035 {
00036     UC_CONT_TRACE("Attempting to load certificate");
00037     arm_uc_error_t err = {ERR_NONE};
00038 
00039     (void)DERCertificateList;
00040     if (certificate == NULL ||
00041             certificate->ptr == NULL ||
00042             callback == NULL) {
00043         err.code = ARM_UC_CM_ERR_INVALID_PARAMETER;
00044         return err;
00045     }
00046 
00047     // Check the buffer size
00048     if (certificate->size_max < arm_uc_raw_certificate_size) {
00049         err.code = ARM_UC_CM_ERR_INVALID_PARAMETER;
00050     }
00051     const arm_uc_buffer_t fingerprintLocalBuffer = {
00052         .size_max = arm_uc_raw_fingerprint_size,
00053         .size = arm_uc_raw_fingerprint_size,
00054         .ptr = (uint8_t *)arm_uc_raw_fingerprint
00055     };
00056     if (err.code == ERR_NONE) {
00057         // Compare the buffers
00058         uint32_t rc = ARM_UC_BinCompareCT(fingerprint, &fingerprintLocalBuffer);
00059         if (rc) {
00060             err.code = ARM_UC_CM_ERR_NOT_FOUND;
00061         } else {
00062             UC_CONT_TRACE("Certificate lookup fingerprint matched.");
00063             err.code = ERR_NONE;
00064             certificate->ptr = (uint8_t *)arm_uc_raw_certificate;
00065             certificate->size = arm_uc_raw_certificate_size;
00066         }
00067 
00068         if (callback && (err.code == ERR_NONE)) {
00069             callback(err, certificate, fingerprint);
00070         }
00071     }
00072     return err;
00073 }
00074 
00075 static arm_uc_error_t arm_uc_raw_cert_storer(
00076     const arm_uc_buffer_t *cert,
00077     const arm_uc_buffer_t *fingerprint,
00078     void(*callback)(arm_uc_error_t, const arm_uc_buffer_t *))
00079 {
00080     arm_uc_error_t err = {ERR_NONE};
00081 
00082     if (cert == NULL ||
00083             fingerprint == NULL ||
00084             callback == NULL) {
00085         err.code = ARM_UC_CM_ERR_INVALID_PARAMETER;
00086         return err;
00087     }
00088 
00089     if (fingerprint->ptr == NULL || fingerprint->size < (256 / 8) ||
00090             cert->ptr == NULL || cert->size < (256 / 8)) {
00091         err.code = ARM_UC_CM_ERR_INVALID_PARAMETER;
00092     }
00093 
00094     if (err.code == ERR_NONE) {
00095         arm_uc_raw_fingerprint = fingerprint->ptr;
00096         arm_uc_raw_fingerprint_size = fingerprint->size;
00097         arm_uc_raw_certificate = cert->ptr;
00098         arm_uc_raw_certificate_size = cert->size;
00099     }
00100 
00101     if (callback && (err.code == ERR_NONE)) {
00102         callback(err, fingerprint);
00103     }
00104 
00105     return err;
00106 }
00107 
00108 const struct arm_uc_certificate_api arm_uc_certificate_raw_api = {
00109     .fetch = arm_uc_raw_cert_fetcher,
00110     .store = arm_uc_raw_cert_storer
00111 };
00112 
00113 #endif /* ARM_UC_FEATURE_CERT_STORE_RAW */