diff --git a/app/controllers/certificates_controller.rb b/app/controllers/certificates_controller.rb index d31535f..220d4c7 100644 --- a/app/controllers/certificates_controller.rb +++ b/app/controllers/certificates_controller.rb @@ -1,3 +1,5 @@ +require 'openssl' + class CertificatesController < ApplicationController before_action :set_certificate, only: [:show, :edit, :update, :destroy] @@ -26,6 +28,22 @@ class CertificatesController < ApplicationController def create @certificate = Certificate.new(certificate_params) + key = OpenSSL::PKey::RSA.new 4096 + name = OpenSSL::X509::Name.parse 'CN=lex-deeit/DC=weird-web-workers/DC=org' + + cert = OpenSSL::X509::Certificate.new + cert.version = 2 + cert.serial = 0 + cert.not_before = Time.now + cert.not_after = Time.now + 3600 + + cert.public_key = key.public_key + cert.subject = name + cert.sign key, OpenSSL::Digest::SHA256.new + + @certificate.key = key.to_pem + @certificate.cert = cert.to_pem + respond_to do |format| if @certificate.save format.html { redirect_to @certificate, notice: 'Certificate was successfully created.' } @@ -72,3 +90,5 @@ class CertificatesController < ApplicationController params.require(:certificate).permit(:key, :cert, :active) end end + +# vim: set et ts=2 sw=2: diff --git a/app/models/certificate.rb b/app/models/certificate.rb index 0489195..aa8efed 100644 --- a/app/models/certificate.rb +++ b/app/models/certificate.rb @@ -1,2 +1,21 @@ +require "openssl" +require 'digest/md5' + class Certificate < ActiveRecord::Base + def key + OpenSSL::PKey::RSA.new read_attribute(:key) if read_attribute(:key) + end + + def cert + OpenSSL::X509::Certificate.new read_attribute(:cert) if read_attribute(:cert) + end + + def key_fpr + Digest::SHA1.hexdigest(key.to_der).upcase + end + + def cert_fpr + Digest::SHA1.hexdigest(cert.to_der).upcase + end end +# vim: set et ts=2 sw=2: diff --git a/app/views/certificates/index.html.erb b/app/views/certificates/index.html.erb index af96ffb..c50ab12 100644 --- a/app/views/certificates/index.html.erb +++ b/app/views/certificates/index.html.erb @@ -15,8 +15,8 @@
<% @certificates.each do |certificate| %>