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"
28 #define TAG "gen_sec_bin"
30 void printStruct(const char * device, OCDtlsPskCredsBlob* s)
32 OC_LOG(INFO, TAG, device);
33 OC_LOG_V(INFO, TAG, "Version - %d", s->blobVer);
34 OC_LOG(INFO, TAG, "My Identity :");
35 OC_LOG_BUFFER(INFO, TAG, s->identity, DTLS_PSK_ID_LEN);
37 OC_LOG_V(INFO, TAG, "Number of trusted Peers - %d", s->num);
38 OC_LOG(INFO, TAG, "Peer Identity :");
39 OC_LOG_BUFFER(INFO, TAG, s->creds[0].id, DTLS_PSK_ID_LEN);
40 OC_LOG(INFO, TAG, "Peer Psk :");
41 OC_LOG_BUFFER(INFO, TAG, s->creds[0].psk, DTLS_PSK_PSK_LEN);
47 OCDtlsPskCredsBlob * s = NULL;
48 OCDtlsPskCredsBlob * c = NULL;
55 s = (OCDtlsPskCredsBlob*) malloc(sizeof(OCDtlsPskCredsBlob));
56 c = (OCDtlsPskCredsBlob*) malloc(sizeof(OCDtlsPskCredsBlob));
58 memset(s, 0, sizeof(OCDtlsPskCredsBlob));
59 memset(c, 0, sizeof(OCDtlsPskCredsBlob));
61 s->blobVer = DtlsPskCredsBlobVer_CurrentVersion;
62 c->blobVer = DtlsPskCredsBlobVer_CurrentVersion;
66 for(i = 0; i < DTLS_PSK_ID_LEN; i++)
68 c->creds[0].id[i] = s->identity[i] = rand() % (2^8);
70 s->creds[0].id[i] = c->identity[i] = rand() % (2^8);
72 c->creds[0].psk[i] = s->creds[0].psk[i] = rand() % (2^8);
76 printStruct("Server", s);
77 printStruct("Client", c);
80 if ((fps = (FILE*) fopen("server_cred.bin", "wb")) != NULL)
82 fwrite(s, sizeof(OCDtlsPskCredsBlob), 1, fps);
87 if ((fpc = (FILE*) fopen("client_cred.bin", "wb")) != NULL)
89 fwrite(c, sizeof(OCDtlsPskCredsBlob), 1, fpc);
93 memset(s, 0, sizeof(OCDtlsPskCredsBlob));
94 memset(c, 0, sizeof(OCDtlsPskCredsBlob));
95 // Read from files; print and verify manually
96 if ((fps = (FILE*) fopen("server_cred.bin", "rb")) != NULL)
98 if (sizeof(OCDtlsPskCredsBlob) != fread(s, 1, sizeof(OCDtlsPskCredsBlob), fps))
100 OC_LOG(INFO, TAG, PCF("Reading from the file failed."));
106 if ((fpc = (FILE*) fopen("client_cred.bin", "rb")) != NULL)
108 if (sizeof(OCDtlsPskCredsBlob) != fread(c, 1, sizeof(OCDtlsPskCredsBlob), fpc))
110 OC_LOG(INFO, TAG, PCF("Reading from the file failed."));
116 OC_LOG(INFO, TAG, PCF("Reading from file and printing again to verify manually"));
117 printStruct("Server", s);
118 printStruct("Client", c);