Class: MooTool::Models::RemoteRequest

Inherits:
Object
  • Object
show all
Includes:
Helpers::IMG4
Defined in:
lib/mootool/models/remote_request.rb

Constant Summary collapse

MATCHER_REGEX =
/---------REQUEST START---------\n.*BODY: \n(?<body>.*)\n\n----------REQUEST END----------/m
PRIME_CURVE =
OpenSSL::PKey::EC::Group.new('prime256v1')
ALGORITHMS =
{ 0 => :SHA1, 1 => :SHA256, 2 => :SHA384 }.freeze
PUBLIC_KEY_PROPERTIES =
{
  UIKPub: :prime256v1,
  RKSigningPub: :prime256v1,
  SIKPub: :secp384r1,
  RKCertificationPub: :secp384r1
}.freeze
MANIFEST_PROPERTIES =
%i[
  Cryptex1Image4Manifest
  FWImage4Manifest
  Image4Manifest
  LocalPolicy
].freeze

Constants included from Helpers::IMG4

Helpers::IMG4::DECODE_TAGS, Helpers::IMG4::FIRMWARE_TAGS, Helpers::IMG4::HASH_LENGTHS, Helpers::IMG4::KEY_INSTANCE_TAGS, Helpers::IMG4::KVP_TAGS, Helpers::IMG4::OCTET_TAGS, Helpers::IMG4::SEQUENCE_TAGS, Helpers::IMG4::SIGNATURE_TAGS

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helpers::IMG4

#construct, #construct_object, #decode_construct, parse_4cc

Constructor Details

#initialize(parsed_data) ⇒ RemoteRequest

Returns a new instance of RemoteRequest.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/mootool/models/remote_request.rb', line 28

def initialize(parsed_data)
  @original = parsed_data.deep_symbolize_keys
  @data = @original.dup

  @rk = {}
  if @data.key? :ActivationInfoXML
    @activation_request = CFPropertyList.native_types(CFPropertyList::List.new(data: @data[:ActivationInfoXML]).value).deep_symbolize_keys
    @activation_request[:UIKCertification][:'Ap,RemotePolicyNonceHash'] = Models::Digest.create(@activation_request[:UIKCertification][:'Ap,RemotePolicyNonceHash'])
    @activation_request[:UIKCertification][:UIKCertification] =
      parse_certification(@activation_request[:UIKCertification][:UIKCertification])
    @data.delete :ActivationInfoXML
  end

  if @data.key? :RKCertification
    @rk[:certification] = parse_certification(@data[:RKCertification]).to_h
    @data.delete :RKCertification
  end

  if @data.key? :RKSignature
    @rk[:signature] = construct(OpenSSL::ASN1.decode(@data[:RKSignature]))
    @data.delete :RKSignature
  end

  if @data.key? :RKProperties
    properties = CFPropertyList.native_types(CFPropertyList::List.new(data: @data[:RKProperties]).value).deep_symbolize_keys
    @data.delete :RKProperties
    @rk[:properties] = parse_properties properties
  end

  if @data.key? :RKSigningPub
    @rk[:signing_pub] = parse_point_with_match(@data[:RKSigningPub])
    @data.delete :RKSigningPub
  end

  if @data.key? :RKPropertiesSignature
    @rk[:properties_signature] = Models::ECCSignature.create(@data[:RKPropertiesSignature])
    @data.delete :RKPropertiesSignature
  end

  return unless @data.key? :RKSigning

  @rk[:signing] = parse_certification(@data[:RKSigning]).to_h
  @data.delete :RKSigning
end

Class Method Details

.load(file) ⇒ Object



124
125
126
127
128
129
130
131
# File 'lib/mootool/models/remote_request.rb', line 124

def self.load(file)
  raw_data = File.read(file)

  body = raw_data.match(MATCHER_REGEX).named_captures['body']
  data = CFPropertyList.native_types(CFPropertyList::List.new(data: body).value)

  new(data)
end

Instance Method Details

#inspectObject



139
140
141
# File 'lib/mootool/models/remote_request.rb', line 139

def inspect
  to_h.ai
end

#parse_certification(certification) ⇒ Object



120
121
122
# File 'lib/mootool/models/remote_request.rb', line 120

def parse_certification(certification)
  EncryptedPayload.new(certification)
end

#parse_point(point, curve = PRIME_CURVE) ⇒ Object



114
115
116
117
118
# File 'lib/mootool/models/remote_request.rb', line 114

def parse_point(point, curve = PRIME_CURVE)
  curve = OpenSSL::PKey::EC::Group.new(curve.to_s) unless curve.is_a? OpenSSL::PKey::EC::Group

  OpenSSL::PKey::EC::Point.new(curve, point)
end

#parse_point_any(point) ⇒ Object



103
104
105
106
107
108
109
110
111
112
# File 'lib/mootool/models/remote_request.rb', line 103

def parse_point_any(point)
  mappings = %w[prime256v1 secp384r1].map do |group|
    group = OpenSSL::PKey::EC::Group.new(group)
    OpenSSL::PKey::EC::Point.new(group, point)
  rescue StandardError
    nil
  end

  mappings.compact.first
end

#parse_point_with_match(point) ⇒ Object



93
94
95
96
97
98
99
100
101
# File 'lib/mootool/models/remote_request.rb', line 93

def parse_point_with_match(point)
  point = parse_point_any(point)
  matches = Models::CertificateIndex.current.matching_key(point)
  if matches.any?
    { key: point, matches: matches }
  else
    point
  end
end

#parse_properties(properties) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/mootool/models/remote_request.rb', line 73

def parse_properties(properties)
  MANIFEST_PROPERTIES.each do |prop|
    next unless properties.key? prop

    properties[prop] = IMG4::File.new(properties[prop]).to_h
  end

  if properties.key? :OIDSToInclude
    properties[:OIDSToInclude] = properties[:OIDSToInclude].map { |oid| Certificate.oid_to_name(oid) }
  end

  PUBLIC_KEY_PROPERTIES.each_key do |prop|
    next unless properties.key? prop

    properties[prop] = parse_point_with_match(properties[prop])
  end

  properties
end

#to_hObject



133
134
135
136
137
# File 'lib/mootool/models/remote_request.rb', line 133

def to_h
  result = {}
  result[:activation_request] = @activation_request if @activation_request
  result[:recovery_kit] = @rk if @rk
end