3 namespace Friendica\Security\OAuth1\Signature;
5 use Friendica\Security\OAuth1\OAuthRequest;
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
13 * - Chapter 9.3 ("RSA-SHA1")
15 abstract class OAuthSignatureMethod_RSA_SHA1 extends OAuthSignatureMethod
17 public function get_name()
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
27 // Either way should return a string representation of the certificate
28 protected abstract function fetch_public_cert(&$request);
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
33 // Either way should return a string representation of the certificate
34 protected abstract function fetch_private_cert(&$request);
36 public function build_signature(OAuthRequest $request, \Friendica\Security\OAuth1\OAuthConsumer $consumer, \Friendica\Security\OAuth1\OAuthToken $token = null)
38 $base_string = $request->get_signature_base_string();
39 $request->base_string = $base_string;
41 // Fetch the private key cert based on the request
42 $cert = $this->fetch_private_cert($request);
44 // Pull the private key ID from the certificate
45 $privatekeyid = openssl_get_privatekey($cert);
48 openssl_sign($base_string, $signature, $privatekeyid);
50 // Release the key resource
51 openssl_free_key($privatekeyid);
53 return base64_encode($signature);
56 public function check_signature(OAuthRequest $request, \Friendica\Security\OAuth1\OAuthConsumer $consumer, $signature, \Friendica\Security\OAuth1\OAuthToken $token = null)
58 $decoded_sig = base64_decode($signature);
60 $base_string = $request->get_signature_base_string();
62 // Fetch the public key cert based on the request
63 $cert = $this->fetch_public_cert($request);
65 // Pull the public key ID from the certificate
66 $publickeyid = openssl_get_publickey($cert);
68 // Check the computed signature against the one passed in the query
69 $ok = openssl_verify($base_string, $decoded_sig, $publickeyid);
71 // Release the key resource
72 openssl_free_key($publickeyid);