<commit_before>/****************************************************************************
 *
 * Copyright 2017 Samsung Electronics All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 * either express or implied. See the License for the specific
 * language governing permissions and limitations under the License.
 *
 ****************************************************************************/

///
/// This sample provides the way to create cloud sample
#include <stdio.h>
#include <string.h>
#include <pthread.h>
#include <unistd.h>
#include <stdlib.h>
#include <signal.h>
#include "logging/things_logger.h"
#include "utils/things_malloc.h"
#include "things_common.h"
#include "securevirtualresourcetypes.h"
#include "octypes.h"
#include "srmresourcestrings.h"
#include "credresource.h"

#include "mbedtls/ssl_ciphersuites.h"
#include "mbedtls/see_api.h"
#include "mbedtls/x509_crt.h"
#include "mbedtls/pem.h"

#include "things_sss_manager.h"

#define TAG "THINGS_SSS"

static OicSecKey_t cacert;
static OicSecKey_t subca;
static OicSecKey_t devicecert;

static uint8_t g_certChain;

OCStackResult things_sss_rootca_handler_init(OicUuid_t* subjectUuid)
{
	OCStackResult res = OC_STACK_ERROR;
	uint16_t cred_id = 0;

	res = CredSaveTrustCertChain(subjectUuid, cacert.data, cacert.len, OIC_ENCODING_DER, MF_TRUST_CA, &cred_id);
	if (OC_STACK_OK != res) {
		THINGS_LOG_E(TAG, "SRPCredSaveTrustCertChain #2 error");
	    return res;
	}
	THINGS_LOG_D(TAG, "CA Cert.der for MF_TRUST_CA HW saved w/ cred ID=%d", cred_id);

	return res;
}

//this callback will be invoked to get key context based on key usage
void *OCGetHwKey(const char *service, const char *usage, const char *keytype)
{
	THINGS_LOG_D(TAG, "IN : %s", __func__);

	if (service == NULL || usage == NULL) {
		return NULL;
	}

	mbedtls_pk_context *pkey;

	pkey = (mbedtls_pk_context *)things_malloc(sizeof(mbedtls_pk_context));

	if (pkey == NULL) {
		THINGS_LOG_E(TAG, "pkey context alloc failed");
		return NULL;
	}
	THINGS_LOG_D(TAG, "Out: %s", __func__);
	return (void *)pkey;
}

//this callback will free key context that was retreived from TZ
int OCFreeHwKey(void* keyContext)
{
	THINGS_LOG_D(TAG, "IN : %s", __func__);

	if (keyContext == NULL) {
		return -1;
	}

	things_free(keyContext);

	THINGS_LOG_D(TAG, "Out: %s", __func__);

	return 0;
}

//this callback will be invoked to load own certificate in case of TZ
int OCGetOwnCertFromHw(const void* keyContext, uint8_t** certChain, size_t* certChainLen)
{
	(void *)keyContext;
	
	THINGS_LOG_D(TAG, "IN : %s", __func__);

	if (certChain == NULL || certChainLen == NULL) {
		return -1;
	}

	/* If input buffer's address is same with previous allocated memory,
       it will return the success value and iotivity stack use previous memory. */
	if (*certChain && *certChain == g_certChain) {
		return 0;
	}

	*certChainLen = devicecert.len + subca.len + cacert.len;
	*certChain = (uint8_t *)things_malloc(*certChainLen);

	memcpy(*certChain, devicecert.data, devicecert.len);
	memcpy(*certChain + devicecert.len, subca.data, subca.len);
	memcpy(*certChain + devicecert.len + subca.len, cacert.data, cacert.len);

	g_certChain = *certChain;

	THINGS_LOG_D(TAG, "Out: %s", __func__);
	return 0;
}

