Module: LibNetPGP

Extended by:
FFI::Library
Defined in:
lib/netpgp/lowlevel/enums.rb,
lib/netpgp/lowlevel/utils.rb,
lib/netpgp/lowlevel/structs.rb,
lib/netpgp/lowlevel/dynarray.rb,
lib/netpgp/lowlevel/constants.rb,
lib/netpgp/lowlevel/libnetpgp.rb

Defined Under Namespace

Classes: PGPArmourHeader, PGPCBData, PGPContents, PGPCrypt, PGPCryptInfo, PGPDSAPubKey, PGPDSASecKey, PGPDSASig, PGPData, PGPDynBody, PGPElGamalPubKey, PGPElGamalSecKey, PGPElGamalSig, PGPErrCode, PGPError, PGPFingerprint, PGPFixedBody, PGPGetSecKey, PGPHash, PGPHeaders, PGPIO, PGPKey, PGPKeyDataKey, PGPKeyring, PGPLitDataBody, PGPLitDataHeader, PGPMemory, PGPOnePassSig, PGPOutput, PGPPKSessKey, PGPPKSessKeyParamsElGamal, PGPPKSessKeyParamsRSA, PGPPKSessKeyParamsU, PGPPTag, PGPPacket, PGPPrintState, PGPPubKey, PGPPubKeyU, PGPRSAPubKey, PGPRSASecKey, PGPRSASig, PGPReader, PGPRevoke, PGPSSNotation, PGPSSRaw, PGPSSRevocation, PGPSSRevocationKey, PGPSSSigTarget, PGPSSTrust, PGPSecKey, PGPSecKeyPassphrase, PGPSecKeyU, PGPSig, PGPSigInfo, PGPSigInfoU, PGPStream, PGPSubPacket, PGPValidation, PGPWriter

Constant Summary collapse

PGP_PUBKEY_ALG_T =
enum :pgp_pubkey_alg_t, [
     :PGP_PKA_NOTHING, 0,
     :PGP_PKA_RSA, 1,
     :PGP_PKA_RSA_ENCRYPT_ONLY, 2,
     :PGP_PKA_RSA_SIGN_ONLY, 3,
     :PGP_PKA_ELGAMAL, 16,
     :PGP_PKA_DSA, 17,
     :PGP_PKA_RESERVED_ELLIPTIC_CURVE, 18,
     :PGP_PKA_RESERVED_ECDSA, 19,
     :PGP_PKA_ELGAMAL_ENCRYPT_OR_SIGN, 20,
     :PGP_PKA_RESERVED_DH, 21,
     :PGP_PKA_PRIVATE00, 100,
     :PGP_PKA_PRIVATE01, 101,
     :PGP_PKA_PRIVATE02, 102,
     :PGP_PKA_PRIVATE03, 103,
     :PGP_PKA_PRIVATE04, 104,
     :PGP_PKA_PRIVATE05, 105,
     :PGP_PKA_PRIVATE06, 106,
     :PGP_PKA_PRIVATE07, 107,
     :PGP_PKA_PRIVATE08, 108,
     :PGP_PKA_PRIVATE09, 109,
     :PGP_PKA_PRIVATE10, 110,
]
PGP_KEY_ID_SIZE =
8
PGP_FINGERPRINT_SIZE =
20
PGP_MAX_KEY_SIZE =
32
PGP_SALT_SIZE =
8
PGP_MAX_BLOCK_SIZE =
16
SHA_DIGEST_LENGTH =
20
PGP_SHA1_HASH_SIZE =
SHA_DIGEST_LENGTH
PGP_CHECKHASH_SIZE =
PGP_SHA1_HASH_SIZE

Class Method Summary collapse

Class Method Details

.bn2hex(bn) ⇒ Object

BIGNUM* to hexadecimal string



7
8
9
10
11
# File 'lib/netpgp/lowlevel/utils.rb', line 7

def self.bn2hex(bn)
  str, ptr = LibOpenSSL::BN_bn2hex(bn)
  LibC::free(ptr)
  str
end

.dynarray_append_item(struct, field, type, value) ⇒ Object

Appends an item to a DYNARRAY, expanding the array as needed.



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/netpgp/lowlevel/dynarray.rb', line 51

