selfsdk 0.0.136 → 0.0.141

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 80d405628b95421e76e1461dcff31f3351109cd009c7b0b677ce29d27ecf133d
4
- data.tar.gz: 20235912eef39c3a084dd7afeab7cbb9d050d1649d02a45acdcf1a4403519e7f
3
+ metadata.gz: 1fc84c8ad47b159c7f07f33226c1273bf30202f3b999ceba02ef51f6b35763fb
4
+ data.tar.gz: 4c88fbad5ffa05cc69977554d454ca7fcc796663400f2e74b770879afe05c2a7
5
5
  SHA512:
6
- metadata.gz: 734d43a91a7485370a5aa3a16fc56f2d097fc510aef007b25ad63645cf255629f286213371f787e7d2bd1e07d22ab7640a55b50b55518927a34c70dd72227d93
7
- data.tar.gz: 9c7484084c68f95078154b2d21884c9a333a7388213fa0782ac6de7b236bedc5c5a6683fb9e62ed1e52c6c31bd63dc46cff159f977aac51886161dfbc5bec169
6
+ metadata.gz: c5f9b8cf446fb87a089d2094449691cd73806af84ce5d3f712a0357a0bdc5c4b2f7fef0dd6552591d56c4c5dd07fcaadcad02173b67b500f07ae80aec519fc2f
7
+ data.tar.gz: ee291b8ca0f22787067ae2edca5a59fee3fea4950a06cfd921c00c9d283d7c769cefeac7dd8bcee0bd2eeee7784fab307a52919d81f87942b8c8ccd4eff6d045
@@ -65,6 +65,34 @@ module SelfSDK
65
65
  i[:public_keys]
66
66
  end
67
67
 
68
+ def post(endpoint, body)
69
+ res = nil
70
+ loop do
71
+ res = HTTParty.post("#{@self_url}#{endpoint}",
72
+ headers: {
73
+ 'Content-Type' => 'application/json',
74
+ 'Authorization' => "Bearer #{@jwt.auth_token}"
75
+ },
76
+ body: body)
77
+ break if res.code != 503
78
+ sleep 2
79
+ end
80
+ return res
81
+ end
82
+
83
+ def get(endpoint)
84
+ res = nil
85
+ loop do
86
+ res = HTTParty.get("#{@self_url}#{endpoint}", headers: {
87
+ 'Content-Type' => 'application/json',
88
+ 'Authorization' => "Bearer #{@jwt.auth_token}"
89
+ })
90
+ break if res.code != 503
91
+ sleep 2
92
+ end
93
+ return res
94
+ end
95
+
68
96
  # Lists all public keys stored on self for the given ID
69
97
  #
70
98
  # @param id [string] identity id
@@ -74,6 +102,15 @@ module SelfSDK
74
102
  sg.key_by_id(kid)
75
103
  end
76
104
 
105
+ # Get the active public key for a device
106
+ #
107
+ # @param id [string] identity id
108
+ def device_public_key(id, did)
109
+ i = entity(id)
110
+ sg = SelfSDK::SignatureGraph.new(i[:history])
111
+ sg.key_by_device(did)
112
+ end
113
+
77
114
  private
78
115
 
79
116
  def get_identity(endpoint)
@@ -86,20 +123,6 @@ module SelfSDK
86
123
  body
87
124
  end
88
125
 
89
- def get(endpoint)
90
- HTTParty.get("#{@self_url}#{endpoint}", headers: {
91
- 'Content-Type' => 'application/json',
92
- 'Authorization' => "Bearer #{@jwt.auth_token}"
93
- })
94
- end
95
126
 
96
- def post(endpoint, body)
97
- HTTParty.post("#{@self_url}#{endpoint}",
98
- headers: {
99
- 'Content-Type' => 'application/json',
100
- 'Authorization' => "Bearer #{@jwt.auth_token}"
101
- },
102
- body: body)
103
- end
104
127
  end
105
128
  end
