class Cryptorecord::Openpgpkey

Cryptorecord::Openpgpkey-class generates openphpkeys-dns-records. Instances must have an uid. The PGP-Key can be read from file @!attribute [r] uid

@return [Mail::Address] the userid or nil

@!attribute [r] key

@return [String] the pgp-key as a string

@!attribute [r] rectype

@return [String] "OPENPGPKEY"

Attributes

key[R]
rectype[R]
uid[R]

Public Class Methods

new(args = {}) click to toggle source

This constructor initializes uid and key by calling the setters. @see uid=

@param [Hash] args the options to initialize the object with @option args [String] uid email-address associated with the pgp-key @option args [String] key pgp-key

# File lib/cryptorecord/openpgpkey.rb, line 44
def initialize(args = {})
  self.uid = args.fetch(:uid, nil)
  self.key = args.fetch(:key, nil)
  @rectype = 'OPENPGPKEY'
end

Public Instance Methods

domain() click to toggle source

This getter returns the domain-part of the uid(email-address) or nil

@return [String] domain the domain-part of the keys uid(email-address)

# File lib/cryptorecord/openpgpkey.rb, line 86
def domain
  @uid.nil? ? nil : @uid.domain
end
key=(val) click to toggle source

This method sets the pgp-key. It takes the public-key-block and trims the header, blankline and checksum

@param [String] val PGP-Public-Key-Block(ASCII Armor)

as defined in rfc4880 section 6.2
# File lib/cryptorecord/openpgpkey.rb, line 95
def key=(val)
  return if val.nil?

  @key = ''
  val.split(/\n/).each do |x|
    @key += trimpgpkey(x).to_s
  end
  @key = @key.gsub(/=.{4}$/, '')
end
left() click to toggle source

This method returns the left-hand name of a dns-record @return [String] left-hand name of a dns-record

# File lib/cryptorecord/openpgpkey.rb, line 117
def left
  "#{localpart}._openpgpkey.#{domain}."
end
localpart() click to toggle source

This getter returns the SHA256sum of the uid-local-part(email-address) as defined in rfc7929

@return [String] the local-part of the keys

uid(email-address) as SHA256 reduced to 56bytes or nil
# File lib/cryptorecord/openpgpkey.rb, line 79
def localpart
  @uid.nil? ? nil : OpenSSL::Digest::SHA256.new(@uid.local.to_s).to_s[0..55]
end
read_file(keyfile) click to toggle source

This method reads the pgp-key from a given file

@param [String] keyfile Path to the keyfile @raise Cryptorecord::ArgumentError

# File lib/cryptorecord/openpgpkey.rb, line 109
def read_file(keyfile)
  raise Cryptorecord::ArgumentError, 'No keyfile defined' if keyfile.nil?
  data = File.read(keyfile)
  self.key = data
end
right() click to toggle source

This method returns the right-hand content of a dns-record @return [String] right-hand content of a dns-record

# File lib/cryptorecord/openpgpkey.rb, line 123
def right
  @key.to_s
end
to_s() click to toggle source

This method concats the openpgpkey-record

@return [String] openpgpkey dns-record as defined in rfc7929

# File lib/cryptorecord/openpgpkey.rb, line 130
def to_s
  "#{left} IN #{@rectype} #{right}"
end
uid=(val) click to toggle source

This setter takes the argument val to create a Mail::Address-object. The argument val can be a email-address-string or a Mail::Address-object. Make sure this is the proper uid for the pgp-key!

@param [String|Mail::Address] val The email-address without brackets @raise Cryptorecord::ArgumentError

# File lib/cryptorecord/openpgpkey.rb, line 56
def uid=(val)
  if val.nil?
    @uid = nil
    return
  end

  case val
  when String
    @uid = Mail::Address.new("<#{val}>")
  when Mail::Address
    @uid = Mail::Address.new("<#{val.address}>")
  else
    raise Cryptorecord::ArgumentError,
          "Unsupported datatype #{val.class} for val"
  end
end

Private Instance Methods

trimpgpkey(val) click to toggle source

This function trims the pgpkey so that all headers, footers, blanklines, and stuff are gone

@param [String] val onne line of the pgpkey-block

@return An empty string if something has to be trimmed, otherwise the line itself

# File lib/cryptorecord/openpgpkey.rb, line 144
def trimpgpkey(val)
  case val
  when '-----BEGIN PGP PUBLIC KEY BLOCK-----'
    ''
  when  '-----END PGP PUBLIC KEY BLOCK-----'
    ''
  when  /^\s*\n$/
    ''
  else
    val.to_s
  end
end