Class: MTProto::AuthKeyGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/mtproto/auth_key_generator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection, public_key, dc_number = nil, test_mode: false, timeout: 10) ⇒ AuthKeyGenerator

Returns a new instance of AuthKeyGenerator.

Raises:

  • (ArgumentError)


19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/mtproto/auth_key_generator.rb', line 19

def initialize(connection, public_key, dc_number = nil, test_mode: false, timeout: 10)
  raise ArgumentError, 'public_key is required' if public_key.nil? || public_key.empty?
  raise ArgumentError, 'dc_number must be positive. Use test_mode: true for test DCs' if dc_number && dc_number < 0

  @connection = connection
  @public_key = public_key
  @dc_number = dc_number
  @test_mode = test_mode
  @timeout = timeout
  @auth_key = nil
  @server_salt = nil
  @time_offset = 0
end

Instance Attribute Details

#auth_keyObject (readonly)

Returns the value of attribute auth_key.



17
18
19
# File 'lib/mtproto/auth_key_generator.rb', line 17

def auth_key
  @auth_key
end

#connectionObject (readonly)

Returns the value of attribute connection.



17
18
19
# File 'lib/mtproto/auth_key_generator.rb', line 17

def connection
  @connection
end

#server_saltObject (readonly)

Returns the value of attribute server_salt.



17
18
19
# File 'lib/mtproto/auth_key_generator.rb', line 17

def server_salt
  @server_salt
end

#time_offsetObject (readonly)

Returns the value of attribute time_offset.



17
18
19
# File 'lib/mtproto/auth_key_generator.rb', line 17

def time_offset
  @time_offset
end

#timeoutObject (readonly)

Returns the value of attribute timeout.



17
18
19
# File 'lib/mtproto/auth_key_generator.rb', line 17

def timeout
  @timeout
end

Instance Method Details

#generateObject



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
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/mtproto/auth_key_generator.rb', line 33

def generate
  res_pq = send_and_receive(TL::ReqPqMulti.new(SecureRandom.random_bytes(16)), TL::ResPq)

  server_key = Crypto::RSAKey.find_by_fingerprint(res_pq.fingerprints, @public_key)
  raise 'No matching RSA key found!' unless server_key

  p, q = Crypto::Factorization.factorize_pq(res_pq.pq)
  new_nonce = SecureRandom.random_bytes(32)

  server_dh_params = send_req_dh_params(res_pq, p, q, server_key, new_nonce)
  server_dh_inner_data = decrypt_server_dh_params(res_pq, new_nonce, server_dh_params)

  Crypto::DHValidator.validate_dh_params(
    server_dh_inner_data.g,
    server_dh_inner_data.dh_prime,
    server_dh_inner_data.g_a
  )

  client_dh_params = Crypto::DHKeyExchange.generate_client_dh_params(
    server_dh_inner_data.g,
    server_dh_inner_data.dh_prime
  )

  auth_key = Crypto::DHKeyExchange.compute_auth_key(
    server_dh_inner_data.g_a,
    client_dh_params[:b],
    server_dh_inner_data.dh_prime
  )

  tmp_aes_key = Crypto::AuthKeyHelper.derive_tmp_aes_key(new_nonce, res_pq.server_nonce)
  tmp_aes_iv = Crypto::AuthKeyHelper.derive_tmp_aes_iv(new_nonce, res_pq.server_nonce)

  dh_gen_response = send_client_dh_params(
    res_pq,
    new_nonce,
    client_dh_params,
    tmp_aes_key,
    tmp_aes_iv
  )

  verify_dh_gen_response(dh_gen_response, new_nonce, auth_key)

  @auth_key = auth_key
  @server_salt = compute_server_salt(new_nonce, res_pq.server_nonce)
  @time_offset = server_dh_inner_data.server_time - Time.now.to_i

  {
    auth_key: @auth_key,
    server_salt: @server_salt,
    time_offset: @time_offset
  }
end