use TCP to connect to mbed connector
Fork of mbedConnectorInterfaceWithDM by
source/PassphraseAuthenticator.cpp@13:9edad7677211, 2016-06-08 (annotated)
- Committer:
- ansond
- Date:
- Wed Jun 08 22:32:08 2016 +0000
- Revision:
- 13:9edad7677211
- Child:
- 24:c92984bede9c
updated to latest revision with new DM functions
Who changed what in which revision?
User | Revision | Line number | New 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 | #if defined (HAS_EXECUTE_PARAMS) |
ansond | 13:9edad7677211 | 44 | // use simple, trivial passphrase based comparison as the check... |
ansond | 13:9edad7677211 | 45 | char *passphrase = (char *)this->m_secret; |
ansond | 13:9edad7677211 | 46 | char *input_passphrase = NULL; |
ansond | 13:9edad7677211 | 47 | |
ansond | 13:9edad7677211 | 48 | // unmarshall the challenge... its a 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 | 13:9edad7677211 | 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 | 13:9edad7677211 | 58 | |
ansond | 13:9edad7677211 | 59 | // DEBUG |
ansond | 13:9edad7677211 | 60 | //this->m_logger->log("Authenticator(passphrase): passphrase: [%s] challenge: [%s]",passphrase,input_passphrase); |
ansond | 13:9edad7677211 | 61 | |
ansond | 13:9edad7677211 | 62 | // parameter checks...the compare passphrases and return the result |
ansond | 13:9edad7677211 | 63 | if (passphrase != NULL && input_passphrase != NULL && strcmp(passphrase,input_passphrase) == 0) { |
ansond | 13:9edad7677211 | 64 | // DEBUG |
ansond | 13:9edad7677211 | 65 | this->m_logger->log("Authenticator(passphrase): Passphrases MATCH. Authenticated."); |
ansond | 13:9edad7677211 | 66 | |
ansond | 13:9edad7677211 | 67 | // passphrases match |
ansond | 13:9edad7677211 | 68 | return true; |
ansond | 13:9edad7677211 | 69 | } |
ansond | 13:9edad7677211 | 70 | |
ansond | 13:9edad7677211 | 71 | // DEBUG |
ansond | 13:9edad7677211 | 72 | this->m_logger->log("Authenticator(passphrase): Passphrases do not match"); |
ansond | 13:9edad7677211 | 73 | |
ansond | 13:9edad7677211 | 74 | // authentication failure |
ansond | 13:9edad7677211 | 75 | return false; |
ansond | 13:9edad7677211 | 76 | #else |
ansond | 13:9edad7677211 | 77 | // DEBUG |
ansond | 13:9edad7677211 | 78 | this->m_logger->log("Authenticator(passphrase): OLD mbed-client detected. Authentication OK (passthru)"); |
ansond | 13:9edad7677211 | 79 | return true; |
ansond | 13:9edad7677211 | 80 | #endif |
ansond | 13:9edad7677211 | 81 | } |
ansond | 13:9edad7677211 | 82 | |
ansond | 13:9edad7677211 | 83 | // convenience method to get the URI from its buffer field... |
ansond | 13:9edad7677211 | 84 | string PassphraseAuthenticator::coapDataToString(uint8_t *coap_data_ptr,int coap_data_ptr_length) { |
ansond | 13:9edad7677211 | 85 | if (coap_data_ptr != NULL && coap_data_ptr_length > 0) { |
ansond | 13:9edad7677211 | 86 | char buf[MAX_VALUE_BUFFER_LENGTH+1]; |
ansond | 13:9edad7677211 | 87 | memset(buf,0,MAX_VALUE_BUFFER_LENGTH+1); |
ansond | 13:9edad7677211 | 88 | memcpy(buf,(char *)coap_data_ptr,coap_data_ptr_length); |
ansond | 13:9edad7677211 | 89 | return string(buf); |
ansond | 13:9edad7677211 | 90 | } |
ansond | 13:9edad7677211 | 91 | return string(""); |
ansond | 13:9edad7677211 | 92 | } |