Class: OpenPGP::Packet

Inherits:
Object
  • Object
show all
Defined in:
lib/openpgp/packet.rb

Overview

OpenPGP packet.

See Also:

Direct Known Subclasses

Packet::AsymmetricSessionKey, Packet::CompressedData, Packet::EncryptedData, Packet::Experimental, Packet::IntegrityProtectedData, Packet::LiteralData, Packet::Marker, Packet::ModificationDetectionCode, Packet::OnePassSignature, Packet::PublicKey, Packet::Signature, Packet::SymmetricSessionKey, Packet::Trust, Packet::UserAttribute, Packet::UserID

Defined Under Namespace

Classes: AsymmetricSessionKey, CompressedData, EncryptedData, Experimental, IntegrityProtectedData, LiteralData, Marker, ModificationDetectionCode, OnePassSignature, PublicKey, PublicSubkey, SecretKey, SecretSubkey, Signature, SymmetricSessionKey, Trust, UserAttribute, UserID

Constant Summary

@@tags =
{
   1 => AsymmetricSessionKey,      # Public-Key Encrypted Session Key
   2 => Signature,                 # Signature Packet
   3 => SymmetricSessionKey,       # Symmetric-Key Encrypted Session Key Packet
   4 => OnePassSignature,          # One-Pass Signature Packet
   5 => SecretKey,                 # Secret-Key Packet
   6 => PublicKey,                 # Public-Key Packet
   7 => SecretSubkey,              # Secret-Subkey Packet
   8 => CompressedData,            # Compressed Data Packet
   9 => EncryptedData,             # Symmetrically Encrypted Data Packet
  10 => Marker,                    # Marker Packet
  11 => LiteralData,               # Literal Data Packet
  12 => Trust,                     # Trust Packet
  13 => UserID,                    # User ID Packet
  14 => PublicSubkey,              # Public-Subkey Packet
  17 => UserAttribute,             # User Attribute Packet
  18 => IntegrityProtectedData,    # Sym. Encrypted and Integrity Protected Data Packet
  19 => ModificationDetectionCode, # Modification Detection Code Packet
  60 => Experimental,              # Private or Experimental Values
  61 => Experimental,              # Private or Experimental Values
  62 => Experimental,              # Private or Experimental Values
  63 => Experimental,              # Private or Experimental Values
}

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Packet) initialize(options = {}, &block)

A new instance of Packet

Parameters:

  • (Hash{Symbol => Object}) options (defaults to: {})


103
104
105
106
# File 'lib/openpgp/packet.rb', line 103

def initialize(options = {}, &block)
  options.each { |k, v| send("#{k}=", v) }
  block.call(self) if block_given?
end

Instance Attribute Details

- (Object) data

Returns the value of attribute data



8
9
10
# File 'lib/openpgp/packet.rb', line 8

def data
  @data
end

- (Integer) size

Returns:

  • (Integer)


112
# File 'lib/openpgp/packet.rb', line 112

def size() body.size end

- (Object) tag

Returns the value of attribute tag



8
9
10
# File 'lib/openpgp/packet.rb', line 8

def tag
  @tag
end

Class Method Details

+ (Class) for(tag)

Returns the implementation class for a packet tag.

Parameters:

  • (Integer, #to_i) tag

Returns:

  • (Class)


15
16
17
# File 'lib/openpgp/packet.rb', line 15

def self.for(tag)
  @@tags[tag.to_i] || self
end

+ (Packet) parse(data)

Parses an OpenPGP packet.

Parameters:

Returns:

See Also:



33
34
35
36
37
38
39
40
41
# File 'lib/openpgp/packet.rb', line 33

def self.parse(data)
  data = Buffer.new(data.to_str) if data.respond_to?(:to_str)

  unless data.eof?
    new = ((tag = data.getbyte) & 64).nonzero? # bit 6 indicates new packet format if set
    data.ungetbyte(tag) rescue data.ungetc(tag.ord) # FIXME in backports/1.8.7
    send(new ? :parse_new_format : :parse_old_format, data)
  end
end

+ (Packet) parse_body(body, options = {})

Parameters:

  • (Buffer) body
  • (Hash{Symbol => Object}) options (defaults to: {})

Returns:



97
98
99
# File 'lib/openpgp/packet.rb', line 97

def self.parse_body(body, options = {})
  self.new(options)
end

+ (Packet) parse_new_format(data)

Parses a new-format (RFC 4880) OpenPGP packet.

Parameters:

Returns:

See Also:



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/openpgp/packet.rb', line 49

def self.parse_new_format(data)
  tag = data.getbyte & 63
  len = data.getbyte

  case len
    when 0..191   # 4.2.2.1. One-Octet Lengths
      data_length = len
    when 192..223 # 4.2.2.2. Two-Octet Lengths
      data_length = ((len - 192) << 8) + data.getbyte + 192
    when 224..254 # 4.2.2.4. Partial Body Lengths
      data_length = 1 << (len & 0x1f)
    when 255      # 4.2.2.3. Five-Octet Lengths
      data_length = (data.getbyte << 24) | (data.getbyte << 16) | (data.getbyte << 8) | data.getbyte
  end

  Packet.for(tag).parse_body(Buffer.new(data.read(data_length)), :tag => tag)
end

+ (Packet) parse_old_format(data)

Parses an old-format (PGP 2.6.x) OpenPGP packet.

Parameters:

Returns:

See Also:



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/openpgp/packet.rb', line 73

def self.parse_old_format(data)
  len = (tag = data.getbyte) & 3
  tag = (tag >> 2) & 15

  case len
    when 0 # The packet has a one-octet length. The header is 2 octets long.
      data_length = data.getbyte
    when 1 # The packet has a two-octet length. The header is 3 octets long.
      data_length = data.read(2).unpack('n').first
    when 2 # The packet has a four-octet length. The header is 5 octets long.
      data_length = data.read(4).unpack('N').first
    when 3 # The packet is of indeterminate length. The header is 1 octet long.
      data_length = false # read to EOF
    else
      raise "Invalid OpenPGP packet length-type: expected 0..3 but got #{len}"
  end

  Packet.for(tag).parse_body(Buffer.new(data_length ? data.read(data_length) : data.read), :tag => tag)
end

+ (Integer) tag

Returns the packet tag for this class.

Returns:

  • (Integer)


23
24
25
# File 'lib/openpgp/packet.rb', line 23

def self.tag
  @@tags.index(self)
end

Instance Method Details

- (String) body

Returns:

  • (String)


116
117
118
# File 'lib/openpgp/packet.rb', line 116

def body
  respond_to?(:write_body) ? Buffer.write { |buffer| write_body(buffer) } : ""
end