Skip to content

openssl: certificate fingerprinting support - #464

Merged
php-pulls merged 13 commits into
php:masterfrom
datibbaw:openssl-x509-digest
Oct 8, 2013
Merged

php-pulls merged 13 commits into
php:masterfrom
datibbaw:openssl-x509-digest

Conversation

@datibbaw

Copy link
Copy Markdown
Contributor

Adds the function openssl_x509_fingerprint() to determine the fingerprint or hash of a certificate.

string|false openssl_x509_fingerprint(
    mixed $x509, 
    [, string $method = "sha1" 
    [, bool $raw_output = false
] ] )

Example:

$context = stream_context_create(['ssl' => ['capture_peer_cert' => true]]);
$f = fopen($url, 'rt', false, $context);
$options = stream_context_get_options($context);

echo openssl_x509_fingerprint($options['ssl']['peer_certificate'], $result, 'sha256'), "\n";

Any hash functions that openssl_digest() supports can be used.

It also comes with the peer_fingerprint connection assertion:

$context = stream_context_create([
    'ssl' => [
        'peer_fingerprint' => 'd41d8cd98f00b204e9800998ecf8427e',
    ]
]);

@nikic

nikic commented Sep 20, 2013

Copy link
Copy Markdown
Member

Why is this making use of a by-reference out parameter?

@datibbaw

Copy link
Copy Markdown
Contributor Author

@nikic Updated to use return value as string or false.

Comment thread ext/openssl/openssl.c Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Won't cert be leaked here (and in the next error condition)?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right. I should really test with memory leaks reporting =S actually report_memleaks is on by default, so maybe this just needs different reproduce code than what's already in the test suite.

Comment thread ext/openssl/openssl.c Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You use int (0/1) return value (I suspect instead of bool) here like in the other openssl patch. Whilt it seemed OK for a match() I don't like it here. We've got SUCCESS/FAILURE for that.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To add to that, parameters and return values that are boolean should be denoted as such, e.g. the raw parameter here and the return value of match should be zend_bool. Furthermore I'd suggest to use minimal necessary amount of indirection, e.g. the zval **val argument of match could just as well be zval *val unless I'm missing something.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi,

On 4 Oct, 2013, at 5:29 PM, nikic notifications@github.com wrote:

In ext/openssl/openssl.c:

  • } else if (!X509_digest(peer, mdtype, md, &n)) {
  •   php_error_docref(NULL TSRMLS_CC, E_ERROR, "Could not generate signature");
    
  •   return 0;
    
  • }
  • if (raw) {
  •   *out_len = n;
    
  •   *out = estrndup(md, n);
    
  • } else {
  •   *out_len = n \* 2;
    
  •   _out = emalloc(_out_len + 1);
    
  •   make_digest_ex(*out, md, n);
    
  • }
  • return 1;
    To add to that, parameters and return values that are boolean should be denoted as such, e.g. the raw parameter here and the return value of match should be zend_bool. Furthermore I'd suggest to use minimal necessary amount of indirection, e.g. the zval **val argument of match could just as well be zval *val unless I'm missing something.

Thanks for reviewing the code, I'll make the changes when I'm hooked up to the "real" Internet again :)

—
Reply to this email directly or view it on GitHub.

@m6w6

m6w6 commented Oct 4, 2013

Copy link
Copy Markdown
Contributor

Else... looks fine now.

Using zend_bool for boolean arguments and return values
Reduced one level of zval indirection where possible
@datibbaw

datibbaw commented Oct 7, 2013

Copy link
Copy Markdown
Contributor Author

@m6w6 @nikic Code updates have been made, thanks!

@php-pulls
php-pulls merged commit 955bc1d into php:master Oct 8, 2013
@datibbaw
datibbaw deleted the openssl-x509-digest branch October 11, 2013 01:58
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.

4 participants