Skip to content

Commit f57138f

Browse files
author
Mika Leppänen
committed
Security key storage and certificate info updates
Added certificate info to security key storage. Certificate information can be used by security protocols to access own certificate chain, trusted certificates and certificate revocation list.
1 parent b7177c5 commit f57138f

File tree

7 files changed

+427
-18
lines changed

7 files changed

+427
-18
lines changed

source/Security/protocols/sec_prot.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ typedef enum {
3131
SEC_RESULT_OK = 0,
3232
SEC_RESULT_ERR_NO_MEM = -1,
3333
SEC_RESULT_TIMEOUT = -2,
34+
SEC_RESULT_ERROR = -3
3435
} sec_prot_result_e;
3536

3637
typedef enum {
@@ -43,6 +44,11 @@ typedef enum {
4344
SEC_STATE_FIRST
4445
} sec_prot_state_e;
4546

47+
typedef enum {
48+
SEC_PROT_TYPE_EAP_TLS = 0,
49+
SEC_PROT_TYPE_TLS
50+
} sec_prot_type_e;
51+
4652
/**
4753
* sec_prot_create_request KMP-CREATE.request to security protocol
4854
*
@@ -181,6 +187,17 @@ typedef void sec_prot_timer_timeout(sec_prot_t *prot, uint16_t ticks);
181187
*/
182188
typedef void sec_prot_eui64_addr_get(sec_prot_t *prot, uint8_t *local_eui64, uint8_t *remote_eui64);
183189

190+
/**
191+
* sec_prot_by_type_get gets security protocol
192+
*
193+
* \param prot protocol
194+
* \param type security protocol type
195+
*
196+
* \return security protocol or NULL
197+
*
198+
*/
199+
typedef sec_prot_t *sec_prot_by_type_get(sec_prot_t *prot, uint8_t type);
200+
184201
// Security protocol data
185202
struct sec_prot_s {
186203
sec_prot_create_request *create_req; /**< Create request */
@@ -204,6 +221,7 @@ struct sec_prot_s {
204221
sec_prot_timer_timeout *timer_timeout; /**< Timer timeout */
205222

206223
sec_prot_eui64_addr_get *addr_get; /**< Gets EUI-64 addresses */
224+
sec_prot_by_type_get *type_get; /**< Gets security protocol by type */
207225

208226
sec_prot_keys_t *sec_keys; /**< Security keys storage pointer */
209227
uint8_t header_size; /**< Header size */
Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
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

Comments
 (0)