fork to force BLE id to have prefix that is assumed by the app

Dependencies:   BLE_API Crypto RNG mbed nRF51822

Fork of Garagem by Diogo Gomes

Committer:
dgomes
Date:
Tue Aug 25 23:26:43 2015 +0000
Revision:
10:80850cd6c29e
Parent:
9:329af8cdc923
Child:
12:eaee29bfa1c7
Version 1; Moving to SecurityService

Who changed what in which revision?

UserRevisionLine numberNew contents of line
dgomes 9:329af8cdc923 1 #ifndef __BLE_GARAGEM_SERVICE_H__
dgomes 9:329af8cdc923 2 #define __BLE_GARAGEM_SERVICE_H__
dgomes 9:329af8cdc923 3 #include "History.h"
dgomes 9:329af8cdc923 4
dgomes 9:329af8cdc923 5 #define SHARED_SECRET "ABRE-TE"
dgomes 9:329af8cdc923 6
dgomes 9:329af8cdc923 7 #define GARAGEM_OK 0
dgomes 9:329af8cdc923 8 #define GARAGEM_ERROR_REPETITION_ATTACK 1
dgomes 9:329af8cdc923 9 #define GARAGEM_ERROR_WRONG_SHARED_SECRET 2
dgomes 9:329af8cdc923 10
dgomes 9:329af8cdc923 11 class GaragemService {
dgomes 9:329af8cdc923 12 public:
dgomes 9:329af8cdc923 13 const static uint16_t GARAGEM_SERVICE_UUID = 0x2000;
dgomes 9:329af8cdc923 14 const static uint16_t GARAGEM_CHALLENGE_CHARACTERISTIC_UUID = 0x2001;
dgomes 9:329af8cdc923 15 const static uint16_t GARAGEM_LAST_OPEN_TS_UUID = 0x2002;
dgomes 9:329af8cdc923 16 const static uint16_t GARAGEM_LAST_OPEN_ID_UUID = 0x2003;
dgomes 9:329af8cdc923 17
dgomes 9:329af8cdc923 18 GaragemService(BLE &_ble) :
dgomes 9:329af8cdc923 19 ble(_ble),
dgomes 9:329af8cdc923 20 GaragemChallenge(GARAGEM_CHALLENGE_CHARACTERISTIC_UUID, (uint8_t *)"INIT"),
dgomes 10:80850cd6c29e 21 GaragemLastOpenTS(GARAGEM_LAST_OPEN_TS_UUID, 0, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY),
dgomes 10:80850cd6c29e 22 GaragemLastOpenID(GARAGEM_LAST_OPEN_ID_UUID, (uint8_t *)"INIT", GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY)
dgomes 9:329af8cdc923 23 {
dgomes 9:329af8cdc923 24 GattCharacteristic *charTable[] = {&GaragemChallenge, &GaragemLastOpenTS, &GaragemLastOpenID};
dgomes 9:329af8cdc923 25 GattService GaragemService(GARAGEM_SERVICE_UUID, charTable, sizeof(charTable) / sizeof(GattCharacteristic *));
dgomes 9:329af8cdc923 26 ble.gattServer().addService(GaragemService);
dgomes 9:329af8cdc923 27 }
dgomes 9:329af8cdc923 28
dgomes 9:329af8cdc923 29 GattAttribute::Handle_t getChallengeHandle() const {
dgomes 9:329af8cdc923 30 return GaragemChallenge.getValueHandle();
dgomes 9:329af8cdc923 31 }
dgomes 10:80850cd6c29e 32 GattAttribute::Handle_t getLastOpenTSHandle() const {
dgomes 10:80850cd6c29e 33 return GaragemLastOpenTS.getValueHandle();
dgomes 10:80850cd6c29e 34 }
dgomes 10:80850cd6c29e 35 void nextLastOpen(const uint8_t *data, uint16_t len) {
dgomes 10:80850cd6c29e 36 uint64_t token = history.getToken();
dgomes 10:80850cd6c29e 37 uint8_t *tok = (uint8_t *) &token;
dgomes 10:80850cd6c29e 38 DBG("LAST:\tTS=%lu\tID=%c%c%c%c\r\n", *((uint32_t *) tok), tok[4], tok[5], tok[6], tok[7]);
dgomes 10:80850cd6c29e 39 ble.gattServer().write(GaragemLastOpenTS.getValueHandle(), (uint8_t *)&tok[0], 4*sizeof(uint8_t));
dgomes 10:80850cd6c29e 40 ble.gattServer().write(GaragemLastOpenID.getValueHandle(), (uint8_t *)&tok[4], 4*sizeof(uint8_t));
dgomes 10:80850cd6c29e 41
dgomes 10:80850cd6c29e 42 }
dgomes 10:80850cd6c29e 43
dgomes 9:329af8cdc923 44
dgomes 9:329af8cdc923 45 int checkMessage(uint8_t *msg) {
dgomes 9:329af8cdc923 46 DBG("WHAT ? %s\r\n", (char *) msg);
dgomes 9:329af8cdc923 47
dgomes 9:329af8cdc923 48 uint64_t token;
dgomes 9:329af8cdc923 49 memcpy(&token, msg, 8);
dgomes 10:80850cd6c29e 50
dgomes 9:329af8cdc923 51 DBG("TS=%lu\tID=%c%c%c%c\tSYSKEY=%s\r\n", *((uint32_t *) msg), msg[4], msg[5], msg[6], msg[7], &msg[8]);
dgomes 9:329af8cdc923 52
dgomes 9:329af8cdc923 53 //check we are not a victim of a repetion attack
dgomes 10:80850cd6c29e 54 DBG("%lu\t>=\t%lu\r\n", history.last_ts(), *((uint32_t*) &msg[0]));
dgomes 10:80850cd6c29e 55 if(history.last_ts() >= *((uint32_t *) &msg[0])) {
dgomes 9:329af8cdc923 56 DBG("HA HA repetion here...\r\n");
dgomes 9:329af8cdc923 57 return GARAGEM_ERROR_REPETITION_ATTACK;
dgomes 9:329af8cdc923 58 }
dgomes 9:329af8cdc923 59
dgomes 10:80850cd6c29e 60 if (strncmp((const char *) &msg[8],SHARED_SECRET,7)==0) {//TODO MOVE TO 8 CHARS
dgomes 9:329af8cdc923 61 //Save our success
dgomes 9:329af8cdc923 62 history.save(token);
dgomes 9:329af8cdc923 63 ble.gattServer().write(GaragemLastOpenTS.getValueHandle(), (const uint8_t *)&msg[0], 4*sizeof(uint8_t));
dgomes 9:329af8cdc923 64 ble.gattServer().write(GaragemLastOpenID.getValueHandle(), (const uint8_t *)&msg[4], 4*sizeof(uint8_t));
dgomes 9:329af8cdc923 65
dgomes 9:329af8cdc923 66 return GARAGEM_OK;
dgomes 9:329af8cdc923 67 } else {
dgomes 9:329af8cdc923 68 return GARAGEM_ERROR_WRONG_SHARED_SECRET;
dgomes 9:329af8cdc923 69 }
dgomes 9:329af8cdc923 70 }
dgomes 9:329af8cdc923 71
dgomes 9:329af8cdc923 72 private:
dgomes 9:329af8cdc923 73 BLE &ble;
dgomes 9:329af8cdc923 74 WriteOnlyArrayGattCharacteristic<uint8_t, 16> GaragemChallenge;
dgomes 9:329af8cdc923 75 ReadOnlyGattCharacteristic<uint32_t> GaragemLastOpenTS;
dgomes 9:329af8cdc923 76 ReadOnlyArrayGattCharacteristic<uint8_t, 4> GaragemLastOpenID;
dgomes 9:329af8cdc923 77
dgomes 9:329af8cdc923 78 History<16> history;
dgomes 9:329af8cdc923 79 };
dgomes 9:329af8cdc923 80
dgomes 9:329af8cdc923 81 #endif /* #ifndef __BLE_GARAGEM_SERVICE_H__ */