//this callback will be invoked to load private key in case of TZ
int OCSetupPkContextFromHw(mbedtls_pk_context* ctx, void* keyContext)
{
	THINGS_LOG_D(TAG, "IN : %s", __func__);

	if (ctx == NULL || keyContext == NULL) {
		return -1;
	}

	mbedtls_pk_context* pkey = keyContext;

	mbedtls_pk_init(pkey);
	const mbedtls_pk_info_t *pk_info;

	if ((pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_ECKEY)) == NULL) {
		mbedtls_pk_free(pkey);
		things_free(pkey);
		return -1;
	}

	if (mbedtls_pk_setup(pkey, pk_info)) {
		mbedtls_pk_free(pkey);
		things_free(pkey);
		return -1;
	}

	mbedtls_ecdsa_init((mbedtls_ecdsa_context*)pkey->pk_ctx);
	((mbedtls_ecdsa_context *)(pkey->pk_ctx))->key_index = FACTORYKEY_ARTIK_DEVICE;
	((mbedtls_ecdsa_context *)(pkey->pk_ctx))->grp.id = MBEDTLS_ECP_DP_SECP256R1;

	ctx->pk_info = pkey->pk_info;
	ctx->pk_ctx = pkey->pk_ctx;

	THINGS_LOG_D(TAG, "Out: %s", __func__);

	return 0;
}

