f3db429823a151145b89963b80aeb4e64c2dec35
[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         uint8_t *temp = NULL;
62         CHECK_NULL(sn.data, PKI_NULL_PASSED);
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         unsigned long int i;
86         int res;
87         CHECK_NULL(sn.data, PKI_NULL_PASSED);
88     );
89     CHECK_NULL(Store.array.data, PKI_SUCCESS);
90     for ( i = 0; i < Store.array.len; i += Store.array.data[i] + 1)
91     {
92         if (sn.len == Store.array.data[i])
93         {
94             res  = memcmp(&Store.array.data[i + 1], sn.data, sn.len);
95             CHECK_NOT_EQUAL(res, 0, PKI_CERT_REVOKED);
96         }
97     }
98     FUNCTION_CLEAR();
99 }
100
101 #ifdef X509_DEBUG
102 //Prints store content
103 void PrintSNStore(void)
104 {
105     ByteArray curr;
106     int i, count = 0;
107     if (Store.array.data != NULL)
108     {
109         for ( i = 0; i < Store.array.len; i += Store.array.data[i] + 1)
110         {
111             curr.len = Store.array.data[i];
112             curr.data = &Store.array.data[i + 1];
113             PRINT_BYTE_ARRAY("", curr);
114             count++;
115         }
116     }
117     printf("\nSN STORE CONTAINS %d ELEMENTS\n", count);
118 }
119 #endif //DEBUG