【发布时间】:2019-01-25 16:16:46
【问题描述】:
我使用 OpenSSL 1.0.2k 来处理软件上的私钥和公钥,但我在一些操作系统(linux-s390、aix power、HPUX IA64)上遇到了一些问题,仅在 64 位架构上。
这不是那么麻烦,但它困扰我。 我从缓冲区(DER 数据)中得到一个 RSA* 结构,我只是检查 RSA 公共指数以确保它是奇数。 它适用于所有操作系统,除了上述三个:似乎 RSA->e 是偶数(而且显然不是)
这是 BIGNUM 结构:
struct bignum_st {
BN_ULONG *d;
int top;
int dmax;
int neg;
int flags;
};
调试时,我看到在功能正常的操作系统上,d[0] == 65537, d[1] == 0。在没有功能的操作系统上,d[0] == 0, d[1] == 65537。 BN_is_odd 是一个宏:
# define BN_is_odd(a) (((a)->top > 0) && ((a)->d[0] & 1))
#include <stdio.h>
#include <openssl/pem.h>
int get_key(const unsigned char *buf, int len) {
RSA *rsa = d2i_RSA_PUBKEY(NULL, &buf, len);
if (rsa != NULL) {
if (rsa->e != NULL) {
printf("BN : <%s> (hex) -- <%s> (dec)\n", BN_bn2hex(rsa->e), BN_bn2dec(rsa->e));
if (BN_is_odd(rsa->e) == 0) {
printf("Error : RSA public exponent is even\n");
} else {
printf("RSA public exponent is OK.\n");
return 0;
}
}
RSA_free(rsa);
} else {
printf("Error : RSA is NULL\n");
}
return 1;
}
int main() {
const unsigned char data[] = { 0x30, 0x81, 0x9f, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x03, 0x81, 0x8d, 0x00, 0x30, 0x81, 0x89, 0x02, 0x81, 0x81, 0x00, 0xd6, 0x70, 0x5d, 0x67, 0xf2, 0xe1, 0x34, 0x82, 0xd5, 0x2d, 0x79, 0xdd, 0x42, 0x55, 0x41, 0xaf, 0x0c, 0xc2, 0xb4, 0xb0, 0x94, 0xc6, 0xa0, 0x40, 0x54, 0x2e, 0x0f, 0xa5, 0x12, 0x3d, 0x43, 0x96, 0x13, 0x2d, 0x17, 0x50, 0xe5, 0x9a, 0x5a, 0x6e, 0x99, 0xc7, 0xd2, 0x63, 0x4c, 0xcd, 0x57, 0xcb, 0x57, 0x7e, 0x1e, 0x5f, 0x97, 0xaa, 0xbd, 0xe5, 0xc0, 0x98, 0xd9, 0x07, 0x52, 0xdc, 0x27, 0xa4, 0x19, 0xb2, 0x81, 0x5d, 0xd5, 0x03, 0x5c, 0xd2, 0xb3, 0xb8, 0x28, 0xaa, 0xd7, 0xaf, 0x02, 0x08, 0x1c, 0x6c, 0xc2, 0xa4, 0x6c, 0x41, 0xd3, 0xa6, 0xae, 0x51, 0x69, 0xb7, 0xd5, 0x79, 0xb8, 0x62, 0x68, 0x9e, 0xa9, 0x44, 0x8e, 0xbe, 0xb1, 0x2e, 0x1a, 0x3c, 0x4b, 0x21, 0x7b, 0x7d, 0x36, 0xf0, 0x97, 0x98, 0x81, 0x63, 0xa6, 0xfa, 0xf8, 0x28, 0x22, 0x72, 0xfe, 0x16, 0xa8, 0x16, 0x89, 0xbb, 0x02, 0x03, 0x01, 0x00, 0x01 }; /* A DER buffer, valid with openssl rsa -pubin -in <file> -inform DER */
return get_key(data, sizeof data);
}
在大多数系统上,我得到输出“RSA public exponent is OK”。然而,在其他一些人身上,我得到了“RSA public exponent is even”。打印的 BIGNUM 是正确的 (010001 -- 65537)。
我理解为什么 BIGNUM 被认为是偶数。但是为什么这三个操作系统都有前导零呢? (不过,在那些具有 32 位的操作系统上没有问题)。 对此表示赞赏:)
【问题讨论】:
-
你似乎发现了endian-ness的概念:这些平台是big endian,x86和amd64是little endian。 en.m.wikipedia.org/wiki/Endianness
-
我在 AIX 中也遇到了这个问题,并参与了 IBM 开发团队。他们确认了 BUG,现在我需要知道你是如何生成这个 DER 缓冲区的? RSA 密钥上的“openssl rsa -pubin -in
-inform DER”似乎不起作用。 -
@LorinczyZsigmond 我迟到了 2 年,但嘿,总比没有好......是的,我熟悉字节序。我只是没想到 OpenSSL 中会出现这样的错误——因为 BN_is_odd() 函数在这种架构上不起作用。无论如何感谢您的回复。
-
在 AIX6.1 上使用 OpenSSL-1.1.1k 进行了尝试,但没有显示问题。请注意,输入
RSA变得不透明,函数RSA_get0_e返回 e. -
很高兴知道。不幸的是,我们仍然坚持使用 OpenSSL 1.0.2,因为 1.1.1 不支持 FIPS。感谢您的帮助,很抱歉……好吧,我们说“迟到”的回复。
标签: c openssl endianness