如何从X509中删除扩展程序?

我正在创建一个api来修改C中的X509证书,我想添加一种删除扩展的方法(例如subjectNameAlt )。 我如何通过OpenSSL API执行此操作?

保罗的答案是释放从X509_get_ext返回的指针,文档明确表示不要这样做。正如文档中所述 :

X509v3_get_ext() [和X509_get_ext() ]从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 } 

您可以使用X509_NAME_delete_entry ()函数:

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

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

编辑:

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

 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 }