2017-03-20 23:26:57 +00:00
|
|
|
# encoding: utf-8
|
|
|
|
|
|
|
|
require 'openssl'
|
|
|
|
require 'hashie/mash'
|
2018-03-22 12:25:45 +00:00
|
|
|
require 'utils/file_reader'
|
2017-03-20 23:26:57 +00:00
|
|
|
|
|
|
|
module Inspec::Resources
|
|
|
|
class RsaKey < Inspec.resource(1)
|
|
|
|
name 'key_rsa'
|
2018-02-19 14:26:49 +00:00
|
|
|
supports platform: 'unix'
|
|
|
|
supports platform: 'windows'
|
2017-03-20 23:26:57 +00:00
|
|
|
desc 'public/private RSA key pair test'
|
|
|
|
example "
|
2017-12-04 16:59:30 +00:00
|
|
|
describe key_rsa('/etc/pki/www.mywebsite.com.key') do
|
2017-03-20 23:26:57 +00:00
|
|
|
its('public_key') { should match /BEGIN RSA PUBLIC KEY/ }
|
|
|
|
end
|
|
|
|
|
2017-12-04 16:59:30 +00:00
|
|
|
describe key_rsa('/etc/pki/www.mywebsite.com.key', 'passphrase') do
|
2017-03-20 23:26:57 +00:00
|
|
|
it { should be_private }
|
|
|
|
it { should be_public }
|
|
|
|
end
|
|
|
|
"
|
|
|
|
|
2018-03-22 12:25:45 +00:00
|
|
|
include FileReader
|
|
|
|
|
2017-03-20 23:26:57 +00:00
|
|
|
def initialize(keypath, passphrase = nil)
|
|
|
|
@key_path = keypath
|
|
|
|
@passphrase = passphrase
|
2018-03-22 12:25:45 +00:00
|
|
|
@key = OpenSSL::PKey.read(read_file_content(@key_path, allow_empty: true), @passphrase)
|
2017-03-20 23:26:57 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def public?
|
|
|
|
return if @key.nil?
|
|
|
|
@key.public?
|
|
|
|
end
|
|
|
|
|
|
|
|
def public_key
|
|
|
|
return if @key.nil?
|
|
|
|
@key.public_key.to_s
|
|
|
|
end
|
|
|
|
|
|
|
|
def private?
|
|
|
|
return if @key.nil?
|
|
|
|
@key.private?
|
|
|
|
end
|
|
|
|
|
|
|
|
def private_key
|
|
|
|
return if @key.nil?
|
|
|
|
@key.to_s
|
|
|
|
end
|
|
|
|
|
|
|
|
def key_length
|
|
|
|
return if @key.nil?
|
|
|
|
@key.public_key.n.num_bytes * 8
|
|
|
|
end
|
|
|
|
|
|
|
|
def to_s
|
|
|
|
"rsa_key #{@key_path}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|