def self.dynarray_append_item(struct, field, type, value)
  dynarray_expand(struct, field, type)

  count = dynarray_count(struct, field)
  items = dynarray_items(struct, field)
  case type
  when :pointer
    ptrs = items.read_array_of_pointer(count + 1)
    ptrs[count] = value
    items.write_array_of_pointer(ptrs)
  when :string
    ptrs = items.read_array_of_pointer(count + 1)
    mem = LibC::calloc(1, value.size + 1)
    mem.write_bytes(value)
    ptrs[count] = mem
    items.write_array_of_pointer(ptrs)
  else
    ptrs = FFI::Pointer.new(type, items)
    bytes = value.pointer.read_bytes(type.size)
    ptrs[count].write_bytes(bytes)
  end
  struct[(field + 'c').to_sym] = count + 1
end

.dynarray_clear(struct, field, type) ⇒ Object

Clear a dynarray so that the item count is zero.



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/netpgp/lowlevel/dynarray.rb', line 108

def self.dynarray_clear(struct, field, type)
  count = dynarray_count(struct, field)
  mem = dynarray_items(struct, field)
  return if count == 0 or mem.null?

  vsize = dynarray_vsize(struct, field)
  case type
  when :pointer, :string
    itemsize = FFI::Pointer.size
    ptrs = FFI::Pointer.new(:pointer, mem)
    (0..count-1).each {|n|
      LibC::free(ptrs[n].read_pointer())
    }
  else
    itemsize = type.size
  end
  LibC::memset(mem, 0, vsize * itemsize)
  struct[(field + 'c').to_sym] = 0
end

.dynarray_count(struct, field) ⇒ Object



5
6
7
# File 'lib/netpgp/lowlevel/dynarray.rb', line 5

def self.dynarray_count(struct, field)
  struct[(field + 'c').to_sym]
end

.dynarray_expand(struct, field, type) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/netpgp/lowlevel/dynarray.rb', line 75

def self.dynarray_expand(struct, field, type)
  count = dynarray_count(struct, field)
  vsize = dynarray_vsize(struct, field)
  # return if expansion is not necessary
  return if count != vsize

  newvsize = (vsize * 2) + 10
  mem = dynarray_items(struct, field)
  case type
  when :pointer, :string
    itemsize = FFI::Pointer.size
  else
    itemsize = type.size
  end
  newarr = LibC::realloc(mem, newvsize * itemsize)
  LibC::memset(newarr + (vsize * itemsize), 0, (newvsize - vsize) * itemsize)
  struct[(field + 'vsize').to_sym] = newvsize
  struct[(field + 's').to_sym] = newarr
end

.dynarray_get_item(struct, field, type, index) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/netpgp/lowlevel/dynarray.rb', line 17

def self.dynarray_get_item(struct, field, type, index)
  count = dynarray_count(struct, field)
  if index >= count
    return nil
  end

  items = dynarray_items(struct, field)
  case type
  when :pointer
    ptrs = items.read_array_of_pointer(count)
    ptrs[index]
  when :string
    ptrs = items.read_array_of_pointer(count)
    ptrs[index].read_string
  else
    ptrs = FFI::Pointer.new(type, items)
    type.new(ptrs[index])
  end
end

.dynarray_items(struct, field) ⇒ Object



13
14
15
# File 'lib/netpgp/lowlevel/dynarray.rb', line 13

def self.dynarray_items(struct, field)
  struct[(field + 's').to_sym]
end

.dynarray_vsize(struct, field) ⇒ Object



9
10
11
# File 'lib/netpgp/lowlevel/dynarray.rb', line 9

def self.dynarray_vsize(struct, field)
  struct[(field + 'vsize').to_sym]
end

.num2bn(num) ⇒ Object

Ruby Fixnum to BIGNUM*



14
15
16
17
18
19
20
21
22
# File 'lib/netpgp/lowlevel/utils.rb', line 14

def self.num2bn(num)
  bn_ptr = FFI::MemoryPointer.new(:pointer)
  hex = num.to_s(16)
  ret = LibOpenSSL::BN_hex2bn(bn_ptr, hex)
  raise 'Fixnum to BIGNUM conversion failed' if ret == 0
  bn = bn_ptr.get_pointer(0)
  bn_ptr.free
  bn
end