@@ -0,0 +1,112 @@
1
+ require 'self_crypto'
2
+
3
+ module SelfSDK
4
+ class Crypto
5
+ def initialize(client, device, storage_folder, storage_key)
6
+ @client = client
7
+ @device = device
8
+ @storage_key = storage_key
9
+ @storage_folder = storage_folder
10
+
11
+ if File.exist?(account_path)
12
+ # 1a) if alice's account file exists load the pickle from the file
13
+ @account = SelfCrypto::Account.from_pickle(File.read(account_path), @storage_key)
14
+ else
15
+ # 1b-i) if create a new account for alice if one doesn't exist already
16
+ @account = SelfCrypto::Account.from_seed(@client.jwt.key)
17
+
18
+ # 1b-ii) generate some keys for alice and publish them
19
+ @account.gen_otk(100)
20
+
21
+ # 1b-iii) convert those keys to json
22
+ keys = @account.otk['curve25519'].map{|k,v| {id: k, key: v}}.to_json
23
+
24
+ # 1b-iv) post those keys to POST /v1/identities/<selfid>/devices/1/pre_keys/
25
+ @client.post("/v1/apps/#{@client.jwt.id}/devices/#{@device}/pre_keys", keys)
26
+
27
+ # 1b-v) store the account to a file
28
+ File.write(account_path, @account.to_pickle(storage_key))
29
+ end
30
+ end
31
+
32
+ def encrypt(message, recipient, recipient_device)
33
+ session_file_name = session_path(recipient, recipient_device)
34
+
35
+ if File.exist?(session_file_name)
36
+ # 2a) if bob's session file exists load the pickle from the file
37
+ session_with_bob = SelfCrypto::Session.from_pickle(File.read(session_file_name), @storage_key)
38
+ else
39
+ # 2b-i) if you have not previously sent or recevied a message to/from bob,
40
+ # you must get his identity key from GET /v1/identities/bob/
41
+ ed25519_identity_key = @client.device_public_key(recipient, recipient_device)
42
+
43
+ # 2b-ii) get a one time key for bob
44
+ res = @client.get("/v1/identities/#{recipient}/devices/#{recipient_device}/pre_keys")
45
+
46
+ if res.code != 200
47
+ Selfid.logger.error "identity response : #{res.body[:message]}"
48
+ raise "could not get identity pre_keys"
49
+ end
50
+
51
+ one_time_key = JSON.parse(res.body)["key"]
52
+
53
+ # 2b-iii) convert bobs ed25519 identity key to a curve25519 key
54
+ curve25519_identity_key = SelfCrypto::Util.ed25519_pk_to_curve25519(ed25519_identity_key.raw_public_key)
55
+
56
+ # 2b-iv) create the session with bob
57
+ session_with_bob = @account.outbound_session(curve25519_identity_key, one_time_key)
58
+
59
+ # 2b-v) store the session to a file
60
+ File.write(session_file_name, session_with_bob.to_pickle(@storage_key))
61
+ end
62
+
63
+ # 3) create a group session and set the identity of the account youre using
64
+ gs = SelfCrypto::GroupSession.new("#{@client.jwt.id}:#{@device}")
65
+
66
+ # 4) add all recipients and their sessions
67
+ gs.add_participant("#{recipient}:#{recipient_device}", session_with_bob)
68
+
69
+ # 5) encrypt a message
70
+ gs.encrypt(message).to_s
71
+ end
72
+
73
+ def decrypt(message, sender, sender_device)
74
+ session_file_name = session_path(sender, sender_device)
75
+
76
+ if File.exist?(session_file_name)
77
+ # 7a) if carol's session file exists load the pickle from the file
78
+ session_with_bob = SelfCrypto::Session.from_pickle(File.read(session_file_name), @storage_key)
79
+ else
80
+ # 7b-i) if you have not previously sent or received a message to/from bob,
81
+ # you should extract the initial message from the group message intended
82
+ # for your account id.
83
+ m = SelfCrypto::GroupMessage.new(message.to_s).get_message("#{@client.jwt.id}:#{@device}")
84
+
85
+ # 7b-ii) use the initial message to create a session for bob or carol
86
+ session_with_bob = @account.inbound_session(m)
87
+
88
+ # 7b-iii) store the session to a file
89
+ File.write(session_file_name, session_with_bob.to_pickle(@storage_key))
90
+ end
91
+
92
+ # 8) create a group session and set the identity of the account you're using
93
+ gs = SelfCrypto::GroupSession.new("#{@client.jwt.id}:#{@device}")
94
+
95
+ # 9) add all recipients and their sessions
96
+ gs.add_participant("#{sender}:#{sender_device}", session_with_bob)
97
+
98
+ # 10) decrypt the message ciphertext
99
+ gs.decrypt("#{sender}:#{sender_device}", message).to_s
100
+ end
101
+
102
+ private
103
+
104
+ def account_path
105
+ "#{@storage_folder}/account.pickle"
106
+ end
107
+
108
+ def session_path(selfid, device)
109
+ "#{@storage_folder}/#{selfid}:#{device}-session.pickle"
110
+ end
111
+ end
112
+ end
@@ -44,7 +44,7 @@ module SelfSDK
44
44
  sender: "#{@jwt.id}:#{@messaging.device_id}",
