已开启
HiSpark_AiFly开发板功能适配 #349
jia_kongfeng创建于 11 天前
HiSpark_AiFly开发板功能适配 #349
已开启
共 3 个文件变更+73-31
| @@ -17,6 +17,7 @@ | |||
| 17 | 17 | ||
| 18 | 18 | ||
| 19 | 19 | ||
| 20 | + | ||
| 20 | 21 | ||
| 21 | 22 | ||
| 22 | 23 | ||
| @@ -17,6 +17,7 @@ | |||
| 17 | 17 | ||
| 18 | 18 | ||
| 19 | 19 | ||
| 20 | + | ||
| 20 | 21 | ||
| 21 | 22 | ||
| 22 | 23 | ||
| @@ -547,7 +547,7 @@ EXIT: | |||
| 547 | APPV_FREE(profileData); | 547 | APPV_FREE(profileData); |
| 548 | return V_ERR; | 548 | return V_ERR; |
| 549 | } | 549 | } |
| 550 | -static unsigned char *GetRsaPk(const mbedtls_pk_context *pk, int32_t *len) | 550 | +static unsigned char *GetRsaPk(const mbedtls_pk_context *pk, size_t *len) |
| 551 | { | 551 | { |
| 552 | unsigned char *buf = APPV_MALLOC(MAX_PK_BUF); | 552 | unsigned char *buf = APPV_MALLOC(MAX_PK_BUF); |
| 553 | if (buf == NULL) { | 553 | if (buf == NULL) { |
| @@ -609,7 +609,7 @@ static unsigned char *GetEcPk(const mbedtls_pk_context *pk, int32_t *len) | |||
| 609 | return NULL; | 609 | return NULL; |
| 610 | } | 610 | } |
| 611 | ret = mbedtls_ecp_point_write_binary(&ecCtx->MBEDTLS_PRIVATE(grp), &ecCtx->MBEDTLS_PRIVATE(Q), | 611 | ret = mbedtls_ecp_point_write_binary(&ecCtx->MBEDTLS_PRIVATE(grp), &ecCtx->MBEDTLS_PRIVATE(Q), |
| 612 | - MBEDTLS_ECP_PF_UNCOMPRESSED, (size_t *)len, buf, MBEDTLS_ECP_MAX_PT_LEN); | 612 | + MBEDTLS_ECP_PF_UNCOMPRESSED, len, buf, MBEDTLS_ECP_MAX_PT_LEN); |
| 613 | if (ret != V_OK) { | 613 | if (ret != V_OK) { |
| 614 | LOG_ERROR("get ecc pk key error"); | 614 | LOG_ERROR("get ecc pk key error"); |
| 615 | (void)memset_s(buf, MBEDTLS_ECP_MAX_PT_LEN, 0, MBEDTLS_ECP_MAX_PT_LEN); | 615 | (void)memset_s(buf, MBEDTLS_ECP_MAX_PT_LEN, 0, MBEDTLS_ECP_MAX_PT_LEN); |
| @@ -640,7 +640,7 @@ static unsigned char *GetEcPk(const mbedtls_pk_context *pk, int32_t *len) | |||
| 640 | return pkBuf; | 640 | return pkBuf; |
| 641 | } | 641 | } |
| 642 | 642 | ||
| 643 | -static unsigned char *GetPkBuf(const mbedtls_pk_context *pk, int32_t *len) | 643 | +static unsigned char *GetPkBuf(const mbedtls_pk_context *pk, size_t *len) |
| 644 | { | 644 | { |
| 645 | unsigned char *bufA = NULL; | 645 | unsigned char *bufA = NULL; |
| 646 | if (mbedtls_pk_get_type(pk) == MBEDTLS_PK_RSA || mbedtls_pk_get_type(pk) == MBEDTLS_PK_RSASSA_PSS) { | 646 | if (mbedtls_pk_get_type(pk) == MBEDTLS_PK_RSA || mbedtls_pk_get_type(pk) == MBEDTLS_PK_RSASSA_PSS) { |
| @@ -663,7 +663,7 @@ static int32_t ParseCertGetPk(const char *certEncoded, AppSignPk *pk) | |||
| 663 | APPV_FREE(cert); | 663 | APPV_FREE(cert); |
| 664 | return V_ERR; | 664 | return V_ERR; |
| 665 | } | 665 | } |
| 666 | - int32_t len = 0; | 666 | + size_t len = 0; |
| 667 | unsigned char *pkBuf = GetPkBuf(&cert->pk, &len); | 667 | unsigned char *pkBuf = GetPkBuf(&cert->pk, &len); |
| 668 | if (pkBuf == NULL) { | 668 | if (pkBuf == NULL) { |
| 669 | LOG_ERROR("get pk error"); | 669 | LOG_ERROR("get pk error"); |
| @@ -672,7 +672,7 @@ static int32_t ParseCertGetPk(const char *certEncoded, AppSignPk *pk) | |||
| 672 | return V_ERR; | 672 | return V_ERR; |
| 673 | } | 673 | } |
| 674 | pk->pk = (char *)pkBuf; | 674 | pk->pk = (char *)pkBuf; |
| 675 | - pk->len = len; | 675 | + pk->len = (int32_t)len; |
| 676 | mbedtls_x509_crt_free(cert); | 676 | mbedtls_x509_crt_free(cert); |
| 677 | APPV_FREE(cert); | 677 | APPV_FREE(cert); |
| 678 | return V_OK; | 678 | return V_OK; |
| @@ -814,7 +814,7 @@ static int32_t CmpCert(const mbedtls_x509_crt *certA, const CertInfo *binSignCer | |||
| 814 | LOG_ERROR("pk type diff"); | 814 | LOG_ERROR("pk type diff"); |
| 815 | return V_ERR; | 815 | return V_ERR; |
| 816 | } | 816 | } |
| 817 | - int32_t lenA = 0; | 817 | + size_t lenA = 0; |
| 818 | unsigned char *bufA = GetPkBuf(&certA->pk, &lenA); | 818 | unsigned char *bufA = GetPkBuf(&certA->pk, &lenA); |
| 819 | P_NULL_RETURN_RET_WTTH_LOG(bufA, V_ERR); | 819 | P_NULL_RETURN_RET_WTTH_LOG(bufA, V_ERR); |
| 820 | 820 | ||
| @@ -952,58 +952,98 @@ void FreeCertInfo(CertInfo *certInfo) | |||
| 952 | return; | 952 | return; |
| 953 | } | 953 | } |
| 954 | 954 | ||
| 955 | -static int32_t GetCertInfo(const mbedtls_x509_crt *ctr, CertInfo **binSignCert) | 955 | +static int32_t InitCertInfo(CertInfo **certInfo) |
| 956 | { | 956 | { |
| 957 | - CertInfo *certInfo = APPV_MALLOC(sizeof(CertInfo)); | 957 | + *certInfo = APPV_MALLOC(sizeof(CertInfo)); |
| 958 | - P_NULL_RETURN_RET_WTTH_LOG(certInfo, V_ERR_MALLOC); | 958 | + P_NULL_RETURN_RET_WTTH_LOG(*certInfo, V_ERR_MALLOC); |
| 959 | 959 | ||
| 960 | - int32_t ret = CertInfoInit(certInfo); | 960 | + int32_t ret = CertInfoInit(*certInfo); |
| 961 | if (ret != V_OK) { | 961 | if (ret != V_OK) { |
| 962 | LOG_ERROR("cert info init"); | 962 | LOG_ERROR("cert info init"); |
| 963 | - ret = V_ERR_MEMSET; | 963 | + return V_ERR_MEMSET; |
| 964 | - goto EXIT; | ||
| 965 | } | 964 | } |
| 965 | + return V_OK; | ||
| 966 | +} | ||
| 967 | + | ||
| 968 | +static int32_t SetCertIssuer(CertInfo *certInfo, const mbedtls_x509_crt *ctr) | ||
| 969 | +{ | ||
| 966 | certInfo->issuerLen = ctr->issuer_raw.len; | 970 | certInfo->issuerLen = ctr->issuer_raw.len; |
| 967 | - certInfo->subjectLen = ctr->subject_raw.len; | 971 | + if (certInfo->issuerLen == 0 || certInfo->issuerLen > MAX_PROFILE_SIZE) { |
| 968 | - if (certInfo->issuerLen == 0 || certInfo->issuerLen > MAX_PROFILE_SIZE || | 972 | + return V_ERR_MALLOC; |
| 969 | - certInfo->subjectLen == 0 || certInfo->subjectLen > MAX_PROFILE_SIZE) { | ||
| 970 | - ret = V_ERR_MALLOC; | ||
| 971 | - goto EXIT; | ||
| 972 | } | 973 | } |
| 973 | certInfo->issuer = APPV_MALLOC(certInfo->issuerLen + 1); | 974 | certInfo->issuer = APPV_MALLOC(certInfo->issuerLen + 1); |
| 974 | if (certInfo->issuer == NULL) { | 975 | if (certInfo->issuer == NULL) { |
| 975 | - ret = V_ERR_MALLOC; | 976 | + return V_ERR_MALLOC; |
| 976 | - goto EXIT; | ||
| 977 | } | 977 | } |
| 978 | certInfo->issuer[certInfo->issuerLen] = '\0'; | 978 | certInfo->issuer[certInfo->issuerLen] = '\0'; |
| 979 | - ret = memcpy_s(certInfo->issuer, certInfo->issuerLen, ctr->issuer_raw.p, ctr->issuer_raw.len); | 979 | + int32_t ret = memcpy_s(certInfo->issuer, certInfo->issuerLen, ctr->issuer_raw.p, ctr->issuer_raw.len); |
| 980 | if (ret != EOK) { | 980 | if (ret != EOK) { |
| 981 | - ret = V_ERR_MEMCPY; | 981 | + return V_ERR_MEMCPY; |
| 982 | - goto EXIT; | 982 | + } |
| 983 | + return V_OK; | ||
| 984 | +} | ||
| 985 | + | ||
| 986 | +static int32_t SetCertSubject(CertInfo *certInfo, const mbedtls_x509_crt *ctr) | ||
| 987 | +{ | ||
| 988 | + certInfo->subjectLen = ctr->subject_raw.len; | ||
| 989 | + if (certInfo->subjectLen == 0 || certInfo->subjectLen > MAX_PROFILE_SIZE) { | ||
| 990 | + return V_ERR_MALLOC; | ||
| 983 | } | 991 | } |
| 984 | certInfo->subject = APPV_MALLOC(certInfo->subjectLen + 1); | 992 | certInfo->subject = APPV_MALLOC(certInfo->subjectLen + 1); |
| 985 | if (certInfo->subject == NULL) { | 993 | if (certInfo->subject == NULL) { |
| 986 | - ret = V_ERR_MALLOC; | 994 | + return V_ERR_MALLOC; |
| 987 | - goto EXIT; | ||
| 988 | } | 995 | } |
| 989 | certInfo->subject[certInfo->subjectLen] = '\0'; | 996 | certInfo->subject[certInfo->subjectLen] = '\0'; |
| 990 | - ret = memcpy_s(certInfo->subject, certInfo->subjectLen, ctr->subject_raw.p, ctr->subject_raw.len); | 997 | + int32_t ret = memcpy_s(certInfo->subject, certInfo->subjectLen, ctr->subject_raw.p, ctr->subject_raw.len); |
| 991 | if (ret != EOK) { | 998 | if (ret != EOK) { |
| 992 | - ret = V_ERR_MEMCPY; | 999 | + return V_ERR_MEMCPY; |
| 993 | - goto EXIT; | ||
| 994 | } | 1000 | } |
| 1001 | + return V_OK; | ||
| 1002 | +} | ||
| 1003 | + | ||
| 1004 | +static int32_t SetCertPublicKey(CertInfo *certInfo, const mbedtls_x509_crt *ctr) | ||
| 1005 | +{ | ||
| 1006 | + size_t pkLen = 0; | ||
| 995 | certInfo->pkType = mbedtls_pk_get_type(&ctr->pk); | 1007 | certInfo->pkType = mbedtls_pk_get_type(&ctr->pk); |
| 996 | - certInfo->pkBuf = (char *)GetPkBuf(&ctr->pk, &certInfo->pkLen); | 1008 | + certInfo->pkBuf = (char *)GetPkBuf(&ctr->pk, &pkLen); |
| 997 | if (certInfo->pkBuf == NULL) { | 1009 | if (certInfo->pkBuf == NULL) { |
| 998 | LOG_ERROR("get pk error"); | 1010 | LOG_ERROR("get pk error"); |
| 999 | - ret = V_ERR; | 1011 | + return V_ERR; |
| 1012 | + } | ||
| 1013 | + certInfo->pkLen = (int32_t)pkLen; | ||
| 1014 | + return V_OK; | ||
| 1015 | +} | ||
| 1016 | + | ||
| 1017 | +static int32_t GetCertInfo(const mbedtls_x509_crt *ctr, CertInfo **binSignCert) | ||
| 1018 | +{ | ||
| 1019 | + CertInfo *certInfo = NULL; | ||
| 1020 | + int32_t ret = InitCertInfo(&certInfo); | ||
| 1021 | + if (ret != V_OK) { | ||
| 1000 | goto EXIT; | 1022 | goto EXIT; |
| 1001 | } | 1023 | } |
| 1024 | + | ||
| 1025 | + ret = SetCertIssuer(certInfo, ctr); | ||
| 1026 | + if (ret != V_OK) { | ||
| 1027 | + goto EXIT; | ||
| 1028 | + } | ||
| 1029 | + | ||
| 1030 | + ret = SetCertSubject(certInfo, ctr); | ||
| 1031 | + if (ret != V_OK) { | ||
| 1032 | + goto EXIT; | ||
| 1033 | + } | ||
| 1034 | + | ||
| 1035 | + ret = SetCertPublicKey(certInfo, ctr); | ||
| 1036 | + if (ret != V_OK) { | ||
| 1037 | + goto EXIT; | ||
| 1038 | + } | ||
| 1039 | + | ||
| 1002 | *binSignCert = certInfo; | 1040 | *binSignCert = certInfo; |
| 1003 | return V_OK; | 1041 | return V_OK; |
| 1004 | EXIT: | 1042 | EXIT: |
| 1005 | - FreeCertInfo(certInfo); | 1043 | + if (certInfo != NULL) { |
| 1006 | - APPV_FREE(certInfo); | 1044 | + FreeCertInfo(certInfo); |
| 1045 | + APPV_FREE(certInfo); | ||
| 1046 | + } | ||
| 1007 | return ret; | 1047 | return ret; |
| 1008 | } | 1048 | } |
| 1009 | 1049 | ||