Add certificate verification
[platform/upstream/iotivity.git] / resource / csdk / connectivity / src / adapter_util / pkix / sn_store.c
1 /******************************************************************
2  *
3  * Copyright 2015 Samsung Electronics 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  *      LICENSE-2.0" target="_blank">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
22 #include "sn_store.h"
23 #include <string.h>
24 #include <stdlib.h>
25
26 //Size of initial memory
27 #define CRL_BLOCK_LEN 20
28
29
30 /**
31  * @struct SNStore
32  *
33  * General structure for storing serial numbers.
34  *
35  * Contains pointer to array of bytes and it's length.
36  */
37 typedef struct
38 {
39     ByteArray array;     /**< Byte array with data*/
40     size_t blockNumber;  /**< Number of used memory blocks */
41 } SNStore;
42
43
44 /**
45  * Internal storage for serial numbers.
46  */
47 static SNStore  Store = {{NULL, 0}, 1};
48
49 // Frees memory occupied by SN storage.
50 void FreeSNStore(void)
51 {
52     free(Store.array.data);
53     INIT_BYTE_ARRAY(Store.array);
54     Store.blockNumber = 1;
55 }
56
57 // Stores serial number to SN storage.
58 PKIError StoreSerialNumber(const ByteArray sn)
59 {
60     FUNCTION_INIT(
61         CHECK_NULL(sn.data, PKI_NULL_PASSED);
62         uint8_t *temp = NULL;
63     );
64     if (Store.array.len == 0 || Store.array.len + sn.len + 1 > CRL_BLOCK_LEN * Store.blockNumber)
65     {
66         temp = (uint8_t *) realloc(Store.array.data,
67                                     sizeof(uint8_t) * CRL_BLOCK_LEN * Store.blockNumber * 2);
68         CHECK_NULL(temp, PKI_MEMORY_ALLOC_FAILED);
69         Store.array.data = temp;
70         Store.blockNumber *= 2;
71     }
72     Store.array.data[Store.array.len] = sn.len;
73     memcpy(&Store.array.data[Store.array.len + 1], sn.data, sn.len);
74     Store.array.len += sn.len + 1;
75     FUNCTION_CLEAR(
76         if (error_value != PKI_SUCCESS)  free(temp);
77     );
78 }
79
80
81 // Checks whether there is serial number in SN storage
82 PKIError CheckSerialNumber(const ByteArray sn)
83 {
84     FUNCTION_INIT(
85         int i, res;
86         CHECK_NULL(sn.data, PKI_NULL_PASSED);
87     );
88     CHECK_NULL(Store.array.data, PKI_SUCCESS);
89     for ( i = 0; i < Store.array.len; i += Store.array.data[i] + 1)
90     {
91         if (sn.len == Store.array.data[i])
92         {
93             res  = memcmp(&Store.array.data[i + 1], sn.data, sn.len);
94             CHECK_NOT_EQUAL(res, 0, PKI_CERT_REVOKED);
95         }
96     }
97     FUNCTION_CLEAR();
98 }
99
100 #ifdef X509_DEBUG
101 //Prints store content
102 void PrintSNStore(void)
103 {
104     ByteArray curr;
105     int i, count = 0;
106     if (Store.array.data != NULL)
107     {
108         for ( i = 0; i < Store.array.len; i += Store.array.data[i] + 1)
109         {
110             curr.len = Store.array.data[i];
111             curr.data = &Store.array.data[i + 1];
112             PRINT_BYTE_ARRAY("", curr);
113             count++;
114         }
115     }
116     printf("\nSN STORE CONTAINS %d ELEMENTS\n", count);
117 }
118 #endif //DEBUG