Class: Cryptorecord::Openpgpkey

Inherits:
Object
  • Object
show all
Defined in:
lib/cryptorecord/openpgpkey.rb

Overview

Cryptorecord::Openpgpkey-class generates openphpkeys-dns-records. Instances must have an uid. The PGP-Key can be read from file

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ Openpgpkey

This constructor initializes uid and key by calling the setters.

Parameters:

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

    the options to initialize the object with

Options Hash (args):

  • uid (String)

    email-address associated with the pgp-key

  • key (String)

    pgp-key

See Also:



44
45
46
47
48
# 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

Instance Attribute Details

#keyString

Returns the pgp-key as a string.

Returns:

  • (String)

    the pgp-key as a string



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/cryptorecord/openpgpkey.rb', line 35

class Openpgpkey
  attr_reader :uid, :key, :rectype

  # 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
  def initialize(args = {})
    self.uid = args.fetch(:uid, nil)
    self.key = args.fetch(:key, nil)
    @rectype = 'OPENPGPKEY'
  end

  # 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
  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

  # 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
  def localpart
    @uid.nil? ? nil : OpenSSL::Digest::SHA256.new(@uid.local.to_s).to_s[0..55]
  end

  # 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)
  def domain
    @uid.nil? ? nil : @uid.domain
  end

  # 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
  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

  # This method reads the pgp-key from a given file
  #
  # @param [String] keyfile Path to the keyfile
  # @raise Cryptorecord::ArgumentError
  def read_file(keyfile)
    raise Cryptorecord::ArgumentError, 'No keyfile defined' if keyfile.nil?
    data = File.read(keyfile)
    self.key = data
  end

  # This method returns the left-hand name of a dns-record
  # @return [String] left-hand name of a dns-record
  def left
    "#{localpart}._openpgpkey.#{domain}."
  end

  # This method returns the right-hand content of a dns-record
  # @return [String] right-hand content of a dns-record
  def right
    @key.to_s
  end

  # This method concats the openpgpkey-record
  #
  # @return [String] openpgpkey dns-record as defined in rfc7929
  def to_s
    "#{left} IN #{@rectype} #{right}"
  end

  private

  # 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
  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
end

#rectypeString (readonly)

Returns “OPENPGPKEY”.

Returns:

  • (String)

    “OPENPGPKEY”



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/cryptorecord/openpgpkey.rb', line 35

class Openpgpkey
  attr_reader :uid, :key, :rectype

  # 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
  def initialize(args = {})
    self.uid = args.fetch(:uid, nil)
    self.key = args.fetch(:key, nil)
    @rectype = 'OPENPGPKEY'
  end

  # 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
  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

  # 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
  def localpart
    @uid.nil? ? nil : OpenSSL::Digest::SHA256.new(@uid.local.to_s).to_s[0..55]
  end

  # 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)
  def domain
    @uid.nil? ? nil : @uid.domain
  end

  # 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
  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

  # This method reads the pgp-key from a given file
  #
  # @param [String] keyfile Path to the keyfile
  # @raise Cryptorecord::ArgumentError
  def read_file(keyfile)
    raise Cryptorecord::ArgumentError, 'No keyfile defined' if keyfile.nil?
    data = File.read(keyfile)
    self.key = data
  end

  # This method returns the left-hand name of a dns-record
  # @return [String] left-hand name of a dns-record
  def left
    "#{localpart}._openpgpkey.#{domain}."
  end

  # This method returns the right-hand content of a dns-record
  # @return [String] right-hand content of a dns-record
  def right
    @key.to_s
  end

  # This method concats the openpgpkey-record
  #
  # @return [String] openpgpkey dns-record as defined in rfc7929
  def to_s
    "#{left} IN #{@rectype} #{right}"
  end

  private

  # 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
  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
end

#uidMail::Address

Returns the userid or nil.

Returns:

  • (Mail::Address)

    the userid or nil



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/cryptorecord/openpgpkey.rb', line 35

class Openpgpkey
  attr_reader :uid, :key, :rectype

  # 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
  def initialize(args = {})
    self.uid = args.fetch(:uid, nil)
    self.key = args.fetch(:key, nil)
    @rectype = 'OPENPGPKEY'
  end

  # 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
  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

  # 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
  def localpart
    @uid.nil? ? nil : OpenSSL::Digest::SHA256.new(@uid.local.to_s).to_s[0..55]
  end

  # 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)
  def domain
    @uid.nil? ? nil : @uid.domain
  end

  # 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
  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

  # This method reads the pgp-key from a given file
  #
  # @param [String] keyfile Path to the keyfile
  # @raise Cryptorecord::ArgumentError
  def read_file(keyfile)
    raise Cryptorecord::ArgumentError, 'No keyfile defined' if keyfile.nil?
    data = File.read(keyfile)
    self.key = data
  end

  # This method returns the left-hand name of a dns-record
  # @return [String] left-hand name of a dns-record
  def left
    "#{localpart}._openpgpkey.#{domain}."
  end

  # This method returns the right-hand content of a dns-record
  # @return [String] right-hand content of a dns-record
  def right
    @key.to_s
  end

  # This method concats the openpgpkey-record
  #
  # @return [String] openpgpkey dns-record as defined in rfc7929
  def to_s
    "#{left} IN #{@rectype} #{right}"
  end

  private

  # 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
  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
end

Instance Method Details

#domainString

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

Returns:

  • (String)

    domain the domain-part of the keys uid(email-address)



86
87
88
# File 'lib/cryptorecord/openpgpkey.rb', line 86

def domain
  @uid.nil? ? nil : @uid.domain
end

#leftString

This method returns the left-hand name of a dns-record

Returns:

  • (String)

    left-hand name of a dns-record



117
118
119
# File 'lib/cryptorecord/openpgpkey.rb', line 117

def left
  "#{localpart}._openpgpkey.#{domain}."
end

#localpartString

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

Returns:

  • (String)

    the local-part of the keys uid(email-address) as SHA256 reduced to 56bytes or nil



79
80
81
# 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) ⇒ Object

This method reads the pgp-key from a given file

Parameters:

  • keyfile (String)

    Path to the keyfile

Raises:

  • Cryptorecord::ArgumentError



109
110
111
112
113
# 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

#rightString

This method returns the right-hand content of a dns-record

Returns:

  • (String)

    right-hand content of a dns-record



123
124
125
# File 'lib/cryptorecord/openpgpkey.rb', line 123

def right
  @key.to_s
end

#to_sString

This method concats the openpgpkey-record

Returns:

  • (String)

    openpgpkey dns-record as defined in rfc7929



130
131
132
# File 'lib/cryptorecord/openpgpkey.rb', line 130

def to_s
  "#{left} IN #{@rectype} #{right}"
end