1 ;;; pgg-parse.el --- OpenPGP packet parsing
3 ;; Copyright (C) 1999, 2003 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., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, 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))
60 "Alist of the assigned number to the cryptographic hash algorithm."
63 (cons (sexp :tag "Number") (sexp :tag "Type"))))
65 (defcustom pgg-parse-compression-algorithm-alist
66 '((0 . nil); Uncompressed
69 "Alist of the assigned number to the compression algorithm."
72 (cons (sexp :tag "Number") (sexp :tag "Type"))))
74 (defcustom pgg-parse-signature-type-alist
75 '((0 . "Signature of a binary document")
76 (1 . "Signature of a canonical text document")
77 (2 . "Standalone signature")
78 (16 . "Generic certification of a User ID and Public Key packet")
79 (17 . "Persona certification of a User ID and Public Key packet")
80 (18 . "Casual certification of a User ID and Public Key packet")
81 (19 . "Positive certification of a User ID and Public Key packet")
82 (24 . "Subkey Binding Signature")
83 (31 . "Signature directly on a key")
84 (32 . "Key revocation signature")
85 (40 . "Subkey revocation signature")
86 (48 . "Certification revocation signature")
87 (64 . "Timestamp signature."))
88 "Alist of the assigned number to the signature type."
91 (cons (sexp :tag "Number") (sexp :tag "Type"))))
93 (defcustom pgg-ignore-packet-checksum t; XXX
94 "If non-nil checksum of each ascii armored packet will be ignored."
98 (defvar pgg-armor-header-lines
99 '("^-----BEGIN PGP MESSAGE\\(, PART [0-9]+\\(/[0-9]+\\)?\\)?-----\r?$"
100 "^-----BEGIN PGP PUBLIC KEY BLOCK-----\r?$"
101 "^-----BEGIN PGP PRIVATE KEY BLOCK-----\r?$"
102 "^-----BEGIN PGP SIGNATURE-----\r?$")
106 (defalias 'pgg-char-int (if (fboundp 'char-int)
110 (defmacro pgg-format-key-identifier (string)
111 `(mapconcat (lambda (c) (format "%02X" (pgg-char-int c)))
113 ;; `(upcase (apply #'format "%02x%02x%02x%02x%02x%02x%02x%02x"
114 ;; (string-to-int-list ,string)))
117 (defmacro pgg-parse-time-field (bytes)
118 `(list (logior (lsh (car ,bytes) 8)
120 (logior (lsh (nth 2 ,bytes) 8)
124 (defmacro pgg-byte-after (&optional pos)
125 `(pgg-char-int (char-after ,(or pos `(point)))))
127 (defmacro pgg-read-byte ()
128 `(pgg-char-int (char-after (prog1 (point) (forward-char)))))
130 (defmacro pgg-read-bytes-string (nbytes)
132 (point) (prog1 (+ ,nbytes (point))
133 (forward-char ,nbytes))))
135 (defmacro pgg-read-bytes (nbytes)
136 `(mapcar #'pgg-char-int (pgg-read-bytes-string ,nbytes))
137 ;; `(string-to-int-list (pgg-read-bytes-string ,nbytes))
140 (defmacro pgg-read-body-string (ptag)
142 (pgg-read-bytes-string (nth 1 ,ptag))
143 (pgg-read-bytes-string (- (point-max) (point)))))
145 (defmacro pgg-read-body (ptag)
146 `(mapcar #'pgg-char-int (pgg-read-body-string ,ptag))
147 ;; `(string-to-int-list (pgg-read-body-string ,ptag))
150 (defalias 'pgg-skip-bytes 'forward-char)
152 (defmacro pgg-skip-header (ptag)
153 `(pgg-skip-bytes (nth 2 ,ptag)))
155 (defmacro pgg-skip-body (ptag)
156 `(pgg-skip-bytes (nth 1 ,ptag)))
158 (defmacro pgg-set-alist (alist key value)
159 `(setq ,alist (nconc ,alist (list (cons ,key ,value)))))
161 (when (fboundp 'define-ccl-program)
163 (define-ccl-program pgg-parse-crc24
166 (read r0) (r1 ^= r0) (r2 ^= 0)
170 (r1 += ((r2 >> 15) & 1))
173 ((r1 ^= 390) (r2 ^= 19707)))
179 (defun pgg-parse-crc24-string (string)
180 (let ((h (vector nil 183 1230 nil nil nil nil nil nil)))
181 (ccl-execute-on-string pgg-parse-crc24 h string)
183 (logand (aref h 1) 255)
184 (logand (lsh (aref h 2) -8) 255)
185 (logand (aref h 2) 255)))))
187 (defmacro pgg-parse-length-type (c)
189 ((< ,c 192) (cons ,c 1))
191 (cons (+ (lsh (- ,c 192) 8)
192 (pgg-byte-after (+ 2 (point)))
196 (cons (cons (logior (lsh (pgg-byte-after (+ 2 (point))) 8)
197 (pgg-byte-after (+ 3 (point))))
198 (logior (lsh (pgg-byte-after (+ 4 (point))) 8)
199 (pgg-byte-after (+ 5 (point)))))
201 (t;partial body length
204 (defun pgg-parse-packet-header ()
205 (let ((ptag (pgg-byte-after))
206 length-type content-tag packet-bytes header-bytes)
207 (if (zerop (logand 64 ptag));Old format
209 (setq length-type (logand ptag 3)
210 length-type (if (= 3 length-type) 0 (lsh 1 length-type))
211 content-tag (logand 15 (lsh ptag -2))
213 header-bytes (1+ length-type))
214 (dotimes (i length-type)
216 (logior (lsh packet-bytes 8)
217 (pgg-byte-after (+ 1 i (point)))))))
218 (setq content-tag (logand 63 ptag)
219 length-type (pgg-parse-length-type
220 (pgg-byte-after (1+ (point))))
221 packet-bytes (car length-type)
222 header-bytes (1+ (cdr length-type))))
223 (list content-tag packet-bytes header-bytes)))
225 (defun pgg-parse-packet (ptag)
227 (1 ;Public-Key Encrypted Session Key Packet
228 (pgg-parse-public-key-encrypted-session-key-packet ptag))
230 (pgg-parse-signature-packet ptag))
231 (3 ;Symmetric-Key Encrypted Session Key Packet
232 (pgg-parse-symmetric-key-encrypted-session-key-packet ptag))
233 ;; 4 -- One-Pass Signature Packet
234 ;; 5 -- Secret Key Packet
235 (6 ;Public Key Packet
236 (pgg-parse-public-key-packet ptag))
237 ;; 7 -- Secret Subkey Packet
238 ;; 8 -- Compressed Data Packet
239 (9 ;Symmetrically Encrypted Data Packet
240 (pgg-read-body-string ptag))
242 (pgg-read-body-string ptag))
243 (11 ;Literal Data Packet
244 (pgg-read-body-string ptag))
245 ;; 12 -- Trust Packet
247 (pgg-read-body-string ptag))
248 ;; 14 -- Public Subkey Packet
249 ;; 60 .. 63 -- Private or Experimental Values
252 (defun pgg-parse-packets (&optional header-parser body-parser)
255 (function pgg-parse-packet-header)))
258 (function pgg-parse-packet)))
260 (while (> (point-max) (1+ (point)))
261 (setq ptag (funcall header-parser))
262 (pgg-skip-header ptag)
263 (push (cons (car ptag)
265 (funcall body-parser ptag)))
267 (if (zerop (nth 1 ptag))
268 (goto-char (point-max))
269 (forward-char (nth 1 ptag))))
272 (defun pgg-parse-signature-subpacket-header ()
273 (let ((length-type (pgg-parse-length-type (pgg-byte-after))))
274 (list (pgg-byte-after (+ (cdr length-type) (point)))
275 (1- (car length-type))
276 (1+ (cdr length-type)))))
278 (defun pgg-parse-signature-subpacket (ptag)
280 (2 ;signature creation time
282 (let ((bytes (pgg-read-bytes 4)))
283 (pgg-parse-time-field bytes))))
284 (3 ;signature expiration time
285 (cons 'signature-expiry
286 (let ((bytes (pgg-read-bytes 4)))
287 (pgg-parse-time-field bytes))))
288 (4 ;exportable certification
289 (cons 'exportability (pgg-read-byte)))
291 (cons 'trust-level (pgg-read-byte)))
292 (6 ;regular expression
293 (cons 'regular-expression
294 (pgg-read-body-string ptag)))
296 (cons 'revocability (pgg-read-byte)))
297 (9 ;key expiration time
299 (let ((bytes (pgg-read-bytes 4)))
300 (pgg-parse-time-field bytes))))
301 ;; 10 = placeholder for backward compatibility
302 (11 ;preferred symmetric algorithms
303 (cons 'preferred-symmetric-key-algorithm
304 (cdr (assq (pgg-read-byte)
305 pgg-parse-symmetric-key-algorithm-alist))))
309 (cons 'key-identifier
310 (pgg-format-key-identifier (pgg-read-body-string ptag))))
314 (let ((name-bytes (pgg-read-bytes 2))
315 (value-bytes (pgg-read-bytes 2)))
316 (cons (pgg-read-bytes-string
317 (logior (lsh (car name-bytes) 8)
319 (pgg-read-bytes-string
320 (logior (lsh (car value-bytes) 8)
321 (nth 1 value-bytes)))))))
322 (21 ;preferred hash algorithms
323 (cons 'preferred-hash-algorithm
324 (cdr (assq (pgg-read-byte)
325 pgg-parse-hash-algorithm-alist))))
326 (22 ;preferred compression algorithms
327 (cons 'preferred-compression-algorithm
328 (cdr (assq (pgg-read-byte)
329 pgg-parse-compression-algorithm-alist))))
330 (23 ;key server preferences
331 (cons 'key-server-preferences
332 (pgg-read-body ptag)))
333 (24 ;preferred key server
334 (cons 'preferred-key-server
335 (pgg-read-body-string ptag)))
336 ;; 25 = primary user id
338 (cons 'policy-url (pgg-read-body-string ptag)))
340 ;; 28 = signer's user id
341 ;; 29 = reason for revocation
342 ;; 100 to 110 = internal or user-defined
345 (defun pgg-parse-signature-packet (ptag)
346 (let* ((signature-version (pgg-byte-after))
347 (result (list (cons 'version signature-version)))
348 hashed-material field n)
350 ((= signature-version 3)
352 (setq hashed-material (pgg-read-bytes 5))
353 (pgg-set-alist result
355 (cdr (assq (pop hashed-material)
356 pgg-parse-signature-type-alist)))
357 (pgg-set-alist result
359 (pgg-parse-time-field hashed-material))
360 (pgg-set-alist result
362 (pgg-format-key-identifier
363 (pgg-read-bytes-string 8)))
364 (pgg-set-alist result
365 'public-key-algorithm (pgg-read-byte))
366 (pgg-set-alist result
367 'hash-algorithm (pgg-read-byte)))
368 ((= signature-version 4)
370 (pgg-set-alist result
372 (cdr (assq (pgg-read-byte)
373 pgg-parse-signature-type-alist)))
374 (pgg-set-alist result
375 'public-key-algorithm
377 (pgg-set-alist result
378 'hash-algorithm (pgg-read-byte))
379 (when (>= 10000 (setq n (pgg-read-bytes 2)
380 n (logior (lsh (car n) 8)
383 (narrow-to-region (point)(+ n (point)))
385 (mapcar (function cdr) ;remove packet types
387 #'pgg-parse-signature-subpacket-header
388 #'pgg-parse-signature-subpacket)))
389 (goto-char (point-max))))
390 (when (>= 10000 (setq n (pgg-read-bytes 2)
391 n (logior (lsh (car n) 8)
394 (narrow-to-region (point)(+ n (point)))
396 (mapcar (function cdr) ;remove packet types
398 #'pgg-parse-signature-subpacket-header
399 #'pgg-parse-signature-subpacket)))))))
401 (setcdr (setq field (assq 'public-key-algorithm
403 (cdr (assq (cdr field)
404 pgg-parse-public-key-algorithm-alist)))
405 (setcdr (setq field (assq 'hash-algorithm
407 (cdr (assq (cdr field)
408 pgg-parse-hash-algorithm-alist)))
411 (defun pgg-parse-public-key-encrypted-session-key-packet (ptag)
413 (pgg-set-alist result
414 'version (pgg-read-byte))
415 (pgg-set-alist result
417 (pgg-format-key-identifier
418 (pgg-read-bytes-string 8)))
419 (pgg-set-alist result
420 'public-key-algorithm
421 (cdr (assq (pgg-read-byte)
422 pgg-parse-public-key-algorithm-alist)))
425 (defun pgg-parse-symmetric-key-encrypted-session-key-packet (ptag)
427 (pgg-set-alist result
430 (pgg-set-alist result
431 'symmetric-key-algorithm
432 (cdr (assq (pgg-read-byte)
433 pgg-parse-symmetric-key-algorithm-alist)))
436 (defun pgg-parse-public-key-packet (ptag)
437 (let* ((key-version (pgg-read-byte))
438 (result (list (cons 'version key-version)))
442 (pgg-set-alist result
444 (let ((bytes (pgg-read-bytes 4)))
445 (pgg-parse-time-field bytes)))
446 (pgg-set-alist result
447 'key-expiry (pgg-read-bytes 2))
448 (pgg-set-alist result
449 'public-key-algorithm (pgg-read-byte)))
451 (pgg-set-alist result
453 (let ((bytes (pgg-read-bytes 4)))
454 (pgg-parse-time-field bytes)))
455 (pgg-set-alist result
456 'public-key-algorithm (pgg-read-byte))))
458 (setcdr (setq field (assq 'public-key-algorithm
460 (cdr (assq (cdr field)
461 pgg-parse-public-key-algorithm-alist)))
464 (defun pgg-decode-packets ()
465 (if (re-search-forward "^=\\([A-Za-z0-9+/]\\{4\\}\\)$" nil t)
466 (let ((p (match-beginning 0))
467 (checksum (match-string 1)))
468 (delete-region p (point-max))
469 (if (ignore-errors (base64-decode-region (point-min) p))
470 (or (not (fboundp 'pgg-parse-crc24-string))
471 pgg-ignore-packet-checksum
472 (string-equal (base64-encode-string (pgg-parse-crc24-string
476 (message "PGP packet checksum does not match")
478 (message "PGP packet contain invalid base64")
480 (message "PGP packet checksum not found")
483 (defun pgg-decode-armor-region (start end)
485 (narrow-to-region start end)
486 (goto-char (point-min))
487 (re-search-forward "^-+BEGIN PGP" nil t)
488 (delete-region (point-min)
489 (and (search-forward "\n\n")
491 (when (pgg-decode-packets)
492 (goto-char (point-min))
493 (pgg-parse-packets))))
495 (defun pgg-parse-armor (string)
497 (buffer-disable-undo)
498 (if (fboundp 'set-buffer-multibyte)
499 (set-buffer-multibyte nil))
501 (pgg-decode-armor-region (point-min)(point))))
504 (defalias 'pgg-string-as-unibyte (if (fboundp 'string-as-unibyte)
508 (defun pgg-parse-armor-region (start end)
509 (pgg-parse-armor (pgg-string-as-unibyte (buffer-substring start end))))
513 ;;; pgg-parse.el ends here