Class: CyberplatPKI::KeyPacket

Inherits:
Packet
  • Object
show all
Defined in:
lib/cyberplat_pki/key_packet.rb

Direct Known Subclasses

PrivateKeyPacket

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Packet

save

Instance Attribute Details

#algorithmObject

Returns the value of attribute algorithm.



5
6
7
# File 'lib/cyberplat_pki/key_packet.rb', line 5

def algorithm
  @algorithm
end

#keyObject

Returns the value of attribute key.



5
6
7
# File 'lib/cyberplat_pki/key_packet.rb', line 5

def key
  @key
end

#serialObject

Returns the value of attribute serial.



5
6
7
# File 'lib/cyberplat_pki/key_packet.rb', line 5

def serial
  @serial
end

#timestampObject

Returns the value of attribute timestamp.



5
6
7
# File 'lib/cyberplat_pki/key_packet.rb', line 5

def timestamp
  @timestamp
end

#valid_daysObject

Returns the value of attribute valid_days.



5
6
7
# File 'lib/cyberplat_pki/key_packet.rb', line 5

def valid_days
  @valid_days
end

Class Method Details

.load(io, context) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/cyberplat_pki/key_packet.rb', line 7

def self.load(io, context)
  version = io.readbyte

  # RFC4880 says:
  #
  # V3 keys are deprecated.  They contain three weaknesses.  First, it is
  # relatively easy to construct a V3 key that has the same Key ID as any
  # other key because the Key ID is simply the low 64 bits of the public
  # modulus.  Secondly, because the fingerprint of a V3 key hashes the
  # key material, but not its length, there is an increased opportunity
  # for fingerprint collisions.  Third, there are weaknesses in the MD5
  # hash algorithm that make developers prefer other algorithms.  See
  # below for a fuller discussion of Key IDs and fingerprints.
  #
  # Beware.

  raise "CyberplatPKI: CRYPT_ERR_INVALID_PACKET_FORMAT (unsupported key version: #{version})" if version != 0x03

  key = KeyPacket.new

  key.serial, key.timestamp, key.valid_days, algorithm = io.read(11).unpack "NNnC"

  raise "CyberplatPKI: CRYPT_ERR_INVALID_PACKET_FORMAT (unsupported algorithm #{algorithm})" if algorithm != 1

  key.key = OpenSSL::PKey::RSA.new
  key.key.n = io.read_mpi
  key.key.e = io.read_mpi

  key
end

Instance Method Details

#save(io, context) ⇒ Object



38
39
40
41
42
43
# File 'lib/cyberplat_pki/key_packet.rb', line 38

def save(io, context)
  io.write [ 3, serial, timestamp, valid_days, 1 ].pack("CNNnC")

  io.write_mpi key.n
  io.write_mpi key.e
end