为什么即使出现错误,OpenSSL也会返回0?

时间:2013-11-10 10:28:15

标签: bash openssl

我进行了

openssl rsa -check -in foo.key

并收到

  

RSA密钥错误:dmq1与d

不一致

然而,

  

壳> echo $?

     

0

即使出现错误,为什么我还会收到0的返回码?

1 个答案:

答案 0 :(得分:4)

不确定这是否是设计选择,但如果您检查OpenSSL来源,则会看到以下内容:

apps/rsa.c使用RSA_check_key()来检查密钥的有效性。该联机帮助页告诉我们:

man RSA_check_key

  

说明

  This function validates RSA keys. It checks that p and q are in fact prime, and that n = p*q.
  It also checks that d*e = 1 mod (p-1*q-1), and that dmp1, dmq1 and iqmp are set correctly or are NULL.
     

[...]

     

返回值

  RSA_check_key() returns 1 if rsa is a valid RSA key, and 0 otherwise.  -1 is returned if an error occurs while checking the key.
  If the key is invalid or an error occurred, the reason code can be obtained using ERR_get_error(3).

因此,它区分了根本无法解析的密钥(-1)和具有无效属性的密钥(0),例如,非素数。

包装代码(apps/rsa.c)确实在1返回RSA_check_key()的情况下以错误(-1)退出,但如果返回0则不会(参见控制流wrt / setting retgoto end;)。

它当然看起来就像是故意选择不在这种情况下出错,但我同意,这似乎很奇怪。你可能想在OpenSSL邮件列表上询问,我相信那里有人可以对这一特定行为有所了解(毕竟这可能是一个错误)。