mbed-os5 only for TYBLE16
Dependents: TYBLE16_simple_data_logger TYBLE16_MP3_Air
features/FEATURE_BLE/ble/generic/FileSecurityDb.h@0:5b88d5760320, 2019-12-17 (annotated)
- Committer:
- kenjiArai
- Date:
- Tue Dec 17 23:23:45 2019 +0000
- Revision:
- 0:5b88d5760320
mbed-os5 only for TYBLE16
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kenjiArai | 0:5b88d5760320 | 1 | /* mbed Microcontroller Library |
kenjiArai | 0:5b88d5760320 | 2 | * Copyright (c) 2018 ARM Limited |
kenjiArai | 0:5b88d5760320 | 3 | * |
kenjiArai | 0:5b88d5760320 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
kenjiArai | 0:5b88d5760320 | 5 | * you may not use this file except in compliance with the License. |
kenjiArai | 0:5b88d5760320 | 6 | * You may obtain a copy of the License at |
kenjiArai | 0:5b88d5760320 | 7 | * |
kenjiArai | 0:5b88d5760320 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
kenjiArai | 0:5b88d5760320 | 9 | * |
kenjiArai | 0:5b88d5760320 | 10 | * Unless required by applicable law or agreed to in writing, software |
kenjiArai | 0:5b88d5760320 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
kenjiArai | 0:5b88d5760320 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
kenjiArai | 0:5b88d5760320 | 13 | * See the License for the specific language governing permissions and |
kenjiArai | 0:5b88d5760320 | 14 | * limitations under the License. |
kenjiArai | 0:5b88d5760320 | 15 | */ |
kenjiArai | 0:5b88d5760320 | 16 | |
kenjiArai | 0:5b88d5760320 | 17 | #ifndef GENERIC_FILE_SECURITY_DB_H_ |
kenjiArai | 0:5b88d5760320 | 18 | #define GENERIC_FILE_SECURITY_DB_H_ |
kenjiArai | 0:5b88d5760320 | 19 | |
kenjiArai | 0:5b88d5760320 | 20 | #include "SecurityDb.h" |
kenjiArai | 0:5b88d5760320 | 21 | |
kenjiArai | 0:5b88d5760320 | 22 | #include <stdio.h> |
kenjiArai | 0:5b88d5760320 | 23 | |
kenjiArai | 0:5b88d5760320 | 24 | namespace ble { |
kenjiArai | 0:5b88d5760320 | 25 | namespace generic { |
kenjiArai | 0:5b88d5760320 | 26 | |
kenjiArai | 0:5b88d5760320 | 27 | /** Filesystem implementation */ |
kenjiArai | 0:5b88d5760320 | 28 | class FileSecurityDb : public SecurityDb { |
kenjiArai | 0:5b88d5760320 | 29 | private: |
kenjiArai | 0:5b88d5760320 | 30 | |
kenjiArai | 0:5b88d5760320 | 31 | struct entry_t { |
kenjiArai | 0:5b88d5760320 | 32 | SecurityDistributionFlags_t flags; |
kenjiArai | 0:5b88d5760320 | 33 | sign_count_t peer_sign_counter; |
kenjiArai | 0:5b88d5760320 | 34 | size_t file_offset; |
kenjiArai | 0:5b88d5760320 | 35 | }; |
kenjiArai | 0:5b88d5760320 | 36 | |
kenjiArai | 0:5b88d5760320 | 37 | static const size_t MAX_ENTRIES = 5; |
kenjiArai | 0:5b88d5760320 | 38 | |
kenjiArai | 0:5b88d5760320 | 39 | static entry_t* as_entry(entry_handle_t db_handle) { |
kenjiArai | 0:5b88d5760320 | 40 | return reinterpret_cast<entry_t*>(db_handle); |
kenjiArai | 0:5b88d5760320 | 41 | } |
kenjiArai | 0:5b88d5760320 | 42 | |
kenjiArai | 0:5b88d5760320 | 43 | template<class T> |
kenjiArai | 0:5b88d5760320 | 44 | void db_read(T *value, long int offset) { |
kenjiArai | 0:5b88d5760320 | 45 | fseek(_db_file, offset, SEEK_SET); |
kenjiArai | 0:5b88d5760320 | 46 | fread(value, sizeof(T), 1, _db_file); |
kenjiArai | 0:5b88d5760320 | 47 | } |
kenjiArai | 0:5b88d5760320 | 48 | |
kenjiArai | 0:5b88d5760320 | 49 | template<class T> |
kenjiArai | 0:5b88d5760320 | 50 | void db_write(T *value, long int offset) { |
kenjiArai | 0:5b88d5760320 | 51 | fseek(_db_file, offset, SEEK_SET); |
kenjiArai | 0:5b88d5760320 | 52 | fwrite(value, sizeof(T), 1, _db_file); |
kenjiArai | 0:5b88d5760320 | 53 | } |
kenjiArai | 0:5b88d5760320 | 54 | |
kenjiArai | 0:5b88d5760320 | 55 | public: |
kenjiArai | 0:5b88d5760320 | 56 | FileSecurityDb(FILE *db_file); |
kenjiArai | 0:5b88d5760320 | 57 | virtual ~FileSecurityDb(); |
kenjiArai | 0:5b88d5760320 | 58 | |
kenjiArai | 0:5b88d5760320 | 59 | /** |
kenjiArai | 0:5b88d5760320 | 60 | * Validates or creates a file for the security database. |
kenjiArai | 0:5b88d5760320 | 61 | * @param db_path path to the file |
kenjiArai | 0:5b88d5760320 | 62 | * @return FILE handle open and ready for use by the database or NULL if unavailable |
kenjiArai | 0:5b88d5760320 | 63 | */ |
kenjiArai | 0:5b88d5760320 | 64 | static FILE* open_db_file(const char *db_path); |
kenjiArai | 0:5b88d5760320 | 65 | |
kenjiArai | 0:5b88d5760320 | 66 | virtual SecurityDistributionFlags_t* get_distribution_flags( |
kenjiArai | 0:5b88d5760320 | 67 | entry_handle_t db_handle |
kenjiArai | 0:5b88d5760320 | 68 | ); |
kenjiArai | 0:5b88d5760320 | 69 | |
kenjiArai | 0:5b88d5760320 | 70 | |
kenjiArai | 0:5b88d5760320 | 71 | /* local keys */ |
kenjiArai | 0:5b88d5760320 | 72 | |
kenjiArai | 0:5b88d5760320 | 73 | /* set */ |
kenjiArai | 0:5b88d5760320 | 74 | virtual void set_entry_local_ltk( |
kenjiArai | 0:5b88d5760320 | 75 | entry_handle_t db_handle, |
kenjiArai | 0:5b88d5760320 | 76 | const ltk_t <k |
kenjiArai | 0:5b88d5760320 | 77 | ); |
kenjiArai | 0:5b88d5760320 | 78 | |
kenjiArai | 0:5b88d5760320 | 79 | virtual void set_entry_local_ediv_rand( |
kenjiArai | 0:5b88d5760320 | 80 | entry_handle_t db_handle, |
kenjiArai | 0:5b88d5760320 | 81 | const ediv_t &ediv, |
kenjiArai | 0:5b88d5760320 | 82 | const rand_t &rand |
kenjiArai | 0:5b88d5760320 | 83 | ); |
kenjiArai | 0:5b88d5760320 | 84 | |
kenjiArai | 0:5b88d5760320 | 85 | /* peer's keys */ |
kenjiArai | 0:5b88d5760320 | 86 | |
kenjiArai | 0:5b88d5760320 | 87 | /* set */ |
kenjiArai | 0:5b88d5760320 | 88 | |
kenjiArai | 0:5b88d5760320 | 89 | virtual void set_entry_peer_ltk( |
kenjiArai | 0:5b88d5760320 | 90 | entry_handle_t db_handle, |
kenjiArai | 0:5b88d5760320 | 91 | const ltk_t <k |
kenjiArai | 0:5b88d5760320 | 92 | ); |
kenjiArai | 0:5b88d5760320 | 93 | |
kenjiArai | 0:5b88d5760320 | 94 | virtual void set_entry_peer_ediv_rand( |
kenjiArai | 0:5b88d5760320 | 95 | entry_handle_t db_handle, |
kenjiArai | 0:5b88d5760320 | 96 | const ediv_t &ediv, |
kenjiArai | 0:5b88d5760320 | 97 | const rand_t &rand |
kenjiArai | 0:5b88d5760320 | 98 | ); |
kenjiArai | 0:5b88d5760320 | 99 | |
kenjiArai | 0:5b88d5760320 | 100 | virtual void set_entry_peer_irk( |
kenjiArai | 0:5b88d5760320 | 101 | entry_handle_t db_handle, |
kenjiArai | 0:5b88d5760320 | 102 | const irk_t &irk |
kenjiArai | 0:5b88d5760320 | 103 | ); |
kenjiArai | 0:5b88d5760320 | 104 | |
kenjiArai | 0:5b88d5760320 | 105 | virtual void set_entry_peer_bdaddr( |
kenjiArai | 0:5b88d5760320 | 106 | entry_handle_t db_handle, |
kenjiArai | 0:5b88d5760320 | 107 | bool address_is_public, |
kenjiArai | 0:5b88d5760320 | 108 | const address_t &peer_address |
kenjiArai | 0:5b88d5760320 | 109 | ); |
kenjiArai | 0:5b88d5760320 | 110 | |
kenjiArai | 0:5b88d5760320 | 111 | virtual void set_entry_peer_csrk( |
kenjiArai | 0:5b88d5760320 | 112 | entry_handle_t db_handle, |
kenjiArai | 0:5b88d5760320 | 113 | const csrk_t &csrk |
kenjiArai | 0:5b88d5760320 | 114 | ); |
kenjiArai | 0:5b88d5760320 | 115 | |
kenjiArai | 0:5b88d5760320 | 116 | virtual void set_entry_peer_sign_counter( |
kenjiArai | 0:5b88d5760320 | 117 | entry_handle_t db_handle, |
kenjiArai | 0:5b88d5760320 | 118 | sign_count_t sign_counter |
kenjiArai | 0:5b88d5760320 | 119 | ); |
kenjiArai | 0:5b88d5760320 | 120 | |
kenjiArai | 0:5b88d5760320 | 121 | /* saving and loading from nvm */ |
kenjiArai | 0:5b88d5760320 | 122 | |
kenjiArai | 0:5b88d5760320 | 123 | virtual void restore(); |
kenjiArai | 0:5b88d5760320 | 124 | |
kenjiArai | 0:5b88d5760320 | 125 | virtual void sync(entry_handle_t db_handle); |
kenjiArai | 0:5b88d5760320 | 126 | |
kenjiArai | 0:5b88d5760320 | 127 | virtual void set_restore(bool reload); |
kenjiArai | 0:5b88d5760320 | 128 | |
kenjiArai | 0:5b88d5760320 | 129 | private: |
kenjiArai | 0:5b88d5760320 | 130 | virtual uint8_t get_entry_count(); |
kenjiArai | 0:5b88d5760320 | 131 | |
kenjiArai | 0:5b88d5760320 | 132 | virtual SecurityDistributionFlags_t* get_entry_handle_by_index(uint8_t index); |
kenjiArai | 0:5b88d5760320 | 133 | |
kenjiArai | 0:5b88d5760320 | 134 | virtual void reset_entry(entry_handle_t db_handle); |
kenjiArai | 0:5b88d5760320 | 135 | |
kenjiArai | 0:5b88d5760320 | 136 | virtual SecurityEntryIdentity_t* read_in_entry_peer_identity(entry_handle_t db_handle); |
kenjiArai | 0:5b88d5760320 | 137 | virtual SecurityEntryKeys_t* read_in_entry_peer_keys(entry_handle_t db_handle); |
kenjiArai | 0:5b88d5760320 | 138 | virtual SecurityEntryKeys_t* read_in_entry_local_keys(entry_handle_t db_handle); |
kenjiArai | 0:5b88d5760320 | 139 | virtual SecurityEntrySigning_t* read_in_entry_peer_signing(entry_handle_t db_handle); |
kenjiArai | 0:5b88d5760320 | 140 | |
kenjiArai | 0:5b88d5760320 | 141 | /** |
kenjiArai | 0:5b88d5760320 | 142 | * Zero the db file. |
kenjiArai | 0:5b88d5760320 | 143 | * @param db_file filehandle for file to erase |
kenjiArai | 0:5b88d5760320 | 144 | * @return filehandle when successful, otherwise NULL |
kenjiArai | 0:5b88d5760320 | 145 | */ |
kenjiArai | 0:5b88d5760320 | 146 | static FILE* erase_db_file(FILE* db_file); |
kenjiArai | 0:5b88d5760320 | 147 | |
kenjiArai | 0:5b88d5760320 | 148 | private: |
kenjiArai | 0:5b88d5760320 | 149 | entry_t _entries[MAX_ENTRIES]; |
kenjiArai | 0:5b88d5760320 | 150 | FILE *_db_file; |
kenjiArai | 0:5b88d5760320 | 151 | uint8_t _buffer[sizeof(SecurityEntryKeys_t)]; |
kenjiArai | 0:5b88d5760320 | 152 | }; |
kenjiArai | 0:5b88d5760320 | 153 | |
kenjiArai | 0:5b88d5760320 | 154 | } /* namespace pal */ |
kenjiArai | 0:5b88d5760320 | 155 | } /* namespace ble */ |
kenjiArai | 0:5b88d5760320 | 156 | |
kenjiArai | 0:5b88d5760320 | 157 | #endif /*GENERIC_FILE_SECURITY_DB_H_*/ |