2005-02-22 Arne Jørgensen <arne@arnested.dk>
authorSimon Josefsson <jas@extundo.com>
Tue, 22 Feb 2005 16:53:22 +0000 (16:53 +0000)
committerSimon Josefsson <jas@extundo.com>
Tue, 22 Feb 2005 16:53:22 +0000 (16:53 +0000)
* smime.el (smime-ldap-host-list): Doc fix.
(smime-ask-passphrase): Use `password-read-and-add' to read (and
cache) password.
(smime-sign-region): Use it.
(smime-decrypt-region): Use it.
(smime-sign-buffer): Signal an error if `smime-sign-region' fails.
(smime-encrypt-buffer): Signal an error if `smime-encrypt-region'
fails.
(smime-cert-by-ldap-1): Use `base64-encode-string' to convert
certificate from DER to PEM format rather than calling openssl.

* mml-smime.el (mml-smime-encrypt-query): Remove obsolete comment.

* mml-sec.el (mml-secure-message): Insert keyfile/certfile tags
for signing/encryption.

* mml.el (mml-parse-1): Use them.

lisp/ChangeLog
lisp/mml-sec.el
lisp/mml-smime.el
lisp/mml.el
lisp/smime.el

index 5754c6e..583e59a 100644 (file)
@@ -1,3 +1,23 @@
+2005-02-22  Arne J\e,Ax\e(Brgensen  <arne@arnested.dk>
+
+       * smime.el (smime-ldap-host-list): Doc fix.
+       (smime-ask-passphrase): Use `password-read-and-add' to read (and
+       cache) password.
+       (smime-sign-region): Use it.
+       (smime-decrypt-region): Use it.
+       (smime-sign-buffer): Signal an error if `smime-sign-region' fails.
+       (smime-encrypt-buffer): Signal an error if `smime-encrypt-region'
+       fails.
+       (smime-cert-by-ldap-1): Use `base64-encode-string' to convert
+       certificate from DER to PEM format rather than calling openssl.
+
+       * mml-smime.el (mml-smime-encrypt-query): Remove obsolete comment.
+
+       * mml-sec.el (mml-secure-message): Insert keyfile/certfile tags
+       for signing/encryption.
+
+       * mml.el (mml-parse-1): Use them.
+
 2005-02-21  Arne J\e,Ax\e(Brgensen <arne@arnested.dk>
 
        * nnrss.el (nnrss-verbose): Removed.
index cdfc378..d254a60 100644 (file)
@@ -1,5 +1,5 @@
 ;;; mml-sec.el --- A package with security functions for MML documents
-;; Copyright (C) 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
+;; Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
 
 ;; Author: Simon Josefsson <simon@josefsson.org>
 
@@ -224,6 +224,13 @@ You can also customize or set `mml-signencrypt-style-alist' instead."
 ;; defuns that add the proper <#secure ...> tag to the top of the message body
 (defun mml-secure-message (method &optional modesym)
   (let ((mode (prin1-to-string modesym))
+       (tags (append
+              (if (or (eq modesym 'sign)
+                      (eq modesym 'signencrypt))
+                  (funcall (nth 2 (assoc method mml-sign-alist))))
+              (if (or (eq modesym 'encrypt)
+                      (eq modesym 'signencrypt))
+                  (funcall (nth 2 (assoc method mml-encrypt-alist))))))
        insert-loc)
     (mml-unsecure-message)
     (save-excursion
@@ -232,8 +239,8 @@ You can also customize or set `mml-signencrypt-style-alist' instead."
              (concat "^" (regexp-quote mail-header-separator) "\n") nil t)
             (goto-char (setq insert-loc (match-end 0)))
             (unless (looking-at "<#secure")
-              (mml-insert-tag
-               'secure 'method method 'mode mode)))
+              (apply 'mml-insert-tag
+               'secure 'method method 'mode mode tags)))
            (t (error
                "The message is corrupted. No mail header separator"))))
     (when (eql insert-loc (point))
index 72f0e2e..6febd61 100644 (file)
     result))
 
 (defun mml-smime-encrypt-query ()
-  ;; todo: add ldap support (xemacs ldap api?)
   ;; todo: try dns/ldap automatically first, before prompting user
   (let (certs done)
     (while (not done)
index 3ccac95..09ba21f 100644 (file)
@@ -158,6 +158,8 @@ one charsets.")
        ;; included in the message
        (let* (secure-mode
               (taginfo (mml-read-tag))
+              (keyfile (cdr (assq 'keyfile taginfo)))
+              (certfile (cdr (assq 'certfile taginfo)))
               (recipients (cdr (assq 'recipients taginfo)))
               (sender (cdr (assq 'sender taginfo)))
               (location (cdr (assq 'tag-location taginfo)))
@@ -181,6 +183,10 @@ one charsets.")
                 (setq tags (list "sign" method "encrypt" method))))
          (eval `(mml-insert-tag ,secure-mode
                                 ,@tags
+                                ,(if keyfile "keyfile")
+                                ,keyfile
+                                ,(if certfile "certfile")
+                                ,certfile
                                 ,(if recipients "recipients")
                                 ,recipients
                                 ,(if sender "sender")
index a93d697..037c4c4 100644 (file)
 
 (require 'dig)
 (require 'smime-ldap)
+(require 'password)
 (eval-when-compile (require 'cl))
 
 (defgroup smime nil
@@ -218,7 +219,9 @@ If nil, use system defaults."
   :group 'smime)
 
 (defcustom smime-ldap-host-list nil
-  "A list of LDAP hosts with S/MIME user certificates."
+  "A list of LDAP hosts with S/MIME user certificates.
+If needed search base, binddn, passwd, etc. for the LDAP host
+must be set in `ldap-host-parameters-alist'."
   :type '(repeat (string :tag "Host name"))
   :group 'smime)
 
@@ -238,11 +241,13 @@ If nil, use system defaults."
 
 ;; Password dialog function
 
-(defun smime-ask-passphrase ()
-  "Asks the passphrase to unlock the secret key."
+(defun smime-ask-passphrase (&optional cache-key)
+  "Asks the passphrase to unlock the secret key.
+If `cache-key' and `password-cache' is non-nil then cache the
+password under `cache-key'."
   (let ((passphrase
-        (read-passwd
-         "Passphrase for secret key (RET for no passphrase): ")))
+        (password-read-and-add
+         "Passphrase for secret key (RET for no passphrase): " cache-key)))
     (if (string= passphrase "")
        nil
       passphrase)))
@@ -274,11 +279,11 @@ certificates to include in its caar.  If no additional certificates is
 included, KEYFILE may be the file containing the PEM encoded private
 key and certificate itself."
   (smime-new-details-buffer)
-  (let ((keyfile (or (car-safe keyfile) keyfile))
-       (certfiles (and (cdr-safe keyfile) (cadr keyfile)))
-       (buffer (generate-new-buffer (generate-new-buffer-name " *smime*")))
-       (passphrase (smime-ask-passphrase))
-       (tmpfile (smime-make-temp-file "smime")))
+  (let* ((certfiles (and (cdr-safe keyfile) (cadr keyfile)))
+        (keyfile (or (car-safe keyfile) keyfile))
+        (buffer (generate-new-buffer (generate-new-buffer-name " *smime*")))
+        (passphrase (smime-ask-passphrase (expand-file-name keyfile)))
+        (tmpfile (smime-make-temp-file "smime")))
     (if passphrase
        (setenv "GNUS_SMIME_PASSPHRASE" passphrase))
     (prog1
@@ -339,16 +344,17 @@ is expected to contain of a PEM encoded certificate."
 KEYFILE should contain a PEM encoded key and certificate."
   (interactive)
   (with-current-buffer (or buffer (current-buffer))
-    (smime-sign-region
-     (point-min) (point-max)
-     (if keyfile
-        keyfile
-       (smime-get-key-with-certs-by-email
-       (completing-read
-        (concat "Sign using which key? "
-                (if smime-keys (concat "(default " (caar smime-keys) ") ")
-                  ""))
-        smime-keys nil nil (car-safe (car-safe smime-keys))))))))
+    (unless (smime-sign-region
+            (point-min) (point-max)
+            (if keyfile
+                keyfile
+              (smime-get-key-with-certs-by-email
+               (completing-read
+                (concat "Sign using which key? "
+                        (if smime-keys (concat "(default " (caar smime-keys) ") ")
+                          ""))
+                smime-keys nil nil (car-safe (car-safe smime-keys))))))
+      (error "Signing failed"))))
 
 (defun smime-encrypt-buffer (&optional certfiles buffer)
   "S/MIME encrypt BUFFER for recipients specified in CERTFILES.
@@ -357,11 +363,12 @@ a PEM encoded key and certificate.  Uses current buffer if BUFFER is
 nil."
   (interactive)
   (with-current-buffer (or buffer (current-buffer))
-    (smime-encrypt-region
-     (point-min) (point-max)
-     (or certfiles
-        (list (read-file-name "Recipient's S/MIME certificate: "
-                              smime-certificate-directory nil))))))
+    (unless (smime-encrypt-region
+            (point-min) (point-max)
+            (or certfiles
+                (list (read-file-name "Recipient's S/MIME certificate: "
+                                      smime-certificate-directory nil))))
+      (error "Encryption failed"))))
 
 ;; Verify+decrypt region
 
@@ -409,7 +416,7 @@ Any details (stderr on success, stdout and stderr on error) are left
 in the buffer specified by `smime-details-buffer'."
   (smime-new-details-buffer)
   (let ((buffer (generate-new-buffer (generate-new-buffer-name " *smime*")))
-       CAs (passphrase (smime-ask-passphrase))
+       CAs (passphrase (smime-ask-passphrase (expand-file-name keyfile)))
        (tmpfile (smime-make-temp-file "smime")))
     (if passphrase
        (setenv "GNUS_SMIME_PASSPHRASE" passphrase))
@@ -567,21 +574,21 @@ A string or a list of strings is returned."
   "Get cetificate for MAIL from the ldap server at HOST."
   (let ((ldapresult (smime-ldap-search (concat "mail=" mail)
                                       host '("userCertificate") nil))
-       (retbuf (generate-new-buffer (format "*certificate for %s*" mail))))
+       (retbuf (generate-new-buffer (format "*certificate for %s*" mail)))
+       cert)
     (if (> (length ldapresult) 1)
        (with-current-buffer retbuf
-         (set-buffer-multibyte nil)
-         (insert (nth 1 (car (nth 1 ldapresult))))
-         (goto-char (point-min))
-         (if (smime-call-openssl-region (point-min) (point-max) t "x509"
-                                        "-inform" "DER" "-outform" "PEM")
-             (progn
-               (delete-region (point) (point-max))
-               retbuf)
-           (kill-buffer retbuf)
-           nil))
+         (setq cert (base64-encode-string (nth 1 (car (nth 1 ldapresult))) t))
+         (insert "-----BEGIN CERTIFICATE-----\n")
+         (let ((i 0) (len (length cert)))
+           (while (> (- len 64) i)
+             (insert (substring cert i (+ i 64)) "\n")
+             (setq i (+ i 64)))
+           (insert (substring cert i len) "\n"))
+         (insert "-----END CERTIFICATE-----\n"))
       (kill-buffer retbuf)
-      nil)))
+      (setq retbuf nil))
+    retbuf))
 
 (defun smime-cert-by-ldap (mail)
   "Find certificate via LDAP for address MAIL."