Class: Bitca::CertificateAuthority

Inherits:
Object
  • Object
show all
Defined in:
lib/bitca/certificate_authority.rb

Instance Method Summary collapse

Constructor Details

#initialize(rootpath = '.', keysize = 2048, days = 3650) ⇒ CertificateAuthority

Returns a new instance of CertificateAuthority.



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/bitca/certificate_authority.rb', line 5

def initialize(rootpath='.',keysize=2048,days=3650)
  @rootpath    = rootpath
  @capath      = "#@rootpath/ca"
  @ca_crt      = "#@capath/ca.crt"
  @ca_key      = "#@capath/ca.key"
  @keyspath    = "#@rootpath/keys"
  @cfgpath     = "#@rootpath/cfg"
  @cfgtemplate = "#@rootpath/cfg.erb"
  @keysize     = keysize
  @days        = days
end

Instance Method Details

#ca_exists?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/bitca/certificate_authority.rb', line 28

def ca_exists?
  File.exist?(@ca_crt) and File.exist?(@ca_key)
end

#crt_exists?(cn) ⇒ Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/bitca/certificate_authority.rb', line 40

def crt_exists?(cn)
  File.exist?("#@keyspath/#{cn}.crt")
end

#genca(params = {}) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/bitca/certificate_authority.rb', line 94

def genca (params = {})
  return false if ca_exists?

  gencfg_template params
  gencfg "ca"

  openssl :genrsa, "-out #@ca_key #@keysize"
  openssl :req, "-x509 -new -days #@days -nodes -out #@ca_crt -key #@ca_key -batch -config #@cfgpath/ca"

  File.chmod(0600, @ca_key, @ca_crt)

  true
end

#genkey(cn) ⇒ Object



44
45
46
# File 'lib/bitca/certificate_authority.rb', line 44

def genkey(cn)
  openssl :genrsa, "-out #@keyspath/#{cn}.key #@keysize"
end

#genreq(cn) ⇒ Object



48
49
50
51
# File 'lib/bitca/certificate_authority.rb', line 48

def genreq(cn)
  gencfg cn
  openssl :req, "-new -key #@keyspath/#{cn}.key -out #@keyspath/#{cn}.csr -batch -config #@cfgpath/#{cn}"
end

#get_bundle(cn) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/bitca/certificate_authority.rb', line 57

def get_bundle(cn)
  list = {}

  { :ca_crt => @ca_crt,
    :cfg => "#@cfgpath/#{cn}",
    :crt => "#@keyspath/#{cn}.crt",
    :csr => "#@keyspath/#{cn}.csr",
    :key => "#@keyspath/#{cn}.key"}.each do |type, f|
    list[type] = File.read(f) if File.exist?(f)
  end

  list
end

#get_files(cn) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/bitca/certificate_authority.rb', line 71

def get_files(cn)
  list = {}

  { :cfg => "#@cfgpath/#{cn}",
    :crt => "#@keyspath/#{cn}.crt",
    :csr => "#@keyspath/#{cn}.csr",
    :key => "#@keyspath/#{cn}.key"}.each do |type, f|
    list[type] = f if File.exist?(f)
  end

  list
end

#key_exists?(cn) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/bitca/certificate_authority.rb', line 36

def key_exists?(cn)
  File.exist?("#@keyspath/#{cn}.key")
end

#list(type = :key) ⇒ Object



17
18
19
20
21
22
23
24
25
26
# File 'lib/bitca/certificate_authority.rb', line 17

def list(type = :key)
  items = []

  Dir.foreach(@keyspath) do |f|
    next unless f =~ /\.#{type}$/
    items << f.gsub(/\.#{type}$/, '')
  end

  items
end

#remove(cn) ⇒ Object



84
85
86
87
88
89
90
91
92
# File 'lib/bitca/certificate_authority.rb', line 84

def remove(cn)
  return false unless key_exists?(cn)

  get_files(cn).each do |type, f|
    File.delete(f)
  end

  true
end

#req_exists?(cn) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/bitca/certificate_authority.rb', line 32

def req_exists?(cn)
  File.exist?("#@keyspath/#{cn}.csr")
end

#sign(cn) ⇒ Object



53
54
55
# File 'lib/bitca/certificate_authority.rb', line 53

def sign(cn)
  openssl :x509, "-req -in #@keyspath/#{cn}.csr -CA #@ca_crt -CAkey #@ca_key -CAcreateserial -out #@keyspath/#{cn}.crt -days #@days"
end