为什么即使发生错误,OpenSSL仍返回0?

Cha*_*Kut 4 bash openssl

我表演了

openssl rsa -check -in foo.key
Run Code Online (Sandbox Code Playgroud)

并收到

RSA密钥错误:dmq1与d不一致

不过,

shell>回声$?

0

即使出现错误,为什么我仍应收到返回码0?

Adr*_*rth 5

不确定这是否是一种设计选择,但是如果您检查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.
Run Code Online (Sandbox Code Playgroud)

[...]

返回值

  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).
Run Code Online (Sandbox Code Playgroud)

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

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

这当然看起来就像是一个深思熟虑的选择不是在这种情况下错误的,但我同意,这似乎很奇怪。您可能想在OpenSSL邮件列表中询问,我确定那里的某人可以对这种特殊行为有所了解(毕竟这可能是一个错误)。

  • @ChaimKut,您最终在 OpenSSL 邮件列表上询问过这个问题吗? (2认同)