2017-08-26 20:47:38 +09:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class ActivityPub::LinkedDataSignature
|
|
|
|
include JsonLdHelper
|
|
|
|
|
|
|
|
CONTEXT = 'https://w3id.org/identity/v1'
|
2024-09-12 21:58:12 +09:00
|
|
|
SIGNATURE_CONTEXT = 'https://w3id.org/security/v1'
|
2017-08-26 20:47:38 +09:00
|
|
|
|
|
|
|
def initialize(json)
|
2017-08-27 20:35:01 +09:00
|
|
|
@json = json.with_indifferent_access
|
2017-08-26 20:47:38 +09:00
|
|
|
end
|
|
|
|
|
2022-09-22 05:45:57 +09:00
|
|
|
def verify_actor!
|
2017-08-26 20:47:38 +09:00
|
|
|
return unless @json['signature'].is_a?(Hash)
|
|
|
|
|
|
|
|
type = @json['signature']['type']
|
|
|
|
creator_uri = @json['signature']['creator']
|
|
|
|
signature = @json['signature']['signatureValue']
|
|
|
|
|
|
|
|
return unless type == 'RsaSignature2017'
|
|
|
|
|
2023-10-20 17:45:46 +09:00
|
|
|
creator = ActivityPub::TagManager.instance.uri_to_actor(creator_uri)
|
2024-02-01 23:56:46 +09:00
|
|
|
creator = ActivityPub::FetchRemoteKeyService.new.call(creator_uri) if creator&.public_key.blank?
|
2017-08-26 20:47:38 +09:00
|
|
|
|
|
|
|
return if creator.nil?
|
|
|
|
|
|
|
|
options_hash = hash(@json['signature'].without('type', 'id', 'signatureValue').merge('@context' => CONTEXT))
|
|
|
|
document_hash = hash(@json.without('signature'))
|
|
|
|
to_be_verified = options_hash + document_hash
|
|
|
|
|
2023-02-18 20:37:47 +09:00
|
|
|
creator if creator.keypair.public_key.verify(OpenSSL::Digest.new('SHA256'), Base64.decode64(signature), to_be_verified)
|
2023-10-20 17:45:46 +09:00
|
|
|
rescue OpenSSL::PKey::RSAError
|
|
|
|
false
|
2017-08-26 20:47:38 +09:00
|
|
|
end
|
|
|
|
|
2018-08-27 03:21:03 +09:00
|
|
|
def sign!(creator, sign_with: nil)
|
2017-08-26 20:47:38 +09:00
|
|
|
options = {
|
2023-02-20 14:58:28 +09:00
|
|
|
'type' => 'RsaSignature2017',
|
2022-09-22 05:45:57 +09:00
|
|
|
'creator' => ActivityPub::TagManager.instance.key_uri_for(creator),
|
2017-08-26 20:47:38 +09:00
|
|
|
'created' => Time.now.utc.iso8601,
|
|
|
|
}
|
|
|
|
|
|
|
|
options_hash = hash(options.without('type', 'id', 'signatureValue').merge('@context' => CONTEXT))
|
|
|
|
document_hash = hash(@json.without('signature'))
|
|
|
|
to_be_signed = options_hash + document_hash
|
2018-08-27 03:21:03 +09:00
|
|
|
keypair = sign_with.present? ? OpenSSL::PKey::RSA.new(sign_with) : creator.keypair
|
2017-08-26 20:47:38 +09:00
|
|
|
|
2020-09-01 10:04:00 +09:00
|
|
|
signature = Base64.strict_encode64(keypair.sign(OpenSSL::Digest.new('SHA256'), to_be_signed))
|
2017-08-26 20:47:38 +09:00
|
|
|
|
2024-09-12 21:58:12 +09:00
|
|
|
# Mastodon's context is either an array or a single URL
|
|
|
|
context_with_security = Array(@json['@context'])
|
|
|
|
context_with_security << 'https://w3id.org/security/v1'
|
|
|
|
context_with_security.uniq!
|
|
|
|
context_with_security = context_with_security.first if context_with_security.size == 1
|
|
|
|
|
|
|
|
@json.merge('signature' => options.merge('signatureValue' => signature), '@context' => context_with_security)
|
2017-08-26 20:47:38 +09:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def hash(obj)
|
|
|
|
Digest::SHA256.hexdigest(canonicalize(obj))
|
|
|
|
end
|
|
|
|
end
|