-
Notifications
You must be signed in to change notification settings - Fork 15
/
mime-pgp.el
430 lines (381 loc) · 14.4 KB
/
mime-pgp.el
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
;;; mime-pgp.el --- mime-view internal methods for EasyPG. -*- lexical-binding: t -*-
;; Copyright (C) 1995,1996,1997,1998,1999,2000 Free Software Foundation, Inc.
;; Author: MORIOKA Tomohiko <[email protected]>
;; Daiki Ueno <[email protected]>
;; Kazuhiro Ito <[email protected]>
;; Created: 1995/12/7
;; Renamed: 1997/2/27 from tm-pgp.el
;; Renamed: 2010/11/27 from mime-pgp.el in emiko-epg
;; Renamed: 2012/10/05 from mime-epg.el
;; Keywords: PGP, security, MIME, multimedia, mail, news
;; This file is part of SEMI (Secure Emacs MIME Interface).
;; This program 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 2, or (at
;; your option) any later version.
;; This program 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; see the file COPYING. If not, write to the
;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
;; Boston, MA 02110-1301, USA.
;;; Commentary:
;;; Code:
(require 'mime-parse)
(require 'mime-play)
(require 'epg)
(require 'epa)
(eval-when-compile (require 'mmgeneric))
;;; @ Internal functions
(eval-and-compile
(defun mime-pgp-concurrency-available-p ()
(and (fboundp 'make-mutex)
(fboundp 'with-mutex)
(fboundp 'make-thread)
(fboundp 'thread-signal)))
(defcustom mime-pgp-use-concurrency
(and (mime-pgp-concurrency-available-p)
;; The feature may make Emacs crash on Windows.
(null (memq system-type '(windows-nt)))
;; Emacs's concurrency feature seems to be unstable on earlier version.
(>= emacs-major-version 28))
"When non-nil, use concurrency feature for inline verifying if available."
:group 'mime-view
:type 'boolean)
(if (mime-pgp-concurrency-available-p)
(progn
(defalias 'mime-pgp-make-mutex 'make-mutex)
(defalias 'mime-pgp-with-mutex 'with-mutex))
(defalias 'mime-pgp-make-mutex 'ignore)
(defmacro mime-pgp-with-mutex (_mutex &rest body)
`(progn ,@body))
))
;; I'm not sure epg-* functions are thread safe.
(defvar mime-pgp-mutex (mime-pgp-make-mutex "MIME-PGP"))
(defun mime-pgp-maybe-remove-cr (string)
;; Remove CRs if header contains CR.
(if (string-match "\\`.*\r\n" string)
(let (inhibit-eol-conversion)
(decode-coding-string string 'raw-text-dos))
string))
(defun mime-pgp-decrypt-string (context cipher)
(mime-pgp-maybe-remove-cr
(mime-pgp-with-mutex mime-pgp-mutex
(epg-decrypt-string context cipher))))
(defun mime-pgp-verify-string (context cipher)
(mime-pgp-maybe-remove-cr
(mime-pgp-with-mutex mime-pgp-mutex
(epg-verify-string context cipher))))
(defun mime-pgp-verify-result-to-string (context)
(let ((result (epg-context-result-for context 'verify)))
(unless (stringp result)
(setq result (epg-verify-result-to-string result)))
(when (> (length result) 0)
(unless (string-equal (substring result -1) "\n")
(setq result (concat result "\n"))))
result))
(defun mime-pgp-pkcs7-decrypt-enveloped-data (context content)
(let (result)
(condition-case error
(setq result (decode-coding-string
(mime-pgp-decrypt-string context content)
'raw-text))
(error (setq result error)))
result))
(autoload 'mime-edit-make-boundary "mime-edit")
(defun mime-pgp-pkcs7-verify-signed-data (context content)
(let (result)
(if (condition-case error
(setq result (decode-coding-string
(mime-pgp-verify-string context content)
'raw-text))
(error (setq result error) nil))
(let ((boundary (concat "PKCS7--" (mime-edit-make-boundary))))
(concat "Content-Type: multipart/mixed;\n"
" boundary=\"" boundary "\"\n"
"Content-Transfer-Encoding: 7bit\n\n"
"--" boundary "\n"
result
"--" boundary "\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n\n"
(encode-coding-string
(mime-pgp-verify-result-to-string context) 'utf-8)
"--" boundary "--\n"))
result)))
(defun mime-pgp-smime-type-from-situation (situation)
(let ((type (cdr (assoc "smime-type" situation))))
(if type
(intern (downcase type))
'enveloped-data)))
(defun mime-pgp-entity-string (entity)
(with-temp-buffer
(buffer-disable-undo)
(set-buffer-multibyte nil)
(mime-insert-entity entity)
(buffer-string)))
;;; @ Internal method for multipart/signed
(defun mime-verify-multipart/signed (entity situation)
"Internal method to verify multipart/signed."
(mime-play-entity
(nth 1 (mime-entity-children entity)) ; entity-info of signature
(list (assq 'mode situation)) ; play-mode
))
;;; @ Internal method for multipart/encrypted
(defun mime-display-multipart/pgp-encrypted (entity situation)
(let ((original-major-mode-cell (assq 'major-mode situation))
(default-situation
(cdr (assq 'childrens-situation situation)))
child-situation)
(unless mime-pgp-decrypt-when-preview
(insert "This part is encrypted.\n"))
(when original-major-mode-cell
(setq default-situation
(cons original-major-mode-cell default-situation)))
(mapc
(lambda (child)
(setq child-situation
(mime-find-entity-preview-situation child default-situation))
(if (and (eq (cdr (assq 'type child-situation)) 'application)
(eq (cdr (assq 'subtype child-situation)) 'pgp-encrypted))
(mime-display-entity
child (put-alist 'body mime-pgp-decrypt-when-preview
(copy-alist child-situation)))
(when mime-view-multipart/related-show-all-children
(mime-display-entity
child (put-alist 'body 'invisible (copy-alist child-situation))))))
(mime-entity-children entity))))
;;; @ Internal method for application/*-signature
(defun mime-verify-application/*-signature-internal (entity _situation)
(let* ((mother (mime-entity-parent entity))
(orig-entity (car (mime-entity-children mother)))
(protocol (cdr (assoc "protocol" (mime-entity-parameters mother))))
context)
(if (null protocol)
"No protocol is specified."
(setq context
(epg-make-context
(cond
((equal protocol "application/pgp-signature")
'OpenPGP)
((string-match
"\\`application/\\(x-\\)?pkcs7-signature\\'" protocol)
'CMS))))
(if (null context)
(format "Unknown protocol: %s." protocol)
(let ((signature (mime-entity-content entity))
(text (let (inhibit-eol-conversion)
(encode-coding-string
(mime-pgp-entity-string orig-entity) 'raw-text-dos))))
(mime-pgp-with-mutex mime-pgp-mutex
(epg-verify-string context signature text)))
(epg-context-result-for context 'verify)))))
(defun mime-verify-application/*-signature (entity situation)
(let ((verify-result
(mime-verify-application/*-signature-internal entity situation))
window)
(cond
((stringp verify-result)
(mime-show-echo-buffer verify-result))
((> (length verify-result) 1)
(mime-show-echo-buffer (epg-verify-result-to-string verify-result)))
(verify-result
(epa-display-info (epg-verify-result-to-string verify-result))
(when (and epa-popup-info-window
(setq window (get-buffer-window epa-info-buffer)))
(select-window window))))))
(defun mime-preview-application/*-signature(entity situation)
(let ((unique (concat (number-to-string (point))
"-" (number-to-string (random))))
(point (point)))
(insert "Verifying...\n")
(set-text-properties point (point) `(mime-pgp-entity ,unique))
(let ((fn
`(lambda ()
(let ((verify-result
(condition-case error
(mime-verify-application/*-signature-internal
',entity ',situation)
(error (format "Verification failed, %s" error))
(quit (format "Verification quitted")))))
(unless (stringp verify-result)
(setq verify-result
(epg-verify-result-to-string verify-result)))
(when (> (length verify-result) 0)
(unless (string-equal (substring verify-result -1) "\n")
(setq verify-result (concat verify-result "\n")))
(let ((point (point-min))
props)
(while (and point
(setq point (next-single-property-change
point 'mime-pgp-entity)))
(setq props (text-properties-at point))
(when (eq (plist-get props 'mime-pgp-entity) ,unique)
(set-text-properties
0 (length verify-result) props verify-result)
(save-excursion
(goto-char point)
(let ((inhibit-read-only t))
(delete-region
point (or (next-single-property-change
point 'mime-pgp-entity)
(point-max)))
(insert verify-result)))
(setq point nil)))))))))
(if (and mime-pgp-use-concurrency
(mime-pgp-concurrency-available-p))
(make-thread fn)
(funcall fn)))))
;;; @ Internal method for application/pgp-encrypted
(defun mime-decrypt-application/pgp-encrypted (entity _situation)
(let* ((mother (mime-entity-parent entity))
(encrypted-entity (nth 1 (mime-entity-children mother)))
(p-win (or (get-buffer-window (current-buffer))
(get-largest-window)))
(new-name
(format "%s-%s" (buffer-name) (mime-entity-number entity)))
(mother (current-buffer))
(preview-buffer (concat "*Preview-" (buffer-name) "*"))
representation-type message-buf context plain)
(set-buffer (setq message-buf (get-buffer-create new-name)))
(erase-buffer)
(mime-insert-entity encrypted-entity)
(goto-char (point-min))
(setq context (epg-make-context)
plain (decode-coding-string
(mime-pgp-decrypt-string
context
(buffer-substring (point-min)(point-max)))
'raw-text))
(delete-region (point-min)(point-max))
(insert plain)
(setq representation-type 'binary
major-mode 'mime-show-message-mode)
(save-window-excursion
(mime-view-buffer nil preview-buffer mother
nil representation-type)
(make-local-variable 'mime-view-temp-message-buffer)
(setq mime-view-temp-message-buffer message-buf))
(set-window-buffer p-win preview-buffer)))
(defvar mime-pgp-decrypted-buffers nil)
(defun mime-pgp-kill-decrypted-buffers ()
(mapc (lambda (buffer)
(when (bufferp buffer)
(kill-buffer buffer)))
mime-pgp-decrypted-buffers))
(defun mime-pgp-register-decrypted-buffer (buffer)
(add-hook 'kill-buffer-hook 'mime-pgp-kill-decrypted-buffers nil t)
(make-local-variable 'mime-pgp-decrypted-buffers)
(add-to-list 'mime-pgp-decrypted-buffers buffer))
;; Imported and modified from Wanderlust.
(defun mime-preview-application/pgp-encrypted (entity _situation)
(let (p buffer decrypted-entity result)
(goto-char (setq p (point-max)))
(save-restriction
(narrow-to-region p p)
(setq buffer (generate-new-buffer (concat mime-temp-buffer-name "PGP*")))
(if (null (stringp
(setq result (mime-pgp-pkcs7-decrypt-enveloped-data
(epg-make-context)
(mime-pgp-entity-string
(nth 1 (mime-entity-children
(mime-entity-parent entity))))))))
(insert (format "%s" (cdr result)))
(with-current-buffer buffer
(require 'mmbuffer)
(insert result)
(setq decrypted-entity
(mime-parse-message
(mm-expand-class-name 'buffer)
nil entity (mime-entity-node-id entity))
buffer-read-only t))
(mime-pgp-register-decrypted-buffer buffer)
(mime-display-entity
decrypted-entity nil '((header . visible)
(body . visible)
(entity-button . invisible)))))))
;;; @ Internal method for application/pgp-keys
(defun mime-add-application/pgp-keys (entity _situation)
(when (y-or-n-p "Do you want to import PGP keys? ")
(let ((context (epg-make-context))
result window)
(epg-import-keys-from-string context (mime-entity-content entity))
(when (setq result (epg-context-result-for context 'import))
(epa-display-info (epg-import-result-to-string result))
(when (and epa-popup-info-window
(setq window (get-buffer-window epa-info-buffer)))
(select-window window))))))
;;; @ Internal method for application/pkcs7-mime
(defun mime-view-application/pkcs7-mime (entity situation)
(let ((smime-type (mime-pgp-smime-type-from-situation situation))
(p-win (or (get-buffer-window (current-buffer))
(get-largest-window)))
(new-name
(format "%s-%s" (buffer-name) (mime-entity-number entity)))
(mother (current-buffer))
(preview-buffer (concat "*Preview-" (buffer-name) "*"))
(context (epg-make-context 'CMS))
message-buf result fn)
(setq fn
(cdr
(assq smime-type
'((signed-data . mime-pgp-pkcs7-verify-signed-data)
(enveloped-data . mime-pgp-pkcs7-decrypt-enveloped-data)))))
(unless fn
(error "Unsupported smime-type (%s)" smime-type))
(if (null (stringp (setq result (funcall fn context
(mime-entity-content entity)))))
(signal (car result) (cdr result))
(set-buffer (setq message-buf (get-buffer-create new-name)))
(let ((inhibit-read-only t)
buffer-read-only)
(set-buffer-multibyte nil)
(erase-buffer)
(insert result)
(setq major-mode 'mime-show-message-mode)
(save-window-excursion
(mime-view-buffer nil preview-buffer mother nil 'binary)
(make-local-variable 'mime-view-temp-message-buffer)
(setq mime-view-temp-message-buffer message-buf))
(set-window-buffer p-win preview-buffer)))))
(defun mime-preview-application/pkcs7-mime (entity situation)
(let ((smime-type (mime-pgp-smime-type-from-situation situation))
(context (epg-make-context 'CMS))
p buffer decrypted-entity result fn)
(setq fn
(cdr
(assq smime-type
'((signed-data . mime-pgp-pkcs7-verify-signed-data)
(enveloped-data . mime-pgp-pkcs7-decrypt-enveloped-data)))))
(if (null fn)
(insert "Unsupported smime-type (" smime-type ")\n")
(goto-char (setq p (point-max)))
(save-restriction
(narrow-to-region p p)
(if (null (stringp (setq result
(funcall fn context
(mime-entity-content entity)))))
(insert (format "%s" (cdr result)))
(setq buffer (generate-new-buffer
(concat mime-temp-buffer-name "PKCS7*")))
(with-current-buffer buffer
(require 'mmbuffer)
(set-buffer-multibyte nil)
(insert result)
(setq decrypted-entity
(mime-parse-message
(mm-expand-class-name 'buffer)
nil entity (mime-entity-node-id-internal entity))
buffer-read-only t))
(mime-pgp-register-decrypted-buffer buffer)
(mime-display-entity
decrypted-entity nil '((header . invisible)
(body . visible)
(entity-button . invisible))))))))
;;; @ end
;;;
(provide 'mime-pgp)
(run-hooks 'mime-pgp-load-hook)
;;; mime-pgp.el ends here