+
+#if defined (__WITH_TLS__) || defined(__WITH_DTLS__)
+void GetDerCaCert(ByteArray_t * crt, const char * usage)
+{
+ OIC_LOG_V(DEBUG, TAG, "In %s", __func__);
+ if (NULL == crt || NULL == usage)
+ {
+ OIC_LOG_V(DEBUG, TAG, "Out %s", __func__);
+ return;
+ }
+ crt->len = 0;
+ OicSecCred_t* temp = NULL;
+
+ LL_FOREACH(gCred, temp)
+ {
+ if ((SIGNED_ASYMMETRIC_KEY == temp->credType) &&
+ (0 == strcmp(temp->credUsage, usage)) && (false == temp->optionalData.revstat))
+ {
+ if(OIC_ENCODING_BASE64 == temp->optionalData.encoding)
+ {
+ size_t bufSize = B64DECODE_OUT_SAFESIZE((temp->optionalData.len + 1));
+ uint8_t * buf = OICCalloc(1, bufSize);
+ if(NULL == buf)
+ {
+ OIC_LOG(ERROR, TAG, "Failed to allocate memory");
+ return;
+ }
+ uint32_t outSize;
+ if(B64_OK != b64Decode((char*)(temp->optionalData.data),
+ temp->optionalData.len, buf, bufSize, &outSize))
+ {
+ OICFree(buf);
+ OIC_LOG(ERROR, TAG, "Failed to decode base64 data");
+ return;
+ }
+ crt->data = OICRealloc(crt->data, crt->len + outSize);
+ memcpy(crt->data + crt->len, buf, outSize);
+ crt->len += outSize;
+ OICFree(buf);
+ }
+ else
+ {
+ crt->data = OICRealloc(crt->data, crt->len + temp->optionalData.len);
+ memcpy(crt->data + crt->len, temp->optionalData.data, temp->optionalData.len);
+ crt->len += temp->optionalData.len;
+ }
+ OIC_LOG_V(DEBUG, TAG, "%s found", usage);
+ }
+ }
+ if(0 == crt->len)
+ {
+ OIC_LOG_V(WARNING, TAG, "%s not found", usage);
+ }
+ OIC_LOG_V(DEBUG, TAG, "Out %s", __func__);
+ return;
+}
+
+void GetDerOwnCert(ByteArray_t * crt, const char * usage)
+{
+ OIC_LOG_V(DEBUG, TAG, "In %s", __func__);
+ if (NULL == crt || NULL == usage)
+ {
+ OIC_LOG_V(DEBUG, TAG, "Out %s", __func__);
+ return;
+ }
+ crt->len = 0;
+ OicSecCred_t * temp = NULL;
+ LL_FOREACH(gCred, temp)
+ {
+ if (SIGNED_ASYMMETRIC_KEY == temp->credType &&
+ 0 == strcmp(temp->credUsage, usage))
+ {
+ crt->data = OICRealloc(crt->data, crt->len + temp->publicData.len);
+ memcpy(crt->data + crt->len, temp->publicData.data, temp->publicData.len);
+ crt->len += temp->publicData.len;
+ OIC_LOG_V(DEBUG, TAG, "%s found", usage);
+ }
+ }
+ if(0 == crt->len)
+ {
+ OIC_LOG_V(WARNING, TAG, "%s not found", usage);
+ }
+ OIC_LOG_V(DEBUG, TAG, "Out %s", __func__);
+ return;
+}
+
+void GetDerKey(ByteArray_t * key, const char * usage)
+{
+ OIC_LOG_V(DEBUG, TAG, "In %s", __func__);
+ if (NULL == key || NULL == usage)
+ {
+ OIC_LOG_V(DEBUG, TAG, "Out %s", __func__);
+ return;
+ }
+
+ OicSecCred_t * temp = NULL;
+ key->len = 0;
+ LL_FOREACH(gCred, temp)
+ {
+ if (SIGNED_ASYMMETRIC_KEY == temp->credType &&
+ 0 == strcmp(temp->credUsage, usage))
+ {
+ key->data = OICRealloc(key->data, key->len + temp->privateData.len);
+ memcpy(key->data + key->len, temp->privateData.data, temp->privateData.len);
+ key->len += temp->privateData.len;
+ OIC_LOG_V(DEBUG, TAG, "Key for %s found", usage);
+ }
+ }
+ if(0 == key->len)
+ {
+ OIC_LOG_V(WARNING, TAG, "Key for %s not found", usage);
+ }
+ OIC_LOG_V(DEBUG, TAG, "Out %s", __func__);
+}
+
+void InitCipherSuiteListInternal(bool * list, const char * usage)
+{
+ OIC_LOG_V(DEBUG, TAG, "In %s", __func__);
+ if (NULL == list || NULL == usage)
+ {
+ OIC_LOG(DEBUG, TAG, "NULL passed");
+ OIC_LOG_V(DEBUG, TAG, "Out %s", __func__);
+ return;
+ }
+ OicSecCred_t * temp = NULL;
+ LL_FOREACH(gCred, temp)
+ {
+ switch (temp->credType)
+ {
+ case PIN_PASSWORD:
+ {
+ list[0] = true;
+ OIC_LOG(DEBUG, TAG, "PIN_PASSWORD found");
+ break;
+ }
+ case SYMMETRIC_PAIR_WISE_KEY:
+ {
+ list[0] = true;
+ OIC_LOG(DEBUG, TAG, "SYMMETRIC_PAIR_WISE_KEY found");
+ break;
+ }
+ case SIGNED_ASYMMETRIC_KEY:
+ {
+ if (0 == strcmp(temp->credUsage, usage))
+ {
+ list[1] = true;
+ OIC_LOG_V(DEBUG, TAG, "SIGNED_ASYMMETRIC_KEY found for %s", usage);
+ }
+ break;
+ }
+ case SYMMETRIC_GROUP_KEY:
+ case ASYMMETRIC_KEY:
+ case ASYMMETRIC_ENCRYPTION_KEY:
+ {
+ OIC_LOG(WARNING, TAG, "Unsupported credential type for TLS.");
+ break;
+ }
+ default:
+ {
+ OIC_LOG(WARNING, TAG, "Unknown credential type for TLS.");
+ break;
+ }
+ }
+ }
+ OIC_LOG_V(DEBUG, TAG, "Out %s", __func__);
+}
+#endif
+
+
+//Added as workaround by Chul Lee
+#if defined(__WITH_DTLS__) || defined(__WITH_TLS__)
+OCStackResult CredSaveTrustCertChain(const OicUuid_t* subject, uint8_t *trustCertChain, size_t chainSize,
+ OicEncodingType_t encodingType, const char* usage, uint16_t *credId)
+{
+ OIC_LOG_V(DEBUG, TAG, "IN %s", __func__);
+
+ if(NULL == trustCertChain || NULL == credId || NULL == usage || NULL == subject)
+ {
+ OIC_LOG_V(ERROR, TAG, "Invaild param");
+ return OC_STACK_INVALID_PARAM;
+ }
+
+ OCStackResult res = OC_STACK_ERROR;
+ OicSecCred_t *cred = (OicSecCred_t*)OICCalloc(1, sizeof(OicSecCred_t));
+ if(NULL == cred)
+ {
+ OIC_LOG_V(ERROR, TAG, "Failed to allocate memory");
+ res = OC_STACK_NO_MEMORY;
+ goto error;
+ }
+ cred->credId = 0;
+ memcpy(cred->subject.id, subject->id, sizeof(cred->subject.id));
+
+ cred->credUsage = OICStrdup(usage);
+ if (NULL == cred->credUsage)
+ {
+ OIC_LOG_V(ERROR, TAG, "Failed to allocate memory");
+ res = OC_STACK_NO_MEMORY;
+ goto error;
+ }
+
+ cred->credType = SIGNED_ASYMMETRIC_KEY;
+
+ if (encodingType == OIC_ENCODING_PEM)
+ {
+ cred->optionalData.data = (uint8_t *)OICCalloc(1, chainSize + 1);
+ if(NULL == cred->optionalData.data)
+ {
+ OIC_LOG_V(ERROR, TAG, "Failed to allocate memory");
+ res = OC_STACK_NO_MEMORY;
+ goto error;
+ }
+ cred->optionalData.len = chainSize + 1;
+ }
+ else
+ {
+ cred->optionalData.data = (uint8_t *)OICCalloc(1, chainSize);
+ if(NULL == cred->optionalData.data)
+ {
+ OIC_LOG_V(ERROR, TAG, "Failed to allocate memory");
+ res = OC_STACK_NO_MEMORY;
+ goto error;
+ }
+ cred->optionalData.len = chainSize;
+ }
+ memcpy(cred->optionalData.data, trustCertChain, chainSize);
+ cred->optionalData.encoding = encodingType;
+ cred->optionalData.revstat = false;
+
+ res = AddCredential(cred);
+ if(res != OC_STACK_OK)
+ {
+ goto error;
+ }
+ *credId = cred->credId;
+
+ OIC_LOG_V(DEBUG, TAG, "OUT %s", __func__);
+
+ return res;
+
+error:
+ DeleteCredList(cred);
+ OIC_LOG_V(ERROR, TAG, "OUT %s : error = %d", __func__, (int)res);
+ return res;
+}
+
+OCStackResult CredSaveOwnCert(const OicUuid_t* subject, OicSecKey_t * cert, OicSecKey_t * key,
+ const char* usage, uint16_t *credId)
+{
+ OIC_LOG_V(DEBUG, TAG, "IN %s", __func__);
+
+ if(NULL == cert || NULL == cert->data || NULL == key || NULL == key->data ||
+ NULL == credId || NULL == usage || NULL == subject)
+ {
+ OIC_LOG_V(ERROR, TAG, "Invalid param");
+ return OC_STACK_INVALID_PARAM;
+ }
+
+ OCStackResult res = OC_STACK_ERROR;
+ OicSecCred_t *cred = (OicSecCred_t *)OICCalloc(1, sizeof(OicSecCred_t));
+ if(NULL == cred)
+ {
+ OIC_LOG_V(ERROR, TAG, "Failed to allocate memory");
+ res = OC_STACK_NO_MEMORY;
+ goto error;
+ }
+ cred->credId = 0;
+ memcpy(cred->subject.id, subject->id, sizeof(cred->subject.id));
+
+ cred->credUsage = OICStrdup(usage);
+ if (NULL == cred->credUsage)
+ {
+ OIC_LOG_V(ERROR, TAG, "Failed to allocate memory");
+ res = OC_STACK_NO_MEMORY;
+ goto error;
+ }
+
+ cred->credType = SIGNED_ASYMMETRIC_KEY;
+
+ OicSecKey_t *publicData = &cred->publicData;
+ publicData->data = (uint8_t *)OICCalloc(1, cert->len);
+ if(NULL == publicData->data)
+ {
+ OIC_LOG_V(ERROR, TAG, "Failed to allocate memory");
+ res = OC_STACK_NO_MEMORY;
+ goto error;
+ }
+ memcpy(publicData->data, cert->data, cert->len);
+ publicData->encoding = cert->encoding;
+ publicData->len = cert->len;
+
+ OicSecKey_t *privateData = &cred->privateData;
+ privateData->data = (uint8_t *)OICCalloc(1, key->len);
+ if(NULL == privateData->data)
+ {
+ OIC_LOG_V(ERROR, TAG, "Failed to allocate memory");
+ res = OC_STACK_NO_MEMORY;
+ goto error;
+ }
+ memcpy(privateData->data, key->data, key->len);
+ privateData->len = key->len;
+ privateData->encoding = key->encoding;
+
+ res = AddCredential(cred);
+ if(res != OC_STACK_OK)
+ {
+ goto error;
+ }
+ *credId = cred->credId;
+
+ OIC_LOG_V(DEBUG, TAG, "OUT %s", __func__);
+
+ return res;
+
+error:
+ DeleteCredList(cred);
+ OIC_LOG_V(ERROR, TAG, "OUT %s : error = %d", __func__, (int)res);
+ return res;
+}
+
+#endif // defined(__WITH_DTLS__) || defined(__WITH_TLS__)