static int things_set_cert_chains(void)
{
	int ret = -1;
	uint8_t *buf = NULL;
	uint32_t buflen = SEE_BUF_MAX_SIZE;

	buf = (uint8_t *)things_malloc(buflen);
	if (buf == NULL) {
		THINGS_LOG_E(TAG, "things_set_cert_chains() : Memory is full");
		return -1;
	}

	if ((ret = see_get_certificate(buf, &buflen, FACTORYKEY_ARTIK_CERT, 0)) != 0) {
		THINGS_LOG_E(TAG, "things_set_cert_chains() : see_get_certificate fail %d", ret);
		things_free(buf);
		return ret;
	}

	int cnt = 0, i = 0;
	uint8_t *ptr = buf;
	// Artik Certificate Order : rootCA -> subCA -> deviceCA
	for (i = 0; i < buflen - 2; i++) {
		if (buf[i] == 0x30 && buf[i + 1] == 0x82 && buf[i + 2] == 0x02) {<commit_msg>Apply the coding rule in things_sss_manager.c Each variable shall be declared in the new line.<commit_after>/****************************************************************************
 *
 * Copyright 2017 Samsung Electronics All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 * either express or implied. See the License for the specific
 * language governing permissions and limitations under the License.
 *
 ****************************************************************************/

///
/// This sample provides the way to create cloud sample
#include <stdio.h>
#include <string.h>
#include <pthread.h>
#include <unistd.h>
#include <stdlib.h>
#include <signal.h>
#include "logging/things_logger.h"
#include "utils/things_malloc.h"
#include "things_common.h"
#include "securevirtualresourcetypes.h"
#include "octypes.h"
#include "srmresourcestrings.h"
#include "credresource.h"

#include "mbedtls/ssl_ciphersuites.h"
#include "mbedtls/see_api.h"
#include "mbedtls/x509_crt.h"
#include "mbedtls/pem.h"

#include "things_sss_manager.h"

#define TAG "THINGS_SSS"

static OicSecKey_t cacert;
static OicSecKey_t subca;
static OicSecKey_t devicecert;

static uint8_t g_certChain;

OCStackResult things_sss_rootca_handler_init(OicUuid_t* subjectUuid)
{
	OCStackResult res = OC_STACK_ERROR;
	uint16_t cred_id = 0;

	res = CredSaveTrustCertChain(subjectUuid, cacert.data, cacert.len, OIC_ENCODING_DER, MF_TRUST_CA, &cred_id);
	if (OC_STACK_OK != res) {
		THINGS_LOG_E(TAG, "SRPCredSaveTrustCertChain #2 error");
	    return res;
	}
	THINGS_LOG_D(TAG, "CA Cert.der for MF_TRUST_CA HW saved w/ cred ID=%d", cred_id);

	return res;
}

//this callback will be invoked to get key context based on key usage
void *OCGetHwKey(const char *service, const char *usage, const char *keytype)
{
	THINGS_LOG_D(TAG, "IN : %s", __func__);

	if (service == NULL || usage == NULL) {
		return NULL;
	}

	mbedtls_pk_context *pkey;

	pkey = (mbedtls_pk_context *)things_malloc(sizeof(mbedtls_pk_context));

	if (pkey == NULL) {
		THINGS_LOG_E(TAG, "pkey context alloc failed");
		return NULL;
	}
	THINGS_LOG_D(TAG, "Out: %s", __func__);
	return (void *)pkey;
}

//this callback will free key context that was retreived from TZ
int OCFreeHwKey(void* keyContext)
{
	THINGS_LOG_D(TAG, "IN : %s", __func__);

	if (keyContext == NULL) {
		return -1;
	}

	things_free(keyContext);

	THINGS_LOG_D(TAG, "Out: %s", __func__);

	return 0;
}

//this callback will be invoked to load own certificate in case of TZ
int OCGetOwnCertFromHw(const void* keyContext, uint8_t** certChain, size_t* certChainLen)
{
	(void *)keyContext;
	
	THINGS_LOG_D(TAG, "IN : %s", __func__);

	if (certChain == NULL || certChainLen == NULL) {
		return -1;
	}

	/* If input buffer's address is same with previous allocated memory,
       it will return the success value and iotivity stack use previous memory. */
	if (*certChain && *certChain == g_certChain) {
		return 0;
	}

	*certChainLen = devicecert.len + subca.len + cacert.len;
	*certChain = (uint8_t *)things_malloc(*certChainLen);

	memcpy(*certChain, devicecert.data, devicecert.len);
	memcpy(*certChain + devicecert.len, subca.data, subca.len);
	memcpy(*certChain + devicecert.len + subca.len, cacert.data, cacert.len);

	g_certChain = *certChain;

	THINGS_LOG_D(TAG, "Out: %s", __func__);
	return 0;
}

//this callback will be invoked to load private key in case of TZ
int OCSetupPkContextFromHw(mbedtls_pk_context* ctx, void* keyContext)
{
	THINGS_LOG_D(TAG, "IN : %s", __func__);

	if (ctx == NULL || keyContext == NULL) {
		return -1;
	}

	mbedtls_pk_context* pkey = keyContext;

	mbedtls_pk_init(pkey);
	const mbedtls_pk_info_t *pk_info;

	if ((pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_ECKEY)) == NULL) {
		mbedtls_pk_free(pkey);
		things_free(pkey);
		return -1;
	}

	if (mbedtls_pk_setup(pkey, pk_info)) {
		mbedtls_pk_free(pkey);
		things_free(pkey);
		return -1;
	}

	mbedtls_ecdsa_init((mbedtls_ecdsa_context*)pkey->pk_ctx);
	((mbedtls_ecdsa_context *)(pkey->pk_ctx))->key_index = FACTORYKEY_ARTIK_DEVICE;
	((mbedtls_ecdsa_context *)(pkey->pk_ctx))->grp.id = MBEDTLS_ECP_DP_SECP256R1;

	ctx->pk_info = pkey->pk_info;
	ctx->pk_ctx = pkey->pk_ctx;

	THINGS_LOG_D(TAG, "Out: %s", __func__);

	return 0;
}

static int things_set_cert_chains(void)
{
	int ret = -1;
	uint8_t *buf = NULL;
	uint32_t buflen = SEE_BUF_MAX_SIZE;

	buf = (uint8_t *)things_malloc(buflen);
	if (buf == NULL) {
		THINGS_LOG_E(TAG, "things_set_cert_chains() : Memory is full");
		return -1;
	}

	if ((ret = see_get_certificate(buf, &buflen, FACTORYKEY_ARTIK_CERT, 0)) != 0) {
		THINGS_LOG_E(TAG, "things_set_cert_chains() : see_get_certificate fail %d", ret);
		things_free(buf);
		return ret;
	}

	int cnt = 0;
	int i = 0;
	uint8_t *ptr = buf;
	// Artik Certificate Order : rootCA -> subCA -> deviceCA
	for (i = 0; i < buflen - 2; i++) {
		if (buf[i] == 0x30 && buf[i + 1] == 0x82 && buf[i + 2] == 0x02) {
{'fraction_non_alphanumeric': 0.1517346549752192, 'fraction_numerical': 0.010484178421654593, 'mean_word_length': 4.865287870318614, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1792889', 'n_tokens_mistral': 4167, 'n_tokens_neox': 3932, 'n_words': 1011}