krz/vox-populi
A Tumblr web client.
clone: git clone https://gitbay.org/krz/vox-populi.git
main: vendor/eher/oauth/src/Eher/OAuth/RsaSha1.php · raw
1<?php
2
3namespace Eher\OAuth\SignatureMethod;
4
5/**
6 * The RSA-SHA1 signature method uses the RSASSA-PKCS1-v1_5 signature algorithm as defined in
7 * [RFC3447] section 8.2 (more simply known as PKCS#1), using SHA-1 as the hash function for
8 * EMSA-PKCS1-v1_5. It is assumed that the Consumer has provided its RSA public key in a
9 * verified way to the Service Provider, in a manner which is beyond the scope of this
10 * specification.
11 * - Chapter 9.3 ("RSA-SHA1")
12 */
13abstract class RsaSha1 extends SignatureMethod {
14 public function get_name() {
15 return "RSA-SHA1";
16 }
17
18 // Up to the SP to implement this lookup of keys. Possible ideas are:
19 // (1) do a lookup in a table of trusted certs keyed off of consumer
20 // (2) fetch via http using a url provided by the requester
21 // (3) some sort of specific discovery code based on request
22 //
23 // Either way should return a string representation of the certificate
24 protected abstract function fetch_public_cert(&$request);
25
26 // Up to the SP to implement this lookup of keys. Possible ideas are:
27 // (1) do a lookup in a table of trusted certs keyed off of consumer
28 //
29 // Either way should return a string representation of the certificate
30 protected abstract function fetch_private_cert(&$request);
31
32 public function build_signature($request, $consumer, $token) {
33 $base_string = $request->get_signature_base_string();
34 $request->base_string = $base_string;
35
36 // Fetch the private key cert based on the request
37 $cert = $this->fetch_private_cert($request);
38
39 // Pull the private key ID from the certificate
40 $privatekeyid = openssl_get_privatekey($cert);
41
42 // Sign using the key
43 $ok = openssl_sign($base_string, $signature, $privatekeyid);
44
45 // Release the key resource
46 openssl_free_key($privatekeyid);
47
48 return base64_encode($signature);
49 }
50
51 public function check_signature($request, $consumer, $token, $signature) {
52 $decoded_sig = base64_decode($signature);
53
54 $base_string = $request->get_signature_base_string();
55
56 // Fetch the public key cert based on the request
57 $cert = $this->fetch_public_cert($request);
58
59 // Pull the public key ID from the certificate
60 $publickeyid = openssl_get_publickey($cert);
61
62 // Check the computed signature against the one passed in the query
63 $ok = openssl_verify($base_string, $decoded_sig, $publickeyid);
64
65 // Release the key resource
66 openssl_free_key($publickeyid);
67
68 return $ok == 1;
69 }
70}