| 1 |
<?php |
| 2 |
namespace Activitypub; |
| 3 |
|
| 4 |
/** |
| 5 |
* ActivityPub Signature Class |
| 6 |
* |
| 7 |
* @author Matthias Pfefferle |
| 8 |
*/ |
| 9 |
class Signature { |
| 10 |
|
| 11 |
/** |
| 12 |
* @param int $user_id |
| 13 |
* |
| 14 |
* @return mixed |
| 15 |
*/ |
| 16 |
public static function get_public_key( $user_id, $force = false ) { |
| 17 |
$key = get_user_meta( $user_id, 'magic_sig_public_key' ); |
| 18 |
|
| 19 |
if ( $key && ! $force ) { |
| 20 |
return $key[0]; |
| 21 |
} |
| 22 |
|
| 23 |
self::generate_key_pair( $user_id ); |
| 24 |
$key = get_user_meta( $user_id, 'magic_sig_public_key' ); |
| 25 |
|
| 26 |
return $key[0]; |
| 27 |
} |
| 28 |
|
| 29 |
/** |
| 30 |
* @param int $user_id |
| 31 |
* |
| 32 |
* @return mixed |
| 33 |
*/ |
| 34 |
public static function get_private_key( $user_id, $force = false ) { |
| 35 |
$key = get_user_meta( $user_id, 'magic_sig_private_key' ); |
| 36 |
|
| 37 |
if ( $key && ! $force ) { |
| 38 |
return $key[0]; |
| 39 |
} |
| 40 |
|
| 41 |
self::generate_key_pair( $user_id ); |
| 42 |
$key = get_user_meta( $user_id, 'magic_sig_private_key' ); |
| 43 |
|
| 44 |
return $key[0]; |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* Generates the pair keys |
| 49 |
* |
| 50 |
* @param int $user_id |
| 51 |
*/ |
| 52 |
public static function generate_key_pair( $user_id ) { |
| 53 |
$config = array( |
| 54 |
'digest_alg' => 'sha512', |
| 55 |
'private_key_bits' => 2048, |
| 56 |
'private_key_type' => OPENSSL_KEYTYPE_RSA, |
| 57 |
); |
| 58 |
|
| 59 |
$key = openssl_pkey_new( $config ); |
| 60 |
$priv_key = null; |
| 61 |
|
| 62 |
openssl_pkey_export( $key, $priv_key ); |
| 63 |
|
| 64 |
// private key |
| 65 |
update_user_meta( $user_id, 'magic_sig_private_key', $priv_key ); |
| 66 |
|
| 67 |
$detail = openssl_pkey_get_details( $key ); |
| 68 |
|
| 69 |
// public key |
| 70 |
update_user_meta( $user_id, 'magic_sig_public_key', $detail['key'] ); |
| 71 |
} |
| 72 |
|
| 73 |
public static function generate_signature( $user_id, $url, $date ) { |
| 74 |
$key = self::get_private_key( $user_id ); |
| 75 |
|
| 76 |
$url_parts = wp_parse_url( $url ); |
| 77 |
|
| 78 |
$host = $url_parts['host']; |
| 79 |
$path = '/'; |
| 80 |
|
| 81 |
// add path |
| 82 |
if ( ! empty( $url_parts['path'] ) ) { |
| 83 |
$path = $url_parts['path']; |
| 84 |
} |
| 85 |
|
| 86 |
// add query |
| 87 |
if ( ! empty( $url_parts['query'] ) ) { |
| 88 |
$path .= '?' . $url_parts['query']; |
| 89 |
} |
| 90 |
|
| 91 |
$signed_string = "(request-target): post $path\nhost: $host\ndate: $date"; |
| 92 |
|
| 93 |
$signature = null; |
| 94 |
openssl_sign( $signed_string, $signature, $key, OPENSSL_ALGO_SHA256 ); |
| 95 |
$signature = base64_encode( $signature ); // phpcs:ignore |
| 96 |
|
| 97 |
$key_id = get_author_posts_url( $user_id ) . '#main-key'; |
| 98 |
|
| 99 |
return sprintf( 'keyId="%s",algorithm="rsa-sha256",headers="(request-target) host date",signature="%s"', $key_id, $signature ); |
| 100 |
} |
| 101 |
|
| 102 |
public static function verify_signature( $headers, $signature ) { |
| 103 |
|
| 104 |
} |
| 105 |
} |
| 106 |
|