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