1 ;;; pgg-parse.el --- OpenPGP packet parsing
3 ;; Copyright (C) 1999 Daiki Ueno
5 ;; Author: Daiki Ueno <ueno@ueda.info.waseda.ac.jp>
7 ;; Keywords: PGP, OpenPGP, GnuPG
9 ;; This file is part of SEMI (Secure Emacs MIME Interface).
11 ;; This program is free software; you can redistribute it and/or
12 ;; modify it under the terms of the GNU General Public License as
13 ;; published by the Free Software Foundation; either version 2, or (at
14 ;; your option) any later version.
16 ;; This program is distributed in the hope that it will be useful, but
17 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 ;; 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 (eval-when-compile (require 'static))
47 (defgroup pgg-parse ()
48 "OpenPGP packet parsing"
51 (defcustom pgg-parse-public-key-algorithm-alist
52 '((1 . RSA) (2 . RSA-E) (3 . RSA-S) (16 . ELG-E) (17 . DSA) (20 . ELG))
53 "Alist of the assigned number to the public key algorithm."
57 (defcustom pgg-parse-symmetric-key-algorithm-alist
58 '((1 . IDEA) (2 . 3DES) (4 . CAST5) (5 . SAFER-SK128))
59 "Alist of the assigned number to the simmetric key algorithm."
63 (defcustom pgg-parse-hash-algorithm-alist
64 '((1 . MD5) (2 . SHA1) (3 . RIPEMD160) (5 . MD2))
65 "Alist of the assigned number to the cryptographic hash algorithm."
69 (defcustom pgg-parse-compression-algorithm-alist
70 '((0 . nil); Uncompressed
73 "Alist of the assigned number to the compression algorithm."
77 (defcustom pgg-parse-signature-type-alist
78 '((0 . "Signature of a binary document")
79 (1 . "Signature of a canonical text document")
80 (2 . "Standalone signature")
81 (16 . "Generic certification of a User ID and Public Key packet")
82 (17 . "Persona certification of a User ID and Public Key packet")
83 (18 . "Casual certification of a User ID and Public Key packet")
84 (19 . "Positive certification of a User ID and Public Key packet")
85 (24 . "Subkey Binding Signature")
86 (31 . "Signature directly on a key")
87 (32 . "Key revocation signature")
88 (40 . "Subkey revocation signature")
89 (48 . "Certification revocation signature")
90 (64 . "Timestamp signature."))
91 "Alist of the assigned number to the signature type."
95 (defcustom pgg-ignore-packet-checksum t; XXX
96 "If non-nil checksum of each ascii armored packet will be ignored."
100 (defvar pgg-armor-header-lines
101 '("^-----BEGIN PGP MESSAGE\\(, PART [0-9]+\\(/[0-9]+\\)?\\)?-----\r?$"
102 "^-----BEGIN PGP PUBLIC KEY BLOCK-----\r?$"
103 "^-----BEGIN PGP PRIVATE KEY BLOCK-----\r?$"
104 "^-----BEGIN PGP SIGNATURE-----\r?$")
107 (defmacro pgg-format-key-identifier (string)
108 `(upcase (apply #'format "%02x%02x%02x%02x%02x%02x%02x%02x"
109 (string-to-int-list ,string))))
111 (defmacro pgg-parse-time-field (bytes)
112 `(list (logior (lsh (car ,bytes) 8)
114 (logior (lsh (nth 2 ,bytes) 8)
118 (defmacro pgg-byte-after (&optional pos)
119 `(char-int (char-after ,(or pos `(point)))))
121 (defmacro pgg-read-byte ()
122 `(char-int (char-after (prog1 (point) (forward-char)))))
124 (defmacro pgg-read-bytes-string (nbytes)
126 (point) (prog1 (+ ,nbytes (point))
127 (forward-char ,nbytes))))
129 (defmacro pgg-read-bytes (nbytes)
130 `(string-to-int-list (pgg-read-bytes-string ,nbytes)))
132 (defmacro pgg-read-body-string (ptag)
134 (pgg-read-bytes-string (nth 1 ,ptag))
135 (pgg-read-bytes-string (- (point-max) (point)))))
137 (defmacro pgg-read-body (ptag)
138 `(string-to-int-list (pgg-read-body-string ,ptag)))
140 (defalias 'pgg-skip-bytes 'forward-char)
142 (defmacro pgg-skip-header (ptag)
143 `(pgg-skip-bytes (nth 2 ,ptag)))
145 (defmacro pgg-skip-body (ptag)
146 `(pgg-skip-bytes (nth 1 ,ptag)))
148 (defmacro pgg-set-alist (alist key value)
149 `(setq ,alist (nconc ,alist (list (cons ,key ,value)))))
151 (unless-broken ccl-usable
152 (define-ccl-program pgg-parse-crc24
155 (read r0) (r1 ^= r0) (r2 ^= 0)
159 (r1 += ((r2 >> 15) & 1))
162 ((r1 ^= 390) (r2 ^= 19707)))
168 (defun pgg-parse-crc24-string (string)
169 (let ((h (vector nil 183 1230 nil nil nil nil nil nil)))
170 (ccl-execute-on-string pgg-parse-crc24 h string)
172 (logand (aref h 1) 255)
173 (logand (lsh (aref h 2) -8) 255)
174 (logand (aref h 2) 255))))
177 (defmacro pgg-parse-length-type (c)
179 ((< ,c 192) (cons ,c 1))
181 (cons (+ (lsh (- ,c 192) 8)
182 (pgg-byte-after (+ 2 (point)))
186 (cons (cons (logior (lsh (pgg-byte-after (+ 2 (point))) 8)
187 (pgg-byte-after (+ 3 (point))))
188 (logior (lsh (pgg-byte-after (+ 4 (point))) 8)
189 (pgg-byte-after (+ 5 (point)))))
191 (t;partial body length
194 (defun pgg-parse-packet-header ()
195 (let ((ptag (pgg-byte-after))
196 length-type content-tag packet-bytes header-bytes)
197 (if (zerop (logand 64 ptag));Old format
199 (setq length-type (logand ptag 3)
200 length-type (if (= 3 length-type) 0 (lsh 1 length-type))
201 content-tag (logand 15 (lsh ptag -2))
203 header-bytes (1+ length-type))
204 (dotimes (i length-type)
206 (logior (lsh packet-bytes 8)
207 (pgg-byte-after (+ 1 i (point))))))
209 (setq content-tag (logand 63 ptag)
210 length-type (pgg-parse-length-type
211 (pgg-byte-after (1+ (point))))
212 packet-bytes (car length-type)
213 header-bytes (1+ (cdr length-type))))
214 (list content-tag packet-bytes header-bytes)))
216 (defun pgg-parse-packet (ptag)
218 (1 ;Public-Key Encrypted Session Key Packet
219 (pgg-parse-public-key-encrypted-session-key-packet ptag))
221 (pgg-parse-signature-packet ptag))
222 (3 ;Symmetric-Key Encrypted Session Key Packet
223 (pgg-parse-symmetric-key-encrypted-session-key-packet ptag))
224 ;; 4 -- One-Pass Signature Packet
225 ;; 5 -- Secret Key Packet
226 (6 ;Public Key Packet
227 (pgg-parse-public-key-packet ptag))
228 ;; 7 -- Secret Subkey Packet
229 ;; 8 -- Compressed Data Packet
230 (9 ;Symmetrically Encrypted Data Packet
231 (pgg-read-body-string ptag))
233 (pgg-read-body-string ptag))
234 (11 ;Literal Data Packet
235 (pgg-read-body-string ptag))
236 ;; 12 -- Trust Packet
238 (pgg-read-body-string ptag))
239 ;; 14 -- Public Subkey Packet
240 ;; 60 .. 63 -- Private or Experimental Values
243 (defun pgg-parse-packets (&optional header-parser body-parser)
246 (function pgg-parse-packet-header)))
249 (function pgg-parse-packet)))
251 (while (> (point-max) (1+ (point)))
252 (setq ptag (funcall header-parser))
253 (pgg-skip-header ptag)
254 (push (cons (car ptag)
256 (funcall body-parser ptag)))
258 (if (zerop (nth 1 ptag))
259 (goto-char (point-max))
260 (forward-char (nth 1 ptag))))
263 (defun pgg-parse-signature-subpacket-header ()
264 (let ((length-type (pgg-parse-length-type (pgg-byte-after))))
265 (list (pgg-byte-after (+ (cdr length-type) (point)))
266 (1- (car length-type))
267 (1+ (cdr length-type)))))
269 (defun pgg-parse-signature-subpacket (ptag)
271 (2 ;signature creation time
273 (let ((bytes (pgg-read-bytes 4)))
274 (pgg-parse-time-field bytes))))
275 (3 ;signature expiration time
276 (cons 'signature-expiry
277 (let ((bytes (pgg-read-bytes 4)))
278 (pgg-parse-time-field bytes))))
279 (4 ;exportable certification
280 (cons 'exportability (pgg-read-byte)))
282 (cons 'trust-level (pgg-read-byte)))
283 (6 ;regular expression
284 (cons 'regular-expression
285 (pgg-read-body-string ptag)))
287 (cons 'revocability (pgg-read-byte)))
288 (9 ;key expiration time
290 (let ((bytes (pgg-read-bytes 4)))
291 (pgg-parse-time-field bytes))))
292 ;; 10 = placeholder for backward compatibility
293 (11 ;preferred symmetric algorithms
294 (cons 'preferred-symmetric-key-algorithm
295 (cdr (assq (pgg-read-byte)
296 pgg-parse-symmetric-key-algorithm-alist))))
300 (cons 'key-identifier
301 (pgg-format-key-identifier (pgg-read-body-string ptag))))
305 (let ((name-bytes (pgg-read-bytes 2))
306 (value-bytes (pgg-read-bytes 2)))
307 (cons (pgg-read-bytes-string
308 (logior (lsh (car name-bytes) 8)
310 (pgg-read-bytes-string
311 (logior (lsh (car value-bytes) 8)
312 (nth 1 value-bytes))))))
314 (21 ;preferred hash algorithms
315 (cons 'preferred-hash-algorithm
316 (cdr (assq (pgg-read-byte)
317 pgg-parse-hash-algorithm-alist))))
318 (22 ;preferred compression algorithms
319 (cons 'preferred-compression-algorithm
320 (cdr (assq (pgg-read-byte)
321 pgg-parse-compression-algorithm-alist))))
322 (23 ;key server preferences
323 (cons 'key-server-preferences
324 (pgg-read-body ptag)))
325 (24 ;preferred key server
326 (cons 'preferred-key-server
327 (pgg-read-body-string ptag)))
328 ;; 25 = primary user id
330 (cons 'policy-url (pgg-read-body-string ptag)))
332 ;; 28 = signer's user id
333 ;; 29 = reason for revocation
334 ;; 100 to 110 = internal or user-defined
337 (defun pgg-parse-signature-packet (ptag)
338 (let* ((signature-version (pgg-byte-after))
339 (result (list (cons 'version signature-version)))
340 hashed-material field n)
342 ((= signature-version 3)
344 (setq hashed-material (pgg-read-bytes 5))
345 (pgg-set-alist result
347 (cdr (assq (pop hashed-material)
348 pgg-parse-signature-type-alist)))
349 (pgg-set-alist result
351 (pgg-parse-time-field hashed-material))
352 (pgg-set-alist result
354 (pgg-format-key-identifier
355 (pgg-read-bytes-string 8)))
356 (pgg-set-alist result
357 'public-key-algorithm (pgg-read-byte))
358 (pgg-set-alist result
359 'hash-algorithm (pgg-read-byte))
361 ((= signature-version 4)
363 (pgg-set-alist result
365 (cdr (assq (pgg-read-byte)
366 pgg-parse-signature-type-alist)))
367 (pgg-set-alist result
368 'public-key-algorithm
370 (pgg-set-alist result
371 'hash-algorithm (pgg-read-byte))
372 (when (>= 10000 (setq n (pgg-read-bytes 2)
373 n (logior (lsh (car n) 8)
376 (narrow-to-region (point)(+ n (point)))
378 (mapcar (function cdr) ;remove packet types
380 #'pgg-parse-signature-subpacket-header
381 #'pgg-parse-signature-subpacket)))
382 (goto-char (point-max)))
384 (when (>= 10000 (setq n (pgg-read-bytes 2)
385 n (logior (lsh (car n) 8)
388 (narrow-to-region (point)(+ n (point)))
390 (mapcar (function cdr) ;remove packet types
392 #'pgg-parse-signature-subpacket-header
393 #'pgg-parse-signature-subpacket)))
397 (setcdr (setq field (assq 'public-key-algorithm
399 (cdr (assq (cdr field)
400 pgg-parse-public-key-algorithm-alist)))
401 (setcdr (setq field (assq 'hash-algorithm
403 (cdr (assq (cdr field)
404 pgg-parse-hash-algorithm-alist)))
407 (defun pgg-parse-public-key-encrypted-session-key-packet (ptag)
409 (pgg-set-alist result
410 'version (pgg-read-byte))
411 (pgg-set-alist result
413 (pgg-format-key-identifier
414 (pgg-read-bytes-string 8)))
415 (pgg-set-alist result
416 'public-key-algorithm
417 (cdr (assq (pgg-read-byte)
418 pgg-parse-public-key-algorithm-alist)))
421 (defun pgg-parse-symmetric-key-encrypted-session-key-packet (ptag)
423 (pgg-set-alist result
426 (pgg-set-alist result
427 'symmetric-key-algorithm
428 (cdr (assq (pgg-read-byte)
429 pgg-parse-symmetric-key-algorithm-alist)))
432 (defun pgg-parse-public-key-packet (ptag)
433 (let* ((key-version (pgg-read-byte))
434 (result (list (cons 'version key-version)))
438 (pgg-set-alist result
440 (let ((bytes (pgg-read-bytes 4)))
441 (pgg-parse-time-field bytes)))
442 (pgg-set-alist result
443 'key-expiry (pgg-read-bytes 2))
444 (pgg-set-alist result
445 'public-key-algorithm (pgg-read-byte))
448 (pgg-set-alist result
450 (let ((bytes (pgg-read-bytes 4)))
451 (pgg-parse-time-field bytes)))
452 (pgg-set-alist result
453 'public-key-algorithm (pgg-read-byte))
456 (setcdr (setq field (assq 'public-key-algorithm
458 (cdr (assq (cdr field)
459 pgg-parse-public-key-algorithm-alist)))
462 (defun pgg-decode-packets ()
464 (set-marker (make-marker)
465 (and (re-search-forward "^=")
466 (match-beginning 0))))
467 (checksum (buffer-substring (point) (+ 4 (point)))))
468 (delete-region marker (point-max))
469 (mime-decode-region (point-min) marker "base64")
470 (static-when (fboundp 'pgg-parse-crc24-string )
471 (or pgg-ignore-packet-checksum
473 (funcall (mel-find-function 'mime-encode-string "base64")
474 (pgg-parse-crc24-string
475 (buffer-substring (point-min)(point-max))))
477 (error "PGP packet checksum does not match.")))))
479 (defun pgg-decode-armor-region (start end)
481 (narrow-to-region start end)
482 (goto-char (point-min))
483 (re-search-forward "^-+BEGIN PGP" nil t)
484 (delete-region (point-min)
485 (and (search-forward "\n\n")
488 (goto-char (point-min))
489 (pgg-parse-packets)))
491 (defun pgg-parse-armor (string)
493 (buffer-disable-undo)
494 (set-buffer-multibyte nil)
496 (pgg-decode-armor-region (point-min)(point))))
498 (defun pgg-parse-armor-region (start end)
499 (pgg-parse-armor (string-as-unibyte (buffer-substring start end))))
503 ;;; pgg-parse.el ends here