45
45
  id: @id,
46
46
  recipient: "#{@to}:#{@to_device}",
47
- ciphertext: @jwt.prepare(body))
47
+ ciphertext: encrypt_message(@jwt.prepare(body), @to, @to_device))
48
48
  end
49
49
 
50
50
  end
@@ -26,6 +26,10 @@ module SelfSDK
26
26
  res
27
27
  end
28
28
 
29
+ def encrypt_message(message, recipient, recipient_device)
30
+ @messaging.encryption_client.encrypt(message, recipient, recipient_device)
31
+ end
32
+
29
33
  def unauthorized?
30
34
  status == "unauthorized"
31
35
  end
@@ -93,19 +93,20 @@ module SelfSDK
93
93
  end
94
94
  @to_device = devices.first
95
95
 
96
- recipient = if @intermediary.nil?
97
- "#{@to}:#{@to_device}"
98
- else
99
- "#{@intermediary}:#{@to_device}"
100
- end
96
+ if @intermediary.nil?
97
+ recipient = "#{@to}:#{@to_device}"
98
+ ciphertext = encrypt_message(@jwt.prepare(body), @to, @to_device)
99
+ else
100
+ recipient = "#{@intermediary}:#{@to_device}"
101
+ ciphertext = encrypt_message(@jwt.prepare(body), @intermediary, @to_device)
102
+ end
101
103
 
102
104
  Msgproto::Message.new(
103
105
  type: Msgproto::MsgType::MSG,
104
106
  id: @id,
105
107
  sender: "#{@jwt.id}:#{@messaging.device_id}",
106
108
  recipient: recipient,
107
- ciphertext: @jwt.prepare(body),
108
- )
109
+ ciphertext: ciphertext )
109
110
  end
110
111
  end
111
112
  end
@@ -11,7 +11,8 @@ module SelfSDK
11
11
  body = if input.is_a? String
12
12
  input
13
13
  else
14
- input.ciphertext
14
+ issuer = input.recipient.split(":")
15
+ messaging.encryption_client.decrypt(input.ciphertext, issuer.first, issuer.last)
15
16
  end
16
17
 
17
18
  jwt = JSON.parse(body, symbolize_names: true)
@@ -4,6 +4,7 @@ require 'json'
4
4
  require 'monitor'
5
5
  require 'faye/websocket'
6
6
  require 'fileutils'
7
+ require_relative 'crypto'
7
8
  require_relative 'messages/message'
8
9
  require_relative 'proto/auth_pb'
9
10
  require_relative 'proto/message_pb'
@@ -18,16 +19,18 @@ module SelfSDK
18
19
  DEFAULT_STORAGE_DIR="./.self_storage"
19
20
  ON_DEMAND_CLOSE_CODE=3999
20
21
 
21
- attr_accessor :client, :jwt, :device_id, :ack_timeout, :timeout, :type_observer, :uuid_observer
22
+ attr_accessor :client, :jwt, :device_id, :ack_timeout, :timeout, :type_observer, :uuid_observer, :encryption_client
22
23
 
23
24
  # RestClient initializer
24
25
  #
25
26
  # @param url [string] self-messaging url
26
27
  # @params client [Object] SelfSDK::Client object
27
28
  # @option opts [string] :storage_dir the folder where encryption sessions and settings will be stored
