Sto provando a generare un certificato autofirmato in ruby, ma sono nei guai. Questo è quello che ho attualmente in questo momento:Ruby Genera certificato autofirmato
require 'openssl'
if ARGV.length != 3 then
puts "USAGE: #{__FILE__} <type[der|pem]> <private-out> <public-out>"
exit
end
type = ARGV[0].downcase
privateKeyFile = ARGV[1]
publicKeyFile = ARGV[2]
values = [{ 'C' => 'US'},
{'ST' => 'SomeState'},
{ 'L' => 'SomeCity'},
{ 'O' => 'Organization'},
{'OU' => 'Organizational Unit'},
{'CN' => "somesite.com"}]
name = values.collect{ |l| l.collect { |k, v| "/#{k}=#{v}" }.join }.join
key = OpenSSL::PKey::RSA.generate(1024)
pub = key.public_key
ca = OpenSSL::X509::Name.parse(name)
cert = OpenSSL::X509::Certificate.new
cert.version = 2
cert.serial = 1
cert.subject = ca
cert.issuer = ca
cert.public_key = pub
cert.not_before = Time.now
cert.not_before = Time.now + (360 * 24 * 3600)
File.open(privateKeyFile + "." + type, "w") {|f| f.write key.send("to_#{type}") }
File.open(publicKeyFile + "." + type, "w") {|f| f.write cert.send("to_#{type}") }
Quando provo ad usare la chiave privata generata e il certificato nel apache, ottengo questo errore:
[Thu Mar 04 10:58:44 2010] [error] Init: Unable to read server certificate from file /etc/ssl/certs/gnarly.pem
[Thu Mar 04 10:58:44 2010] [error] SSL Library Error: 218529960 error:0D0680A8:asn1 encoding routines:ASN1_CHECK_TLEN:wrong tag
[Thu Mar 04 10:58:44 2010] [error] SSL Library Error: 218595386 error:0D07803A:asn1 encoding routines:ASN1_ITEM_EX_D2I:nested asn1 error
Questo è ciò che dice il mio certificato:
-----BEGIN CERTIFICATE-----
<lots of stuff>
-----END CERTIFICATE-----
Si chiama un certificato invece di un CSR, che è ciò che la maggior parte delle cose che ho trovato online dicono di quell'errore apache2 (che avrei potuto ottenuto il CSR e CERT mescolato). La mia ipotesi è che non sto generando il giusto tipo di certificato. Forse devo cambiare gli attributi di serie o di versione. Inoltre, non sto facendo alcuna auto-firma da nessuna parte, non che io sappia comunque. So che si può fare qualcosa di simile però:
require "openssl"
key = OpenSSL::PKey::RSA.generate(1024)
signature = key.sign(OpenSSL::Digest::SHA1.new, "data to sign")
Promemoria: Il mio obiettivo è quello di generare un certificato auto-firmato, nel caso in cui la mia domanda a lungo ish perso concentrarsi sulla strada.
EDIT: Credo che il vero problema è come firmare un certificato con una chiave
Uno che saranno interessati ho preparato exammple csr in ruby, puoi trovarlo qui: https://gist.github.com/1922961 – mtfk