1 # Nick's web site: Export public keys for the Web Key Directory
3 # Copyright © 2022 Nick Bowler
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <https://www.gnu.org/licenses/>.
21 @@gpg2 = "/usr/bin/gpg"
22 @@wksclient = "/usr/libexec/gpg-wks-client"
24 def WKD.gpg2; @@gpg2 end
25 def WKD.gpg2=(x); @@gpg2 = x end
26 def WKD.wksclient; @@wksclient end
27 def WKD.wksclient=(x); @@wksclient = x end
29 # Convert a list of keyring filenames into GPG keyring arguments
30 def WKD.keyring_args(args)
31 return [ "--no-default-keyring",
32 *args.map { |x| "--keyring=" + (x['/'] ? x : "./" + x) } ]
35 # Helper for implementing export filters below
36 def WKD.export(item, uid, *args)
37 data, result = Open3.capture2(@@gpg2, "--export", *args,
38 *WKD.keyring_args(item[:keyrings]), uid.chomp)
39 raise "gpg failed" unless result.success?
43 # Return a list list of all UIDs known from the given GPG keyrings.
44 def WKD.uids_from_keyrings(*args)
48 "--with-colons", "--list-keys", *WKD.keyring_args(args)
49 ) do |stdin, stdout, result|
52 fields = line.split(":")
53 next if fields[0] != "uid"
54 fields[9].gsub!(/\\x../) { |x| x[2..].hex.chr }
55 uids[fields[9]] = true
59 raise "gpg failed" unless result.value.success?
65 # Given a list of UIDs, return a dictionary where the keys are UIDs
66 # and the values are the WKS hash.
67 def WKD.hashes_from_uids(*args)
70 consume_output = Proc.new do |s|
71 while l = s.slice!(/([^\n]*)\n/) do
72 hash, uid = l.chomp.split(nil, 2)
77 Open3.popen2(@@wksclient,
79 ) do |stdin, stdout, result|
81 args.flatten.each do |uid|
86 buf += stdout.read_nonblock(100)
87 consume_output.call(buf)
88 rescue EOFError, IO::WaitReadable
95 buf += stdout.readpartial(100)
96 consume_output.call(buf)
102 raise "gpg-wks-client failed" unless result.value.success?
110 # Call during preprocessing to create items for each unique UID found in the
111 # given keyring items. The items have the identifier /gpg/UID and the content
112 # is the same UID. The items are created with the :keyrings attribute set to
113 # the list of keyring files and :wkd_hash is for the Web Key Directory.
114 def create_wkd_items(keyring_items)
116 [*keyring_items].each { |item| keyring_files[item.raw_filename] = true }
118 wkd = WKD.hashes_from_uids(WKD.uids_from_keyrings(*keyring_files.keys))
119 wkd.each do |uid, hash|
121 keyrings: keyring_files.keys,
124 @items.create(uid, attrs, "/gpg/" + uid)
128 # Convert items created by create_wkd_items into real GPG keyrings.
129 class WKDExport < Nanoc::Filter
130 identifier :wkd_export
131 type :text => :binary
133 def run(content, params = {})
134 WKD.export(item, content, "--output=" + output_filename)
138 class WKDExportArmor < Nanoc::Filter
139 identifier :wkd_export_armor
142 def run(content, params = {})
143 data = WKD.export(item, content, "--armor")