1 /* *****************************************************************
3 * Copyright 2016 Samsung Electronics All Rights Reserved.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 * *****************************************************************/
24 #include "securevirtualresourcetypes.h"
25 #include "doxmresource.h"
26 #include "credresource.h"
28 #include "cainterface.h"
29 #include "casecurityinterface.h"
31 #include "oic_malloc.h"
36 #include "oxmmanufacturercert.h"
37 #include "ownershiptransfermanager.h"
38 #include "srmresourcestrings.h"
39 #include "pkix_interface.h"
40 #include "mbedtls/ssl_ciphersuites.h"
42 #define TAG "OXM_MCertificate"
44 OCStackResult CreateMCertificateBasedSelectOxmPayload(OTMContext_t* otmCtx, uint8_t **payload, size_t *size)
46 if (!otmCtx || !otmCtx->selectedDeviceInfo || !payload || *payload || !size)
48 return OC_STACK_INVALID_PARAM;
51 otmCtx->selectedDeviceInfo->doxm->oxmSel = OIC_MANUFACTURER_CERTIFICATE;
53 return DoxmToCBORPayload(otmCtx->selectedDeviceInfo->doxm, payload, size, true);
56 OCStackResult CreateMCertificateBasedOwnerTransferPayload(OTMContext_t* otmCtx, uint8_t **payload, size_t *size)
58 if (!otmCtx || !otmCtx->selectedDeviceInfo || !payload || *payload || !size)
60 return OC_STACK_INVALID_PARAM;
63 OicUuid_t uuidPT = {.id={0}};
67 if (OC_STACK_OK != GetDoxmDeviceID(&uuidPT))
69 OIC_LOG(ERROR, TAG, "Error while retrieving provisioning tool's device ID");
70 return OC_STACK_ERROR;
72 memcpy(otmCtx->selectedDeviceInfo->doxm->owner.id, uuidPT.id , UUID_LENGTH);
74 return DoxmToCBORPayload(otmCtx->selectedDeviceInfo->doxm, payload, size, true);
77 OCStackResult PrepareMCertificateCallback(OTMContext_t *otmCtx)
79 OIC_LOG(INFO, TAG, "IN PrepareMCertificateCallback");
81 if (CA_STATUS_OK != CAregisterPkixInfoHandler(GetManufacturerPkixInfo))
83 OIC_LOG(ERROR, TAG, "Failed to register PkixInfohandler");
84 return OC_STACK_ERROR;
87 if (CA_STATUS_OK != CAregisterGetCredentialTypesHandler(InitManufacturerCipherSuiteList))
89 OIC_LOG(ERROR, TAG, "Failed to register CredentialTypesHandler");
90 return OC_STACK_ERROR;
93 OIC_LOG(INFO, TAG, "OUT PrepareMCertificateCallback");
98 OCStackResult CreateSecureSessionMCertificateCallback(OTMContext_t* otmCtx)
100 OIC_LOG(INFO, TAG, "IN CreateSecureSessionMCertificateCallback");
102 if (!otmCtx || !otmCtx->selectedDeviceInfo)
104 return OC_STACK_INVALID_PARAM;
107 CAResult_t caresult = CAEnableAnonECDHCipherSuite(false);
108 if (CA_STATUS_OK != caresult)
110 OIC_LOG_V(ERROR, TAG, "Failed to disable anon cipher suite");
111 return OC_STACK_ERROR;
113 OIC_LOG(INFO, TAG, "Anonymous cipher suite disabled.");
115 caresult = CASelectCipherSuite(MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8,
116 otmCtx->selectedDeviceInfo->endpoint.adapter);
117 if (CA_STATUS_OK != caresult)
119 OIC_LOG_V(ERROR, TAG, "Failed to select TLS_TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8");
120 return OC_STACK_ERROR;
122 OIC_LOG(INFO, TAG, "TLS_TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8 cipher suite selected.");
124 OCProvisionDev_t* selDevInfo = otmCtx->selectedDeviceInfo;
125 CAEndpoint_t *endpoint = (CAEndpoint_t *)OICCalloc(1, sizeof (CAEndpoint_t));
126 if (NULL == endpoint)
128 return OC_STACK_NO_MEMORY;
130 memcpy(endpoint,&selDevInfo->endpoint,sizeof(CAEndpoint_t));
131 endpoint->port = selDevInfo->securePort;
132 caresult = CAInitiateHandshake(endpoint);
134 if (CA_STATUS_OK != caresult)
136 OIC_LOG_V(ERROR, TAG, "DTLS handshake failure.");
137 return OC_STACK_ERROR;
140 OIC_LOG(INFO, TAG, "OUT CreateSecureSessionMCertificateCallback");