Class: NetPGP::PublicKey

Inherits:
Object
  • Object
show all
Defined in:
lib/netpgp/highlevel/publickey.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePublicKey

Returns a new instance of PublicKey.



15
16
17
18
19
20
21
22
23
24
# File 'lib/netpgp/highlevel/publickey.rb', line 15

def initialize
  @version = nil
  @creation_time = nil
  @expiration_time = 0
  @public_key_algorithm = nil
  @mpi = {}
  @userids = []
  @parent = nil
  @subkeys = []
end

Instance Attribute Details

#creation_timeObject

Returns the value of attribute creation_time.



6
7
8
# File 'lib/netpgp/highlevel/publickey.rb', line 6

def creation_time
  @creation_time
end

#expiration_timeObject

Returns the value of attribute expiration_time.



6
7
8
# File 'lib/netpgp/highlevel/publickey.rb', line 6

def expiration_time
  @expiration_time
end

#mpiObject

Returns the value of attribute mpi.



6
7
8
# File 'lib/netpgp/highlevel/publickey.rb', line 6

def mpi
  @mpi
end

#parentObject

Returns the value of attribute parent.



6
7
8
# File 'lib/netpgp/highlevel/publickey.rb', line 6

def parent
  @parent
end

#public_key_algorithmObject

Returns the value of attribute public_key_algorithm.



6
7
8
# File 'lib/netpgp/highlevel/publickey.rb', line 6

def public_key_algorithm
  @public_key_algorithm
end

#subkeysObject

Returns the value of attribute subkeys.



6
7
8
# File 'lib/netpgp/highlevel/publickey.rb', line 6

def subkeys
  @subkeys
end

#useridsObject

Returns the value of attribute userids.



6
7
8
# File 'lib/netpgp/highlevel/publickey.rb', line 6

def userids
  @userids
end

#versionObject

Returns the value of attribute version.



6
7
8
# File 'lib/netpgp/highlevel/publickey.rb', line 6

def version
  @version
end

Class Method Details

.from_native(native) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
# File 'lib/netpgp/highlevel/publickey.rb', line 112

def self.from_native(native)
  pubkey = PublicKey.new
  pubkey.version = LibNetPGP::enum_value(native[:version])
  pubkey.creation_time = Time.at(native[:birthtime])
  if pubkey.version == 3
    pubkey.expiration_time = Time.at(native[:birthtime]) + (native[:days_valid] * 86400)
  end
  pubkey.public_key_algorithm = PublicKeyAlgorithm::from_native(native[:alg])
  pubkey.mpi = NetPGP::mpis_from_native(native[:alg], native)
  pubkey
end

Instance Method Details

#add_subkey(subkey) ⇒ Object



105
106
107
108
109
110
# File 'lib/netpgp/highlevel/publickey.rb', line 105

def add_subkey(subkey)
  raise if subkey.subkeys.any?
  subkey.parent = self
  subkey.userids = @userids
  @subkeys.push(subkey)
end

#encrypt(data, armored = true, sk_algorithm = SymmetricKeyAlgorithm::CAST5) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/netpgp/highlevel/publickey.rb', line 76

def encrypt(data, armored=true, sk_algorithm=SymmetricKeyAlgorithm::CAST5)
  cipher = SymmetricKeyAlgorithm::to_s(sk_algorithm)
  memory = nil

  begin
    pubkey_ptr = LibC::calloc(1, LibNetPGP::PGPKey.size)
    pubkey = LibNetPGP::PGPKey.new(pubkey_ptr)
    pubkey_auto = FFI::AutoPointer.new(pubkey_ptr, LibNetPGP::PGPKey.method(:release))

    to_native_key(pubkey)
    data_buf = FFI::MemoryPointer.new(:uint8, data.bytesize)
    data_buf.write_bytes(data)
    pgpio = LibNetPGP::PGPIO.new
    pgpio[:outs] = LibC::fdopen($stdout.to_i, 'w')
    pgpio[:errs] = LibC::fdopen($stderr.to_i, 'w')
    pgpio[:res] = pgpio[:errs]
    memory_ptr = LibNetPGP::pgp_encrypt_buf(pgpio, data_buf, data_buf.size, pubkey, armored ? 1 : 0, cipher)
    return nil if memory_ptr.null?
    memory = LibNetPGP::PGPMemory.new(memory_ptr)
    memory[:buf].read_bytes(memory[:length])
  ensure
    LibNetPGP::pgp_memory_free(memory) if memory
  end
