forked from 0xAalaoui/RubySinglePKI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServeur.rb
60 lines (53 loc) · 1.97 KB
/
Serveur.rb
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
require 'thread'
require 'socket'
require 'openssl'
require 'base64'
$cipher = OpenSSL::Cipher.new("AES-256-ECB")
def encryption(msg,key)
$cipher.encrypt
$cipher.key = key
crypt = $cipher.update(msg) + $cipher.final()
crypt_string = (Base64.encode64(crypt))
return crypt_string
end
socket = TCPServer.new('localhost', 2001)
keys = OpenSSL::PKey::RSA.new(File.open("End_Entity/Serveur.key"))
if not File.exist?('End_Entity/Serveur.crt')
randomAESkey = $cipher.random_key
root_ca = OpenSSL::X509::Certificate.new(File.open("CA/CA.crt"))
PubKey_CA = root_ca.public_key
aeskeyEncWithPubKeyCA = PubKey_CA.public_encrypt(randomAESkey)
aeskeyEncWithPubKeyCA = Base64.encode64(aeskeyEncWithPubKeyCA)
socketWithCA = TCPSocket.new('localhost', 3000)
puts "[SERVER] Waiting for certificate"
socketWithCA.write aeskeyEncWithPubKeyCA #AES key encrypted with PubKey CA
message = socketWithCA.recv(512) #Message from CA
puts message
pubKey = keys.public_key.to_s
pubKeyEncWithAES = encryption(pubKey, randomAESkey) #PubKey encrypted with AES
puts "[SERVER] Sending my encrypted public key"
socketWithCA.write pubKeyEncWithAES
certificate = socketWithCA.recv(2048) #Certificate from CA
certificate = OpenSSL::X509::Certificate.new(certificate)
puts "[SERVER] Storing certificate"
File.open 'End_Entity/Serveur.crt', 'w' do |io| io.write certificate.to_pem end
end
ssl_context = OpenSSL::SSL::SSLContext.new()
ssl_context.cert = OpenSSL::X509::Certificate.new(File.open("End_Entity/Serveur.crt"))
ssl_context.key = keys
ssl_context.verify_mode = OpenSSL::SSL::VERIFY_PEER|OpenSSL::SSL::VERIFY_FAIL_IF_NO_PEER_CERT
ssl_context.ca_file = 'CA/CA.crt'
ssl_socket = OpenSSL::SSL::SSLServer.new(socket, ssl_context)
puts "[SERVER] Waiting for clients"
loop do
begin
Thread.start(ssl_socket.accept) do |s|
identity = s.gets.chomp
puts "[SERVER] " + identity + " connected"
s.puts "Bonjour " + identity
s.close
end
rescue => e
puts "ERREUR #{e.message}"
end
end