gmssl 从证书中提取身份标识
时间: 2023-09-12 16:05:18 浏览: 54
在 gmssl 中,可以使用 `X509_get_serialNumber` 函数来从证书中提取身份标识。示例代码如下:
```c
#include <openssl/x509.h>
#include <openssl/asn1.h>
#include <openssl/err.h>
int main()
{
X509 *cert;
ASN1_INTEGER *serial;
unsigned char *serial_data;
int serial_len;
/* 从文件中读取证书 */
cert = X509_new();
FILE *fp = fopen("cert.pem", "r");
if (!fp) {
perror("open cert.pem failed");
return -1;
}
PEM_read_X509(fp, &cert, NULL, NULL);
fclose(fp);
/* 获取证书序列号 */
serial = X509_get_serialNumber(cert);
serial_len = i2d_ASN1_INTEGER(serial, NULL);
serial_data = malloc(serial_len);
i2d_ASN1_INTEGER(serial, &serial_data);
/* 打印证书序列号 */
printf("Serial number: ");
for (int i = 0; i < serial_len; i++) {
printf("%02X", serial_data[i]);
}
printf("\n");
free(serial_data);
X509_free(cert);
return 0;
}
```
在上述代码中,我们首先使用 `PEM_read_X509` 函数从文件中读取证书,然后使用 `X509_get_serialNumber` 函数获取证书序列号,最后打印证书序列号。在实际应用中,可以根据需要选择其他函数来提取身份标识。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)