use TCP to connect to mbed connector

Fork of mbedConnectorInterfaceWithDM by Doug Anson

Committer:
Maggie17
Date:
Mon Nov 28 15:52:56 2016 +0000
Revision:
90:b738617379a6
Parent:
38:bb6d2be4d54c
first release

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ansond 13:9edad7677211 1 /**
ansond 13:9edad7677211 2 * @file PassphraseAuthenticator.cpp
ansond 13:9edad7677211 3 * @brief mbed CoAP Endpoint Device Management Passphrase-based Authenticator class
ansond 13:9edad7677211 4 * @author Doug Anson
ansond 13:9edad7677211 5 * @version 1.0
ansond 13:9edad7677211 6 * @see
ansond 13:9edad7677211 7 *
ansond 13:9edad7677211 8 * Copyright (c) 2016
ansond 13:9edad7677211 9 *
ansond 13:9edad7677211 10 * Licensed under the Apache License, Version 2.0 (the "License");
ansond 13:9edad7677211 11 * you may not use this file except in compliance with the License.
ansond 13:9edad7677211 12 * You may obtain a copy of the License at
ansond 13:9edad7677211 13 *
ansond 13:9edad7677211 14 * http://www.apache.org/licenses/LICENSE-2.0
ansond 13:9edad7677211 15 *
ansond 13:9edad7677211 16 * Unless required by applicable law or agreed to in writing, software
ansond 13:9edad7677211 17 * distributed under the License is distributed on an "AS IS" BASIS,
ansond 13:9edad7677211 18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
ansond 13:9edad7677211 19 * See the License for the specific language governing permissions and
ansond 13:9edad7677211 20 * limitations under the License.
ansond 13:9edad7677211 21 */
ansond 13:9edad7677211 22
ansond 13:9edad7677211 23 // Class support
ansond 13:9edad7677211 24 #include "mbed-connector-interface/PassphraseAuthenticator.h"
ansond 13:9edad7677211 25
ansond 13:9edad7677211 26 // demarshalling support
ansond 13:9edad7677211 27 #include "mbed-client/m2mresource.h"
ansond 13:9edad7677211 28
ansond 13:9edad7677211 29 // constructor
ansond 13:9edad7677211 30 PassphraseAuthenticator::PassphraseAuthenticator(const Logger *logger,const void *passphrase) : Authenticator(logger,passphrase) {
ansond 13:9edad7677211 31 }
ansond 13:9edad7677211 32
ansond 13:9edad7677211 33 // copy constructor
ansond 13:9edad7677211 34 PassphraseAuthenticator::PassphraseAuthenticator(const PassphraseAuthenticator &authenticator) : Authenticator(authenticator) {
ansond 13:9edad7677211 35 }
ansond 13:9edad7677211 36
ansond 13:9edad7677211 37 // destructor
ansond 13:9edad7677211 38 PassphraseAuthenticator::~PassphraseAuthenticator() {
ansond 13:9edad7677211 39 }
ansond 13:9edad7677211 40
ansond 13:9edad7677211 41 // basic (trivial passphrase authentication)
ansond 13:9edad7677211 42 bool PassphraseAuthenticator::authenticate(void *challenge) {
ansond 13:9edad7677211 43 // use simple, trivial passphrase based comparison as the check...
ansond 13:9edad7677211 44 char *passphrase = (char *)this->m_secret;
ansond 13:9edad7677211 45 char *input_passphrase = NULL;
ansond 13:9edad7677211 46
ansond 24:c92984bede9c 47 #if defined (HAS_EXECUTE_PARAMS)
ansond 24:c92984bede9c 48 // ExecParam mbed-client: un-marshall the ExecuteParameter to get the simple string...
ansond 13:9edad7677211 49 M2MResource::M2MExecuteParameter* param = (M2MResource::M2MExecuteParameter*)challenge;
ansond 13:9edad7677211 50 if (param != NULL) {
ansond 13:9edad7677211 51 // use parameters to extract the passphrase
ansond 13:9edad7677211 52 String object_name = param->get_argument_object_name();
ansond 38:bb6d2be4d54c 53 // int instance_id = (int)param->get_argument_object_instance_id();
ansond 13:9edad7677211 54 String resource_name = param->get_argument_resource_name();
ansond 13:9edad7677211 55 string value = this->coapDataToString(param->get_argument_value(),param->get_argument_value_length());
ansond 13:9edad7677211 56 input_passphrase = (char *)value.c_str();
ansond 13:9edad7677211 57 }
ansond 24:c92984bede9c 58 #else
ansond 24:c92984bede9c 59 // Non-ExecParam mbed-client: use the parameter directly...
ansond 24:c92984bede9c 60 input_passphrase = (char *)challenge;
ansond 24:c92984bede9c 61 #endif
ansond 13:9edad7677211 62
ansond 13:9edad7677211 63 // DEBUG
ansond 13:9edad7677211 64 //this->m_logger->log("Authenticator(passphrase): passphrase: [%s] challenge: [%s]",passphrase,input_passphrase);
ansond 13:9edad7677211 65
ansond 13:9edad7677211 66 // parameter checks...the compare passphrases and return the result
ansond 13:9edad7677211 67 if (passphrase != NULL && input_passphrase != NULL && strcmp(passphrase,input_passphrase) == 0) {
ansond 13:9edad7677211 68 // DEBUG
ansond 13:9edad7677211 69 this->m_logger->log("Authenticator(passphrase): Passphrases MATCH. Authenticated.");
ansond 13:9edad7677211 70
ansond 13:9edad7677211 71 // passphrases match
ansond 13:9edad7677211 72 return true;
ansond 13:9edad7677211 73 }
ansond 13:9edad7677211 74
ansond 13:9edad7677211 75 // DEBUG
ansond 13:9edad7677211 76 this->m_logger->log("Authenticator(passphrase): Passphrases do not match");
ansond 13:9edad7677211 77
ansond 13:9edad7677211 78 // authentication failure
ansond 13:9edad7677211 79 return false;
ansond 13:9edad7677211 80 }
ansond 13:9edad7677211 81
ansond 13:9edad7677211 82 // convenience method to get the URI from its buffer field...
ansond 13:9edad7677211 83 string PassphraseAuthenticator::coapDataToString(uint8_t *coap_data_ptr,int coap_data_ptr_length) {
ansond 13:9edad7677211 84 if (coap_data_ptr != NULL && coap_data_ptr_length > 0) {
ansond 13:9edad7677211 85 char buf[MAX_VALUE_BUFFER_LENGTH+1];
ansond 13:9edad7677211 86 memset(buf,0,MAX_VALUE_BUFFER_LENGTH+1);
ansond 13:9edad7677211 87 memcpy(buf,(char *)coap_data_ptr,coap_data_ptr_length);
ansond 13:9edad7677211 88 return string(buf);
ansond 13:9edad7677211 89 }
ansond 13:9edad7677211 90 return string("");
ansond 13:9edad7677211 91 }