Permalink
Browse files

Initial revision

  • Loading branch information...
1 parent d684f45 commit 74dd04f7d06a9282b02b161cc0dd68d576f657b0 @nahi committed Oct 30, 2004
Showing with 5,212 additions and 0 deletions.
  1. +6 −0 bin/pgpdump.rb
  2. +160 −0 lib/pgp/armor.rb
  3. +61 −0 lib/pgp/compressionalgorithm.rb
  4. +86 −0 lib/pgp/hashalgorithm.rb
  5. +40 −0 lib/pgp/hexdump.rb
  6. +32 −0 lib/pgp/indentdumpport.rb
  7. +89 −0 lib/pgp/mpi.rb
  8. +40 −0 lib/pgp/packet.rb
  9. +57 −0 lib/pgp/packet/compresseddata.rb
  10. +136 −0 lib/pgp/packet/dsakeysupport.rb
  11. +134 −0 lib/pgp/packet/elgamalkeysupport.rb
  12. +24 −0 lib/pgp/packet/key.rb
  13. +83 −0 lib/pgp/packet/literaldata.rb
  14. +44 −0 lib/pgp/packet/marker.rb
  15. +227 −0 lib/pgp/packet/packet.rb
  16. +39 −0 lib/pgp/packet/publickey.rb
  17. +15 −0 lib/pgp/packet/publickeydsa.rb
  18. +15 −0 lib/pgp/packet/publickeyelgamal.rb
  19. +101 −0 lib/pgp/packet/publickeyencryptedsessionkey.rb
  20. +15 −0 lib/pgp/packet/publickeyencryptedsessionkeyrsa.rb
  21. +15 −0 lib/pgp/packet/publickeyrsa.rb
  22. +100 −0 lib/pgp/packet/publickeysupport.rb
  23. +58 −0 lib/pgp/packet/publicsubkey.rb
  24. +15 −0 lib/pgp/packet/publicsubkeydsa.rb
  25. +15 −0 lib/pgp/packet/publicsubkeyelgamal.rb
  26. +15 −0 lib/pgp/packet/publicsubkeyrsa.rb
  27. +143 −0 lib/pgp/packet/rsakeysupport.rb
  28. +49 −0 lib/pgp/packet/s2ksupport.rb
  29. +39 −0 lib/pgp/packet/secretkey.rb
  30. +15 −0 lib/pgp/packet/secretkeydsa.rb
  31. +15 −0 lib/pgp/packet/secretkeyelgamal.rb
  32. +15 −0 lib/pgp/packet/secretkeyrsa.rb
  33. +102 −0 lib/pgp/packet/secretkeysupport.rb
  34. +49 −0 lib/pgp/packet/secretsubkey.rb
  35. +15 −0 lib/pgp/packet/secretsubkeydsa.rb
  36. +15 −0 lib/pgp/packet/secretsubkeyelgamal.rb
  37. +15 −0 lib/pgp/packet/secretsubkeyrsa.rb
  38. +347 −0 lib/pgp/packet/signature.rb
  39. +14 −0 lib/pgp/packet/sigsubpacket.rb
  40. +45 −0 lib/pgp/packet/sigsubpacket/creationtime.rb
  41. +45 −0 lib/pgp/packet/sigsubpacket/expirationtime.rb
  42. +56 −0 lib/pgp/packet/sigsubpacket/features.rb
  43. +40 −0 lib/pgp/packet/sigsubpacket/internal.rb
  44. +48 −0 lib/pgp/packet/sigsubpacket/issuerkeyid.rb
  45. +47 −0 lib/pgp/packet/sigsubpacket/keyexpirationtime.rb
  46. +120 −0 lib/pgp/packet/sigsubpacket/keyflags.rb
  47. +53 −0 lib/pgp/packet/sigsubpacket/keyserverpreferences.rb
  48. +154 −0 lib/pgp/packet/sigsubpacket/packet.rb
  49. +56 −0 lib/pgp/packet/sigsubpacket/preferredcompressionalgorithm.rb
  50. +56 −0 lib/pgp/packet/sigsubpacket/preferredhashalgorithm.rb
  51. +56 −0 lib/pgp/packet/sigsubpacket/preferredskeyalgorithm.rb
  52. +49 −0 lib/pgp/packet/sigsubpacket/primaryuserid.rb
  53. +65 −0 lib/pgp/packet/sigsubpacket/reasonforrevocation.rb
  54. +57 −0 lib/pgp/packet/sigsubpacket/revocable.rb
  55. +124 −0 lib/pgp/packet/support.rb
  56. +289 −0 lib/pgp/packet/symencryptedintegrityprotecteddata.rb
  57. +42 −0 lib/pgp/packet/trust.rb
  58. +63 −0 lib/pgp/packet/userattribute.rb
  59. +6 −0 lib/pgp/packet/userattrsubpacket.rb
  60. +79 −0 lib/pgp/packet/userattrsubpacket/image.rb
  61. +76 −0 lib/pgp/packet/userattrsubpacket/packet.rb
  62. +41 −0 lib/pgp/packet/userid.rb
  63. +192 −0 lib/pgp/pkeyalgorithm.rb
  64. +55 −0 lib/pgp/port.rb
  65. +60 −0 lib/pgp/s2kspecifier.rb
  66. +146 −0 lib/pgp/skeyalgorithm.rb
  67. +48 −0 lib/pgp/util.rb
  68. +133 −0 test/pgp/packet/test_publickeyrsa.rb
  69. +73 −0 test/pgp/packet/test_secretkeyrsa.rb
  70. +84 −0 test/pgp/packet/test_signature.rb
  71. +128 −0 test/pgp/packet/test_symencryptedintegrityprotecteddata.rb
  72. +116 −0 test/pgp/packet/testbase.rb
  73. +25 −0 test/pgp/test_armor.rb
  74. +21 −0 test/pgp/test_mpi.rb
  75. +9 −0 test/runner.rb
View
@@ -0,0 +1,6 @@
+#!/usr/bin/env ruby
+
+require 'pgp/packet'
+require 'pp'
+
+PGP::Packet::Packet.scan(ARGF)
View
@@ -0,0 +1,160 @@
+require 'pgp/util'
+
+
+module PGP
+
+
+class Armor
+ TYPES = {
+ :MESSAGE => "MESSAGE",
+ :PUBLIC_KEY_BLOCK => "PUBLIC KEY BLOCK",
+ :PRIVATE_KEY_BLOCK => "PRIVATE KEY BLOCK",
+ :SIGNATURE => "SIGNATURE"
+ }
+
+ def initialize(src = nil)
+ @type = nil
+ @header = nil
+ @body = nil
+ @checksum = nil
+ if src
+ if (src[0] & 0b0_1000_0000).nonzero?
+ self.body = src
+ else
+ parse(src)
+ end
+ end
+ end
+
+ def type
+ @type
+ end
+
+ def type=(type)
+ type = type.intern if type.respond_to?(:intern)
+ unless TYPES.key?(type)
+ raise "Unknown type: " + type.to_s
+ end
+ @type = type
+ end
+
+ def header=(header)
+ @header = header
+ end
+
+ def body
+ @body
+ end
+
+ def body=(body)
+ @body = body
+ calc_checksum
+ end
+
+ def dump
+ check_dump
+ dump_header_line + dump_header + dump_blank + dump_body + dump_checksum +
+ dump_tail
+ end
+
+ def self.parse(src)
+ Armor.new(src)
+ end
+
+private
+
+ def parse(src)
+ status = :header_line
+ bodysrc = ""
+ src.each do |line|
+ line.sub!(/\r?\n\z/, '')
+ case status
+ when :header_line
+ if /^-----BEGIN PGP ([^-]+)-----$/ =~ line
+ self.type = $1.gsub(/ /, '_')
+ status = :header
+ else
+ # skip this line
+ end
+ when :header
+ if /^\S*$/ =~ line
+ status = :body
+ end
+ when :body
+ if /^=(....)$/ =~ line
+ checksum = $1.unpack("m*")[0]
+ self.body = bodysrc.unpack("m*")[0]
+ if checksum != @checksum
+ raise "Illegal checksum: #{@checksum} expected: #{checksum}"
+ end
+ status = :tail_line
+ else
+ bodysrc << line
+ end
+ when :tail_line
+ if /^-----END PGP ([^-]+)-----$/ =~ line
+ if self.type != ($1.gsub(/ /, '_')).intern
+ raise "Illegal tail format definition"
+ end
+ return # ignore trailing lines
+ else
+ raise "Tail line not found"
+ end
+ else
+ raise "Illegal state"
+ end
+ end
+ raise "Parsing failed: not an armor format?"
+ end
+
+ def check_dump
+ raise "Not initialized" if @type.nil? or @body.nil? or @checksum.nil?
+ end
+
+ def dump_header_line
+ "-----BEGIN PGP " + TYPES[@type] + "-----\n"
+ end
+
+ def dump_header
+ @header ? @header.sub(/[\r\n]*\z/, "\n") : ""
+ end
+
+ def dump_blank
+ "\n"
+ end
+
+ def dump_body
+ [@body].pack("m50")
+ end
+
+ def dump_checksum
+ "=" + [@checksum].pack("m*")
+ end
+
+ def dump_tail
+ "-----END PGP " + TYPES[@type] + "-----\n"
+ end
+
+ def calc_checksum
+ @checksum = [Util.crc24(@body)].pack("N")[1, 3]
+ end
+end
+
+
+end
+
+
+if __FILE__ == $0
+ src = <<EOP
+-----BEGIN PGP MESSAGE-----
+Version: OpenPrivacy 0.99
+
+yDgBO22WxBHv7O8X7O/jygAEzol56iUKiXmV+XmpCtmpqQUKiQrFqclFqUDBovzS
+vBSFjNSiVHsuAA==
+=njUN
+-----END PGP MESSAGE-----
+EOP
+ include PGP
+ d = Armor.new(src)
+ p d
+end
@@ -0,0 +1,61 @@
+module PGP
+
+
+module CompressionAlgorithm
+ ALGORITHMS = {
+ 0 => "Uncompressed",
+ 1 => "ZIP (RFC1951)",
+ 2 => "ZLIB (RFC1950)",
+ 3 => "BZip2",
+ 100 => "Private/Experimental algorithm",
+ 101 => "Private/Experimental algorithm",
+ 102 => "Private/Experimental algorithm",
+ 103 => "Private/Experimental algorithm",
+ 104 => "Private/Experimental algorithm",
+ 105 => "Private/Experimental algorithm",
+ 106 => "Private/Experimental algorithm",
+ 107 => "Private/Experimental algorithm",
+ 108 => "Private/Experimental algorithm",
+ 109 => "Private/Experimental algorithm",
+ 110 => "Private/Experimental algorithm",
+ }
+
+ def self.include?(algorithm)
+ ALGORITHMS.key?(algorithm)
+ end
+
+ def self.label(algorithm)
+ ALGORITHMS[algorithm]
+ end
+
+ def self.dump_summary(algorithm)
+ "Compression algorithm - #{label(algorithm)}(#{algorithm})"
+ end
+
+ def self.compress(algorithm, data)
+ case algorithm
+ when 0
+ data
+ when 1
+ raise "ToDo"
+ else
+ raise "Not supported"
+ end
+ end
+
+ def self.decompress(algorithm, data)
+ case algorithm
+ when 0
+ data
+ when 1
+ require 'zlib'
+ z = Zlib::Inflate.new(-15)
+ z.inflate(data) + z.finish
+ else
+ raise "Not supported"
+ end
+ end
+end
+
+
+end
View
@@ -0,0 +1,86 @@
+require 'digest/md5'
+require 'digest/sha1'
+
+
+module PGP
+
+
+module HashAlgorithm
+ ALGORITHMS = {
+ # MD5: 1.2.840.113549.2.5
+ 1 => ["MD5", 16, [0x30, 0x20, 0x30, 0x0C, 0x06, 0x08, 0x2A, 0x86, 0x48,
+ 0x86, 0xF7, 0x0D, 0x02, 0x05, 0x05, 0x00, 0x04, 0x10]],
+ # SHA-1: 1.3.14.3.2.26
+ 2 => ["SHA-1", 20, [0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0E, 0x03,
+ 0x02, 0x1A, 0x05, 0x00, 0x04, 0x14]],
+ # RIPEMD-160: 1.3.36.3.2.1
+ 3 => ["RIPEMD-160", 20, [0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2B, 0x24,
+ 0x03, 0x02, 0x01, 0x05, 0x00, 0x04, 0x14]],
+ # SHA256: 2.16.840.1.101.3.4.2.1
+ 8 => ["SHA256", 32, [0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48,
+ 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05, 0x00, 0x04, 0x20]],
+ # SHA384: 2.16.840.1.101.3.4.2.2
+ 9 => ["SHA384", 48, [0x30, 0x41, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48,
+ 0x01, 0x65, 0x03, 0x04, 0x02, 0x02, 0x05, 0x00, 0x04, 0x30]],
+ # SHA512:2.16.840.1.101.3.4.2.3
+ 10 => ["SHA512", 64, [0x30, 0x51, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48,
+ 0x01, 0x65, 0x03, 0x04, 0x02, 0x03, 0x05, 0x00, 0x04, 0x40]],
+ 100 => ["Private/Experimental algorithm"],
+ 101 => ["Private/Experimental algorithm"],
+ 102 => ["Private/Experimental algorithm"],
+ 103 => ["Private/Experimental algorithm"],
+ 104 => ["Private/Experimental algorithm"],
+ 105 => ["Private/Experimental algorithm"],
+ 106 => ["Private/Experimental algorithm"],
+ 107 => ["Private/Experimental algorithm"],
+ 108 => ["Private/Experimental algorithm"],
+ 109 => ["Private/Experimental algorithm"],
+ 110 => ["Private/Experimental algorithm"],
+ }
+
+ def self.include?(algorithm)
+ ALGORITHMS.key?(algorithm)
+ end
+
+ def self.label(algorithm)
+ if data = ALGORITHMS[algorithm]
+ data[0]
+ else
+ raise "Not supported: #{algorithm}"
+ end
+ end
+
+ def self.dump_summary(algorithm)
+ "Hash algorithm - #{label(algorithm)}(#{algorithm})"
+ end
+
+ def self.calc(algorithm, data)
+ case algorithm
+ when 1
+ Digest::MD5.digest(data)
+ when 2
+ Digest::SHA1.digest(data)
+ else
+ raise "Not supported: #{algorithm}"
+ end
+ end
+
+ def self.hashlength(algorithm)
+ if data = ALGORITHMS[algorithm]
+ data[1]
+ else
+ raise "Not supported: #{algorithm}"
+ end
+ end
+
+ def self.asnoid(algorithm)
+ if data = ALGORITHMS[algorithm]
+ data[2] ? data[2].pack("c*") : nil
+ else
+ nil
+ end
+ end
+end
+
+
+end
View
@@ -0,0 +1,40 @@
+module PGP
+
+
+module HexDump
+ # Written by Arai-san and published at [ruby-list:31987].
+ # http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-list/31987
+ def encode(str)
+ offset = 0
+ result = []
+ while raw = str.slice(offset, 16) and raw.length > 0
+ # data field
+ data = ''
+ for v in raw.unpack('N* a*')
+ if v.kind_of? Integer
+ data << sprintf("%08x ", v)
+ else
+ v.each_byte {|c| data << sprintf("%02x", c) }
+ end
+ end
+ # text field
+ text = raw.tr("\000-\037\177-\377", ".")
+ result << sprintf("%08x %-36s %s", offset, data, text)
+ offset += 16
+ # omit duplicate line
+ if /^(#{ Regexp.quote(raw) })+/n =~ str[offset .. -1]
+ result << sprintf("%08x ...", offset)
+ offset += $&.length
+ # should print at the end
+ if offset == str.length
+ result << sprintf("%08x %-36s %s", offset-16, data, text)
+ end
+ end
+ end
+ result
+ end
+ module_function :encode
+end
+
+
+end
View
@@ -0,0 +1,32 @@
+module PGP
+
+
+class IndentDumpPort
+ def self.for(port)
+ new(port)
+ end
+
+ class << self
+ private :new
+ end
+
+ def initialize(port)
+ @port = port
+ @indent = 0
+ end
+
+ def puts(str)
+ @port.print(" " * @indent)
+ @port.puts(str)
+ end
+
+ def indent(size, &block)
+ record = @indent
+ @indent += size
+ yield
+ @indent = record
+ end
+end
+
+
+end
Oops, something went wrong.

0 comments on commit 74dd04f

Please sign in to comment.