Contiki-NG
Loading...
Searching...
No Matches
coap-keystore-simple.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 2017, RISE SICS AB.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the copyright holder nor the names of its
14 * contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS
18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
20 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
28 * OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31/**
32 * \file
33 * A simple keystore with fixed credentials.
34 * \author
35 * Niclas Finne <nfi@sics.se>
36 * Joakim Eriksson <joakime@sics.se>
37 */
38
39/**
40 * \addtogroup coap-keystore
41 * @{
42 */
43
44#include "coap-endpoint.h"
45#include "coap-keystore.h"
46#include <string.h>
47
48#ifdef WITH_DTLS
49#ifdef COAP_DTLS_PSK_DEFAULT_IDENTITY
50#ifdef COAP_DTLS_PSK_DEFAULT_KEY
51/*---------------------------------------------------------------------------*/
52static int
53get_default_psk_info(const coap_endpoint_t *address_info,
55{
56 if(info == NULL) {
57 return 0;
58 }
59
60 /* Return the default identify if no identity is provided. */
61 if(info->identity == NULL || info->identity_len == 0) {
62 /* Identity requested */
63 info->identity = (uint8_t *)COAP_DTLS_PSK_DEFAULT_IDENTITY;
64 info->identity_len = strlen(COAP_DTLS_PSK_DEFAULT_IDENTITY);
65 return 1;
66 }
67
68 /* We support only the default identity when querying for a key. */
69 if(info->identity_len != strlen(COAP_DTLS_PSK_DEFAULT_IDENTITY) ||
70 memcmp(info->identity, COAP_DTLS_PSK_DEFAULT_IDENTITY,
71 info->identity_len) != 0) {
72 /* Identity not matching */
73 return 0;
74 }
75
76 /* The identity matches the default identity -- fill in the key. */
77 info->key = (uint8_t *)COAP_DTLS_PSK_DEFAULT_KEY;
78 info->key_len = strlen(COAP_DTLS_PSK_DEFAULT_KEY);
79 return 1;
80}
81/*---------------------------------------------------------------------------*/
82static const coap_keystore_t simple_key_store = {
83 .coap_get_psk_info = get_default_psk_info
84};
85/*---------------------------------------------------------------------------*/
86#endif /* COAP_DTLS_PSK_DEFAULT_KEY */
87#endif /* COAP_DTLS_PSK_DEFAULT_IDENTITY */
88
89#ifdef COAP_DTLS_TEST_CA_CERT
90#ifdef COAP_DTLS_TEST_OWN_CERT
91#ifdef COAP_DTLS_TEST_PRIV_KEY
92/*---------------------------------------------------------------------------*/
93static int
94get_default_cert_info(const coap_endpoint_t *address_info,
96{
97 if(info == NULL) {
98 return 0;
99 }
100
101 info->ca_cert = (uint8_t *)COAP_DTLS_TEST_CA_CERT;
102 info->ca_cert_len = sizeof(COAP_DTLS_TEST_CA_CERT);
103
104 info->own_cert = (uint8_t *)COAP_DTLS_TEST_OWN_CERT;
105 info->own_cert_len = sizeof(COAP_DTLS_TEST_OWN_CERT);
106
107 info->priv_key = (uint8_t *)COAP_DTLS_TEST_PRIV_KEY;
108 info->priv_key_len = sizeof(COAP_DTLS_TEST_PRIV_KEY);
109 return 1;
110}
111/*---------------------------------------------------------------------------*/
112static const coap_keystore_t simple_key_store = {
113 .coap_get_cert_info = get_default_cert_info
114};
115/*---------------------------------------------------------------------------*/
116#endif /* COAP_DTLS_TEST_CA_CERT */
117#endif /* COAP_DTLS_TEST_OWN_CERT */
118#endif /* COAP_DTLS_TEST_PRIV_KEY */
119#endif /* WITH_DTLS */
120/*---------------------------------------------------------------------------*/
121void
123{
124#ifdef WITH_DTLS
125#if (defined(COAP_DTLS_PSK_DEFAULT_IDENTITY) \
126 && defined(COAP_DTLS_PSK_DEFAULT_KEY)) \
127 || (defined(COAP_DTLS_TEST_CA_CERT) \
128 && defined(COAP_DTLS_TEST_OWN_CERT) \
129 && defined(COAP_DTLS_TEST_PRIV_KEY))
130
131 coap_set_keystore(&simple_key_store);
132#endif /* (defined(COAP_DTLS_PSK_DEFAULT_IDENTITY) \
133 && defined(COAP_DTLS_PSK_DEFAULT_KEY)) \
134 || (defined(COAP_DTLS_TEST_CA_CERT) \
135 && defined(COAP_DTLS_TEST_OWN_CERT) \
136 && defined(COAP_DTLS_TEST_PRIV_KEY)) */
137#endif /* WITH_DTLS */
138}
139/*---------------------------------------------------------------------------*/
140/** @} */
API to address CoAP endpoints.
API for CoAP keystore.
void coap_keystore_simple_init(void)
Registers a simple CoAP DTLS keystore with fixed pre-shared key credentials.
void coap_set_keystore(const coap_keystore_t *keystore)
Set the CoAP keystore to use by CoAP.
The structure of a CoAP PKI certificate info.
The structure of a CoAP pre-shared key info.
The structure of a CoAP keystore.