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