Manufacturer certificate based ownership transfer support.
[platform/upstream/iotivity.git] / resource / csdk / security / provisioning / src / oxmmanufacturercert.c
1 /* *****************************************************************
2  *
3  * Copyright 2016 Samsung Electronics All Rights Reserved.
4  *
5  *
6  *
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
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  * *****************************************************************/
20
21 #include <memory.h>
22
23 #include "ocstack.h"
24 #include "securevirtualresourcetypes.h"
25 #include "doxmresource.h"
26 #include "credresource.h"
27 #include "cacommon.h"
28 #include "cainterface.h"
29 #include "casecurityinterface.h"
30 #include "ocrandom.h"
31 #include "oic_malloc.h"
32 #include "logger.h"
33 #include "pbkdf2.h"
34 #include "global.h"
35 #include "base64.h"
36 #include "oxmmanufacturercert.h"
37 #include "ownershiptransfermanager.h"
38 #include "srmresourcestrings.h"
39 #include "pkix_interface.h"
40 #include "mbedtls/ssl_ciphersuites.h"
41
42 #define TAG "OXM_MCertificate"
43
44 OCStackResult CreateMCertificateBasedSelectOxmPayload(OTMContext_t* otmCtx, uint8_t **payload, size_t *size)
45 {
46     if (!otmCtx || !otmCtx->selectedDeviceInfo || !payload || *payload || !size)
47     {
48         return OC_STACK_INVALID_PARAM;
49     }
50
51     otmCtx->selectedDeviceInfo->doxm->oxmSel = OIC_MANUFACTURER_CERTIFICATE;
52
53     return DoxmToCBORPayload(otmCtx->selectedDeviceInfo->doxm, payload, size, true);
54 }
55
56 OCStackResult CreateMCertificateBasedOwnerTransferPayload(OTMContext_t* otmCtx, uint8_t **payload, size_t *size)
57 {
58     if (!otmCtx || !otmCtx->selectedDeviceInfo || !payload || *payload || !size)
59     {
60         return OC_STACK_INVALID_PARAM;
61     }
62
63     OicUuid_t uuidPT = {.id={0}};
64     *payload = NULL;
65     *size = 0;
66
67     if (OC_STACK_OK != GetDoxmDeviceID(&uuidPT))
68     {
69         OIC_LOG(ERROR, TAG, "Error while retrieving provisioning tool's device ID");
70         return OC_STACK_ERROR;
71     }
72     memcpy(otmCtx->selectedDeviceInfo->doxm->owner.id, uuidPT.id , UUID_LENGTH);
73
74     return DoxmToCBORPayload(otmCtx->selectedDeviceInfo->doxm, payload, size, true);
75 }
76
77 OCStackResult PrepareMCertificateCallback(OTMContext_t *otmCtx)
78 {
79     OIC_LOG(INFO, TAG, "IN PrepareMCertificateCallback");
80
81     if (CA_STATUS_OK != CAregisterPkixInfoHandler(GetManufacturerPkixInfo))
82     {
83         OIC_LOG(ERROR, TAG, "Failed to register PkixInfohandler");
84         return OC_STACK_ERROR;
85     }
86
87     if (CA_STATUS_OK != CAregisterGetCredentialTypesHandler(InitManufacturerCipherSuiteList))
88     {
89         OIC_LOG(ERROR, TAG, "Failed to register CredentialTypesHandler");
90         return OC_STACK_ERROR;
91     }
92
93     OIC_LOG(INFO, TAG, "OUT PrepareMCertificateCallback");
94
95     return OC_STACK_OK;
96 }
97
98 OCStackResult CreateSecureSessionMCertificateCallback(OTMContext_t* otmCtx)
99 {
100     OIC_LOG(INFO, TAG, "IN CreateSecureSessionMCertificateCallback");
101
102     if (!otmCtx || !otmCtx->selectedDeviceInfo)
103     {
104         return OC_STACK_INVALID_PARAM;
105     }
106
107     CAResult_t caresult = CAEnableAnonECDHCipherSuite(false);
108     if (CA_STATUS_OK != caresult)
109     {
110         OIC_LOG_V(ERROR, TAG, "Failed to disable anon cipher suite");
111         return OC_STACK_ERROR;
112     }
113     OIC_LOG(INFO, TAG, "Anonymous cipher suite disabled.");
114
115     caresult  = CASelectCipherSuite(MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8,
116                                     otmCtx->selectedDeviceInfo->endpoint.adapter);
117     if (CA_STATUS_OK != caresult)
118     {
119         OIC_LOG_V(ERROR, TAG, "Failed to select TLS_TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8");
120         return OC_STACK_ERROR;
121     }
122     OIC_LOG(INFO, TAG, "TLS_TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8 cipher suite selected.");
123
124     OCProvisionDev_t* selDevInfo = otmCtx->selectedDeviceInfo;
125     CAEndpoint_t *endpoint = (CAEndpoint_t *)OICCalloc(1, sizeof (CAEndpoint_t));
126     if (NULL == endpoint)
127     {
128         return OC_STACK_NO_MEMORY;
129     }
130     memcpy(endpoint,&selDevInfo->endpoint,sizeof(CAEndpoint_t));
131     endpoint->port = selDevInfo->securePort;
132     caresult = CAInitiateHandshake(endpoint);
133     OICFree(endpoint);
134     if (CA_STATUS_OK != caresult)
135     {
136         OIC_LOG_V(ERROR, TAG, "DTLS handshake failure.");
137         return OC_STACK_ERROR;
138     }
139
140     OIC_LOG(INFO, TAG, "OUT CreateSecureSessionMCertificateCallback");
141
142     return OC_STACK_OK;
143 }