aboutsummaryrefslogtreecommitdiff
path: root/2.1/charon.rb
blob: f41bfa53fe0cfe0d05f07257456f0844c550145b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#!/usr/bin/env ruby

require 'openssl'
require 'securerandom'
require 'sinatra'

Users = {
  "Athena" => "Passw0rd!"
}

Services = {
  "Mail" => "{FvM<kgG}VpHxKJO;6Zo"
}

def ticket(username, service, password)
  cipher = OpenSSL::Cipher::AES.new(256, :CBC).encrypt
  cipher.key = Digest::SHA2.digest(password)
  s = cipher.update("#{username}:#{service}") + cipher.final

  s.unpack('H*')[0].upcase + ?\n
end

def noleak(msg, ul, sl)
  puts "Error: #{msg}, returning nonsense to avoid leakage."
  ticket(SecureRandom.alphanumeric(ul), SecureRandom.alphanumeric(sl), SecureRandom.alphanumeric(16))
end

post '/ticket' do
  request.body.rewind
  data = JSON.parse(request.body.read)
  next "Invalid request\n" unless data.keys.sort == %w(password service username)
  ul = data["username"].length
  sl = data["service"].length
  next noleak("Invalid service", ul, sl) unless Services.keys.include?(data["service"])
  next noleak("Invalid username", ul, sl) unless Users.keys.include?(data["username"])
  next noleak("Invalid password", ul, sl) unless Users[data["username"]] == data["password"]
  next ticket(data["username"], data["service"], Services[data["service"]])
end