Imported Upstream version 0.9.1
[platform/upstream/iotivity.git] / resource / csdk / stack / samples / linux / secure / gen_sec_bin.cpp
1 //******************************************************************
2 //
3 // Copyright 2014 Intel Mobile Communications GmbH All Rights Reserved.
4 //
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6 //
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
10 //
11 //      http://www.apache.org/licenses/LICENSE-2.0
12 //
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.
18 //
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20
21 #include "ocsecurityconfig.h"
22 #include "logger.h"
23 #include <stdio.h>
24 #include <time.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <sys/stat.h>
28
29 #define TAG "gen_sec_bin"
30
31 //scratch buffer
32 const int WORK_BUF_LEN = 512;
33
34 const char SERVER_CRED_FILE[] = "server_cred.bin";
35 const char CLIENT_CRED_FILE[] = "client_cred.bin";
36
37 static void printStruct(const char * device, OCSecConfigData* s)
38 {
39     if (device && s)
40     {
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);
44
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);
48
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);
52
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);
58     }
59 }
60
61
62 static int SizeOfOCConfigData (OCSecConfigData *oscd)
63 {
64     int len = 0;
65     if(oscd)
66     {
67         int i = 0;
68         OCSecBlob * osb;
69         len = len + sizeof(OCSecConfigData) - sizeof(uint8_t);
70
71         //go to first blob
72         osb = (OCSecBlob*)(oscd->blob);
73         for( i =0; i < oscd->numBlob; i++)
74         {
75             len += (sizeof(OCSecBlob) - sizeof(uint8_t) + osb->len);
76             osb = config_data_next_blob(osb);
77         }
78     }
79     return len;
80 }
81
82 int main()
83 {
84     unsigned char buf_s[WORK_BUF_LEN];
85     unsigned char buf_c[WORK_BUF_LEN];
86
87     srand(time(NULL));
88
89     OCSecConfigData * oscd_s = (OCSecConfigData*)buf_s;
90     OCSecConfigData * oscd_c = (OCSecConfigData*)buf_c;
91     oscd_s->version = oscd_c->version = OCSecConfigVer_CurrentVersion;
92
93     //Only storing 1 blob of type 'OC_BLOB_TYPE_PSK'
94     oscd_s->numBlob = oscd_c->numBlob = 1;
95
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);
102
103     OCDtlsPskCredsBlob * odpcb_s = (OCDtlsPskCredsBlob*)(osb_s->val);
104     OCDtlsPskCredsBlob * odpcb_c = (OCDtlsPskCredsBlob*)(osb_c->val);
105
106     odpcb_s->num = odpcb_c->num = 1;
107
108     for(int i = 0; i < DTLS_PSK_ID_LEN; i++)
109     {
110         odpcb_c->creds[0].id[i] = odpcb_s->identity[i] = rand() % (2^8);
111
112         odpcb_s->creds[0].id[i] = odpcb_c->identity[i] = rand() % (2^8);
113
114         odpcb_c->creds[0].psk[i] = odpcb_s->creds[0].psk[i] = rand() % (2^8);
115     }
116
117     // Print Credentials
118     printStruct("Server", oscd_s);
119     printStruct("Client", oscd_c);
120
121     // Write to files
122     FILE* fps, *fpc;
123     if ((fps = (FILE*) fopen("server_cred.bin", "wb")) != NULL)
124     {
125         fwrite(oscd_s, SizeOfOCConfigData(oscd_s), 1, fps);
126         fclose(fps);
127     }
128
129
130     if ((fpc = (FILE*) fopen("client_cred.bin", "wb")) != NULL)
131     {
132         fwrite(oscd_c, SizeOfOCConfigData(oscd_c), 1, fpc);
133         fclose(fpc);
134     }
135
136     struct stat st;
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)
141     {
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))
145         {
146             OC_LOG(INFO, TAG, PCF("Reading from the file failed."));
147         }
148         fclose(fps);
149     }
150
151
152     if ((fpc = (FILE*) fopen(CLIENT_CRED_FILE, "rb")) != NULL)
153     {
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))
157         {
158             OC_LOG(INFO, TAG, PCF("Reading from the file failed."));
159         }
160         fclose(fpc);
161     }
162
163     printf("\n\n");
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);
167
168     return 1;
169 }
170
171