1 ;;; pgg-parse.el --- OpenPGP packet parsing
3 ;; Copyright (C) 1999, 2002-2014 Free Software Foundation, Inc.
5 ;; Author: Daiki Ueno <ueno@unixuser.org>
7 ;; Keywords: PGP, OpenPGP, GnuPG
9 ;; Obsolete-since: 24.1
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
28 ;; This module is based on
30 ;; [OpenPGP] RFC 2440: "OpenPGP Message Format"
31 ;; by John W. Noerenberg, II <jwn2@qualcomm.com>,
32 ;; Jon Callas <jon@pgp.com>, Lutz Donnerhacke <lutz@iks-jena.de>,
33 ;; Hal Finney <hal@pgp.com> and Rodney Thayer <rodney@unitran.com>
39 ;; For Emacs <22.2 and XEmacs.
40 (unless (fboundp 'declare-function
) (defmacro declare-function
(&rest r
)))
43 (defgroup pgg-parse
()
44 "OpenPGP packet parsing."
47 (defcustom pgg-parse-public-key-algorithm-alist
48 '((1 . RSA
) (2 . RSA-E
) (3 . RSA-S
) (16 . ELG-E
) (17 . DSA
) (20 . ELG
))
49 "Alist of the assigned number to the public key algorithm."
52 (cons (sexp :tag
"Number") (sexp :tag
"Type"))))
54 (defcustom pgg-parse-symmetric-key-algorithm-alist
55 '((1 . IDEA
) (2 .
3DES
) (4 . CAST5
) (5 . SAFER-SK128
))
56 "Alist of the assigned number to the symmetric key algorithm."
59 (cons (sexp :tag
"Number") (sexp :tag
"Type"))))
61 (defcustom pgg-parse-hash-algorithm-alist
62 '((1 . MD5
) (2 . SHA1
) (3 . RIPEMD160
) (5 . MD2
) (8 . SHA256
) (9 . SHA384
)
64 "Alist of the assigned number to the cryptographic hash algorithm."
67 (cons (sexp :tag
"Number") (sexp :tag
"Type"))))
69 (defcustom pgg-parse-compression-algorithm-alist
70 '((0 . nil
); Uncompressed
73 "Alist of the assigned number to the compression algorithm."
76 (cons (sexp :tag
"Number") (sexp :tag
"Type"))))
78 (defcustom pgg-parse-signature-type-alist
79 '((0 .
"Signature of a binary document")
80 (1 .
"Signature of a canonical text document")
81 (2 .
"Standalone signature")
82 (16 .
"Generic certification of a User ID and Public Key packet")
83 (17 .
"Persona certification of a User ID and Public Key packet")
84 (18 .
"Casual certification of a User ID and Public Key packet")
85 (19 .
"Positive certification of a User ID and Public Key packet")
86 (24 .
"Subkey Binding Signature")
87 (31 .
"Signature directly on a key")
88 (32 .
"Key revocation signature")
89 (40 .
"Subkey revocation signature")
90 (48 .
"Certification revocation signature")
91 (64 .
"Timestamp signature."))
92 "Alist of the assigned number to the signature type."
95 (cons (sexp :tag
"Number") (sexp :tag
"Type"))))
97 (defcustom pgg-ignore-packet-checksum t
; XXX
98 "If non-nil checksum of each ascii armored packet will be ignored."
102 (defvar pgg-armor-header-lines
103 '("^-----BEGIN PGP MESSAGE\\(, PART [0-9]+\\(/[0-9]+\\)?\\)?-----\r?$"
104 "^-----BEGIN PGP PUBLIC KEY BLOCK-----\r?$"
105 "^-----BEGIN PGP PRIVATE KEY BLOCK-----\r?$"
106 "^-----BEGIN PGP SIGNATURE-----\r?$")
110 (defalias 'pgg-char-int
(if (fboundp 'char-int
)
114 (defmacro pgg-format-key-identifier
(string)
115 `(mapconcat (lambda (c) (format "%02X" (pgg-char-int c
)))
117 ;; `(upcase (apply #'format "%02x%02x%02x%02x%02x%02x%02x%02x"
118 ;; (string-to-number-list ,string)))
121 (defmacro pgg-parse-time-field
(bytes)
122 `(list (logior (lsh (car ,bytes
) 8)
124 (logior (lsh (nth 2 ,bytes
) 8)
128 (defmacro pgg-byte-after
(&optional pos
)
129 `(pgg-char-int (char-after ,(or pos
`(point)))))
131 (defmacro pgg-read-byte
()
132 `(pgg-char-int (char-after (prog1 (point) (forward-char)))))
134 (defmacro pgg-read-bytes-string
(nbytes)
136 (point) (prog1 (+ ,nbytes
(point))
137 (forward-char ,nbytes
))))
139 (defmacro pgg-read-bytes
(nbytes)
140 `(mapcar #'pgg-char-int
(pgg-read-bytes-string ,nbytes
))
141 ;; `(string-to-number-list (pgg-read-bytes-string ,nbytes))
144 (defmacro pgg-read-body-string
(ptag)
146 (pgg-read-bytes-string (nth 1 ,ptag
))
147 (pgg-read-bytes-string (- (point-max) (point)))))
149 (defmacro pgg-read-body
(ptag)
150 `(mapcar #'pgg-char-int
(pgg-read-body-string ,ptag
))
151 ;; `(string-to-number-list (pgg-read-body-string ,ptag))
154 (defalias 'pgg-skip-bytes
'forward-char
)
156 (defmacro pgg-skip-header
(ptag)
157 `(pgg-skip-bytes (nth 2 ,ptag
)))
159 (defmacro pgg-skip-body
(ptag)
160 `(pgg-skip-bytes (nth 1 ,ptag
)))
162 (defmacro pgg-set-alist
(alist key value
)
163 `(setq ,alist
(nconc ,alist
(list (cons ,key
,value
)))))
165 (when (fboundp 'define-ccl-program
)
167 (define-ccl-program pgg-parse-crc24
170 (read r0
) (r1 ^
= r0
) (r2 ^
= 0)
174 (r1 += ((r2 >> 15) & 1))
177 ((r1 ^
= 390) (r2 ^
= 19707)))
183 (defvar pgg-parse-crc24
)
185 (defun pgg-parse-crc24-string (string)
186 (let ((h (vector nil
183 1230 nil nil nil nil nil nil
)))
187 (ccl-execute-on-string pgg-parse-crc24 h string
)
189 (logand (aref h
1) 255)
190 (logand (lsh (aref h
2) -
8) 255)
191 (logand (aref h
2) 255)))))
193 (defmacro pgg-parse-length-type
(c)
195 ((< ,c
192) (cons ,c
1))
197 (cons (+ (lsh (- ,c
192) 8)
198 (pgg-byte-after (+ 2 (point)))
202 (cons (cons (logior (lsh (pgg-byte-after (+ 2 (point))) 8)
203 (pgg-byte-after (+ 3 (point))))
204 (logior (lsh (pgg-byte-after (+ 4 (point))) 8)
205 (pgg-byte-after (+ 5 (point)))))
207 (t;partial body length
210 (defun pgg-parse-packet-header ()
211 (let ((ptag (pgg-byte-after))
212 length-type content-tag packet-bytes header-bytes
)
213 (if (zerop (logand 64 ptag
));Old format
215 (setq length-type
(logand ptag
3)
216 length-type
(if (= 3 length-type
) 0 (lsh 1 length-type
))
217 content-tag
(logand 15 (lsh ptag -
2))
219 header-bytes
(1+ length-type
))
220 (dotimes (i length-type
)
222 (logior (lsh packet-bytes
8)
223 (pgg-byte-after (+ 1 i
(point)))))))
224 (setq content-tag
(logand 63 ptag
)
225 length-type
(pgg-parse-length-type
226 (pgg-byte-after (1+ (point))))
227 packet-bytes
(car length-type
)
228 header-bytes
(1+ (cdr length-type
))))
229 (list content-tag packet-bytes header-bytes
)))
231 (defun pgg-parse-packet (ptag)
233 (1 ;Public-Key Encrypted Session Key Packet
234 (pgg-parse-public-key-encrypted-session-key-packet ptag
))
236 (pgg-parse-signature-packet ptag
))
237 (3 ;Symmetric-Key Encrypted Session Key Packet
238 (pgg-parse-symmetric-key-encrypted-session-key-packet ptag
))
239 ;; 4 -- One-Pass Signature Packet
240 ;; 5 -- Secret Key Packet
241 (6 ;Public Key Packet
242 (pgg-parse-public-key-packet ptag
))
243 ;; 7 -- Secret Subkey Packet
244 ;; 8 -- Compressed Data Packet
245 (9 ;Symmetrically Encrypted Data Packet
246 (pgg-read-body-string ptag
))
248 (pgg-read-body-string ptag
))
249 (11 ;Literal Data Packet
250 (pgg-read-body-string ptag
))
251 ;; 12 -- Trust Packet
253 (pgg-read-body-string ptag
))
254 ;; 14 -- Public Subkey Packet
255 ;; 60 .. 63 -- Private or Experimental Values
258 (defun pgg-parse-packets (&optional header-parser body-parser
)
261 (function pgg-parse-packet-header
)))
264 (function pgg-parse-packet
)))
266 (while (> (point-max) (1+ (point)))
267 (setq ptag
(funcall header-parser
))
268 (pgg-skip-header ptag
)
269 (push (cons (car ptag
)
271 (funcall body-parser ptag
)))
273 (if (zerop (nth 1 ptag
))
274 (goto-char (point-max))
275 (forward-char (nth 1 ptag
))))
278 (defun pgg-parse-signature-subpacket-header ()
279 (let ((length-type (pgg-parse-length-type (pgg-byte-after))))
280 (list (pgg-byte-after (+ (cdr length-type
) (point)))
281 (1- (car length-type
))
282 (1+ (cdr length-type
)))))
284 (defun pgg-parse-signature-subpacket (ptag)
286 (2 ;signature creation time
288 (let ((bytes (pgg-read-bytes 4)))
289 (pgg-parse-time-field bytes
))))
290 (3 ;signature expiration time
291 (cons 'signature-expiry
292 (let ((bytes (pgg-read-bytes 4)))
293 (pgg-parse-time-field bytes
))))
294 (4 ;exportable certification
295 (cons 'exportability
(pgg-read-byte)))
297 (cons 'trust-level
(pgg-read-byte)))
298 (6 ;regular expression
299 (cons 'regular-expression
300 (pgg-read-body-string ptag
)))
302 (cons 'revocability
(pgg-read-byte)))
303 (9 ;key expiration time
305 (let ((bytes (pgg-read-bytes 4)))
306 (pgg-parse-time-field bytes
))))
307 ;; 10 = placeholder for backward compatibility
308 (11 ;preferred symmetric algorithms
309 (cons 'preferred-symmetric-key-algorithm
310 (cdr (assq (pgg-read-byte)
311 pgg-parse-symmetric-key-algorithm-alist
))))
315 (cons 'key-identifier
316 (pgg-format-key-identifier (pgg-read-body-string ptag
))))
320 (let ((name-bytes (pgg-read-bytes 2))
321 (value-bytes (pgg-read-bytes 2)))
322 (cons (pgg-read-bytes-string
323 (logior (lsh (car name-bytes
) 8)
325 (pgg-read-bytes-string
326 (logior (lsh (car value-bytes
) 8)
327 (nth 1 value-bytes
)))))))
328 (21 ;preferred hash algorithms
329 (cons 'preferred-hash-algorithm
330 (cdr (assq (pgg-read-byte)
331 pgg-parse-hash-algorithm-alist
))))
332 (22 ;preferred compression algorithms
333 (cons 'preferred-compression-algorithm
334 (cdr (assq (pgg-read-byte)
335 pgg-parse-compression-algorithm-alist
))))
336 (23 ;key server preferences
337 (cons 'key-server-preferences
338 (pgg-read-body ptag
)))
339 (24 ;preferred key server
340 (cons 'preferred-key-server
341 (pgg-read-body-string ptag
)))
342 ;; 25 = primary user id
344 (cons 'policy-url
(pgg-read-body-string ptag
)))
346 ;; 28 = signer's user id
347 ;; 29 = reason for revocation
348 ;; 100 to 110 = internal or user-defined
351 (defun pgg-parse-signature-packet (ptag)
352 (let* ((signature-version (pgg-byte-after))
353 (result (list (cons 'version signature-version
)))
354 hashed-material field n
)
356 ((= signature-version
3)
358 (setq hashed-material
(pgg-read-bytes 5))
359 (pgg-set-alist result
361 (cdr (assq (pop hashed-material
)
362 pgg-parse-signature-type-alist
)))
363 (pgg-set-alist result
365 (pgg-parse-time-field hashed-material
))
366 (pgg-set-alist result
368 (pgg-format-key-identifier
369 (pgg-read-bytes-string 8)))
370 (pgg-set-alist result
371 'public-key-algorithm
(pgg-read-byte))
372 (pgg-set-alist result
373 'hash-algorithm
(pgg-read-byte)))
374 ((= signature-version
4)
376 (pgg-set-alist result
378 (cdr (assq (pgg-read-byte)
379 pgg-parse-signature-type-alist
)))
380 (pgg-set-alist result
381 'public-key-algorithm
383 (pgg-set-alist result
384 'hash-algorithm
(pgg-read-byte))
385 (when (>= 10000 (setq n
(pgg-read-bytes 2)
386 n
(logior (lsh (car n
) 8)
389 (narrow-to-region (point)(+ n
(point)))
391 (mapcar (function cdr
) ;remove packet types
393 #'pgg-parse-signature-subpacket-header
394 #'pgg-parse-signature-subpacket
)))
395 (goto-char (point-max))))
396 (when (>= 10000 (setq n
(pgg-read-bytes 2)
397 n
(logior (lsh (car n
) 8)
400 (narrow-to-region (point)(+ n
(point)))
402 (mapcar (function cdr
) ;remove packet types
404 #'pgg-parse-signature-subpacket-header
405 #'pgg-parse-signature-subpacket
)))))))
407 (setcdr (setq field
(assq 'public-key-algorithm
409 (cdr (assq (cdr field
)
410 pgg-parse-public-key-algorithm-alist
)))
411 (setcdr (setq field
(assq 'hash-algorithm
413 (cdr (assq (cdr field
)
414 pgg-parse-hash-algorithm-alist
)))
417 (defun pgg-parse-public-key-encrypted-session-key-packet (ptag)
419 (pgg-set-alist result
420 'version
(pgg-read-byte))
421 (pgg-set-alist result
423 (pgg-format-key-identifier
424 (pgg-read-bytes-string 8)))
425 (pgg-set-alist result
426 'public-key-algorithm
427 (cdr (assq (pgg-read-byte)
428 pgg-parse-public-key-algorithm-alist
)))
431 (defun pgg-parse-symmetric-key-encrypted-session-key-packet (ptag)
433 (pgg-set-alist result
436 (pgg-set-alist result
437 'symmetric-key-algorithm
438 (cdr (assq (pgg-read-byte)
439 pgg-parse-symmetric-key-algorithm-alist
)))
442 (defun pgg-parse-public-key-packet (ptag)
443 (let* ((key-version (pgg-read-byte))
444 (result (list (cons 'version key-version
)))
448 (pgg-set-alist result
450 (let ((bytes (pgg-read-bytes 4)))
451 (pgg-parse-time-field bytes
)))
452 (pgg-set-alist result
453 'key-expiry
(pgg-read-bytes 2))
454 (pgg-set-alist result
455 'public-key-algorithm
(pgg-read-byte)))
457 (pgg-set-alist result
459 (let ((bytes (pgg-read-bytes 4)))
460 (pgg-parse-time-field bytes
)))
461 (pgg-set-alist result
462 'public-key-algorithm
(pgg-read-byte))))
464 (setcdr (setq field
(assq 'public-key-algorithm
466 (cdr (assq (cdr field
)
467 pgg-parse-public-key-algorithm-alist
)))
470 ;; p-d-p only calls this if it is defined, but the compiler does not
472 (declare-function pgg-parse-crc24-string
"pgg-parse" (string))
474 (defun pgg-decode-packets ()
475 (if (re-search-forward "^=\\([A-Za-z0-9+/]\\{4\\}\\)$" nil t
)
476 (let ((p (match-beginning 0))
477 (checksum (match-string 1)))
478 (delete-region p
(point-max))
479 (if (ignore-errors (base64-decode-region (point-min) p
))
480 (or (not (fboundp 'pgg-parse-crc24-string
))
481 pgg-ignore-packet-checksum
482 (string-equal (base64-encode-string (pgg-parse-crc24-string
486 (message "PGP packet checksum does not match")
488 (message "PGP packet contain invalid base64")
490 (message "PGP packet checksum not found")
493 (defun pgg-decode-armor-region (start end
)
495 (narrow-to-region start end
)
496 (goto-char (point-min))
497 (re-search-forward "^-+BEGIN PGP" nil t
)
498 (delete-region (point-min)
499 (and (search-forward "\n\n")
501 (when (pgg-decode-packets)
502 (goto-char (point-min))
503 (pgg-parse-packets))))
505 (defun pgg-parse-armor (string)
507 (buffer-disable-undo)
508 (unless (featurep 'xemacs
)
509 (set-buffer-multibyte nil
))
511 (pgg-decode-armor-region (point-min)(point))))
514 (defalias 'pgg-string-as-unibyte
(if (fboundp 'string-as-unibyte
)
518 (defun pgg-parse-armor-region (start end
)
519 (pgg-parse-armor (pgg-string-as-unibyte (buffer-substring start end
))))
523 ;;; pgg-parse.el ends here