29
+ # @params storage_key [String] seed to encrypt messaging
30
+ # @params storage_folder [String] folder to perist messaging encryption
28
31
  # @option opts [Bool] :auto_reconnect Automatically reconnects to websocket if connection is lost (defaults to true).
29
32
  # @option opts [String] :device_id The device id to be used by the app defaults to "1".
30
- def initialize(url, client, options = {})
33
+ def initialize(url, client, storage_key, options = {})
31
34
  @mon = Monitor.new
32
35
  @url = url
33
36
  @messages = {}
@@ -45,6 +48,9 @@ module SelfSDK
45
48
  @offset = read_offset
46
49
 
47
50
  FileUtils.mkdir_p @storage_dir unless File.exist? @storage_dir
51
+ unless options.include? :no_crypto
52
+ @encryption_client = Crypto.new(@client, @device_id, @storage_dir, storage_key)
53
+ end
48
54
 
49
55
  if options.include? :ws
50
56
  @ws = options[:ws]
@@ -53,6 +53,7 @@ module SelfSDK
53
53
  unless messaging_url.nil?
54
54
  @messaging_client = MessagingClient.new(messaging_url,
55
55
  @client,
56
+ storage_key,
56
57
  storage_dir: opts.fetch(:storage_dir, MessagingClient::DEFAULT_STORAGE_DIR),
57
58
  auto_reconnect: opts.fetch(:auto_reconnect, MessagingClient::DEFAULT_AUTO_RECONNECT),
58
59
  device_id: opts.fetch(:device_id, MessagingClient::DEFAULT_DEVICE))
@@ -39,13 +39,16 @@ module SelfSDK
39
39
  # @return [String, String] conversation id or encoded body.
40
40
  def request(selfid, opts = {}, &block)
41
41
  SelfSDK.logger.info "authenticating #{selfid}"
42
- raise "You're not permitting connections from #{selfid}" unless @messaging_service.is_permitted?(selfid)
42
+ rq = opts.fetch(:request, true)
43
+ if rq
44
+ raise "You're not permitting connections from #{selfid}" unless @messaging_service.is_permitted?(selfid)
45
+ end
43
46
 
44
47
  req = SelfSDK::Messages::AuthenticationReq.new(@messaging)
45
48
  req.populate(selfid, opts)
46
49
 
47
50
  body = @client.jwt.prepare(req.body)
48
- return body unless opts.fetch(:request, true)
51
+ return body unless rq
49
52
  return req.send_message if opts.fetch(:async, false)
50
53
 
51
54
  # when a block is given the request will always be asynchronous.
@@ -41,13 +41,16 @@ module SelfSDK
41
41
  # @return [Object] SelfSDK:::Messages::FactRequest
42
42
  def request(selfid, facts, opts = {}, &block)
43
43
  SelfSDK.logger.info "authenticating #{selfid}"
44
- raise "You're not permitting connections from #{selfid}" unless @messaging_service.is_permitted?(selfid)
44
+ rq = opts.fetch(:request, true)
45
+ if rq
46
+ raise "You're not permitting connections from #{selfid}" unless @messaging_service.is_permitted?(selfid)
47
+ end
45
48
 
46
49
  req = SelfSDK::Messages::FactRequest.new(@messaging)
47
50
  req.populate(selfid, prepare_facts(facts), opts)
48
51
 
49
52
  body = @client.jwt.prepare(req.body)
50
- return body unless opts.fetch(:request, true)
53
+ return body unless rq
51
54
 
52
55
  # when a block is given the request will always be asynchronous.
53
56
  if block_given?
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: selfsdk
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.136
4
+ version: 0.0.141
5
5
  platform: ruby
6
6
  authors:
7
7
  - Aldgate Ventures
@@ -10,6 +10,20 @@ bindir: bin
10
10
  cert_chain: []
11
11
  date: 2011-09-29 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: self_crypto
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: async
15
29
  requirement: !ruby/object:Gem::Requirement
@@ -299,6 +313,7 @@ files:
299
313
  - lib/acl.rb
300
314
  - lib/authenticated.rb
301
315
  - lib/client.rb
316
+ - lib/crypto.rb
302
317
  - lib/jwt_service.rb
303
318
  - lib/log.rb
304
319
  - lib/messages/attestation.rb