Class: Certify::Authority

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/certify/authority.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#cityObject



49
50
51
52
53
54
55
# File 'app/models/certify/authority.rb', line 49

def city
  if root_certificate
    subject_hash["L"]
  else
    @city
  end
end

#commonnameObject



33
34
35
36
37
38
39
# File 'app/models/certify/authority.rb', line 33

def commonname
  if root_certificate
    subject_hash["CN"]
  else
    @commonname
  end
end

#countryObject



65
66
67
68
69
70
71
# File 'app/models/certify/authority.rb', line 65

def country
  if root_certificate
    subject_hash["C"]
  else
    @country
  end
end

#emailObject



73
74
75
76
77
78
79
# File 'app/models/certify/authority.rb', line 73

def email
  if root_certificate
    subject_hash["emailAddress"]
  else
    @email
  end
end

#organizationObject



41
42
43
44
45
46
47
# File 'app/models/certify/authority.rb', line 41

def organization
  if root_certificate
    subject_hash["O"]
  else
    @organization
  end
end

#stateObject



57
58
59
60
61
62
63
# File 'app/models/certify/authority.rb', line 57

def state
  if root_certificate
    subject_hash["ST"]
  else
    @state
  end
end

Instance Method Details

#generate_new_caObject

builds a new CA



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'app/models/certify/authority.rb', line 100

def generate_new_ca()
  # generate the root key pair
  root_key = OpenSSL::PKey::RSA.new 2048 # the CA's public/private key
  self.rsakey = root_key.to_pem

  # generate the CA name
  ca_name_str = "/C=#{country}/ST=#{state}/O=#{organization}/L=#{city}/CN=#{commonname}/emailAddress=#{email}"

  # parse the name
  ca_name = OpenSSL::X509::Name.parse  ca_name_str

  # generate the root certificate
  root_ca = OpenSSL::X509::Certificate.new
  root_ca.version = 2 # cf. RFC 5280 - to make it a "v3" certificate
  root_ca.serial = 1
  root_ca.subject = ca_name
  root_ca.issuer = root_ca.subject # root CA's are "self-signed"
  root_ca.public_key = root_key.public_key
  root_ca.not_before = Time.now
  root_ca.not_after = root_ca.not_before + 2 * 365 * 24 * 60 * 60 # 2 years validity
  ef = OpenSSL::X509::ExtensionFactory.new
  ef.subject_certificate = root_ca
  ef.issuer_certificate = root_ca
  root_ca.add_extension(ef.create_extension("basicConstraints","CA:TRUE",true))
  root_ca.add_extension(ef.create_extension("keyUsage","keyCertSign, cRLSign", true))
  root_ca.add_extension(ef.create_extension("subjectKeyIdentifier","hash",false))
  root_ca.add_extension(ef.create_extension("authorityKeyIdentifier","keyid:always",false))
  root_ca.sign(root_key, OpenSSL::Digest::SHA256.new)

  # store the root ca
  self.sslcert = root_ca.to_pem
end

#private_keyObject

property accessors



25
26
27
# File 'app/models/certify/authority.rb', line 25

def private_key
  OpenSSL::PKey::RSA.new(self.rsakey) if self.rsakey
end

#root_certificateObject



29
30
31
# File 'app/models/certify/authority.rb', line 29

def root_certificate
  OpenSSL::X509::Certificate.new(self.sslcert) if self.sslcert
end

#subject_hashObject

This method builds the subject hash from the x509 name



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'app/models/certify/authority.rb', line 83

def subject_hash
  # get the array from the name
  dataArray = self.root_certificate.subject.to_a

  # create the result hash
  dataHash = Hash.new()

  # go through
  dataArray.each do |item|
    dataHash[item[0]] = item[1]
  end

  # emit
  dataHash
end