Committer:
leothedragon
Date:
Sun Apr 18 15:20:23 2021 +0000
Revision:
0:25fa8795676b
DS

Who changed what in which revision?

UserRevisionLine numberNew contents of line
leothedragon 0:25fa8795676b 1 // ----------------------------------------------------------------------------
leothedragon 0:25fa8795676b 2 // Copyright 2018 ARM Ltd.
leothedragon 0:25fa8795676b 3 //
leothedragon 0:25fa8795676b 4 // Licensed under the Apache License, Version 2.0 (the "License");
leothedragon 0:25fa8795676b 5 // you may not use this file except in compliance with the License.
leothedragon 0:25fa8795676b 6 // You may obtain a copy of the License at
leothedragon 0:25fa8795676b 7 //
leothedragon 0:25fa8795676b 8 // http://www.apache.org/licenses/LICENSE-2.0
leothedragon 0:25fa8795676b 9 //
leothedragon 0:25fa8795676b 10 // Unless required by applicable law or agreed to in writing, software
leothedragon 0:25fa8795676b 11 // distributed under the License is distributed on an "AS IS" BASIS,
leothedragon 0:25fa8795676b 12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
leothedragon 0:25fa8795676b 13 // See the License for the specific language governing permissions and
leothedragon 0:25fa8795676b 14 // limitations under the License.
leothedragon 0:25fa8795676b 15 // ----------------------------------------------------------------------------
leothedragon 0:25fa8795676b 16
leothedragon 0:25fa8795676b 17 #ifndef __CE_TLV_H__
leothedragon 0:25fa8795676b 18 #define __CE_TLV_H__
leothedragon 0:25fa8795676b 19
leothedragon 0:25fa8795676b 20 #ifdef __cplusplus
leothedragon 0:25fa8795676b 21 extern "C" {
leothedragon 0:25fa8795676b 22 #endif
leothedragon 0:25fa8795676b 23 #include <stdint.h>
leothedragon 0:25fa8795676b 24 #include <stdbool.h>
leothedragon 0:25fa8795676b 25 #include <stddef.h>
leothedragon 0:25fa8795676b 26
leothedragon 0:25fa8795676b 27 typedef enum {
leothedragon 0:25fa8795676b 28 CE_TLV_STATUS_SUCCESS,
leothedragon 0:25fa8795676b 29 CE_TLV_STATUS_END,
leothedragon 0:25fa8795676b 30 CE_TLV_STATUS_INVALID_ARG,
leothedragon 0:25fa8795676b 31 CE_TLV_STATUS_TEXT_NOT_TERMINATED,
leothedragon 0:25fa8795676b 32 CE_TLV_STATUS_MALFORMED_TLV,
leothedragon 0:25fa8795676b 33 CE_TLV_STATUS_ERROR,
leothedragon 0:25fa8795676b 34 CE_TLV_STATUS_ENCODER_INSUFFICIENT_BUFFER
leothedragon 0:25fa8795676b 35 } ce_tlv_status_e;
leothedragon 0:25fa8795676b 36
leothedragon 0:25fa8795676b 37 typedef enum {
leothedragon 0:25fa8795676b 38 CE_TLV_TYPE_CERT_NAME = 1
leothedragon 0:25fa8795676b 39 } ce_tlv_type_e;
leothedragon 0:25fa8795676b 40
leothedragon 0:25fa8795676b 41 typedef struct ce_tlv_element_ {
leothedragon 0:25fa8795676b 42 const uint8_t *_current; // 4 bytes
leothedragon 0:25fa8795676b 43 const uint8_t *_end; // 4 bytes
leothedragon 0:25fa8795676b 44 union {
leothedragon 0:25fa8795676b 45 const uint8_t *bytes;
leothedragon 0:25fa8795676b 46 const char *text;
leothedragon 0:25fa8795676b 47 int integer;
leothedragon 0:25fa8795676b 48 } val; // 4 bytes
leothedragon 0:25fa8795676b 49 bool is_required;
leothedragon 0:25fa8795676b 50 uint16_t type; // 2 bytes
leothedragon 0:25fa8795676b 51 uint16_t len; // 2 bytes
leothedragon 0:25fa8795676b 52 } ce_tlv_element_s;
leothedragon 0:25fa8795676b 53
leothedragon 0:25fa8795676b 54 ce_tlv_status_e ce_tlv_parser_init(const uint8_t *tlv_buf, size_t tlv_buf_len, ce_tlv_element_s *element_out);
leothedragon 0:25fa8795676b 55 ce_tlv_status_e ce_tlv_parse_next(ce_tlv_element_s *element);
leothedragon 0:25fa8795676b 56
leothedragon 0:25fa8795676b 57 /* Checks if the given element is required or optional by testing
leothedragon 0:25fa8795676b 58 * the "type" MSB bit. [0 == required] while [1 == optional]
leothedragon 0:25fa8795676b 59 *
leothedragon 0:25fa8795676b 60 * @param element[IN] The element to test
leothedragon 0:25fa8795676b 61 *
leothedragon 0:25fa8795676b 62 * @return "true" if element is required, "false" otherwise.
leothedragon 0:25fa8795676b 63 */
leothedragon 0:25fa8795676b 64 bool is_required(const ce_tlv_element_s *element);
leothedragon 0:25fa8795676b 65
leothedragon 0:25fa8795676b 66 #ifdef CERT_RENEWAL_TEST
leothedragon 0:25fa8795676b 67 typedef struct ce_tlv_encoder_ {
leothedragon 0:25fa8795676b 68 uint8_t *buf; // 4 bytes
leothedragon 0:25fa8795676b 69 uint16_t encoded_length; // 2 bytes
leothedragon 0:25fa8795676b 70 uint16_t _buf_size; // 2 bytes
leothedragon 0:25fa8795676b 71 } ce_tlv_encoder_s;
leothedragon 0:25fa8795676b 72
leothedragon 0:25fa8795676b 73 void ce_tlv_encoder_init(uint8_t *buf, uint16_t buf_size, ce_tlv_encoder_s *encoder);
leothedragon 0:25fa8795676b 74 ce_tlv_status_e tlv_add_str(ce_tlv_type_e type, uint16_t length, const char *value, bool is_tlv_required, ce_tlv_encoder_s *encoder);
leothedragon 0:25fa8795676b 75
leothedragon 0:25fa8795676b 76 #endif // CERT_RENEWAL_TEST
leothedragon 0:25fa8795676b 77
leothedragon 0:25fa8795676b 78 #ifdef __cplusplus
leothedragon 0:25fa8795676b 79 }
leothedragon 0:25fa8795676b 80 #endif
leothedragon 0:25fa8795676b 81
leothedragon 0:25fa8795676b 82 #endif // __CE_TLV_H__
leothedragon 0:25fa8795676b 83