unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
blob dcdbefc57bb8f8211a928633bbeb8aab9aa064de 12654 bytes (raw)
name: lisp/epa-ks.el 	 # note: path name is non-authoritative(*)

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
 
;;; epa-ks.el --- EasyPG Key Server Client -*- lexical-binding: t -*-

;; Copyright (C) 2021 Free Software Foundation, Inc.

;; Author: Philip K. <philipk@posteo.net>
;; Keywords: PGP, GnuPG

;; This file is part of GNU Emacs.

;; GNU Emacs is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs.  If not, see <https://www.gnu.org/licenses/>.

;;; Commentary:

;; Keyserver client in Emacs.

;;; Code:

(require 'cl-lib)
(require 'epa)
(require 'subr-x)
(require 'tabulated-list)
(require 'url)
(require 'url-http)

(defgroup epa-ks nil
  "The EasyPG Assistant Keyserver client."
  :version "28.1"
  :group 'epa)

(defvar epa-keyserver-list
  '("keyring.debian.org"
    "keys.gnupg.net"
    "keyserver.ubuntu.com"
    "pgp.mit.edu"
    "pool.sks-keyservers.net"
    "zimmermann.mayfirst.org")
  "List of default keyservers.")

(defcustom epa-keyserver "pgp.mit.edu"
  "Domain of keyserver.

This is used by `epa-ks-lookup-key', for looking up public keys."
  :type `(choice :tag "Keyserver"
                 (const random)
                 ,@(mapcar (lambda (server) `(const ,server))
                           epa-keyserver-list)
                 (string :tag "Custom keyserver"))
  :version "28.1")

(cl-defstruct epa-ks-key
  "Structure to hold key data."
  id algo len created expires names flags)

(cl-defstruct epa-ks-name
  "Structure to hold user associated with keys data."
  uid created expires flags)

(defvar epa-ks-last-query nil
  "List of arguments to pass to `epa-search-keys'.
This is used when reverting a buffer to restart search.")

(defvar epa-ks-search-mode-map
  (let ((map (make-sparse-keymap)))
    (suppress-keymap map)
    (define-key map (kbd "f") #'epa-ks-mark-key-to-fetch)
    (define-key map (kbd "i") #'epa-ks-inspect-key-to-fetch)
    (define-key map (kbd "u") #'epa-ks-unmark-key-to-fetch)
    (define-key map (kbd "x") #'epa-ks-do-key-to-fetch)
    map))

(define-derived-mode epa-ks-search-mode tabulated-list-mode "Keyserver"
  "Major mode for listing public key search results."
  (buffer-disable-undo)
  (setq tabulated-list-format [("ID" 8 t)
                               ("Algo." 5 nil)
                               ("Created" 10 t)
                               ("Expires" 10 t)
                               ("User" 0 t)]
        tabulated-list-sort-key '("User" . nil)
        tabulated-list-padding 2)
  (add-hook 'tabulated-list-revert-hook
            #'epa-ks--restart-search
            nil t)
  (tabulated-list-init-header))

(defun epa-ks-inspect-key-to-fetch ()
  "Display full ID of key under point in the minibuffer."
  (interactive)
  (message "Full ID: %s" (epa-ks-key-id (car (tabulated-list-get-id)))))

(defun epa-ks-unmark-key-to-fetch ()
  "Remove fetch mark for key under point.

If a region is active, unmark all keys in active region."
  (interactive)
  (epa-ks-mark-key-to-fetch ""))

(defun epa-ks-mark-key-to-fetch (tag)
  "Add fetch-mark to key under point.

If a region is active, mark all keys in active region.

When all keys have been selected, use \\[epa-ks-do-key-to-fetch] to
actually import the keys.

When called interactively, `epa-ks-mark-key-to-fetch' will always
add a \"F\" tag.  Non-interactivly the tag must be specified by
setting the TAG parameter."
  (interactive (list "F"))
  (if (region-active-p)
      (save-mark-and-excursion
        (save-restriction
          (narrow-to-region (region-beginning) (1- (region-end)))
          (goto-char (point-min))
          (while (not (eobp))
            (tabulated-list-put-tag tag t))))
    (tabulated-list-put-tag tag t)))

(defun epa-ks-do-key-to-fetch ()
  "Fetch all marked keys from keyserver and import them.

Keys are marked using `epa-ks-mark-key-to-fetch'."
  (interactive)
  (save-excursion
    (let (keys)
      (goto-char (point-min))
      (while (not (eobp))
        (when (looking-at-p (rx bol "F"))
          (push (epa-ks-key-id (car (tabulated-list-get-id)))
                keys))
        (forward-line))
      (when (yes-or-no-p (format "Proceed with fetching all %d key(s)? "
                                 (length keys))))
      (dolist (id keys)
        (epa-ks--fetch-key id))))
  (tabulated-list-clear-all-tags))

(defun epa-ks--fetch-key (id)
  "Send request to import key with specified ID."
  (url-retrieve
   (format "https://%s/pks/lookup?%s"
           (if (eq epa-keyserver 'random)
               (nth (random (length epa-keyserver-list))
                    epa-keyserver-list)
               epa-keyserver)
           (url-build-query-string
            `(("search" ,(concat "0x" (url-hexify-string id)))
              ("options" "mr")
              ("op" "get"))))
   (lambda (status)
     (when (plist-get status :error)
       (error "Request failed: %s"
           (caddr (assq (caddr (plist-get status :error))
                        url-http-codes))))
     (forward-paragraph)
     (save-excursion
       (goto-char (point-max))
       (while (memq (char-before) '(?\s ?\t ?\n))
         (forward-char -1))
       (delete-region (point) (point-max)))
     (let ((epa-popup-info-window nil))
       (epa-import-armor-in-region (point) (point-max)))
     (kill-buffer))))

(defun epa-ks--display-keys (buf keys)
  "Prepare KEYS for `tabulated-list-mode', for buffer BUF.

KEYS is a list of `epa-ks-key' structures, as parsed by
`epa-ks-parse-result'."
  (when (buffer-live-p buf)
    (let (entries)
      (dolist (key keys)
        (dolist (name (epa-ks-key-names key))
          (push (list (cons key name)
                      (vector
                       (substring (epa-ks-key-id key) -8)
                       (cdr (epa-ks-key-algo key))
                       (if (epa-ks-key-created key)
                           (format-time-string "%F" (epa-ks-key-created key))
                         "N/A")
                       (if (epa-ks-key-expires key)
                           (let* ((date (epa-ks-key-expires key))
                                  (str (format-time-string "%F" date)))
                             (when (< 0 (time-to-seconds (time-since date)))
                               (setq str (propertize str 'face
                                                     'font-lock-warning-face)))
                             str)
                         (propertize "N/A" 'face 'shadow))
                       (decode-coding-string
                        (epa-ks-name-uid name)
                        (select-safe-coding-system (epa-ks-name-uid name)
                                                   nil 'utf-8))))
                entries)))
      (with-current-buffer buf
        (setq tabulated-list-entries entries)
        (tabulated-list-print t t))
      (message "Press `f' to mark a key, `x' to fetch all marked keys."))))

(defun epa-ks--restart-search ()
  (when epa-ks-last-query
    (apply #'epa-search-keys epa-ks-last-query)))

;;;###autoload
(defun epa-search-keys (query exact)
  "Ask a keyserver for all keys matching QUERY.

The keyserver to be used is specified by `epa-keyserver'.

If EXACT is non-nil (interactively, prefix argument), require
exact matches.

Note that the request may fail if the query is not specific
enough, since keyservers have strict timeout settings."
  (interactive (list (read-string "Search for: ")
                     current-prefix-arg))
  (when (string-empty-p query)
    (user-error "No query"))
  (let ((buf (get-buffer-create "*Key search*")))
    (with-current-buffer buf
      (let ((inhibit-read-only t))
        (erase-buffer))
      (epa-ks-search-mode))
    (url-retrieve
     (format "https://%s/pks/lookup?%s"
             (if (eq epa-keyserver 'random)
               (nth (random (length epa-keyserver-list))
                    epa-keyserver-list)
               epa-keyserver)
             (url-build-query-string
              (append `(("search" ,query)
                        ("options" "mr")
                        ("op" "index"))
                      (and exact '(("exact" "on"))))))
     (lambda (status)
       (when (plist-get status :error)
         (when buf
           (kill-buffer buf))
         (error "Request failed: %s"
                (caddr (assq (caddr (plist-get status :error))
                             url-http-codes))))
       (goto-char (point-min))
       (while (search-forward "\r\n" nil t)
         (replace-match "\n" t t))
       (goto-char (point-min))
       (re-search-forward "\n\n")
       (let (keys)
         (save-match-data
           (setq keys (epa-ks--parse-buffer))
           (kill-buffer (current-buffer)))
         (when buf
           (epa-ks--display-keys buf keys) keys))))
    (pop-to-buffer buf)
    (setq epa-ks-last-query (list query exact)))
  (message "Searching keys..."))

(defun epa-ks--parse-buffer ()
  ;; parse machine readable response according to
  ;; https://tools.ietf.org/html/draft-shaw-openpgp-hkp-00#section-5.2
  (when (looking-at (rx bol "info:" (group (+ digit))
                        ":" (* digit) eol))
    (unless (string= (match-string 1) "1")
      (error "Unsupported keyserver version")))
  (forward-line 1)
  (let (key keys)
    (while (and (not (eobp))
                (not (looking-at "[ \t]*\n")))
      (cond
       ((looking-at (rx bol "pub:" (group (+ alnum))
                        ":" (group (* digit))
                        ":" (group (* digit))
                        ":" (group (* digit))
                        ":" (group (* digit))
                        ":" (group (* (any ?r ?d ?e)))
                        eol))
        (setq key
              (make-epa-ks-key
               :id (match-string 1)
               :algo
               (and (match-string 2)
                    (not (string-empty-p (match-string 2)))
                    (assoc (string-to-number (match-string 2))
                           epg-pubkey-algorithm-alist))
               :len
               (and (match-string 3)
                    (not (string-empty-p (match-string 3)))
                    (string-to-number (match-string 3)))
               :created
               (and  (match-string 4)
                     (not (string-empty-p (match-string 4)))
                     (seconds-to-time
                      (string-to-number (match-string 4))))
               :expires
               (and (match-string 5)
                    (not (string-empty-p (match-string 5)))
                    (seconds-to-time
                     (string-to-number (match-string 5))))
               :flags
               (mapcar (lambda (flag)
                         (cdr (assq flag '((?r revoked)
                                           (?d disabled)
                                           (?e expired)))))
                       (match-string 6))))
        (push key keys))
       ((looking-at (rx bol "uid:" (group (+ (not ":")))
                        ":" (group (* digit))
                        ":" (group (* digit))
                        ":" (group (* (any ?r ?d ?e)))
                        eol))
        (push (make-epa-ks-name
               :uid (url-unhex-string (match-string 1) t)
               :created
               (and (match-string 2)
                    (not (string-empty-p (match-string 2)))
                    (decode-time (seconds-to-time
                                  (string-to-number
                                   (match-string 2)))))
               :expires
               (and (match-string 3)
                    (not (string-empty-p (match-string 3)))
                    (decode-time (seconds-to-time
                                  (string-to-number
                                   (match-string 3)))))
               :flags
               (mapcar (lambda (flag)
                         (cdr (assq flag '((?r revoked)
                                           (?d disabled)
                                           (?e expired)))))
                       (match-string 4)))
              (epa-ks-key-names key)))
       ((looking-at-p (rx bol "uat:"))
        ;; user attribute fields are ignored
        nil)
       (t (error "Invalid server response")))
      (forward-line))
    keys))

;;; epa-ks.el ends here

debug log:

solving dcdbefc57b ...
found dcdbefc57b in https://yhetil.org/emacs-devel/20210530183629.599038-1-philipk@posteo.net/
found a33025b112 in https://git.savannah.gnu.org/cgit/emacs.git
preparing index
index prepared:
100644 a33025b1125857ff130164d142f718722d9103f4	lisp/epa-ks.el

applying [1/1] https://yhetil.org/emacs-devel/20210530183629.599038-1-philipk@posteo.net/
diff --git a/lisp/epa-ks.el b/lisp/epa-ks.el
index a33025b112..dcdbefc57b 100644

Checking patch lisp/epa-ks.el...
Applied patch lisp/epa-ks.el cleanly.

index at:
100644 dcdbefc57bb8f8211a928633bbeb8aab9aa064de	lisp/epa-ks.el

(*) Git path names are given by the tree(s) the blob belongs to.
    Blobs themselves have no identifier aside from the hash of its contents.^

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).