iotivity 0.9.0
[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
28 #define TAG "gen_sec_bin"
29
30 void printStruct(const char * device, OCDtlsPskCredsBlob* s)
31 {
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);
36
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);
42 }
43
44
45 int main()
46 {
47     OCDtlsPskCredsBlob * s = NULL;
48     OCDtlsPskCredsBlob * c = NULL;
49     FILE* fps, *fpc;
50
51     int i;
52
53     srand(time(NULL));
54
55     s = (OCDtlsPskCredsBlob*) malloc(sizeof(OCDtlsPskCredsBlob));
56     c = (OCDtlsPskCredsBlob*) malloc(sizeof(OCDtlsPskCredsBlob));
57
58     memset(s, 0, sizeof(OCDtlsPskCredsBlob));
59     memset(c, 0, sizeof(OCDtlsPskCredsBlob));
60
61     s->blobVer = DtlsPskCredsBlobVer_CurrentVersion;
62     c->blobVer = DtlsPskCredsBlobVer_CurrentVersion;
63
64     s->num = c->num = 1;
65
66     for(i = 0; i < DTLS_PSK_ID_LEN; i++)
67     {
68         c->creds[0].id[i] = s->identity[i] = rand() % (2^8);
69
70         s->creds[0].id[i] = c->identity[i] = rand() % (2^8);
71
72         c->creds[0].psk[i] = s->creds[0].psk[i] = rand() % (2^8);
73     }
74
75     // Print Credentials
76     printStruct("Server", s);
77     printStruct("Client", c);
78
79     // Write to files
80     if ((fps = (FILE*) fopen("server_cred.bin", "wb")) != NULL)
81     {
82         fwrite(s, sizeof(OCDtlsPskCredsBlob), 1, fps);
83         fclose(fps);
84     }
85
86
87     if ((fpc = (FILE*) fopen("client_cred.bin", "wb")) != NULL)
88     {
89         fwrite(c, sizeof(OCDtlsPskCredsBlob), 1, fpc);
90         fclose(fpc);
91     }
92
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)
97     {
98         if (sizeof(OCDtlsPskCredsBlob) != fread(s, 1, sizeof(OCDtlsPskCredsBlob), fps))
99         {
100             OC_LOG(INFO, TAG, PCF("Reading from the file failed."));
101         }
102         fclose(fps);
103     }
104
105
106     if ((fpc = (FILE*) fopen("client_cred.bin", "rb")) != NULL)
107     {
108         if (sizeof(OCDtlsPskCredsBlob) != fread(c, 1, sizeof(OCDtlsPskCredsBlob), fpc))
109         {
110             OC_LOG(INFO, TAG, PCF("Reading from the file failed."));
111         }
112         fclose(fpc);
113     }
114
115     printf("\n\n");
116     OC_LOG(INFO, TAG, PCF("Reading from file and printing again to verify manually"));
117     printStruct("Server", s);
118     printStruct("Client", c);
119
120     free(s);
121     free(c);
122 }
123