OpenSSL - BIGNUM 检查问题
OpenSSL - BIGNUM check issue
我使用 OpenSSL 1.0.2k 来处理软件上的私钥和 public 密钥,我对一些 OSes(linux-s390,aix power , HPUX IA64), 仅适用于 64 位架构。
倒是没那么麻烦,就是让我很困扰。
我从缓冲区(DER 数据)中得到一个 RSA* 结构,我只是检查 RSA public 指数以确保它是奇数。
它适用于每个 OSes,除了上面三个:RSA->e 似乎是偶数(而且,显然不是)
这是 BIGNUM 结构:
struct bignum_st {
BN_ULONG *d;
int top;
int dmax;
int neg;
int flags;
};
调试时,我看到在功能性 OS 上,d[0] == 65537,d[1] == 0。在非功能性 OS 上,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 被认为是偶数。但是为什么这三个 OSes 有一个前导零? (不过,那些 OSes 上的 32 位没问题)。
对此表示赞赏的任何想法:)
很可能是因为您使用 OpenSSL 头文件编译了 64 位可执行文件
适用于 32 位。
准确的说是opensslconf.h
包含以下两部分
(第一个用于 32 位,第二个用于 64 位):
181 /* Only one for the following should be defined */
182 #undef SIXTY_FOUR_BIT_LONG
183 #undef SIXTY_FOUR_BIT
184 #define THIRTY_TWO_BIT
185 #endif
或
181 /* Only one for the following should be defined */
182 #define SIXTY_FOUR_BIT_LONG
183 #undef SIXTY_FOUR_BIT
184 #undef THIRTY_TWO_BIT
185 #endif
要使其同时兼容 32 位和 64 位,请按以下方式进行编辑:
181 /* Only one for the following should be defined */
182 #undef SIXTY_FOUR_BIT_LONG
183 #undef SIXTY_FOUR_BIT
184 #undef THIRTY_TWO_BIT
185 #if defined(__64BIT__)
186 #define SIXTY_FOUR_BIT_LONG
187 #else
188 #define THIRTY_TWO_BIT
189 #endif
190 #endif
我使用 OpenSSL 1.0.2k 来处理软件上的私钥和 public 密钥,我对一些 OSes(linux-s390,aix power , HPUX IA64), 仅适用于 64 位架构。
倒是没那么麻烦,就是让我很困扰。 我从缓冲区(DER 数据)中得到一个 RSA* 结构,我只是检查 RSA public 指数以确保它是奇数。 它适用于每个 OSes,除了上面三个:RSA->e 似乎是偶数(而且,显然不是)
这是 BIGNUM 结构:
struct bignum_st {
BN_ULONG *d;
int top;
int dmax;
int neg;
int flags;
};
调试时,我看到在功能性 OS 上,d[0] == 65537,d[1] == 0。在非功能性 OS 上,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 被认为是偶数。但是为什么这三个 OSes 有一个前导零? (不过,那些 OSes 上的 32 位没问题)。 对此表示赞赏的任何想法:)
很可能是因为您使用 OpenSSL 头文件编译了 64 位可执行文件 适用于 32 位。
准确的说是opensslconf.h
包含以下两部分
(第一个用于 32 位,第二个用于 64 位):
181 /* Only one for the following should be defined */
182 #undef SIXTY_FOUR_BIT_LONG
183 #undef SIXTY_FOUR_BIT
184 #define THIRTY_TWO_BIT
185 #endif
或
181 /* Only one for the following should be defined */
182 #define SIXTY_FOUR_BIT_LONG
183 #undef SIXTY_FOUR_BIT
184 #undef THIRTY_TWO_BIT
185 #endif
要使其同时兼容 32 位和 64 位,请按以下方式进行编辑:
181 /* Only one for the following should be defined */
182 #undef SIXTY_FOUR_BIT_LONG
183 #undef SIXTY_FOUR_BIT
184 #undef THIRTY_TWO_BIT
185 #if defined(__64BIT__)
186 #define SIXTY_FOUR_BIT_LONG
187 #else
188 #define THIRTY_TWO_BIT
189 #endif
190 #endif