Class: R509::Spki

Inherits:
Object
  • Object
show all
Includes:
IOHelpers
Defined in:
lib/r509/spki.rb

Overview

class for handling SPKAC/SPKI requests (typically generated by the <keygen> tag

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Methods included from IOHelpers

#read_data, read_data, #write_data, write_data

Constructor Details

- (Spki) initialize(opts = {})

you can also pass OIDs (see tests)

Examples:

['CN','langui.sh'],,['L','Chicago'],,['emailAddress','ca@langui.sh']


    
  

Parameters:

  • opts (Hash) (defaults to: {})

    a customizable set of options

Options Hash (opts):

  • :spki (String, OpenSSL::Netscape::SPKI)

    the spki you want to parse

  • :subject (R509::Subject, Array, OpenSSL::X509::Name)

    array of subject items

  • :san_names (Array)

    array of SAN names



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/r509/spki.rb', line 16

def initialize(opts={})
    if not opts.kind_of?(Hash)
        raise ArgumentError, 'Must provide a hash of options'
    end
    if opts.has_key?(:spki) and not opts.has_key?(:subject)
        raise ArgumentError, "Must provide both spki and subject"
    end
    if opts.has_key?(:san_names) and not opts[:san_names].kind_of?(Array)
        raise ArgumentError, "if san_names are provided they must be in an Array"
    end
    @spki = OpenSSL::Netscape::SPKI.new(opts[:spki].sub("SPKAC=",""))
    @subject = R509::Subject.new(opts[:subject])
    @san_names = opts[:san_names] || []
end

Instance Attribute Details

- (Object) san_names (readonly)

Returns the value of attribute san_names



10
11
12
# File 'lib/r509/spki.rb', line 10

def san_names
  @san_names
end

- (Object) spki (readonly)

Returns the value of attribute spki



10
11
12
# File 'lib/r509/spki.rb', line 10

def spki
  @spki
end

- (Object) subject (readonly)

Returns the value of attribute subject



10
11
12
# File 'lib/r509/spki.rb', line 10

def subject
  @subject
end

Instance Method Details

- (Integer) bit_strength

Returns the bit strength of the key used to create the SPKI

Returns:

  • (Integer)

    the integer bit strength.



84
85
86
87
88
89
90
# File 'lib/r509/spki.rb', line 84

def bit_strength
    if self.rsa?
        return @spki.public_key.n.num_bits
    elsif self.dsa?
        return @spki.public_key.p.num_bits
    end
end

- (Boolean) dsa?

Returns whether the public key is DSA

Returns:

  • (Boolean)

    true if the public key is DSA, false otherwise



78
79
80
# File 'lib/r509/spki.rb', line 78

def dsa?
    @spki.public_key.kind_of?(OpenSSL::PKey::DSA)
end

- (String) key_algorithm

Returns key algorithm (RSA/DSA)

Returns:

  • (String)

    value of the key algorithm. RSA or DSA



95
96
97
98
99
100
101
# File 'lib/r509/spki.rb', line 95

def key_algorithm
    if @spki.public_key.kind_of? OpenSSL::PKey::RSA then
        'RSA'
    elsif @spki.public_key.kind_of? OpenSSL::PKey::DSA then
        'DSA'
    end
end

- (OpenSSL::PKey::RSA) public_key

Public key

Returns:

  • (OpenSSL::PKey::RSA)

    public key



32
33
34
# File 'lib/r509/spki.rb', line 32

def public_key
    @spki.public_key
end

- (Boolean) rsa?

Returns whether the public key is RSA

Returns:

  • (Boolean)

    true if the public key is RSA, false otherwise



71
72
73
# File 'lib/r509/spki.rb', line 71

def rsa?
    @spki.public_key.kind_of?(OpenSSL::PKey::RSA)
end

- (String) to_der

Converts the SPKI into the DER format

Returns:

  • (String)

    the SPKI converted into DER format.



48
49
50
# File 'lib/r509/spki.rb', line 48

def to_der
    @spki.to_der
end

- (Hash) to_hash

Returns a hash structure you can pass to the Ca You will want to call this method if you intend to alter the values and then pass them to the Ca class.

Returns:

  • (Hash)

    :subject and :san_names you can pass to Ca



108
109
110
# File 'lib/r509/spki.rb', line 108

def to_hash
    { :subject => @subject.dup , :san_names => @san_names.dup }
end

- (String) to_pem Also known as: to_s

Converts the SPKI into the PEM format

Returns:

  • (String)

    the SPKI converted into PEM format.



39
40
41
# File 'lib/r509/spki.rb', line 39

def to_pem
    @spki.to_pem
end

- (Object) write_der(filename_or_io)

Writes the SPKI into the DER format

Parameters:

  • filename_or_io (String, #write)

    Either a string of the path for the file that you'd like to write, or an IO-like object.



64
65
66
# File 'lib/r509/spki.rb', line 64

def write_der(filename_or_io)
    write_data(filename_or_io, @spki.to_der)
end

- (Object) write_pem(filename_or_io)

Writes the SPKI into the PEM format

Parameters:

  • filename_or_io (String, #write)

    Either a string of the path for the file that you'd like to write, or an IO-like object.



56
57
58
# File 'lib/r509/spki.rb', line 56

def write_pem(filename_or_io)
    write_data(filename_or_io, @spki.to_pem)
end