end

#fingerprintObject



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/netpgp/highlevel/publickey.rb', line 26

def fingerprint
  fp = LibNetPGP::PGPFingerprint.new
  native_pubkey_ptr = LibC::calloc(1, LibNetPGP::PGPPubKey.size)
  native_pubkey = LibNetPGP::PGPPubKey.new(native_pubkey_ptr)
  native_pubkey_auto = FFI::AutoPointer.new(native_pubkey_ptr, LibNetPGP::PGPPubKey.method(:release))
  to_native(native_pubkey)
  hash = @version == 3 ? :PGP_HASH_MD5 : :PGP_HASH_SHA1
  ret = LibNetPGP::pgp_fingerprint(fp, native_pubkey, hash)
  raise 'pgp_fingerprint failed' if ret != 1
  fp[:fingerprint].to_s[0, fp[:length]]
end

#fingerprint_hexObject



38
39
40
# File 'lib/netpgp/highlevel/publickey.rb', line 38

def fingerprint_hex
  fingerprint.bytes.collect {|byte| '%02X' % byte}.join
end

#key_idObject



42
43
44
45
46
47
48
49
# File 'lib/netpgp/highlevel/publickey.rb', line 42

def key_id
  keyid_ptr = FFI::MemoryPointer.new(:uint8, LibNetPGP::PGP_KEY_ID_SIZE)
  native_pubkey = LibNetPGP::PGPPubKey.new
  to_native(native_pubkey)
  ret = LibNetPGP::pgp_keyid(keyid_ptr, LibNetPGP::PGP_KEY_ID_SIZE, native_pubkey, :PGP_HASH_SHA1)
  raise 'pgp_keyid failed' if ret != 1
  keyid_ptr.read_bytes(LibNetPGP::PGP_KEY_ID_SIZE)
end

#key_id_hexObject



51
52
53
# File 'lib/netpgp/highlevel/publickey.rb', line 51

def key_id_hex
  key_id.bytes.collect {|byte| '%02X' % byte}.join
end

#key_lengthObject



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/netpgp/highlevel/publickey.rb', line 55

def key_length
  case @public_key_algorithm
  when PublicKeyAlgorithm::RSA,
       PublicKeyAlgorithm::RSA_ENCRYPT_ONLY,
       PublicKeyAlgorithm::RSA_SIGN_ONLY
    return NetPGP::bignum_byte_count(@mpi[:n]) * 8
  when PublicKeyAlgorithm::DSA
    case NetPGP::bignum_byte_count(@mpi[:q])
    when 20
      1024
    when 28
      2048
    when 32
      3072
    end
  when PublicKeyAlgorithm::ELGAMAL
    NetPGP::bignum_byte_count(@mpi[:y]) * 8
  end
  0
end

#to_native(native) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
# File 'lib/netpgp/highlevel/publickey.rb', line 124

def to_native(native)
  native[:version] = @version
  native[:birthtime] = @creation_time.to_i
  if @version == 3 and @expiration_time 
    native[:days_valid] = ((@expiration_time.to_i - @creation_time.to_i) / 86400).to_i
  else
    native[:duration] = (@expiration_time.to_i - @creation_time.to_i).to_i
  end
  native[:alg] = @public_key_algorithm
  NetPGP::mpis_to_native(native[:alg], @mpi, native)
end

#to_native_key(native_key) ⇒ Object



136
137
138
139
140
141
142
143
144
145
# File 'lib/netpgp/highlevel/publickey.rb', line 136

def to_native_key(native_key)
  native_key[:type] = :PGP_PTAG_CT_PUBLIC_KEY
  native_key[:sigid] = key_id
  to_native(native_key[:key][:pubkey])
  if not @parent
    @userids.each {|userid|
      LibNetPGP::dynarray_append_item(native_key, 'uid', :string, userid)
    }
  end
end

#verify(data, armored = true) ⇒ Object



101
102
103
# File 'lib/netpgp/highlevel/publickey.rb', line 101

def verify(data, armored=true)
  NetPGP::verify([self], data, armored)
end