Sha256: 73dcf90d9bdf269e4a7212c5c98b2cf8c13fcf887bbf2003e3189c3acccce38f

Contents?: true

Size: 1.38 KB

Versions: 1

Compression:

Stored size: 1.38 KB

Contents

# frozen_string_literal: true

def check_crt(filename)
  %w[key crt].each {|ext|
    abort "#{filename}.#{ext} already exists, exiting" if File.exist? "#{filename}.#{ext}"
  }
end

def check_client(name)
  abort 'Error: client should have an alphanumeric name' unless name
  check_crt(name)
end

def exe(cmd)
  system(cmd) or abort "error executing: #{cmd}"
end

def gen_and_sign(type, certname, no_password)
  gen_key(certname, no_password)
  sign_key(type, certname, certname)
end

def gen_key(certname, no_password)
  if no_password
    exe "#{OPENSSL} genrsa -out '#{certname}.key' #{KEY_SIZE}"
  else
    exe "#{OPENSSL} genrsa -#{ENCRYPT} -out '#{certname}.key' #{KEY_SIZE}"
  end
end

def sign_key(type, certname, cn)
  if certname == 'ca'
    exe "#{OPENSSL} req -new -x509 -key '#{certname}.key' -out '#{certname}.crt' -config #{SSL_CONF} -subj '/CN=#{cn}#{REQ}' -extensions ext.#{type} -days #{CA_DAYS}"
  else
    exe "#{OPENSSL} req -new -key '#{certname}.key' -out '#{certname}.csr' -config #{SSL_CONF} -subj '/CN=#{cn}#{REQ}' -extensions ext.#{type}"
    exe "#{OPENSSL} ca -in '#{certname}.csr' -out '#{certname}.crt' -config #{SSL_CONF} -extensions ext.#{type} -batch"
    File.delete "#{certname}.csr"
  end
end

def gen_crl
  exe "#{OPENSSL} ca -gencrl -out crl.pem -config #{SSL_CONF}"
end

def create_dir(name)
  return if Dir.exist? name

  Dir.mkdir name
  puts "Created directory: #{name}"
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ovpn-key-0.7.7 lib/functions.rb