3

我正在尝试使用 asn1c 解析 RSA(私有)密钥。我基于https://www.rfc-editor.org/rfc/rfc3447的 asn1 模块,它看起来如下(我试图只使用私钥和公钥部分):

-- ===================
--   Main structures
-- ===================

RSAPrivateKey DEFINITIONS ::= 
BEGIN
--
-- Representation of RSA private key with information for the CRT
-- algorithm.
--

RSAPublicKey ::= SEQUENCE {
    modulus           INTEGER,  -- n
    publicExponent    INTEGER   -- e 
}

RSAPrivateKey ::= SEQUENCE {
    version           Version,
    modulus           INTEGER,  -- n
    publicExponent    INTEGER,  -- e
    privateExponent   INTEGER,  -- d
    prime1            INTEGER,  -- p
    prime2            INTEGER,  -- q
    exponent1         INTEGER,  -- d mod (p-1)
    exponent2         INTEGER,  -- d mod (q-1)
    coefficient       INTEGER,  -- (inverse of q) mod p
    otherPrimeInfos   OtherPrimeInfos OPTIONAL
}

Version ::= INTEGER { two-prime(0), multi(1) }
    (CONSTRAINED BY {
        -- version must be multi if otherPrimeInfos present --
    })

OtherPrimeInfos ::= SEQUENCE SIZE(1..MAX) OF OtherPrimeInfo

OtherPrimeInfo ::= SEQUENCE {
    prime             INTEGER,  -- ri
    exponent          INTEGER,  -- di
    coefficient       INTEGER   -- ti
}

END  -- PKCS1Definitions

但是,当我编译模块并尝试使用以下内容解析 .der 私钥时,它以 RC_FAIL 结束。

RSAPrivateKey_t *rsa_p_key;
rsa_p_key= (RSAPrivateKey_t*)calloc(1, sizeof *rsa_p_key);

asn_dec_rval_t  rval = ber_decode(
    0,
    &asn_DEF_RSAPrivateKey,
    (void**)&rsa_p_key,
    buffer, // buffer containing key (unsigned char*)
    buffer_len); // buffer length (amount of read bytes)

我一直试图在 asn1 模块中找到错误,但没有运气。使用 openssl 打印出 .der 文件,似乎与 RSAPrivateKey 匹配。我还测试了错误的文件读取,但使用了缓冲区匹配和二进制读取模式。

4

1 回答 1

2

您需要使用-fwide-typesflag进行编译。

-fwide-types 使用宽整数类型(INTEGER_t、REAL_t)而不是机器的本机数据类型(long、double)。

问题是大多数INTEGER字段都RSAPrivateKey包含非常大的数字,这些数字不适合long默认使用的类型。

没有-fwide-types

