forked from 0xAalaoui/RubySinglePKI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCA.rb
67 lines (55 loc) · 1.9 KB
/
CA.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
61
62
63
64
65
66
67
require 'socket'
require 'openssl'
require 'base64'
socket = TCPServer.new('localhost', 3000)
$root_key = OpenSSL::PKey::RSA.new(File.read('CA/CA.key'))
$cipher = OpenSSL::Cipher.new("AES-256-ECB")
def AESdecryption(msg, key)
$cipher.decrypt()
$cipher.key = key
tempkey = Base64.decode64(msg)
crypt = $cipher.update(tempkey)
crypt << $cipher.final()
return crypt
end
def certify(rsaPubKey)
root_ca = OpenSSL::X509::Certificate.new(File.read('CA/CA.crt'))
cert = OpenSSL::X509::Certificate.new
cert.version = 2
cert.serial = Random.rand(100000)
cert.subject = OpenSSL::X509::Name.parse "/O=AalMokh/C=FR/CN=AalMokh CA"
cert.issuer = root_ca.subject
cert.public_key = rsaPubKey
cert.not_before = Time.now
cert.not_after = cert.not_before + 1 * 365 * 24 * 60 * 60 # 1 years validity
ef = OpenSSL::X509::ExtensionFactory.new
ef.subject_certificate = cert
ef.issuer_certificate = root_ca
cert.add_extension(ef.create_extension("keyUsage","digitalSignature", true))
cert.add_extension(ef.create_extension("subjectKeyIdentifier","hash",false))
cert.sign($root_key, OpenSSL::Digest::SHA256.new)
return cert
end
loop do
begin
Thread.start(socket.accept) do |s|
puts "[CA] Client certifying..."
encrypted_AESkey = s.recv(1024) #AES key encrypted with CA PubKey
puts "[CA] AES Key recieved"
encrypted_AESkey = Base64.decode64(encrypted_AESkey)
aesKey = $root_key.private_decrypt(encrypted_AESkey)
s.write "[CA] Send your public key"
encrypted_PUBkey = s.recv(1024) #Client's PubKey encrypted with AES
puts "[CA] Public key recieved"
pubKey = AESdecryption(encrypted_PUBkey,aesKey)
pubKey = OpenSSL::PKey::RSA.new(pubKey)
certificate = certify(pubKey) #Generate certificate for the client
puts "[CA] Sending certificate"
s.write certificate
s.close
puts "[CA] Done"
end
rescue => e
puts "Error #{e.message}"
end
end