Contiki-NG
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 /*---------------------------------------------------------------------------*/
52 static int
53 get_default_psk_info(const coap_endpoint_t *address_info,
55 {
56  if(info != NULL) {
57  if(info->identity == NULL || info->identity_len == 0) {
58  /* Identity requested */
59  info->identity = (uint8_t *)COAP_DTLS_PSK_DEFAULT_IDENTITY;
60  info->identity_len = strlen(COAP_DTLS_PSK_DEFAULT_IDENTITY);
61  return 1;
62  }
63  if(info->identity_len != strlen(COAP_DTLS_PSK_DEFAULT_IDENTITY) ||
64  memcmp(info->identity, COAP_DTLS_PSK_DEFAULT_IDENTITY,
65  info->identity_len) != 0) {
66  /* Identity not matching */
67  return 0;
68  }
69  info->key = (uint8_t *)COAP_DTLS_PSK_DEFAULT_KEY;
70  info->key_len = strlen(COAP_DTLS_PSK_DEFAULT_KEY);
71  return 1;
72  }
73  return 0;
74 }
75 static const coap_keystore_t simple_key_store = {
76  .coap_get_psk_info = get_default_psk_info
77 };
78 /*---------------------------------------------------------------------------*/
79 #endif /* COAP_DTLS_PSK_DEFAULT_KEY */
80 #endif /* COAP_DTLS_PSK_DEFAULT_IDENTITY */
81 #endif /* WITH_DTLS */
82 /*---------------------------------------------------------------------------*/
83 void
85 {
86 #ifdef WITH_DTLS
87 #ifdef COAP_DTLS_PSK_DEFAULT_IDENTITY
88 #ifdef COAP_DTLS_PSK_DEFAULT_KEY
89 
90  coap_set_keystore(&simple_key_store);
91 
92 #endif /* COAP_DTLS_PSK_DEFAULT_KEY */
93 #endif /* COAP_DTLS_PSK_DEFAULT_IDENTITY */
94 #endif /* WITH_DTLS */
95 }
96 /*---------------------------------------------------------------------------*/
97 /** @} */
API to address CoAP endpoints
The structure of a CoAP keystore.
Definition: coap-keystore.h:75
void coap_set_keystore(const coap_keystore_t *keystore)
Set the CoAP keystore to use by CoAP.
The structure of a CoAP pre-shared key info.
Definition: coap-keystore.h:59
void coap_keystore_simple_init(void)
Registers a simple CoAP DTLS keystore with fixed pre-shared key credentials.
API for CoAP keystore