Module: Bixby::Agent::Crypto

Included in:
Handshake
Defined in:
lib/bixby-agent/agent/crypto.rb

Instance Method Summary collapse

Instance Method Details

#create_keypairObject

create crypto keypair and save in config folder



11
12
13
14
15
# File 'lib/bixby-agent/agent/crypto.rb', line 11

def create_keypair
  init_config_dir()
  pair = OpenSSL::PKey::RSA.generate(2048)
  File.open(private_key_file, 'w') { |out| out.write(pair.to_s) }
end

#crypto_enabled?Boolean

Returns:

  • (Boolean)


45
46
47
48
# File 'lib/bixby-agent/agent/crypto.rb', line 45

def crypto_enabled?
  b = ENV["BIXBY_NOCRYPTO"]
  !(b and %w{1 true yes}.include? b)
end

#decrypt_from_server(data) ⇒ String

Decrypt data that was encrypted with our public key

Parameters:

  • data (String)

    Base64 encoded data

Returns:

  • (String)

    unencrypted data



64
65
66
67
68
# File 'lib/bixby-agent/agent/crypto.rb', line 64

def decrypt_from_server(data)
  data = StringIO.new(data, 'rb')
  uuid = data.readline.strip # TODO throwaway the uuid for now
  Bixby::CryptoUtil.decrypt(data, keypair, server_key)
end

#encrypt_for_server(data) ⇒ String

Encrypt data using the server’s public key

Parameters:

  • data (String)

    data to encrypt

Returns:

  • (String)

    Base64 result



55
56
57
# File 'lib/bixby-agent/agent/crypto.rb', line 55

def encrypt_for_server(data)
  Bixby::CryptoUtil.encrypt(data, self.uuid, server_key, keypair)
end

#have_server_key?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/bixby-agent/agent/crypto.rb', line 37

def have_server_key?
  File.exists? server_key_file
end

#keypairObject



21
22
23
# File 'lib/bixby-agent/agent/crypto.rb', line 21

def keypair
  @keypair ||= OpenSSL::PKey::RSA.new(File.read(private_key_file))
end

#private_keyObject



29
30
31
# File 'lib/bixby-agent/agent/crypto.rb', line 29

def private_key
  keypair
end

#private_key_fileObject



17
18
19
# File 'lib/bixby-agent/agent/crypto.rb', line 17

def private_key_file
  File.join(self.config_dir, "id_rsa")
end

#public_keyObject



25
26
27
# File 'lib/bixby-agent/agent/crypto.rb', line 25

def public_key
  @public_key ||= keypair.public_key
end

#server_keyObject



41
42
43
# File 'lib/bixby-agent/agent/crypto.rb', line 41

def server_key
  @server_key ||= OpenSSL::PKey::RSA.new(File.read(server_key_file))
end

#server_key_fileObject



33
34
35
# File 'lib/bixby-agent/agent/crypto.rb', line 33

def server_key_file
  File.join(self.config_dir, "server.pub")
end