Skip to content

Never return -1 from BN_exp - #1455

Closed
bukka wants to merge 1 commit into
openssl:masterfrom
bukka:bn_exp_valid_rc
Closed

bukka wants to merge 1 commit into
openssl:masterfrom
bukka:bn_exp_valid_rc

Conversation

@bukka

@bukka bukka commented Aug 14, 2016

Copy link
Copy Markdown
Member

The return code of BN_exp, BN_mod_exp is documented as 0. However it might return -1 in the cases below which can happen if BN is not correct. This is an issue for example in DSA_generate_key that relies on that return value.

This is actually not a theoretical issue as it happened when I was playing with DSA in PHP openssl ext:

https://github.com/php/php-src/blob/c3c90abb17017af83133f57ec5d7663c0b33fdff/ext/openssl/tests/bug72336.phpt

The problem was that we expect binary value - BN_bin2bn is used on the string (meaning user forgot to call hex2bin). What happened is that DSA_generate_key returned 1 (success) but in fact priv key wasn't generated. Currently I have to do this after porting to 1.1 which is a bit messy.

https://github.com/php/php-src/blob/c3c90abb17017af83133f57ec5d7663c0b33fdff/ext/openssl/openssl.c#L4077-L4082

I understand that it might not be good to fix it in 1.0.2 due to BC break but 1.1 would be good IMHO.

@richsalz richsalz added this to the 1.1.0 milestone Aug 14, 2016
@richsalz richsalz self-assigned this Aug 14, 2016
@richsalz

Copy link
Copy Markdown
Contributor

Yes, return 0 on error is what is documented, pretty clearly. We should fix this now.

@t-j-h

t-j-h commented Aug 14, 2016

Copy link
Copy Markdown
Member

That the actual function can return -1 and always has means that this is actually a documentation error in this instance. That it should be consistent and not do so is a separate item. Taking a quick "grep" for return statements in the bn code I think this won't be the only function where a return of neither 0 or 1 is possible.

@richsalz

Copy link
Copy Markdown
Contributor

Okay, so you think we should update the doc. I'm fine with that.

@t-j-h

t-j-h commented Aug 14, 2016

Copy link
Copy Markdown
Member

No - just pointing out that the source of truth for what an API does isn't the documentation.

Ideally the documentation and the code should match - but when they don't we cannot just automatically assume the documentation is the correct item. We need to look at it. There is only one use of BN_exp in the tree in bntest and it does a <= 0 check for failure. All the BN functions should be consistent - but they are not - and I do think this specific change makes sense to make - but the first check needs to be to see the history of the function and its usage - rather than leaping to must-match-the-doc on the assumption that the documentation is correct.

On this specific change I'm +1 for including it as it should be this way - and there is no library code impacted - and the change basically doesn't break anything (we just will never return a -1 error indication and that is okay). So it is a change to an API - but it is a compatible change.

@richsalz

Copy link
Copy Markdown
Contributor

ah, thanks. i agree the 1//0/-1 thing needs to be looked at. i will merge this one.

levitte pushed a commit that referenced this pull request Aug 14, 2016
Reviewed-by: Tim Hudson <[email protected]>
Reviewed-by: Rich Salz <[email protected]>
(Merged from #1455)
@richsalz

Copy link
Copy Markdown
Contributor

merged with commit 0818dba thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants