Module: CyberplatPKI::PacketIORoutines
- Defined in:
- lib/cyberplat_pki/packet_io_routines.rb
Instance Attribute Summary collapse
-
#checksum ⇒ Object
Returns the value of attribute checksum.
-
#cipher ⇒ Object
Returns the value of attribute cipher.
Instance Method Summary collapse
- #read_mpi ⇒ Object
- #read_packet(password = nil) ⇒ Object
- #write_mpi(bn) ⇒ Object
- #write_packet(packet, password = nil) ⇒ Object
Instance Attribute Details
#checksum ⇒ Object
Returns the value of attribute checksum.
6 7 8 |
# File 'lib/cyberplat_pki/packet_io_routines.rb', line 6 def checksum @checksum end |
#cipher ⇒ Object
Returns the value of attribute cipher.
6 7 8 |
# File 'lib/cyberplat_pki/packet_io_routines.rb', line 6 def cipher @cipher end |
Instance Method Details
#read_mpi ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/cyberplat_pki/packet_io_routines.rb', line 65 def read_mpi header = read(2) mpi_bits, = header.unpack("n") data = read((mpi_bits + 7) / 8) if !cipher.nil? cipher.resync data = cipher.decrypt data end add_checksum header add_checksum data OpenSSL::BN.new data, 2 end |
#read_packet(password = nil) ⇒ Object
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 37 |
# File 'lib/cyberplat_pki/packet_io_routines.rb', line 8 def read_packet(password = nil) header = readbyte raise "CyberplatPKI: CRYPT_ERR_INVALID_PACKET_FORMAT (invalid packet header)" if (header & 0xC0) != 0x80 case header & 3 when 0 data_length = readbyte when 1 data_length, = read(2).unpack 'n' when 2 data_length, = read(4).unpack 'N' else raise "CyberplatPKI: CRYPT_ERR_INVALID_PACKET_FORMAT (invalid packet length type: #{header % 3}" end packet_type = (header >> 2) & 0x0F data = read data_length packet_class = PACKET_TYPES[packet_type] raise "CyberplatPKI: CRYPT_ERR_INVALID_PACKET_FORMAT (unsupported packet type: #{packet_type})" if packet_class.nil? StringIO.open(data, "rb") do |io| io.extend PacketIORoutines packet_class.load io, context_for_password(password) end end |
#write_mpi(bn) ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/cyberplat_pki/packet_io_routines.rb', line 82 def write_mpi(bn) header = [ bn.num_bits ].pack("n") data = bn.to_s 2 add_checksum header add_checksum data write header if !cipher.nil? cipher.resync data = cipher.encrypt data end write data end |
#write_packet(packet, password = nil) ⇒ Object
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 |
# File 'lib/cyberplat_pki/packet_io_routines.rb', line 39 def write_packet(packet, password = nil) io = StringIO.open ''.encode('BINARY'), 'wb' io.extend PacketIORoutines packet.save io, context_for_password(password) data = io.string packet_type = (PACKET_TYPES.key(packet.class) << 2) | 0x80 if data.length <= 0xFF && !packet.kind_of?(SignaturePacket) && !packet.kind_of?(KeyPacket) putc packet_type putc data.length elsif data.length <= 0xFFFF putc packet_type | 1 write [ data.length ].pack("n") else putc packet_type | 2 write [ data.length ].pack("N") end write data end |