使用 openssl 库获取 x509 证书哈希
Get x509 certificate hash with openssl library
我目前正在开发一个应用程序,它使用 openssl 库 (libcrypto) 生成证书。现在我必须得到一个已经存在的证书的哈希值。
当我使用终端时,我可以使用
生成哈希值
openssl x509 -hash -in cert.pem -noout
Output: 01da0e2b
这是我的代码,我尝试使用 C 中的库生成哈希值。
X509 *cert = NULL;
FILE *fp = fopen(currentCert.UTF8String, "r");
PEM_read_X509(fp, &cert, NULL, NULL);
long hash = X509_subject_name_hash(cert);
char *mdString = malloc(sizeof(long));
sprintf(mdString, "%lx",hash);
printf(mdString);
Output: 1817886a
但实际上我的输出是不同的。有谁知道我做错了什么?
您没有为字符串分配足够的内存,但我不能确定这是您问题的原因。
char *mdString = malloc(sizeof(long));
将为字符串分配 4 个字节,但它显然需要容纳 8 个字节和一个终止符,所以我建议
char *mdString = malloc(sizeof(long)*2 + 1);
But actually my output is a different one. Has anybody an idea what am I doing wrong ?
以下是 OpenSSL 的使用方式...
$ cd openssl-1.0.2-src
$ grep -R X509_subject_name_hash *
apps/x509.c: BIO_printf(STDout, "%08lx\n", X509_subject_name_hash(x));
apps/x509.c: BIO_printf(STDout, "%08lx\n", X509_subject_name_hash_old(x));
crypto/x509/x509.h:unsigned long X509_subject_name_hash(X509 *x);
crypto/x509/x509.h:unsigned long X509_subject_name_hash_old(X509 *x);
crypto/x509/x509_cmp.c:unsigned long X509_subject_name_hash(X509 *x)
crypto/x509/x509_cmp.c:unsigned long X509_subject_name_hash_old(X509 *x)
...
然后,查看apps/x509.c
:
...
} else if (subject_hash == i) {
BIO_printf(STDout, "%08lx\n", X509_subject_name_hash(x));
}
...
你的声明应该是:
unsigned long hash = X509_subject_name_hash(cert);
然后:
fprintf(stdout, "%08lx\n", hash);
此外,OpenSSL 在 OpenSSL 1.0.1 前后的某个时间更改了计算主题哈希的方式。这就是为什么有 X509_subject_name_hash
和 X509_subject_name_hash_old
.
如果您正在使用或与 OpenSSL 0.9.8 进行比较(比如 Mac OS X 10),请参阅 。虽然它 Java,但它详细说明了主题哈希的 OpenSSL 处理。
我目前正在开发一个应用程序,它使用 openssl 库 (libcrypto) 生成证书。现在我必须得到一个已经存在的证书的哈希值。
当我使用终端时,我可以使用
生成哈希值openssl x509 -hash -in cert.pem -noout
Output: 01da0e2b
这是我的代码,我尝试使用 C 中的库生成哈希值。
X509 *cert = NULL;
FILE *fp = fopen(currentCert.UTF8String, "r");
PEM_read_X509(fp, &cert, NULL, NULL);
long hash = X509_subject_name_hash(cert);
char *mdString = malloc(sizeof(long));
sprintf(mdString, "%lx",hash);
printf(mdString);
Output: 1817886a
但实际上我的输出是不同的。有谁知道我做错了什么?
您没有为字符串分配足够的内存,但我不能确定这是您问题的原因。
char *mdString = malloc(sizeof(long));
将为字符串分配 4 个字节,但它显然需要容纳 8 个字节和一个终止符,所以我建议
char *mdString = malloc(sizeof(long)*2 + 1);
But actually my output is a different one. Has anybody an idea what am I doing wrong ?
以下是 OpenSSL 的使用方式...
$ cd openssl-1.0.2-src
$ grep -R X509_subject_name_hash *
apps/x509.c: BIO_printf(STDout, "%08lx\n", X509_subject_name_hash(x));
apps/x509.c: BIO_printf(STDout, "%08lx\n", X509_subject_name_hash_old(x));
crypto/x509/x509.h:unsigned long X509_subject_name_hash(X509 *x);
crypto/x509/x509.h:unsigned long X509_subject_name_hash_old(X509 *x);
crypto/x509/x509_cmp.c:unsigned long X509_subject_name_hash(X509 *x)
crypto/x509/x509_cmp.c:unsigned long X509_subject_name_hash_old(X509 *x)
...
然后,查看apps/x509.c
:
...
} else if (subject_hash == i) {
BIO_printf(STDout, "%08lx\n", X509_subject_name_hash(x));
}
...
你的声明应该是:
unsigned long hash = X509_subject_name_hash(cert);
然后:
fprintf(stdout, "%08lx\n", hash);
此外,OpenSSL 在 OpenSSL 1.0.1 前后的某个时间更改了计算主题哈希的方式。这就是为什么有 X509_subject_name_hash
和 X509_subject_name_hash_old
.
如果您正在使用或与 OpenSSL 0.9.8 进行比较(比如 Mac OS X 10),请参阅