Skip to content

Commit

Permalink
Merge pull request #19 from asquelt/master
Browse files Browse the repository at this point in the history
shellsafe returns unsafe chars in 0.0.11
  • Loading branch information
duritong committed Dec 25, 2014
2 parents c0d2832 + 9d59c09 commit ec8593b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
23 changes: 20 additions & 3 deletions lib/trocla/formats/x509.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ def format(plain_password,options={})
else
raise "You need to pass \"subject\" or \"CN\" as an option to use this format"
end
hash = options['hash'] || 'sha2'
sign_with = options['ca'] || nil
keysize = options['keysize'] || 2048
serial = options['serial'] || 1
Expand All @@ -42,14 +43,14 @@ def format(plain_password,options={})
begin
subj = OpenSSL::X509::Name.parse(subject)
request = mkreq(subj, key.public_key)
request.sign(key, OpenSSL::Digest::SHA1.new)
request.sign(key, signature(hash))
rescue Exception => e
raise "Certificate request #{subject} creation failed: #{e.message}"
end

begin
csr_cert = mkcert(caserial, request.subject, ca, request.public_key, days, altnames)
csr_cert.sign(cakey, OpenSSL::Digest::SHA1.new)
csr_cert.sign(cakey, signature(hash))
setserial(sign_with, caserial)
rescue Exception => e
raise "Certificate #{subject} signing failed: #{e.message}"
Expand All @@ -60,7 +61,7 @@ def format(plain_password,options={})
begin
subj = OpenSSL::X509::Name.parse(subject)
cert = mkcert(serial, subj, nil, key.public_key, days, altnames)
cert.sign(key, OpenSSL::Digest::SHA1.new)
cert.sign(key, signature(hash))
rescue Exception => e
raise "Self-signed certificate #{subject} creation failed: #{e.message}"
end
Expand All @@ -72,6 +73,22 @@ def format(plain_password,options={})

# nice help: https://gist.github.com/mitfik/1922961

def signature(hash = 'sha2')
if hash == 'sha1'
OpenSSL::Digest::SHA1.new
elsif hash == 'sha224'
OpenSSL::Digest::SHA224.new
elsif hash == 'sha2' || hash == 'sha256'
OpenSSL::Digest::SHA256.new
elsif hash == 'sha384'
OpenSSL::Digest::SHA384.new
elsif hash == 'sha512'
OpenSSL::Digest::SHA512.new
else
raise "Unrecognized hash: #{hash}"
end
end

def mkkey(len)
OpenSSL::PKey::RSA.generate(len)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/trocla/util.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def chars
@chars ||= shellsafe + special_chars
end
def shellsafe
@chars ||= alphanumeric + shellsafe_chars
@shellsafe ||= alphanumeric + shellsafe_chars
end
def alphanumeric
@alphanumeric ||= ('a'..'z').to_a + ('A'..'Z').to_a + ('0'..'9').to_a
Expand Down

0 comments on commit ec8593b

Please sign in to comment.