1 //******************************************************************
3 // Copyright 2014 Intel Mobile Communications GmbH All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
11 // http://www.apache.org/licenses/LICENSE-2.0
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21 #include "ocsecurityconfig.h"
29 #define TAG "gen_sec_bin"
32 const int WORK_BUF_LEN = 512;
34 const char SERVER_CRED_FILE[] = "server_cred.bin";
35 const char CLIENT_CRED_FILE[] = "client_cred.bin";
37 static void printStruct(const char * device, OCSecConfigData* s)
41 OC_LOG(INFO, TAG, device);
42 OC_LOG_V(INFO, TAG, "Version - %d", s->version);
43 OC_LOG_V(INFO, TAG, "Number of blobs - %d", s->numBlob);
45 OCSecBlob* osb = (OCSecBlob*)(s->blob);
46 OC_LOG_V(INFO, TAG, "Blob Type - %d", osb->type);
47 OC_LOG_V(INFO, TAG, "Blob Data Length - %d", osb->len);
49 OCDtlsPskCredsBlob* odpcb = (OCDtlsPskCredsBlob*)(osb->val);
50 OC_LOG(INFO, TAG, "My Identity :");
51 OC_LOG_BUFFER(INFO, TAG, odpcb->identity, DTLS_PSK_ID_LEN);
53 OC_LOG_V(INFO, TAG, "Number of trusted Peers - %d", odpcb->num);
54 OC_LOG(INFO, TAG, "Peer Identity :");
55 OC_LOG_BUFFER(INFO, TAG, odpcb->creds[0].id, DTLS_PSK_ID_LEN);
56 OC_LOG(INFO, TAG, "Peer Psk :");
57 OC_LOG_BUFFER(INFO, TAG, odpcb->creds[0].psk, DTLS_PSK_PSK_LEN);
62 static int SizeOfOCConfigData (OCSecConfigData *oscd)
69 len = len + sizeof(OCSecConfigData) - sizeof(uint8_t);
72 osb = (OCSecBlob*)(oscd->blob);
73 for( i =0; i < oscd->numBlob; i++)
75 len += (sizeof(OCSecBlob) - sizeof(uint8_t) + osb->len);
76 osb = config_data_next_blob(osb);
84 unsigned char buf_s[WORK_BUF_LEN];
85 unsigned char buf_c[WORK_BUF_LEN];
89 OCSecConfigData * oscd_s = (OCSecConfigData*)buf_s;
90 OCSecConfigData * oscd_c = (OCSecConfigData*)buf_c;
91 oscd_s->version = oscd_c->version = OCSecConfigVer_CurrentVersion;
93 //Only storing 1 blob of type 'OC_BLOB_TYPE_PSK'
94 oscd_s->numBlob = oscd_c->numBlob = 1;
96 OCSecBlob * osb_s = (OCSecBlob*)oscd_s->blob;
97 OCSecBlob * osb_c = (OCSecBlob*)oscd_c->blob;
98 osb_s->type = osb_c->type = OC_BLOB_TYPE_PSK;
99 //length of this blob will be the length to contain PSK credentials
100 // for '1' peer device
101 osb_s->len = osb_c->len = sizeof(OCDtlsPskCredsBlob);
103 OCDtlsPskCredsBlob * odpcb_s = (OCDtlsPskCredsBlob*)(osb_s->val);
104 OCDtlsPskCredsBlob * odpcb_c = (OCDtlsPskCredsBlob*)(osb_c->val);
106 odpcb_s->num = odpcb_c->num = 1;
108 for(int i = 0; i < DTLS_PSK_ID_LEN; i++)
110 odpcb_c->creds[0].id[i] = odpcb_s->identity[i] = rand() % (2^8);
112 odpcb_s->creds[0].id[i] = odpcb_c->identity[i] = rand() % (2^8);
114 odpcb_c->creds[0].psk[i] = odpcb_s->creds[0].psk[i] = rand() % (2^8);
118 printStruct("Server", oscd_s);
119 printStruct("Client", oscd_c);
123 if ((fps = (FILE*) fopen("server_cred.bin", "wb")) != NULL)
125 fwrite(oscd_s, SizeOfOCConfigData(oscd_s), 1, fps);
130 if ((fpc = (FILE*) fopen("client_cred.bin", "wb")) != NULL)
132 fwrite(oscd_c, SizeOfOCConfigData(oscd_c), 1, fpc);
137 memset(buf_s, 0, sizeof(buf_s));
138 memset(buf_c, 0, sizeof(buf_c));
139 // Read from files; print and verify manually
140 if ((fps = (FILE*) fopen(SERVER_CRED_FILE, "rb")) != NULL)
142 stat(SERVER_CRED_FILE, &st);
143 if ((sizeof(buf_s) < (unsigned int)st.st_size) ||
144 (fread(buf_s, 1, st.st_size, fps) != (unsigned int)st.st_size))
146 OC_LOG(INFO, TAG, PCF("Reading from the file failed."));
152 if ((fpc = (FILE*) fopen(CLIENT_CRED_FILE, "rb")) != NULL)
154 stat(CLIENT_CRED_FILE, &st);
155 if ((sizeof(buf_c) < (unsigned int)st.st_size) ||
156 (fread(buf_c, 1, st.st_size, fpc) != (unsigned int)st.st_size))
158 OC_LOG(INFO, TAG, PCF("Reading from the file failed."));
164 OC_LOG(INFO, TAG, PCF("Reading from file and printing again to verify manually"));
165 printStruct("Server", (OCSecConfigData*)buf_s);
166 printStruct("Client", (OCSecConfigData*)buf_c);