1 ;;; pgg-parse.el --- OpenPGP packet parsing
3 ;; Copyright (C) 1999, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
5 ;; Author: Daiki Ueno <ueno@unixuser.org>
7 ;; Keywords: PGP, OpenPGP, GnuPG
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
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>
38 (eval-when-compile (require 'cl))
40 (defgroup pgg-parse ()
41 "OpenPGP packet parsing."
44 (defcustom pgg-parse-public-key-algorithm-alist
45 '((1 . RSA) (2 . RSA-E) (3 . RSA-S) (16 . ELG-E) (17 . DSA) (20 . ELG))
46 "Alist of the assigned number to the public key algorithm."
49 (cons (sexp :tag "Number") (sexp :tag "Type"))))
51 (defcustom pgg-parse-symmetric-key-algorithm-alist
52 '((1 . IDEA) (2 . 3DES) (4 . CAST5) (5 . SAFER-SK128))
53 "Alist of the assigned number to the simmetric key algorithm."
56 (cons (sexp :tag "Number") (sexp :tag "Type"))))
58 (defcustom pgg-parse-hash-algorithm-alist
59 '((1 . MD5) (2 . SHA1) (3 . RIPEMD160) (5 . MD2) (8 . SHA256) (9 . SHA384)
61 "Alist of the assigned number to the cryptographic hash algorithm."
64 (cons (sexp :tag "Number") (sexp :tag "Type"))))
66 (defcustom pgg-parse-compression-algorithm-alist
67 '((0 . nil); Uncompressed
70 "Alist of the assigned number to the compression algorithm."
73 (cons (sexp :tag "Number") (sexp :tag "Type"))))
75 (defcustom pgg-parse-signature-type-alist
76 '((0 . "Signature of a binary document")
77 (1 . "Signature of a canonical text document")
78 (2 . "Standalone signature")
79 (16 . "Generic certification of a User ID and Public Key packet")
80 (17 . "Persona certification of a User ID and Public Key packet")
81 (18 . "Casual certification of a User ID and Public Key packet")
82 (19 . "Positive certification of a User ID and Public Key packet")
83 (24 . "Subkey Binding Signature")
84 (31 . "Signature directly on a key")
85 (32 . "Key revocation signature")
86 (40 . "Subkey revocation signature")
87 (48 . "Certification revocation signature")
88 (64 . "Timestamp signature."))
89 "Alist of the assigned number to the signature type."
92 (cons (sexp :tag "Number") (sexp :tag "Type"))))
94 (defcustom pgg-ignore-packet-checksum t; XXX
95 "If non-nil checksum of each ascii armored packet will be ignored."
99 (defvar pgg-armor-header-lines
100 '("^-----BEGIN PGP MESSAGE\\(, PART [0-9]+\\(/[0-9]+\\)?\\)?-----\r?$"
101 "^-----BEGIN PGP PUBLIC KEY BLOCK-----\r?$"
102 "^-----BEGIN PGP PRIVATE KEY BLOCK-----\r?$"
103 "^-----BEGIN PGP SIGNATURE-----\r?$")
107 (defalias 'pgg-char-int (if (fboundp 'char-int)
111 (defmacro pgg-format-key-identifier (string)
112 `(mapconcat (lambda (c) (format "%02X" (pgg-char-int c)))
114 ;; `(upcase (apply #'format "%02x%02x%02x%02x%02x%02x%02x%02x"
115 ;; (string-to-number-list ,string)))
118 (defmacro pgg-parse-time-field (bytes)
119 `(list (logior (lsh (car ,bytes) 8)
121 (logior (lsh (nth 2 ,bytes) 8)
125 (defmacro pgg-byte-after (&optional pos)
126 `(pgg-char-int (char-after ,(or pos `(point)))))
128 (defmacro pgg-read-byte ()
129 `(pgg-char-int (char-after (prog1 (point) (forward-char)))))
131 (defmacro pgg-read-bytes-string (nbytes)
133 (point) (prog1 (+ ,nbytes (point))
134 (forward-char ,nbytes))))
136 (defmacro pgg-read-bytes (nbytes)
137 `(mapcar #'pgg-char-int (pgg-read-bytes-string ,nbytes))
138 ;; `(string-to-number-list (pgg-read-bytes-string ,nbytes))
141 (defmacro pgg-read-body-string (ptag)
143 (pgg-read-bytes-string (nth 1 ,ptag))
144 (pgg-read-bytes-string (- (point-max) (point)))))
146 (defmacro pgg-read-body (ptag)
147 `(mapcar #'pgg-char-int (pgg-read-body-string ,ptag))
148 ;; `(string-to-number-list (pgg-read-body-string ,ptag))
151 (defalias 'pgg-skip-bytes 'forward-char)
153 (defmacro pgg-skip-header (ptag)
154 `(pgg-skip-bytes (nth 2 ,ptag)))
156 (defmacro pgg-skip-body (ptag)
157 `(pgg-skip-bytes (nth 1 ,ptag)))
159 (defmacro pgg-set-alist (alist key value)
160 `(setq ,alist (nconc ,alist (list (cons ,key ,value)))))
162 (when (fboundp 'define-ccl-program)
164 (define-ccl-program pgg-parse-crc24
167 (read r0) (r1 ^= r0) (r2 ^= 0)
171 (r1 += ((r2 >> 15) & 1))
174 ((r1 ^= 390) (r2 ^= 19707)))
180 (defun pgg-parse-crc24-string (string)
181 (let ((h (vector nil 183 1230 nil nil nil nil nil nil)))
182 (ccl-execute-on-string pgg-parse-crc24 h string)
184 (logand (aref h 1) 255)
185 (logand (lsh (aref h 2) -8) 255)
186 (logand (aref h 2) 255)))))
188 (defmacro pgg-parse-length-type (c)
190 ((< ,c 192) (cons ,c 1))
192 (cons (+ (lsh (- ,c 192) 8)
193 (pgg-byte-after (+ 2 (point)))
197 (cons (cons (logior (lsh (pgg-byte-after (+ 2 (point))) 8)
198 (pgg-byte-after (+ 3 (point))))
199 (logior (lsh (pgg-byte-after (+ 4 (point))) 8)
200 (pgg-byte-after (+ 5 (point)))))
202 (t;partial body length
205 (defun pgg-parse-packet-header ()
206 (let ((ptag (pgg-byte-after))
207 length-type content-tag packet-bytes header-bytes)
208 (if (zerop (logand 64 ptag));Old format
210 (setq length-type (logand ptag 3)
211 length-type (if (= 3 length-type) 0 (lsh 1 length-type))
212 content-tag (logand 15 (lsh ptag -2))
214 header-bytes (1+ length-type))
215 (dotimes (i length-type)
217 (logior (lsh packet-bytes 8)
218 (pgg-byte-after (+ 1 i (point)))))))
219 (setq content-tag (logand 63 ptag)
220 length-type (pgg-parse-length-type
221 (pgg-byte-after (1+ (point))))
222 packet-bytes (car length-type)
223 header-bytes (1+ (cdr length-type))))
224 (list content-tag packet-bytes header-bytes)))
226 (defun pgg-parse-packet (ptag)
228 (1 ;Public-Key Encrypted Session Key Packet
229 (pgg-parse-public-key-encrypted-session-key-packet ptag))
231 (pgg-parse-signature-packet ptag))
232 (3 ;Symmetric-Key Encrypted Session Key Packet
233 (pgg-parse-symmetric-key-encrypted-session-key-packet ptag))
234 ;; 4 -- One-Pass Signature Packet
235 ;; 5 -- Secret Key Packet
236 (6 ;Public Key Packet
237 (pgg-parse-public-key-packet ptag))
238 ;; 7 -- Secret Subkey Packet
239 ;; 8 -- Compressed Data Packet
240 (9 ;Symmetrically Encrypted Data Packet
241 (pgg-read-body-string ptag))
243 (pgg-read-body-string ptag))
244 (11 ;Literal Data Packet
245 (pgg-read-body-string ptag))
246 ;; 12 -- Trust Packet
248 (pgg-read-body-string ptag))
249 ;; 14 -- Public Subkey Packet
250 ;; 60 .. 63 -- Private or Experimental Values
253 (defun pgg-parse-packets (&optional header-parser body-parser)
256 (function pgg-parse-packet-header)))
259 (function pgg-parse-packet)))
261 (while (> (point-max) (1+ (point)))
262 (setq ptag (funcall header-parser))
263 (pgg-skip-header ptag)
264 (push (cons (car ptag)
266 (funcall body-parser ptag)))
268 (if (zerop (nth 1 ptag))
269 (goto-char (point-max))
270 (forward-char (nth 1 ptag))))
273 (defun pgg-parse-signature-subpacket-header ()
274 (let ((length-type (pgg-parse-length-type (pgg-byte-after))))
275 (list (pgg-byte-after (+ (cdr length-type) (point)))
276 (1- (car length-type))
277 (1+ (cdr length-type)))))
279 (defun pgg-parse-signature-subpacket (ptag)
281 (2 ;signature creation time
283 (let ((bytes (pgg-read-bytes 4)))
284 (pgg-parse-time-field bytes))))
285 (3 ;signature expiration time
286 (cons 'signature-expiry
287 (let ((bytes (pgg-read-bytes 4)))
288 (pgg-parse-time-field bytes))))
289 (4 ;exportable certification
290 (cons 'exportability (pgg-read-byte)))
292 (cons 'trust-level (pgg-read-byte)))
293 (6 ;regular expression
294 (cons 'regular-expression
295 (pgg-read-body-string ptag)))
297 (cons 'revocability (pgg-read-byte)))
298 (9 ;key expiration time
300 (let ((bytes (pgg-read-bytes 4)))
301 (pgg-parse-time-field bytes))))
302 ;; 10 = placeholder for backward compatibility
303 (11 ;preferred symmetric algorithms
304 (cons 'preferred-symmetric-key-algorithm
305 (cdr (assq (pgg-read-byte)
306 pgg-parse-symmetric-key-algorithm-alist))))
310 (cons 'key-identifier
311 (pgg-format-key-identifier (pgg-read-body-string ptag))))
315 (let ((name-bytes (pgg-read-bytes 2))
316 (value-bytes (pgg-read-bytes 2)))
317 (cons (pgg-read-bytes-string
318 (logior (lsh (car name-bytes) 8)
320 (pgg-read-bytes-string
321 (logior (lsh (car value-bytes) 8)
322 (nth 1 value-bytes)))))))
323 (21 ;preferred hash algorithms
324 (cons 'preferred-hash-algorithm
325 (cdr (assq (pgg-read-byte)
326 pgg-parse-hash-algorithm-alist))))
327 (22 ;preferred compression algorithms
328 (cons 'preferred-compression-algorithm
329 (cdr (assq (pgg-read-byte)
330 pgg-parse-compression-algorithm-alist))))
331 (23 ;key server preferences
332 (cons 'key-server-preferences
333 (pgg-read-body ptag)))
334 (24 ;preferred key server
335 (cons 'preferred-key-server
336 (pgg-read-body-string ptag)))
337 ;; 25 = primary user id
339 (cons 'policy-url (pgg-read-body-string ptag)))
341 ;; 28 = signer's user id
342 ;; 29 = reason for revocation
343 ;; 100 to 110 = internal or user-defined
346 (defun pgg-parse-signature-packet (ptag)
347 (let* ((signature-version (pgg-byte-after))
348 (result (list (cons 'version signature-version)))
349 hashed-material field n)
351 ((= signature-version 3)
353 (setq hashed-material (pgg-read-bytes 5))
354 (pgg-set-alist result
356 (cdr (assq (pop hashed-material)
357 pgg-parse-signature-type-alist)))
358 (pgg-set-alist result
360 (pgg-parse-time-field hashed-material))
361 (pgg-set-alist result
363 (pgg-format-key-identifier
364 (pgg-read-bytes-string 8)))
365 (pgg-set-alist result
366 'public-key-algorithm (pgg-read-byte))
367 (pgg-set-alist result
368 'hash-algorithm (pgg-read-byte)))
369 ((= signature-version 4)
371 (pgg-set-alist result
373 (cdr (assq (pgg-read-byte)
374 pgg-parse-signature-type-alist)))
375 (pgg-set-alist result
376 'public-key-algorithm
378 (pgg-set-alist result
379 'hash-algorithm (pgg-read-byte))
380 (when (>= 10000 (setq n (pgg-read-bytes 2)
381 n (logior (lsh (car n) 8)
384 (narrow-to-region (point)(+ n (point)))
386 (mapcar (function cdr) ;remove packet types
388 #'pgg-parse-signature-subpacket-header
389 #'pgg-parse-signature-subpacket)))
390 (goto-char (point-max))))
391 (when (>= 10000 (setq n (pgg-read-bytes 2)
392 n (logior (lsh (car n) 8)
395 (narrow-to-region (point)(+ n (point)))
397 (mapcar (function cdr) ;remove packet types
399 #'pgg-parse-signature-subpacket-header
400 #'pgg-parse-signature-subpacket)))))))
402 (setcdr (setq field (assq 'public-key-algorithm
404 (cdr (assq (cdr field)
405 pgg-parse-public-key-algorithm-alist)))
406 (setcdr (setq field (assq 'hash-algorithm
408 (cdr (assq (cdr field)
409 pgg-parse-hash-algorithm-alist)))
412 (defun pgg-parse-public-key-encrypted-session-key-packet (ptag)
414 (pgg-set-alist result
415 'version (pgg-read-byte))
416 (pgg-set-alist result
418 (pgg-format-key-identifier
419 (pgg-read-bytes-string 8)))
420 (pgg-set-alist result
421 'public-key-algorithm
422 (cdr (assq (pgg-read-byte)
423 pgg-parse-public-key-algorithm-alist)))
426 (defun pgg-parse-symmetric-key-encrypted-session-key-packet (ptag)
428 (pgg-set-alist result
431 (pgg-set-alist result
432 'symmetric-key-algorithm
433 (cdr (assq (pgg-read-byte)
434 pgg-parse-symmetric-key-algorithm-alist)))
437 (defun pgg-parse-public-key-packet (ptag)
438 (let* ((key-version (pgg-read-byte))
439 (result (list (cons 'version key-version)))
443 (pgg-set-alist result
445 (let ((bytes (pgg-read-bytes 4)))
446 (pgg-parse-time-field bytes)))
447 (pgg-set-alist result
448 'key-expiry (pgg-read-bytes 2))
449 (pgg-set-alist result
450 'public-key-algorithm (pgg-read-byte)))
452 (pgg-set-alist result
454 (let ((bytes (pgg-read-bytes 4)))
455 (pgg-parse-time-field bytes)))
456 (pgg-set-alist result
457 'public-key-algorithm (pgg-read-byte))))
459 (setcdr (setq field (assq 'public-key-algorithm
461 (cdr (assq (cdr field)
462 pgg-parse-public-key-algorithm-alist)))
465 (defun pgg-decode-packets ()
466 (if (re-search-forward "^=\\([A-Za-z0-9+/]\\{4\\}\\)$" nil t)
467 (let ((p (match-beginning 0))
468 (checksum (match-string 1)))
469 (delete-region p (point-max))
470 (if (ignore-errors (base64-decode-region (point-min) p))
471 (or (not (fboundp 'pgg-parse-crc24-string))
472 pgg-ignore-packet-checksum
473 (string-equal (base64-encode-string (pgg-parse-crc24-string
477 (message "PGP packet checksum does not match")
479 (message "PGP packet contain invalid base64")
481 (message "PGP packet checksum not found")
484 (defun pgg-decode-armor-region (start end)
486 (narrow-to-region start end)
487 (goto-char (point-min))
488 (re-search-forward "^-+BEGIN PGP" nil t)
489 (delete-region (point-min)
490 (and (search-forward "\n\n")
492 (when (pgg-decode-packets)
493 (goto-char (point-min))
494 (pgg-parse-packets))))
496 (defun pgg-parse-armor (string)
498 (buffer-disable-undo)
499 (if (fboundp 'set-buffer-multibyte)
500 (set-buffer-multibyte nil))
502 (pgg-decode-armor-region (point-min)(point))))
505 (defalias 'pgg-string-as-unibyte (if (fboundp 'string-as-unibyte)
509 (defun pgg-parse-armor-region (start end)
510 (pgg-parse-armor (pgg-string-as-unibyte (buffer-substring start end))))
514 ;;; arch-tag: 16c2eb82-1313-4a7c-a70f-420709b5b43e
515 ;;; pgg-parse.el ends here