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