mbed Connector Interface simplification API on top of mbed-client
Fork of mbedConnectorInterfaceV3 by
NOTE:
This repo has been replaced with https://github.com/ARMmbed/mbedConnectorInterface. No further updates will occur with this repo. Please use the github repo instead. Thanks!
source/PassphraseAuthenticator.cpp@91:179b5cb420de, 2017-03-01 (annotated)
- Committer:
- ansond
- Date:
- Wed Mar 01 16:18:52 2017 +0000
- Revision:
- 91:179b5cb420de
- Parent:
- 38:bb6d2be4d54c
- Child:
- 92:dffefc450d6c
updated maximum CoAP packet size handling to 1024; added additional memory bounds checking
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 | // 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 | 91:179b5cb420de | 87 | int length = coap_data_ptr_length; |
ansond | 91:179b5cb420de | 88 | if (length > MAX_VALUE_BUFFER_LENGTH) { |
ansond | 91:179b5cb420de | 89 | length = MAX_VALUE_BUFFER_LENGTH; |
ansond | 91:179b5cb420de | 90 | this->logger()->log("PassphraseAuthenticator::coapDataToString: WARNING clipped data: %d bytes to %d bytes. Increase MAX_VALUE_BUFFER_LENGTH", |
ansond | 91:179b5cb420de | 91 | coap_data_ptr_length,length); |
ansond | 91:179b5cb420de | 92 | } |
ansond | 91:179b5cb420de | 93 | memcpy(buf,(char *)coap_data_ptr,length); |
ansond | 13:9edad7677211 | 94 | return string(buf); |
ansond | 13:9edad7677211 | 95 | } |
ansond | 13:9edad7677211 | 96 | return string(""); |
ansond | 13:9edad7677211 | 97 | } |