int sc_pkcs15_pubkey_from_cert(struct sc_context *ctx, struct sc_pkcs15_der *cert_blob, struct sc_pkcs15_pubkey **out) { int rv; struct sc_pkcs15_cert * cert; cert = calloc(1, sizeof(struct sc_pkcs15_cert)); if (cert == NULL) return SC_ERROR_OUT_OF_MEMORY; rv = parse_x509_cert(ctx, cert_blob, cert); *out = cert->key; cert->key = NULL; sc_pkcs15_free_certificate(cert); LOG_FUNC_RETURN(ctx, rv); }
int sc_pkcs15_read_certificate(struct sc_pkcs15_card *p15card, const struct sc_pkcs15_cert_info *info, struct sc_pkcs15_cert **cert_out) { struct sc_context *ctx = NULL; struct sc_pkcs15_cert *cert = NULL; struct sc_pkcs15_der der; int r; assert(p15card != NULL && info != NULL && cert_out != NULL); ctx = p15card->card->ctx; LOG_FUNC_CALLED(ctx); if (info->value.len && info->value.value) { sc_der_copy(&der, &info->value); } else if (info->path.len) { r = sc_pkcs15_read_file(p15card, &info->path, &der.value, &der.len); LOG_TEST_RET(ctx, r, "Unable to read certificate file."); } else { LOG_FUNC_RETURN(ctx, SC_ERROR_OBJECT_NOT_FOUND); } cert = malloc(sizeof(struct sc_pkcs15_cert)); if (cert == NULL) { free(der.value); LOG_FUNC_RETURN(ctx, SC_ERROR_OUT_OF_MEMORY); } memset(cert, 0, sizeof(struct sc_pkcs15_cert)); if (parse_x509_cert(ctx, &der, cert)) { free(der.value); sc_pkcs15_free_certificate(cert); LOG_FUNC_RETURN(ctx, SC_ERROR_INVALID_ASN1_OBJECT); } free(der.value); *cert_out = cert; LOG_FUNC_RETURN(ctx, SC_SUCCESS); }
int sc_pkcs15_read_certificate(struct sc_pkcs15_card *p15card, const struct sc_pkcs15_cert_info *info, struct sc_pkcs15_cert **cert_out) { struct sc_pkcs15_cert *cert; struct sc_pkcs15_der der; int r; assert(p15card != NULL && info != NULL && cert_out != NULL); LOG_FUNC_CALLED(p15card->card->ctx); if (info->value.len && info->value.value) { sc_der_copy(&der, &info->value); } else if (info->path.len) { r = sc_pkcs15_read_file(p15card, &info->path, &der.value, &der.len); if (r) return r; } else { return SC_ERROR_OBJECT_NOT_FOUND; } cert = malloc(sizeof(struct sc_pkcs15_cert)); if (cert == NULL) { free(der.value); return SC_ERROR_OUT_OF_MEMORY; } memset(cert, 0, sizeof(struct sc_pkcs15_cert)); if (parse_x509_cert(p15card->card->ctx, der.value, der.len, cert)) { free(der.value); sc_pkcs15_free_certificate(cert); return SC_ERROR_INVALID_ASN1_OBJECT; } cert->data = der; *cert_out = cert; return SC_SUCCESS; }
int sc_pkcs15_read_certificate(struct sc_pkcs15_card *p15card, const struct sc_pkcs15_cert_info *info, struct sc_pkcs15_cert **cert_out) { int r; struct sc_pkcs15_cert *cert; u8 *data = NULL; size_t len; assert(p15card != NULL && info != NULL && cert_out != NULL); SC_FUNC_CALLED(p15card->card->ctx, SC_LOG_DEBUG_VERBOSE); if (info->path.len) { r = sc_pkcs15_read_file(p15card, &info->path, &data, &len, NULL); if (r) return r; } else { sc_pkcs15_der_t copy; sc_der_copy(©, &info->value); data = copy.value; len = copy.len; } cert = malloc(sizeof(struct sc_pkcs15_cert)); if (cert == NULL) { free(data); return SC_ERROR_OUT_OF_MEMORY; } memset(cert, 0, sizeof(struct sc_pkcs15_cert)); if (parse_x509_cert(p15card->card->ctx, data, len, cert)) { free(data); sc_pkcs15_free_certificate(cert); return SC_ERROR_INVALID_ASN1_OBJECT; } cert->data = data; *cert_out = cert; return 0; }