leo hendrickson / Mbed OS example-Ethernet-mbed-Cloud-connect
Committer:
leothedragon
Date:
Tue May 04 08:55:12 2021 +0000
Revision:
0:8f0bb79ddd48
nmn

Who changed what in which revision?

UserRevisionLine numberNew contents of line
leothedragon 0:8f0bb79ddd48 1 // ----------------------------------------------------------------------------
leothedragon 0:8f0bb79ddd48 2 // Copyright 2016-2017 ARM Ltd.
leothedragon 0:8f0bb79ddd48 3 //
leothedragon 0:8f0bb79ddd48 4 // SPDX-License-Identifier: Apache-2.0
leothedragon 0:8f0bb79ddd48 5 //
leothedragon 0:8f0bb79ddd48 6 // Licensed under the Apache License, Version 2.0 (the "License");
leothedragon 0:8f0bb79ddd48 7 // you may not use this file except in compliance with the License.
leothedragon 0:8f0bb79ddd48 8 // You may obtain a copy of the License at
leothedragon 0:8f0bb79ddd48 9 //
leothedragon 0:8f0bb79ddd48 10 // http://www.apache.org/licenses/LICENSE-2.0
leothedragon 0:8f0bb79ddd48 11 //
leothedragon 0:8f0bb79ddd48 12 // Unless required by applicable law or agreed to in writing, software
leothedragon 0:8f0bb79ddd48 13 // distributed under the License is distributed on an "AS IS" BASIS,
leothedragon 0:8f0bb79ddd48 14 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
leothedragon 0:8f0bb79ddd48 15 // See the License for the specific language governing permissions and
leothedragon 0:8f0bb79ddd48 16 // limitations under the License.
leothedragon 0:8f0bb79ddd48 17 // ----------------------------------------------------------------------------
leothedragon 0:8f0bb79ddd48 18
leothedragon 0:8f0bb79ddd48 19 #include <stdint.h>
leothedragon 0:8f0bb79ddd48 20 #include "pal.h"
leothedragon 0:8f0bb79ddd48 21 #include "mbed-trace/mbed_trace.h"
leothedragon 0:8f0bb79ddd48 22
leothedragon 0:8f0bb79ddd48 23 #define TRACE_GROUP "mClt"
leothedragon 0:8f0bb79ddd48 24
leothedragon 0:8f0bb79ddd48 25 bool extract_field_from_certificate(const uint8_t* cer, size_t cer_len, const char *field, char* value)
leothedragon 0:8f0bb79ddd48 26 {
leothedragon 0:8f0bb79ddd48 27 #if 1 // TODO : Uncomment once PAL has feature to extract "L" from certificate
leothedragon 0:8f0bb79ddd48 28 tr_debug("extract_field_from_certificate");
leothedragon 0:8f0bb79ddd48 29
leothedragon 0:8f0bb79ddd48 30 palX509Attr_t attr = PAL_X509_L_ATTR;
leothedragon 0:8f0bb79ddd48 31 if (strcmp(field,"CN") == 0) {
leothedragon 0:8f0bb79ddd48 32 attr = PAL_X509_CN_ATTR;
leothedragon 0:8f0bb79ddd48 33 } else if (strcmp(field,"L") == 0) {
leothedragon 0:8f0bb79ddd48 34 attr = PAL_X509_L_ATTR;
leothedragon 0:8f0bb79ddd48 35 } else {
leothedragon 0:8f0bb79ddd48 36 return false;
leothedragon 0:8f0bb79ddd48 37 }
leothedragon 0:8f0bb79ddd48 38
leothedragon 0:8f0bb79ddd48 39 palX509Handle_t cert = 0;
leothedragon 0:8f0bb79ddd48 40 size_t len = 0;
leothedragon 0:8f0bb79ddd48 41 palStatus_t ret = pal_x509Initiate(&cert);
leothedragon 0:8f0bb79ddd48 42 if (ret != PAL_SUCCESS) {
leothedragon 0:8f0bb79ddd48 43 tr_error("extract_field_from_certificate - cert init failed: %d", (int)ret);
leothedragon 0:8f0bb79ddd48 44 pal_x509Free(&cert);
leothedragon 0:8f0bb79ddd48 45 return false;
leothedragon 0:8f0bb79ddd48 46 }
leothedragon 0:8f0bb79ddd48 47 ret = pal_x509CertParse(cert, cer, cer_len);
leothedragon 0:8f0bb79ddd48 48 if (ret != PAL_SUCCESS) {
leothedragon 0:8f0bb79ddd48 49 tr_error("extract_field_from_certificate - cert parse failed: %d", (int)ret);
leothedragon 0:8f0bb79ddd48 50 pal_x509Free(&cert);
leothedragon 0:8f0bb79ddd48 51 return false;
leothedragon 0:8f0bb79ddd48 52 }
leothedragon 0:8f0bb79ddd48 53 ret = pal_x509CertGetAttribute(cert, attr, value, 64, &len);
leothedragon 0:8f0bb79ddd48 54 if (ret != PAL_SUCCESS) {
leothedragon 0:8f0bb79ddd48 55 tr_error("extract_field_from_certificate - cert attr get failed: %d", (int)ret);
leothedragon 0:8f0bb79ddd48 56 pal_x509Free(&cert);
leothedragon 0:8f0bb79ddd48 57 return false;
leothedragon 0:8f0bb79ddd48 58 }
leothedragon 0:8f0bb79ddd48 59 pal_x509Free(&cert);
leothedragon 0:8f0bb79ddd48 60 return true;
leothedragon 0:8f0bb79ddd48 61 #else
leothedragon 0:8f0bb79ddd48 62 return false;
leothedragon 0:8f0bb79ddd48 63 #endif
leothedragon 0:8f0bb79ddd48 64 }
leothedragon 0:8f0bb79ddd48 65
leothedragon 0:8f0bb79ddd48 66