Kenji Arai / TYBLE16_mbedlized_os5_several_examples_1st

Dependencies:   nRF51_Vdd TextLCD BME280

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers FileSecurityDb.h Source File

FileSecurityDb.h

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2018 ARM Limited
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #ifndef GENERIC_FILE_SECURITY_DB_H_
00018 #define GENERIC_FILE_SECURITY_DB_H_
00019 
00020 #include "SecurityDb.h"
00021 
00022 #include <stdio.h>
00023 
00024 namespace ble {
00025 namespace generic {
00026 
00027 /** Filesystem implementation */
00028 class FileSecurityDb : public SecurityDb {
00029 private:
00030 
00031     struct entry_t {
00032         SecurityDistributionFlags_t flags;
00033         sign_count_t peer_sign_counter;
00034         size_t file_offset;
00035     };
00036 
00037     static const size_t MAX_ENTRIES = 5;
00038 
00039     static entry_t* as_entry(entry_handle_t db_handle) {
00040         return reinterpret_cast<entry_t*>(db_handle);
00041     }
00042 
00043     template<class T>
00044     void db_read(T *value, long int offset) {
00045         fseek(_db_file, offset, SEEK_SET);
00046         fread(value, sizeof(T), 1, _db_file);
00047     }
00048 
00049     template<class T>
00050     void db_write(T *value, long int offset) {
00051         fseek(_db_file, offset, SEEK_SET);
00052         fwrite(value, sizeof(T), 1, _db_file);
00053     }
00054 
00055 public:
00056     FileSecurityDb(FILE *db_file);
00057     virtual ~FileSecurityDb();
00058 
00059     /**
00060      * Validates or creates a file for the security database.
00061      * @param db_path path to the file
00062      * @return FILE handle open and ready for use by the database or NULL if unavailable
00063      */
00064     static FILE* open_db_file(const char *db_path);
00065 
00066     virtual SecurityDistributionFlags_t* get_distribution_flags(
00067         entry_handle_t db_handle
00068     );
00069 
00070 
00071     /* local keys */
00072 
00073     /* set */
00074     virtual void set_entry_local_ltk(
00075         entry_handle_t db_handle,
00076         const ltk_t &ltk
00077     );
00078 
00079     virtual void set_entry_local_ediv_rand(
00080         entry_handle_t db_handle,
00081         const ediv_t &ediv,
00082         const rand_t &rand
00083     );
00084 
00085     /* peer's keys */
00086 
00087     /* set */
00088 
00089     virtual void set_entry_peer_ltk(
00090         entry_handle_t db_handle,
00091         const ltk_t &ltk
00092     );
00093 
00094     virtual void set_entry_peer_ediv_rand(
00095         entry_handle_t db_handle,
00096         const ediv_t &ediv,
00097         const rand_t &rand
00098     );
00099 
00100     virtual void set_entry_peer_irk(
00101         entry_handle_t db_handle,
00102         const irk_t &irk
00103     );
00104 
00105     virtual void set_entry_peer_bdaddr(
00106         entry_handle_t db_handle,
00107         bool address_is_public,
00108         const address_t &peer_address
00109     );
00110 
00111     virtual void set_entry_peer_csrk(
00112         entry_handle_t db_handle,
00113         const csrk_t &csrk
00114     );
00115 
00116     virtual void set_entry_peer_sign_counter(
00117         entry_handle_t db_handle,
00118         sign_count_t sign_counter
00119     );
00120 
00121     /* saving and loading from nvm */
00122 
00123     virtual void restore();
00124 
00125     virtual void sync(entry_handle_t db_handle);
00126 
00127     virtual void set_restore(bool reload);
00128 
00129 private:
00130     virtual uint8_t get_entry_count();
00131 
00132     virtual SecurityDistributionFlags_t* get_entry_handle_by_index(uint8_t index);
00133 
00134     virtual void reset_entry(entry_handle_t db_handle);
00135 
00136     virtual SecurityEntryIdentity_t* read_in_entry_peer_identity(entry_handle_t db_handle);
00137     virtual SecurityEntryKeys_t* read_in_entry_peer_keys(entry_handle_t db_handle);
00138     virtual SecurityEntryKeys_t* read_in_entry_local_keys(entry_handle_t db_handle);
00139     virtual SecurityEntrySigning_t* read_in_entry_peer_signing(entry_handle_t db_handle);
00140 
00141     /**
00142      * Zero the db file.
00143      * @param db_file filehandle for file to erase
00144      * @return filehandle when successful, otherwise NULL
00145      */
00146     static FILE* erase_db_file(FILE* db_file);
00147 
00148 private:
00149     entry_t _entries[MAX_ENTRIES];
00150     FILE *_db_file;
00151     uint8_t _buffer[sizeof(SecurityEntryKeys_t)];
00152 };
00153 
00154 } /* namespace pal */
00155 } /* namespace ble */
00156 
00157 #endif /*GENERIC_FILE_SECURITY_DB_H_*/