| 1 | # You may distribute under the terms of either the GNU General Public License | ||||||||||
| 2 | # or the Artistic License (the same terms as Perl itself) | ||||||||||
| 3 | # | ||||||||||
| 4 | # (C) Paul Evans, 2015 -- leonerd@leonerd.org.uk | ||||||||||
| 5 | |||||||||||
| 6 | package Protocol::Matrix; | ||||||||||
| 7 | |||||||||||
| 8 | 6 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 6 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 6 | ▶ | use 5.014; # s///r | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | |||||||||||
| 12 | 6 | our $VERSION = '0.02'; | |||||||||
| 13 | |||||||||||
| 14 | 6 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | |||||||||||
| 16 | 6 | ▶ | use Crypt::NaCl::Sodium; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 17 | 6 | ▶ | use Digest::SHA qw( sha256 ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 18 | 6 | ▶ | use JSON; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 19 | 6 | ▶ | use MIME::Base64 qw( encode_base64 decode_base64 ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 20 | |||||||||||
| 21 | 6 | ▶ | use Exporter 'import'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 22 | 6 | our @EXPORT_OK = qw( | |||||||||
| 23 | encode_json_for_signing | ||||||||||
| 24 | encode_base64_unpadded | ||||||||||
| 25 | decode_base64 | ||||||||||
| 26 | |||||||||||
| 27 | sign_json signed_json | ||||||||||
| 28 | verify_json_signature | ||||||||||
| 29 | |||||||||||
| 30 | redact_event redacted_event | ||||||||||
| 31 | |||||||||||
| 32 | sign_event_json signed_event_json | ||||||||||
| 33 | verify_event_json_signature | ||||||||||
| 34 | ); | ||||||||||
| 35 | |||||||||||
| 36 | 6 | my $sign = Crypt::NaCl::Sodium->sign; | |||||||||
| 37 | |||||||||||
| 38 | 6 | my $json_canon = JSON->new | |||||||||
| 39 | ->convert_blessed | ||||||||||
| 40 | ->canonical | ||||||||||
| 41 | ->utf8; | ||||||||||
| 42 | |||||||||||
| 43 | =head1 NAME | ||||||||||
| 44 | |||||||||||
| 45 | C<Protocol::Matrix> - Helper functions for the Matrix protocol | ||||||||||
| 46 | |||||||||||
| 47 | =head1 DESCRIPTION | ||||||||||
| 48 | |||||||||||
| 49 | This module provides some helper functions for implementing a F<matrix> client | ||||||||||
| 50 | or server. Currently it only contains a few base-level functions to assist | ||||||||||
| 51 | with signing and verifying signatures on federation-level events. | ||||||||||
| 52 | |||||||||||
| 53 | =cut | ||||||||||
| 54 | |||||||||||
| 55 | =head1 FUNCTIONS | ||||||||||
| 56 | |||||||||||
| 57 | =cut | ||||||||||
| 58 | |||||||||||
| 59 | =head2 encode_json_for_signing | ||||||||||
| 60 | |||||||||||
| 61 | $json = encode_json_for_signing( $data ) | ||||||||||
| 62 | |||||||||||
| 63 | Encodes a given HASH reference as Canonical JSON, having removed the | ||||||||||
| 64 | C<signatures> and C<unsigned> keys if present. This is the first step | ||||||||||
| 65 | towards signing it or verifying an embedded signature in it. The hash | ||||||||||
| 66 | referred to by C<$data> remains unmodified by this function. | ||||||||||
| 67 | |||||||||||
| 68 | =cut | ||||||||||
| 69 | |||||||||||
| 70 | sub encode_json_for_signing | ||||||||||
| 71 | 42 | ▶ | { | ||||||||
|
Subroutine
encode_json_for_signing: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 72 | 42 | my ( $d ) = @_; | |||||||||
| 73 | |||||||||||
| 74 | # Remove keys that don't get signed | ||||||||||
| 75 | 42 | my %to_sign = %$d; | |||||||||
| 76 | 42 | delete $to_sign{signatures}; | |||||||||
| 77 | 42 | delete $to_sign{unsigned}; | |||||||||
| 78 | |||||||||||
| 79 | 42 | return $json_canon->encode( \%to_sign ); | |||||||||
| 80 | } | ||||||||||
| 81 | |||||||||||
| 82 | =head2 encode_base64_unpadded | ||||||||||
| 83 | |||||||||||
| 84 | $base64 = encode_base64( $bytes ) | ||||||||||
| 85 | |||||||||||
| 86 | Returns a character string containing the Base-64 encoding of the given bytes, | ||||||||||
| 87 | with no internal linebreaks and no trailing padding. | ||||||||||
| 88 | |||||||||||
| 89 | =cut | ||||||||||
| 90 | |||||||||||
| 91 | sub encode_base64_unpadded | ||||||||||
| 92 | 16 | ▶ | { | ||||||||
|
Subroutine
encode_base64_unpadded: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 93 | 16 | return encode_base64( $_[0], "" ) =~ s/=+$//r; | |||||||||
| 94 | } | ||||||||||
| 95 | |||||||||||
| 96 | =head2 decode_base64 | ||||||||||
| 97 | |||||||||||
| 98 | $bytes = decode_base64( $base64 ) | ||||||||||
| 99 | |||||||||||
| 100 | Returns a byte string containing the bytes obtained by decoding the given | ||||||||||
| 101 | character string. This is re-exported from L<MIME::Base64> for convenience. | ||||||||||
| 102 | |||||||||||
| 103 | =cut | ||||||||||
| 104 | |||||||||||
| 105 | =head2 sign_json | ||||||||||
| 106 | |||||||||||
| 107 | sign_json( $data, secret_key => $key, origin => $name, key_id => $id ) | ||||||||||
| 108 | |||||||||||
| 109 | Modifies the given HASH reference in-place to add a signature. This signature | ||||||||||
| 110 | is created from the given key, and annotated as being from the given origin | ||||||||||
| 111 | name and key ID. Existing signatures already in the hash are not disturbed. | ||||||||||
| 112 | |||||||||||
| 113 | The C<$key> should be a plain byte string or L<Data::Locker> object obtained | ||||||||||
| 114 | from L<Crypt::NaCl::Sodium::sign>'s C<keypair> method. | ||||||||||
| 115 | |||||||||||
| 116 | =cut | ||||||||||
| 117 | |||||||||||
| 118 | sub sign_json | ||||||||||
| 119 | 10 | ▶ | { | ||||||||
|
Subroutine
sign_json: called CC=4 SCAR=14.5
Pod
documented
| |||||||||||
| 120 | 10 | my ( $data, %args ) = @_; | |||||||||
| 121 | |||||||||||
| 122 | 10 | ▶ | my $key = $args{secret_key} or croak "Require a 'secret_key'"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 123 | |||||||||||
| 124 | 10 | ▶ | my $origin = $args{origin} or croak "Require an 'origin'"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 125 | 10 | ▶ | my $key_id = $args{key_id} or croak "Require a 'key_id'"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 126 | |||||||||||
| 127 | 10 | my $signature = $sign->mac( encode_json_for_signing( $data ), $key ); | |||||||||
| 128 | |||||||||||
| 129 | 10 | $data->{signatures}{$origin}{$key_id} = encode_base64_unpadded( $signature ); | |||||||||
| 130 | } | ||||||||||
| 131 | |||||||||||
| 132 | =head2 signed_json | ||||||||||
| 133 | |||||||||||
| 134 | my $data = signed_json( $data, ... ) | ||||||||||
| 135 | |||||||||||
| 136 | Returns a new HASH reference by cloning the original and applying | ||||||||||
| 137 | L</sign_json> to it. The originally-passed data is unmodified. Takes the same | ||||||||||
| 138 | arguments as L</sign_json>. | ||||||||||
| 139 | |||||||||||
| 140 | =cut | ||||||||||
| 141 | |||||||||||
| 142 | sub signed_json | ||||||||||
| 143 | 3 | ▶ | { | ||||||||
|
Subroutine
signed_json: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 144 | 3 | my ( $data, @args ) = @_; | |||||||||
| 145 | 3 | sign_json( $data = { %$data }, @args ); | |||||||||
| 146 | 3 | return $data; | |||||||||
| 147 | } | ||||||||||
| 148 | |||||||||||
| 149 | =head2 verify_json_signature | ||||||||||
| 150 | |||||||||||
| 151 | verify_json_signature( $data, public_key => $key, origin => $name, key_id => $id ) | ||||||||||
| 152 | |||||||||||
| 153 | Inspects the given HASH reference to check that it contains a signature from | ||||||||||
| 154 | the named origin, with the given key ID, and that it is actually valid. | ||||||||||
| 155 | |||||||||||
| 156 | This function does not return an interesting value; all failures are indicated | ||||||||||
| 157 | by thrown exceptions. If no exception is thrown, it can be presumed valid. | ||||||||||
| 158 | |||||||||||
| 159 | =cut | ||||||||||
| 160 | |||||||||||
| 161 | sub verify_json_signature | ||||||||||
| 162 | 5 | ▶ | { | ||||||||
|
Subroutine
verify_json_signature: called CC=8 SCAR=21.7
Pod
documented
| |||||||||||
| 163 | 5 | my ( $data, %args ) = @_; | |||||||||
| 164 | |||||||||||
| 165 | 5 | ▶ | my $key = $args{public_key} or croak "Require a 'public_key'"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 166 | |||||||||||
| 167 | 5 | ▶ | my $origin = $args{origin} or croak "Require an 'origin'"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 168 | 5 | ▶ | my $key_id = $args{key_id} or croak "Require a 'key_id'"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 169 | |||||||||||
| 170 | $data->{signatures} or | ||||||||||
| 171 | 5 | ▶ | croak "No 'signatures'"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 172 | 5 | ▶ | $data->{signatures}{$origin} or | ||||||||
|
Branch: 1 branch
| |||||||||||
| 173 | croak "No signatures from '$origin'"; | ||||||||||
| 174 | |||||||||||
| 175 | 4 | ▶ | my $signature = $data->{signatures}{$origin}{$key_id} or | ||||||||
|
Branch: 1 branch
| |||||||||||
| 176 | croak "No signature from '$origin' using key '$key_id'"; | ||||||||||
| 177 | |||||||||||
| 178 | 3 | ▶ | $sign->verify( decode_base64( $signature ), encode_json_for_signing( $data ), $key ) or | ||||||||
|
Branch: 1 branch
| |||||||||||
| 179 | croak "Signature verification failed"; | ||||||||||
| 180 | } | ||||||||||
| 181 | |||||||||||
| 182 | =head2 redact_event | ||||||||||
| 183 | |||||||||||
| 184 | redact_event( $event ) | ||||||||||
| 185 | |||||||||||
| 186 | Modifies the given HASH reference in-place to apply the transformation given | ||||||||||
| 187 | by the Matrix Event Redaction specification. | ||||||||||
| 188 | |||||||||||
| 189 | =cut | ||||||||||
| 190 | |||||||||||
| 191 | 6 | my %ALLOWED_KEYS = map { $_ => 1 } qw( | |||||||||
| 192 | auth_events | ||||||||||
| 193 | depth | ||||||||||
| 194 | event_id | ||||||||||
| 195 | hashes | ||||||||||
| 196 | membership | ||||||||||
| 197 | origin | ||||||||||
| 198 | origin_server_ts | ||||||||||
| 199 | prev_events | ||||||||||
| 200 | prev_state | ||||||||||
| 201 | room_id | ||||||||||
| 202 | sender | ||||||||||
| 203 | signatures | ||||||||||
| 204 | state_key | ||||||||||
| 205 | type | ||||||||||
| 206 | ); | ||||||||||
| 207 | |||||||||||
| 208 | 6 | my %ALLOWED_CONTENT_BY_TYPE = ( | |||||||||
| 209 | "m.room.aliases" => [qw( aliases )], | ||||||||||
| 210 | "m.room.create" => [qw( creator )], | ||||||||||
| 211 | "m.room.history_visibility" => [qw( history_visibility )], | ||||||||||
| 212 | "m.room.join_rules" => [qw( join_rule )], | ||||||||||
| 213 | "m.room.member" => [qw( membership )], | ||||||||||
| 214 | "m.room.power_levels" => [qw( | ||||||||||
| 215 | users users_default events events_default state_default ban kick redact | ||||||||||
| 216 | )], | ||||||||||
| 217 | ); | ||||||||||
| 218 | |||||||||||
| 219 | sub redact_event | ||||||||||
| 220 | 13 | ▶ | { | ||||||||
|
Subroutine
redact_event: called CC=8 SCAR=20.9
Pod
documented
| |||||||||||
| 221 | 13 | my ( $event ) = @_; | |||||||||
| 222 | |||||||||||
| 223 | 13 | ▶ | defined( my $type = $event->{type} ) or | ||||||||
|
Branch: 1 branch
| |||||||||||
| 224 | croak "Event requires a 'type'"; | ||||||||||
| 225 | |||||||||||
| 226 | 13 | my $old_content = delete $event->{content}; | |||||||||
| 227 | 13 | my $old_unsigned = delete $event->{unsigned}; | |||||||||
| 228 | |||||||||||
| 229 | 13 | ▶ | $ALLOWED_KEYS{$_} or delete $event->{$_} for keys %$event; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 230 | |||||||||||
| 231 | 13 | my $new_content = $event->{content} = {}; | |||||||||
| 232 | |||||||||||
| 233 | 13 | ▶ | if( my $allowed_content_keys = $ALLOWED_CONTENT_BY_TYPE{$type} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 234 | 1 | ▶ | exists $old_content->{$_} and $new_content->{$_} = $old_content->{$_} for | ||||||||
|
Branch: 1 branch
| |||||||||||
| 235 | @$allowed_content_keys; | ||||||||||
| 236 | } | ||||||||||
| 237 | |||||||||||
| 238 | 13 | ▶ | $event->{unsigned}{age_ts} = $old_unsigned->{age_ts} if exists $old_unsigned->{age_ts}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 239 | } | ||||||||||
| 240 | |||||||||||
| 241 | sub redacted_event | ||||||||||
| 242 | 12 | ▶ | { | ||||||||
|
Subroutine
redacted_event: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 243 | 12 | my ( $event ) = @_; | |||||||||
| 244 | 12 | redact_event( $event = { %$event } ); | |||||||||
| 245 | 12 | return $event; | |||||||||
| 246 | } | ||||||||||
| 247 | |||||||||||
| 248 | =head2 sign_event_json | ||||||||||
| 249 | |||||||||||
| 250 | sign_event_json( $data, secret_key => $key, origin => $name, key_id => $id ) | ||||||||||
| 251 | |||||||||||
| 252 | Modifies the given HASH reference in-place to add a hash and signature, | ||||||||||
| 253 | presuming it to be a Matrix event structure. This operates in a fashion | ||||||||||
| 254 | analogous to L</sign_json>. | ||||||||||
| 255 | |||||||||||
| 256 | =cut | ||||||||||
| 257 | |||||||||||
| 258 | sub sign_event_json | ||||||||||
| 259 | 4 | ▶ | { | ||||||||
|
Subroutine
sign_event_json: called CC=4 SCAR=14.1
Pod
documented
| |||||||||||
| 260 | 4 | my ( $event, %args ) = @_; | |||||||||
| 261 | |||||||||||
| 262 | 4 | ▶ | my $key = $args{secret_key} or croak "Require a 'secret_key'"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 263 | |||||||||||
| 264 | 4 | ▶ | my $origin = $args{origin} or croak "Require an 'origin'"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 265 | 4 | ▶ | my $key_id = $args{key_id} or croak "Require a 'key_id'"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 266 | |||||||||||
| 267 | # 'hashes' records the original unredacted version | ||||||||||
| 268 | { | ||||||||||
| 269 | 4 | my %event_without_hashes = %$event; delete $event_without_hashes{hashes}; | |||||||||
| 270 | 4 | my $bytes_to_hash = encode_json_for_signing( \%event_without_hashes ); | |||||||||
| 271 | |||||||||||
| 272 | 4 | $event->{hashes}{sha256} = encode_base64_unpadded( sha256( $bytes_to_hash ) ); | |||||||||
| 273 | } | ||||||||||
| 274 | |||||||||||
| 275 | # Signature is of redacted version | ||||||||||
| 276 | 4 | sign_json( my $signed = redacted_event( $event ), %args ); | |||||||||
| 277 | |||||||||||
| 278 | 4 | $event->{signatures} = $signed->{signatures}; | |||||||||
| 279 | } | ||||||||||
| 280 | |||||||||||
| 281 | =head2 signed_event_json | ||||||||||
| 282 | |||||||||||
| 283 | my $event = signed_event_json( $event, ... ) | ||||||||||
| 284 | |||||||||||
| 285 | Returns a new HASH reference by cloning the original and applying | ||||||||||
| 286 | L</sign_event_json> to it. The originally-passed data is unmodified. Takes the | ||||||||||
| 287 | same arguments as L</sign_event_json>. | ||||||||||
| 288 | |||||||||||
| 289 | =cut | ||||||||||
| 290 | |||||||||||
| 291 | sub signed_event_json | ||||||||||
| 292 | 2 | ▶ | { | ||||||||
|
Subroutine
signed_event_json: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 293 | 2 | my ( $event, @args ) = @_; | |||||||||
| 294 | 2 | sign_event_json( $event = { %$event }, @args ); | |||||||||
| 295 | 2 | return $event; | |||||||||
| 296 | } | ||||||||||
| 297 | |||||||||||
| 298 | =head2 verify_event_json_signature | ||||||||||
| 299 | |||||||||||
| 300 | verify_event_json_signature( $event, public_key => $key, origin => $name, key_id => $id ) | ||||||||||
| 301 | |||||||||||
| 302 | =cut | ||||||||||
| 303 | |||||||||||
| 304 | sub verify_event_json_signature | ||||||||||
| 305 | 1 | ▶ | { | ||||||||
|
Subroutine
verify_event_json_signature: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 306 | 1 | my ( $event, @args ) = @_; | |||||||||
| 307 | |||||||||||
| 308 | 1 | verify_json_signature( redacted_event( $event ), @args ); | |||||||||
| 309 | } | ||||||||||
| 310 | |||||||||||
| 311 | =head1 AUTHOR | ||||||||||
| 312 | |||||||||||
| 313 | Paul Evans <leonerd@leonerd.org.uk> | ||||||||||
| 314 | |||||||||||
| 315 | =cut | ||||||||||
| 316 | |||||||||||
| 317 | 6 | 0x55AA; | |||||||||