Commit a54d413e222d9a3fac65783b4ed0f566f2570e7b

Authored by Georg Hopp
1 parent d691b150

generate self signed key/cert pair and display the fingerprint

  1 +require 'openssl'
  2 +
1 3 class CertificatesController < ApplicationController
2 4 before_action :set_certificate, only: [:show, :edit, :update, :destroy]
3 5
... ... @@ -26,6 +28,22 @@ class CertificatesController < ApplicationController
26 28 def create
27 29 @certificate = Certificate.new(certificate_params)
28 30
  31 + key = OpenSSL::PKey::RSA.new 4096
  32 + name = OpenSSL::X509::Name.parse 'CN=lex-deeit/DC=weird-web-workers/DC=org'
  33 +
  34 + cert = OpenSSL::X509::Certificate.new
  35 + cert.version = 2
  36 + cert.serial = 0
  37 + cert.not_before = Time.now
  38 + cert.not_after = Time.now + 3600
  39 +
  40 + cert.public_key = key.public_key
  41 + cert.subject = name
  42 + cert.sign key, OpenSSL::Digest::SHA256.new
  43 +
  44 + @certificate.key = key.to_pem
  45 + @certificate.cert = cert.to_pem
  46 +
29 47 respond_to do |format|
30 48 if @certificate.save
31 49 format.html { redirect_to @certificate, notice: 'Certificate was successfully created.' }
... ... @@ -72,3 +90,5 @@ class CertificatesController < ApplicationController
72 90 params.require(:certificate).permit(:key, :cert, :active)
73 91 end
74 92 end
  93 +
  94 +# vim: set et ts=2 sw=2:
... ...
  1 +require "openssl"
  2 +require 'digest/md5'
  3 +
1 4 class Certificate < ActiveRecord::Base
  5 + def key
  6 + OpenSSL::PKey::RSA.new read_attribute(:key) if read_attribute(:key)
  7 + end
  8 +
  9 + def cert
  10 + OpenSSL::X509::Certificate.new read_attribute(:cert) if read_attribute(:cert)
  11 + end
  12 +
  13 + def key_fpr
  14 + Digest::SHA1.hexdigest(key.to_der).upcase
  15 + end
  16 +
  17 + def cert_fpr
  18 + Digest::SHA1.hexdigest(cert.to_der).upcase
  19 + end
2 20 end
  21 +# vim: set et ts=2 sw=2:
... ...
... ... @@ -15,8 +15,8 @@
15 15 <tbody>
16 16 <% @certificates.each do |certificate| %>
17 17 <tr>
18   - <td><%= certificate.key %></td>
19   - <td><%= certificate.cert %></td>
  18 + <td><%= certificate.key_fpr.scan(/../).join(':') %></td>
  19 + <td><%= certificate.cert_fpr.scan(/../).join(':') %></td>
20 20 <td><%= certificate.active %></td>
21 21 <td><%= link_to 'Show', certificate %></td>
22 22 <td><%= link_to 'Edit', edit_certificate_path(certificate) %></td>
... ...
Please register or login to post a comment