|
| 1 | +/* |
| 2 | + * Copyright (c) 2016-2018, Arm Limited and affiliates. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +#include "nsconfig.h" |
| 19 | +#include <string.h> |
| 20 | +#include "ns_types.h" |
| 21 | +#include "ns_list.h" |
| 22 | +#include "ns_trace.h" |
| 23 | +#include "nsdynmemLIB.h" |
| 24 | +#include "NWK_INTERFACE/Include/protocol.h" |
| 25 | +#include "Common_Protocols/ipv6_constants.h" |
| 26 | +#include "socket_api.h" |
| 27 | +#include "6LoWPAN/ws/ws_config.h" |
| 28 | +#include "Security/protocols/sec_prot_certs.h" |
| 29 | +#include "Security/protocols/sec_prot_keys.h" |
| 30 | + |
| 31 | +#ifdef HAVE_WS |
| 32 | + |
| 33 | +#define TRACE_GROUP "spce" |
| 34 | + |
| 35 | +int8_t sec_prot_certs_init(sec_prot_certs_t *certs) |
| 36 | +{ |
| 37 | + if (!certs) { |
| 38 | + return -1; |
| 39 | + } |
| 40 | + |
| 41 | + sec_prot_certs_chain_entry_init(&certs->own_cert_chain); |
| 42 | + ns_list_init(&certs->trusted_cert_chain_list); |
| 43 | + ns_list_init(&certs->cert_revocat_lists); |
| 44 | + |
| 45 | + return 0; |
| 46 | +} |
| 47 | + |
| 48 | +cert_chain_entry_t *sec_prot_certs_chain_entry_create(void) |
| 49 | +{ |
| 50 | + cert_chain_entry_t *entry = ns_dyn_mem_alloc(sizeof(cert_chain_entry_t)); |
| 51 | + if (!entry) { |
| 52 | + return NULL; |
| 53 | + } |
| 54 | + sec_prot_certs_chain_entry_init(entry); |
| 55 | + return entry; |
| 56 | +} |
| 57 | + |
| 58 | +void sec_prot_certs_chain_entry_init(cert_chain_entry_t *entry) |
| 59 | +{ |
| 60 | + memset(entry, 0, sizeof(cert_chain_entry_t)); |
| 61 | +} |
| 62 | + |
| 63 | +int8_t sec_prot_certs_cert_set(cert_chain_entry_t *entry, uint8_t index, uint8_t *cert, uint16_t cert_len) |
| 64 | +{ |
| 65 | + if (!entry || index >= SEC_PROT_CERT_CHAIN_DEPTH) { |
| 66 | + return -1; |
| 67 | + } |
| 68 | + |
| 69 | + entry->cert[index] = cert; |
| 70 | + entry->cert_len[index] = cert_len; |
| 71 | + |
| 72 | + return 0; |
| 73 | +} |
| 74 | + |
| 75 | +uint8_t *sec_prot_certs_cert_get(const cert_chain_entry_t *entry, uint8_t index, uint16_t *cert_len) |
| 76 | +{ |
| 77 | + if (!entry || index >= SEC_PROT_CERT_CHAIN_DEPTH || !entry->cert[index]) { |
| 78 | + return NULL; |
| 79 | + } |
| 80 | + |
| 81 | + *cert_len = entry->cert_len[index]; |
| 82 | + return entry->cert[index]; |
| 83 | +} |
| 84 | + |
| 85 | +int8_t sec_prot_certs_priv_key_set(cert_chain_entry_t *entry, uint8_t *key, uint8_t key_len) |
| 86 | +{ |
| 87 | + if (!entry) { |
| 88 | + return -1; |
| 89 | + } |
| 90 | + |
| 91 | + entry->key = key; |
| 92 | + entry->key_len = key_len; |
| 93 | + |
| 94 | + return 0; |
| 95 | +} |
| 96 | + |
| 97 | +uint8_t *sec_prot_certs_priv_key_get(const cert_chain_entry_t *entry, uint8_t *key_len) |
| 98 | +{ |
| 99 | + if (!entry) { |
| 100 | + return NULL; |
| 101 | + } |
| 102 | + *key_len = entry->key_len; |
| 103 | + return entry->key; |
| 104 | +} |
| 105 | + |
| 106 | +void sec_prot_certs_chain_list_add(cert_chain_list_t *cert_chain_list, cert_chain_entry_t *entry) |
| 107 | +{ |
| 108 | + ns_list_add_to_end(cert_chain_list, entry); |
| 109 | +} |
| 110 | + |
| 111 | +void sec_prot_certs_chain_list_delete(cert_chain_list_t *chain_list) |
| 112 | +{ |
| 113 | + ns_list_foreach_safe(cert_chain_entry_t, entry, chain_list) { |
| 114 | + ns_list_remove(chain_list, entry); |
| 115 | + ns_dyn_mem_free(entry); |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +cert_revocat_list_entry_t *sec_prot_certs_revocat_list_entry_create(void) |
| 120 | +{ |
| 121 | + cert_revocat_list_entry_t *entry = ns_dyn_mem_alloc(sizeof(cert_revocat_list_entry_t)); |
| 122 | + if (!entry) { |
| 123 | + return NULL; |
| 124 | + } |
| 125 | + sec_prot_certs_revocat_list_entry_init(entry); |
| 126 | + return entry; |
| 127 | +} |
| 128 | + |
| 129 | +void sec_prot_certs_revocat_list_entry_init(cert_revocat_list_entry_t *entry) |
| 130 | +{ |
| 131 | + memset(entry, 0, sizeof(cert_revocat_list_entry_t)); |
| 132 | +} |
| 133 | + |
| 134 | +int8_t sec_prot_certs_revocat_list_set(cert_revocat_list_entry_t *entry, uint8_t *crl, uint16_t crl_len) |
| 135 | +{ |
| 136 | + if (!entry) { |
| 137 | + return -1; |
| 138 | + } |
| 139 | + |
| 140 | + entry->crl = crl; |
| 141 | + entry->crl_len = crl_len; |
| 142 | + |
| 143 | + return 0; |
| 144 | +} |
| 145 | + |
| 146 | +uint8_t *sec_prot_certs_revocat_list_get(const cert_revocat_list_entry_t *entry, uint16_t *crl_len) |
| 147 | +{ |
| 148 | + if (!entry) { |
| 149 | + return NULL; |
| 150 | + } |
| 151 | + *crl_len = entry->crl_len; |
| 152 | + return entry->crl; |
| 153 | +} |
| 154 | + |
| 155 | +void sec_prot_certs_revocat_lists_add(cert_revocat_lists_t *cert_revocat_lists, cert_revocat_list_entry_t *entry) |
| 156 | +{ |
| 157 | + ns_list_add_to_end(cert_revocat_lists, entry); |
| 158 | +} |
| 159 | + |
| 160 | +void sec_prot_certs_revocat_lists_delete(cert_revocat_lists_t *cert_revocat_lists) |
| 161 | +{ |
| 162 | + ns_list_foreach_safe(cert_revocat_list_entry_t, entry, cert_revocat_lists) { |
| 163 | + ns_list_remove(cert_revocat_lists, entry); |
| 164 | + ns_dyn_mem_free(entry); |
| 165 | + } |
| 166 | +} |
| 167 | + |
| 168 | +#endif /* HAVE_WS */ |
0 commit comments