1 #include <cert-service.h>
2 #include <cert-service-debug.h>
3 #include <cert-svc/cerror.h>
11 void _copy_field(const unsigned char *in, unsigned char **out)
13 size_t in_len = strlen((const char *)(in));
15 *out = (unsigned char *)malloc(sizeof(unsigned char) * (in_len + 1));
17 LOGE("Failed to allocate memory.");
21 memcpy(*out, in, in_len + 1);
24 char *get_complete_path(const char *str1, const char *str2)
26 size_t str1_len = strlen(str1);
30 if (str1[str1_len - 1] != '/')
31 as_result = asprintf(&result, "%s/%s", str1, str2);
33 as_result = asprintf(&result, "%s%s", str1, str2);
42 int get_common_name(const char *path, struct x509_st *x509Struct, char **commonName)
44 int result = CERTSVC_SUCCESS;
45 const unsigned char* data = NULL;
46 CERT_CONTEXT* context = NULL;
47 unsigned char *_commonName = NULL;
48 unsigned char *tmpSubjectStr = NULL;
49 cert_svc_name_fld_data *certFieldData = NULL;
51 if (!path && !x509Struct) {
52 LOGE("Invalid input parameter.");
53 return CERTSVC_WRONG_ARGUMENT;
56 /* If x509Struct is empty, we need to read the certificate and construct the x509 structure */
58 context = cert_svc_cert_context_init();
60 LOGE("Failed to allocate memory.");
61 return CERTSVC_BAD_ALLOC;
64 result = cert_svc_load_file_to_context(context, path);
65 if (result != CERT_SVC_ERR_NO_ERROR) {
66 LOGE("Failed to load file into context.");
67 result = CERTSVC_FAIL;
71 if (!context->certBuf || !context->certBuf->data) {
72 LOGE("Empty certificate buffer.");
73 result = CERTSVC_FAIL;
77 data = context->certBuf->data;
78 d2i_X509(&x509Struct, &data, context->certBuf->size);
81 LOGE("[ERR][%s] Fail to construct X509 structure.", __func__);
82 result = CERT_SVC_ERR_INVALID_CERTIFICATE;
87 /* At this point we assume that we have the x509Struct filled with information */
88 tmpSubjectStr = (unsigned char *)X509_NAME_oneline((x509Struct->cert_info->subject), NULL, 0);
90 LOGE("[ERR][%s] Fail to parse certificate.", __func__);
91 result = CERTSVC_FAIL;
95 certFieldData = (cert_svc_name_fld_data *)malloc(sizeof(cert_svc_name_fld_data));
97 LOGE("Failed to allocate memory.");
98 result = CERTSVC_BAD_ALLOC;
102 certFieldData->commonName = NULL;
103 certFieldData->organizationName = NULL;
104 certFieldData->organizationUnitName = NULL;
105 certFieldData->emailAddress = NULL;
107 result = cert_svc_util_parse_name_fld_data(tmpSubjectStr, certFieldData);
108 if (result != CERT_SVC_ERR_NO_ERROR) {
109 LOGE("[ERR][%s] Fail to parse cert_svc_name_fld_data.", __func__);
110 result = CERTSVC_FAIL;
114 result = CERTSVC_SUCCESS;
116 if (certFieldData->commonName)
117 _copy_field(certFieldData->commonName, &_commonName);
118 else if (certFieldData->organizationName)
119 _copy_field(certFieldData->organizationName, &_commonName);
120 else if (certFieldData->organizationUnitName)
121 _copy_field(certFieldData->organizationUnitName, &_commonName);
122 else if (certFieldData->emailAddress)
123 _copy_field(certFieldData->emailAddress, &_commonName);
126 LOGE("Failed to get common name");
127 result = CERTSVC_FAIL;
131 *commonName = (char *)_commonName;
132 LOGD("Success to get common name for title. commonname[%s]", *commonName);
136 X509_free(x509Struct);
139 cert_svc_cert_context_final(context);
142 OPENSSL_free(tmpSubjectStr);
145 free(certFieldData->countryName);
146 free(certFieldData->localityName);
147 free(certFieldData->stateOrProvinceName);
148 free(certFieldData->organizationName);
149 free(certFieldData->organizationUnitName);
150 free(certFieldData->commonName);
151 free(certFieldData->emailAddress);