1 /******************************************************************
3 * Copyright 2015 Samsung Electronics All Rights Reserved.
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 * LICENSE-2.0" target="_blank">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.
20 ******************************************************************/
26 //Size of initial memory
27 #define CRL_BLOCK_LEN 20
33 * General structure for storing serial numbers.
35 * Contains pointer to array of bytes and it's length.
39 ByteArray array; /**< Byte array with data*/
40 size_t blockNumber; /**< Number of used memory blocks */
45 * Internal storage for serial numbers.
47 static SNStore Store = {{NULL, 0}, 1};
49 // Frees memory occupied by SN storage.
50 void FreeSNStore(void)
52 free(Store.array.data);
53 INIT_BYTE_ARRAY(Store.array);
54 Store.blockNumber = 1;
57 // Stores serial number to SN storage.
58 PKIError StoreSerialNumber(const ByteArray sn)
61 CHECK_NULL(sn.data, PKI_NULL_PASSED);
64 if (Store.array.len == 0 || Store.array.len + sn.len + 1 > CRL_BLOCK_LEN * Store.blockNumber)
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;
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;
76 if (error_value != PKI_SUCCESS) free(temp);
81 // Checks whether there is serial number in SN storage
82 PKIError CheckSerialNumber(const ByteArray sn)
86 CHECK_NULL(sn.data, PKI_NULL_PASSED);
88 CHECK_NULL(Store.array.data, PKI_SUCCESS);
89 for ( i = 0; i < Store.array.len; i += Store.array.data[i] + 1)
91 if (sn.len == Store.array.data[i])
93 res = memcmp(&Store.array.data[i + 1], sn.data, sn.len);
94 CHECK_NOT_EQUAL(res, 0, PKI_CERT_REVOKED);
101 //Prints store content
102 void PrintSNStore(void)
106 if (Store.array.data != NULL)
108 for ( i = 0; i < Store.array.len; i += Store.array.data[i] + 1)
110 curr.len = Store.array.data[i];
111 curr.data = &Store.array.data[i + 1];
112 PRINT_BYTE_ARRAY("", curr);
116 printf("\nSN STORE CONTAINS %d ELEMENTS\n", count);