1

我正在创建一个用于X509在 C 中修改证书的 api,并且我想添加一种方法来删除扩展名(例如subjectNameAlt)。我将如何通过 OpenSSL API 做到这一点?

4

2 回答 2

2

您可以X509_NAME_delete_entry ()为此使用函数:

X509_NAME_delete_entry() 从位置 loc 的名称中删除一个条目。已删除的条目将被返回,并且必须被释放。

手册页: http: //linux.die.net/man/3/x509_name_delete_entry

编辑:

要实际获取和删除扩展,您可以使用以下函数:

X509_EXTENSION *X509_delete_ext(X509 *x, int loc);

例子:

int idx = X509_get_ext_by_NID( cert, nid, -1 ); //get the index
X509_EXTENSION *ext = X509_get_ext(cert, idx); //get the extension
if (ext != NULL){ //check that the extension was found
    X509_delete_ext(cert, idx); //delete the extension
    X509_EXTENSION_free(ext); //free the memory
}
于 2013-04-12T19:04:14.200 回答
1

Paul 的回答是释放从 X509_get_ext 返回的指针,文档明确表示不要这样做。如文档所述

X509v3_get_ext()[and X509_get_ext()] 从 检索扩展名 loc x。索引 loc 可以取从 0 到 的任何值X509_get_ext_count(x) - 1。返回的扩展是一个内部指针,应用程序不能释放它

释放扩展的正确方法如下。

int idx = X509_get_ext_by_NID( cert, nid, -1 ); //get the index
X509_EXTENSION *ext = X509_get_ext(cert, idx); //get the extension
if (ext != NULL){ //check that the extension was found
    X509_EXTENSION *tmp = X509_delete_ext(cert, idx); //delete the extension
    X509_EXTENSION_free(tmp); //free the memory
}
于 2017-09-29T00:08:55.680 回答