Class: OpenPGP::Engine::GnuPG

Inherits:
OpenPGP::Engine show all
Defined in:
lib/openpgp/engine/gnupg.rb

Overview

GNU Privacy Guard (GnuPG) wrapper.

Defined Under Namespace

Classes: Error

Constant Summary collapse

OPTIONS =
{
  :batch                 => true,
  :quiet                 => true,
  :no_verbose            => true,
  :no_tty                => true,
  :no_permission_warning => true,
  :no_random_seed_file   => true,
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from OpenPGP::Engine

install!, load!, use

Constructor Details

#initialize(options = {}) ⇒ GnuPG

Returns a new instance of GnuPG.



25
26
27
28
# File 'lib/openpgp/engine/gnupg.rb', line 25

def initialize(options = {})
  @where   = '/usr/bin/env gpg' # FIXME
  @options = OPTIONS.merge!(options)
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



23
24
25
# File 'lib/openpgp/engine/gnupg.rb', line 23

def options
  @options
end

#whereObject

Returns the value of attribute where.



22
23
24
# File 'lib/openpgp/engine/gnupg.rb', line 22

def where
  @where
end

Class Method Details

.available?Boolean

Returns:

  • (Boolean)


9
10
11
# File 'lib/openpgp/engine/gnupg.rb', line 9

def self.available?
  self.new.available?
end

Instance Method Details

#available?Boolean

Determines if GnuPG is available.

Returns:

  • (Boolean)


32
33
34
# File 'lib/openpgp/engine/gnupg.rb', line 32

def available?
  !!version
end

#clearsignObject

Makes a clear text OpenPGP signature.



123
124
125
# File 'lib/openpgp/engine/gnupg.rb', line 123

def clearsign()
  # TODO
end

#decrypt(ciphertext, options = {}) ⇒ Object

Decrypts the given ciphertext using the specified key ID.



105
106
107
# File 'lib/openpgp/engine/gnupg.rb', line 105

def decrypt(ciphertext, options = {})
  # TODO
end

#delete_secret_and_public_key(key_id) ⇒ Object



78
79
80
81
# File 'lib/openpgp/engine/gnupg.rb', line 78

def delete_secret_and_public_key(key_id)
  opts = {:batch => true}
  OpenPGP::Message.parse(exec([:delete_secret_and_public_key, key_fingerprint(key_id)], opts ).read)
end

#detach_signObject

Makes a detached OpenPGP signature.



129
130
131
# File 'lib/openpgp/engine/gnupg.rb', line 129

def detach_sign()
  # TODO
end

#encrypt(plaintext, options = {}) ⇒ Object

Encrypts the given plaintext to the specified recipients.



99
100
101
# File 'lib/openpgp/engine/gnupg.rb', line 99

def encrypt(plaintext, options = {})
  # TODO
end

#exec(command, options = {}, &block) ⇒ Object

Executes a GnuPG command, yielding the standard input and returning the standard output.



142
143
144
145
146
147
148
149
150
# File 'lib/openpgp/engine/gnupg.rb', line 142

def exec(command, options = {}, &block) #:yields: stdin
  exec4(command, options) do |pid, stdin, stdout, stderr|
    block.call(stdin) if block_given?
    stdin.close_write
    pid, status = Process.waitpid2(pid)
    raise Error, stderr.read.chomp if status.exitstatus.nonzero?
    stdout
  end
end

#exec3(command, options = {}, &block) ⇒ Object

Executes a GnuPG command, yielding and returning the standard input, output and error.



155
156
157
158
159
160
161
162
163
# File 'lib/openpgp/engine/gnupg.rb', line 155

def exec3(command, options = {}, &block) #:yields: stdin, stdout, stderr
  exec4(command, options) do |pid, stdin, stdout, stderr|
    block.call(stdin, stdout, stderr) if block_given?
    stdin.close_write
    pid, status = Process.waitpid2(pid)
    raise Error, stderr.read.chomp if status.exitstatus.nonzero?
    [stdin, stdout, stderr]
  end
end

#exec4(command, options = {}, &block) ⇒ Object

Executes a GnuPG command, yielding the process identifier as well as the standard input, output and error.



168
169
170
171
172
# File 'lib/openpgp/engine/gnupg.rb', line 168

def exec4(command, options = {}, &block) #:yields: pid, stdin, stdout, stderr
  require 'rubygems'
  require 'open4'
  block.call(*Open4.popen4(cmdline(command, options)))
end

#export(key_id = nil, opts = {}) ⇒ Object

Exports a specified key from the GnuPG keyring.



67
68
69
# File 'lib/openpgp/engine/gnupg.rb', line 67

def export(key_id = nil, opts = {})
  OpenPGP::Message.parse(exec([:export, *[key_id].flatten], opts ).read)
end

#gen_key(info = {}) ⇒ Object

Generates a new OpenPGP keypair and stores it GnuPG’s keyring.



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/openpgp/engine/gnupg.rb', line 44

def gen_key(info = {})
  stdin, stdout, stderr = exec3(:gen_key) do |stdin, stdout, stderr|
    stdin.puts "Key-Type: #{info[:key_type]}"           if info[:key_type]
    stdin.puts "Key-Length: #{info[:key_length]}"       if info[:key_length]
    stdin.puts "Subkey-Type: #{info[:subkey_type]}"     if info[:subkey_type]
    stdin.puts "Subkey-Length: #{info[:subkey_length]}" if info[:subkey_length]
    stdin.puts "Name-Real: #{info[:name]}"              if info[:name]
    stdin.puts "Name-Comment: #{info[:comment]}"        if info[:comment]
    stdin.puts "Name-Email: #{info[:email]}"            if info[:email]
    stdin.puts "Expire-Date: #{info[:expire_date]}"     if info[:expire_date]
    stdin.puts "Passphrase: #{info[:passphrase]}"       if info[:passphrase]
    stdin.puts "%commit"
  end
  stderr.each_line do |line|
    if (line = line.chomp) =~ /^gpg: key ([0-9A-F]+) marked as ultimately trusted/
      return $1.to_i(16) # the key ID
    end
  end
  return nil
end

#importObject

Imports a specified keyfile into the GnuPG keyring.



74
75
76
# File 'lib/openpgp/engine/gnupg.rb', line 74

def import()
  # TODO
end

#key_fingerprint(key_id, opts = {}) ⇒ Object



83
84
85
86
87
88
89
# File 'lib/openpgp/engine/gnupg.rb', line 83

def key_fingerprint(key_id, opts = {})
  message = exec([:fingerprint, *[key_id].flatten], opts ).read
  if message =~ /Key fingerprint = (.*)\n/
    return $1.delete(" ")
  end
  nil
end

#list_keysObject

Returns an array of key IDs/titles of the keys in the public keyring.



93
94
95
# File 'lib/openpgp/engine/gnupg.rb', line 93

def list_keys()
  # TODO
end

#signObject

Makes an OpenPGP signature.



111
112
113
# File 'lib/openpgp/engine/gnupg.rb', line 111

def sign()
  # TODO
end

#sign_file(key_id, file, passphrase) ⇒ Object

Makes an OpenPGP signature.



117
118
119
# File 'lib/openpgp/engine/gnupg.rb', line 117

def sign_file(key_id, file, passphrase)
  OpenPGP::Message.parse(exec([:sign, file],{ :local_user => key_id, :passphrase => passphrase}).read)
end

#verify(key_id, file) ⇒ Object

Verifies an OpenPGP signature.



135
136
137
# File 'lib/openpgp/engine/gnupg.rb', line 135

def verify(key_id, file)
  OpenPGP::Message.parse(exec([:verify, file],{ :local_user => key_id}).read)
end

#versionObject

Returns the GnuPG version number.



38
39
40
# File 'lib/openpgp/engine/gnupg.rb', line 38

def version
  exec(:version).readline =~ /^gpg \(GnuPG\) (.*)$/ ? $1 : nil
end