/* RSAPrivateKey */
typedef struct RSAPrivateKey {
    Version_t    version;
    long     modulus;
    long     publicExponent;
    long     privateExponent;

-fwide-types

/* RSAPrivateKey */
typedef struct RSAPrivateKey {
    Version_t    version;
    INTEGER_t    modulus;
    INTEGER_t    publicExponent;
    INTEGER_t    privateExponent;
$ asn1c -fwide-types rfc3447.asn1
$ make -f converter-example.mk
$ ./converter-example -p RSAPrivateKey -iber ./temp/key.der 
<RSAPrivateKey>
    <version>0</version>
    <modulus>00:C1:3D:F5:BD:51:55:B8:94:45:B9:93:53:46:81:58:77:F0:3A:30:75:30:40:F5:84:C2:6C:11:00:6A:00:CC:15:5D:FD:6F:04:1F:3F:92:39:84:AF:8D:DC:3C:58:84:2A:54:9B:AC:E7:77:55:FF:C5:95:94:F6:D7:16:C3:AA:B7:31:0B:BB:72:1A:A4:45:50:D3:D7:3A:98:C6:A0:8E:1F:66:AE:5F:40:94:46:26:64:22:07:F5:84:D8:AD:BD:C6:D1:00:4B:50:FB:66:BF:D1:05:61:84:3B:0D:6D:1E:9C:1F:CB:0C:35:FC:A5:FA:AF:8A:DD:14:0A:3E:1F:8F:E2:40:C0:4F:A2:3D:90:97:10:7F:A1:92:74:C9:97:6A:AD:E9:66:C8:0A:DE:91:88:8B:CB:DD:09:7D:35:CD:78:00:88:0D:A6:CE:1E:71:8C:7D:96:68:74:9E:F4:7E:CA:1A:82:AF:5E:42:A3:6A:60:47:98:00:CA:2A:59:D4:86:CA:AF:B5:CA:C2:38:1A:04:3E:33:86:81:17:78:27:BA:E3:17:D1:52:5A:DF:B5:31:9C:D4:4B:5C:06:EC:93:80:06:86:FB:33:55:68:30:72:91:36:B4:C7:6B:EB:3A:A6:40:0F:C3:78:ED:1A:22:0E:F9:77:44:65:91:E5:C5:B1:53</modulus>
    <publicExponent>65537</publicExponent>
    <privateExponent>00:9E:85:B7:8B:80:A7:73:6D:9E:ED:27:60:4F:1C:58:78:BB:86:E0:AD:A1:D2:08:16:CA:6F:60:5B:18:9A:62:D0:BC:73:E4:98:5B:12:09:60:49:EA:C1:D3:03:66:11:B5:B0:06:AD:06:8C:AC:ED:CF:26:70:37:36:27:24:88:6D:13:3C:EE:9E:22:20:D4:04:04:64:31:5B:96:C5:AB:11:33:68:A4:17:14:0B:9F:FE:D0:B3:FA:C2:EA:05:4D:03:45:FC:99:CC:6B:0F:D5:17:20:F4:E8:46:91:33:0C:C3:42:89:8D:10:D4:9B:4C:54:A8:F3:C7:36:C7:D3:98:71:B3:3E:FC:37:CB:E4:7D:68:11:BF:42:6B:C4:1E:F9:0C:B8:AD:10:F1:58:C8:66:11:CF:84:09:B0:B2:BE:1E:50:3C:80:F1:09:D4:C7:FD:35:3B:EE:AB:AB:14:1F:D1:78:56:EC:98:AE:37:C6:7E:DF:87:EF:76:A6:C4:E5:21:84:73:12:41:F7:C7:17:69:85:B0:C1:FA:9F:E5:11:A0:29:F5:95:22:64:BB:5D:4E:6E:52:A6:BA:E6:50:23:8C:D7:BB:69:C4:D1:36:B3:62:37:0E:C5:76:B0:D3:14:D7:7C:B3:4A:4F:58:59:F0:16:F1:19:42:12:6B:A3:D6:73:71</privateExponent>
    <prime1>00:E7:38:46:89:D6:43:E5:A2:80:F7:EB:03:8B:15:B0:24:CE:9E:0F:4F:4D:1C:C5:7F:15:9B:12:05:D5:4C:20:C8:D2:9C:7C:30:3F:99:84:3A:6E:4B:BA:A0:BF:34:40:C3:30:80:D1:AE:6B:04:C6:DB:E0:FA:5F:65:14:96:0B:3B:9A:75:8E:84:C2:F5:96:98:09:09:87:2F:62:19:87:AB:BB:C7:87:67:67:07:61:5B:8B:CC:B2:19:52:74:1C:91:F7:12:F0:E5:9B:B3:3C:81:2E:F4:2A:E4:AC:56:6D:38:95:DB:18:5E:7B:4F:96:B1:E5:80:41:C2:28:AC:2A:E7</prime1>
    <prime2>00:D5:F3:BA:B4:3C:3B:B0:02:0C:E9:BA:21:CC:03:23:26:F5:0B:2B:27:B0:74:C6:E2:F8:FD:3F:CB:1F:CA:1A:B9:12:4C:B6:7E:56:D4:AB:A8:F2:8D:81:54:63:0C:0E:16:79:54:0B:7C:13:7F:E6:66:12:BD:A0:62:F3:D6:8A:AC:B5:2A:58:70:58:8C:16:94:95:97:2D:9C:2A:A8:30:3F:35:43:57:D7:79:3D:9B:EF:56:95:A0:81:24:DC:67:C5:DA:66:F0:7E:02:94:59:4C:1B:EB:AB:67:0E:B6:C3:BD:92:0B:7C:B4:8E:44:AE:32:1A:42:A1:C7:93:6A:44:B5</prime2>
    <exponent1>00:82:E3:65:72:E3:9A:FD:E4:36:D3:A0:F3:19:89:C6:73:9F:8E:F4:25:B5:06:43:7A:84:55:8B:27:48:2E:57:24:B7:AC:A3:D4:80:3C:3C:11:03:9C:D4:E1:E8:3B:01:2A:3D:4B:BE:E6:D8:68:14:D6:25:8E:35:F0:37:6E:14:9F:C1:F9:28:1B:59:6D:C2:B8:FF:EC:A7:DD:17:D0:51:EF:D2:55:C9:FD:AB:E2:0E:A7:CF:04:AA:11:11:8E:EF:19:65:DF:10:05:3A:55:85:3B:AF:C3:C2:80:3E:5A:92:6B:84:D1:49:03:3B:14:BB:BE:AA:A7:27:12:6D:09:C1:23</exponent1>
    <exponent2>6E:22:70:D1:A6:CF:F2:E2:9B:53:15:85:A0:47:5D:29:08:AB:1F:23:E7:29:B5:D7:D0:E4:4C:9A:7B:5A:C6:36:CE:BC:BE:94:7A:8E:2F:6F:60:AC:87:0E:B1:8D:DB:12:A6:92:24:F7:51:F2:5C:DF:DE:75:CE:C2:21:53:27:3F:90:62:A3:F3:F1:20:EB:DE:C0:C2:79:B0:12:25:51:F0:B7:B2:5A:DD:88:83:B6:69:95:E0:A0:26:DA:9A:BA:B0:96:A4:B6:D7:A6:EC:46:AB:6F:13:F9:BF:AB:4B:59:A7:94:2E:65:9B:6C:40:DE:8A:DC:09:C0:CD:C3:8C:C8:A1</exponent2>
    <coefficient>43:9A:41:22:B1:F4:15:A9:C7:95:FD:F7:7E:55:BC:24:16:5F:E2:9D:B0:D5:74:54:1B:F6:C9:76:C4:6A:4E:5E:6C:AE:71:E1:9A:DE:F1:26:47:B4:41:45:BD:0A:2E:E4:02:DE:AD:28:21:2D:50:59:99:DA:26:E0:90:1A:84:2B:22:46:48:CC:DB:1F:7E:9B:9B:F5:02:D8:24:6F:7E:F3:D9:30:91:1F:83:22:9A:94:C7:F4:29:B2:93:68:CB:57:BC:C5:60:96:0E:42:42:55:D2:3A:71:B5:31:78:D3:D1:2A:74:03:C2:45:A2:9A:A2:89:6F:62:63:C6:42:7B:2D</coefficient>
</RSAPrivateKey>

于 2019-11-08T20:36:11.453 回答