static int dnie_read_certificate(sc_card_t * card, char *certpath, X509 ** cert) { sc_file_t *file = NULL; sc_path_t *path = NULL; u8 *buffer = NULL; char *msg = NULL; size_t bufferlen = 0; int res = SC_SUCCESS; LOG_FUNC_CALLED(card->ctx); path = (sc_path_t *) calloc(1, sizeof(sc_path_t)); if (!path) { msg = "Cannot allocate path data for cert read"; res = SC_ERROR_OUT_OF_MEMORY; goto read_cert_end; } sc_format_path(certpath, path); res = dnie_read_file(card, path, &file, &buffer, &bufferlen); if (res != SC_SUCCESS) { msg = "Cannot get intermediate CA cert"; goto read_cert_end; } *cert = d2i_X509(NULL, (const unsigned char **)&buffer, bufferlen); if (*cert == NULL) { /* received data is not a certificate */ res = SC_ERROR_OBJECT_NOT_VALID; msg = "Readed data is not a certificate"; goto read_cert_end; } res = SC_SUCCESS; read_cert_end: if (file) { sc_file_free(file); file = NULL; buffer = NULL; bufferlen = 0; } if (msg) sc_log(card->ctx, msg); LOG_FUNC_RETURN(card->ctx, res); }
/** * Read SM required certificates from card. * * This function uses received path to read a certificate file from * card. * No validation is done except that received data is effectively a certificate * @param card Pointer to card driver structure * @param certpat path to requested certificate * @param cert where to store resultig data * @return SC_SUCCESS if ok, else error code */ static int dnie_read_certificate(sc_card_t * card, char *certpath, X509 ** cert) { sc_file_t *file = NULL; sc_path_t path; u8 *buffer = NULL, *buffer2 = NULL; char *msg = NULL; size_t bufferlen = 0; int res = SC_SUCCESS; LOG_FUNC_CALLED(card->ctx); sc_format_path(certpath, &path); res = dnie_read_file(card, &path, &file, &buffer, &bufferlen); if (res != SC_SUCCESS) { msg = "Cannot get intermediate CA cert"; goto read_cert_end; } buffer2 = buffer; *cert = d2i_X509(NULL, (const unsigned char **)&buffer2, bufferlen); if (*cert == NULL) { /* received data is not a certificate */ res = SC_ERROR_OBJECT_NOT_VALID; msg = "Read data is not a certificate"; goto read_cert_end; } res = SC_SUCCESS; read_cert_end: if (buffer) { free(buffer); buffer = NULL; bufferlen = 0; } if (file) { sc_file_free(file); file = NULL; } if (msg) sc_log(card->ctx, msg); LOG_FUNC_RETURN(card->ctx, res); }