epg.el 80 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252
  1. ;;; epg.el --- the EasyPG Library -*- lexical-binding: t -*-
  2. ;; Copyright (C) 1999-2000, 2002-2017 Free Software Foundation, Inc.
  3. ;; Author: Daiki Ueno <ueno@unixuser.org>
  4. ;; Keywords: PGP, GnuPG
  5. ;; Version: 1.0.0
  6. ;; This file is part of GNU Emacs.
  7. ;; GNU Emacs is free software: you can redistribute it and/or modify
  8. ;; it under the terms of the GNU General Public License as published by
  9. ;; the Free Software Foundation, either version 3 of the License, or
  10. ;; (at your option) any later version.
  11. ;; GNU Emacs is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;; GNU General Public License for more details.
  15. ;; You should have received a copy of the GNU General Public License
  16. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  17. ;;; Code:
  18. (require 'epg-config)
  19. (eval-when-compile (require 'cl-lib))
  20. (defvar epg-user-id nil
  21. "GnuPG ID of your default identity.")
  22. (defvar epg-user-id-alist nil
  23. "An alist mapping from key ID to user ID.")
  24. (defvar epg-last-status nil)
  25. (defvar epg-read-point nil)
  26. (defvar epg-process-filter-running nil)
  27. (defvar epg-pending-status-list nil)
  28. (defvar epg-key-id nil)
  29. (defvar epg-context nil)
  30. (defvar epg-debug-buffer nil)
  31. (defvar epg-agent-file nil)
  32. (defvar epg-agent-mtime nil)
  33. ;; from gnupg/include/cipher.h
  34. (defconst epg-cipher-algorithm-alist
  35. '((0 . "NONE")
  36. (1 . "IDEA")
  37. (2 . "3DES")
  38. (3 . "CAST5")
  39. (4 . "BLOWFISH")
  40. (7 . "AES")
  41. (8 . "AES192")
  42. (9 . "AES256")
  43. (10 . "TWOFISH")
  44. (11 . "CAMELLIA128")
  45. (12 . "CAMELLIA256")
  46. (110 . "DUMMY")))
  47. ;; from gnupg/include/cipher.h
  48. (defconst epg-pubkey-algorithm-alist
  49. '((1 . "RSA")
  50. (2 . "RSA_E")
  51. (3 . "RSA_S")
  52. (16 . "ELGAMAL_E")
  53. (17 . "DSA")
  54. (20 . "ELGAMAL")))
  55. ;; from gnupg/include/cipher.h
  56. (defconst epg-digest-algorithm-alist
  57. '((1 . "MD5")
  58. (2 . "SHA1")
  59. (3 . "RIPEMD160")
  60. (8 . "SHA256")
  61. (9 . "SHA384")
  62. (10 . "SHA512")
  63. (11 . "SHA224")))
  64. ;; from gnupg/include/cipher.h
  65. (defconst epg-compress-algorithm-alist
  66. '((0 . "NONE")
  67. (1 . "ZIP")
  68. (2 . "ZLIB")
  69. (3 . "BZIP2")))
  70. (defconst epg-invalid-recipients-reason-alist
  71. '((0 . "No specific reason given")
  72. (1 . "Not Found")
  73. (2 . "Ambiguous specification")
  74. (3 . "Wrong key usage")
  75. (4 . "Key revoked")
  76. (5 . "Key expired")
  77. (6 . "No CRL known")
  78. (7 . "CRL too old")
  79. (8 . "Policy mismatch")
  80. (9 . "Not a secret key")
  81. (10 . "Key not trusted")))
  82. (defconst epg-delete-problem-reason-alist
  83. '((1 . "No such key")
  84. (2 . "Must delete secret key first")
  85. (3 . "Ambiguous specification")))
  86. (defconst epg-import-ok-reason-alist
  87. '((0 . "Not actually changed")
  88. (1 . "Entirely new key")
  89. (2 . "New user IDs")
  90. (4 . "New signatures")
  91. (8 . "New subkeys")
  92. (16 . "Contains private key")))
  93. (defconst epg-import-problem-reason-alist
  94. '((0 . "No specific reason given")
  95. (1 . "Invalid Certificate")
  96. (2 . "Issuer Certificate missing")
  97. (3 . "Certificate Chain too long")
  98. (4 . "Error storing certificate")))
  99. (defconst epg-no-data-reason-alist
  100. '((1 . "No armored data")
  101. (2 . "Expected a packet but did not found one")
  102. (3 . "Invalid packet found, this may indicate a non OpenPGP message")
  103. (4 . "Signature expected but not found")))
  104. (defconst epg-unexpected-reason-alist nil)
  105. (defvar epg-key-validity-alist
  106. '((?o . unknown)
  107. (?i . invalid)
  108. (?d . disabled)
  109. (?r . revoked)
  110. (?e . expired)
  111. (?- . none)
  112. (?q . undefined)
  113. (?n . never)
  114. (?m . marginal)
  115. (?f . full)
  116. (?u . ultimate)))
  117. (defvar epg-key-capability-alist
  118. '((?e . encrypt)
  119. (?s . sign)
  120. (?c . certify)
  121. (?a . authentication)
  122. (?D . disabled)))
  123. (defvar epg-new-signature-type-alist
  124. '((?D . detached)
  125. (?C . clear)
  126. (?S . normal)))
  127. (defvar epg-dn-type-alist
  128. '(("1.2.840.113549.1.9.1" . "EMail")
  129. ("2.5.4.12" . "T")
  130. ("2.5.4.42" . "GN")
  131. ("2.5.4.4" . "SN")
  132. ("0.2.262.1.10.7.20" . "NameDistinguisher")
  133. ("2.5.4.16" . "ADDR")
  134. ("2.5.4.15" . "BC")
  135. ("2.5.4.13" . "D")
  136. ("2.5.4.17" . "PostalCode")
  137. ("2.5.4.65" . "Pseudo")
  138. ("2.5.4.5" . "SerialNumber")))
  139. (defvar epg-prompt-alist nil)
  140. (define-error 'epg-error "GPG error")
  141. (cl-defstruct (epg-data
  142. (:constructor nil)
  143. (:constructor epg-make-data-from-file (file))
  144. (:constructor epg-make-data-from-string (string))
  145. (:copier nil)
  146. (:predicate nil))
  147. (file nil :read-only t)
  148. (string nil :read-only t))
  149. (defmacro epg--gv-nreverse (place)
  150. (gv-letplace (getter setter) place
  151. (funcall setter `(nreverse ,getter))))
  152. (cl-defstruct (epg-context
  153. (:constructor nil)
  154. (:constructor epg-context--make
  155. (protocol &optional armor textmode include-certs
  156. cipher-algorithm digest-algorithm
  157. compress-algorithm
  158. &aux
  159. (program
  160. (let ((configuration (epg-find-configuration protocol)))
  161. (unless configuration
  162. (signal 'epg-error
  163. (list "no usable configuration" protocol)))
  164. (alist-get 'program configuration)))))
  165. (:copier nil)
  166. (:predicate nil))
  167. protocol
  168. program
  169. (home-directory epg-gpg-home-directory)
  170. armor
  171. textmode
  172. include-certs
  173. cipher-algorithm
  174. digest-algorithm
  175. compress-algorithm
  176. (passphrase-callback (list #'epg-passphrase-callback-function))
  177. progress-callback
  178. edit-callback
  179. signers
  180. sig-notations
  181. process
  182. output-file
  183. result
  184. operation
  185. pinentry-mode
  186. (error-output "")
  187. error-buffer)
  188. ;; This is not an alias, just so we can mark it as autoloaded.
  189. ;;;###autoload
  190. (defun epg-make-context (&optional protocol armor textmode include-certs
  191. cipher-algorithm digest-algorithm
  192. compress-algorithm)
  193. "Return a context object."
  194. (epg-context--make (or protocol 'OpenPGP)
  195. armor textmode include-certs
  196. cipher-algorithm digest-algorithm
  197. compress-algorithm))
  198. (defun epg-context-set-armor (context armor)
  199. "Specify if the output should be ASCII armored in CONTEXT."
  200. (declare (obsolete setf "25.1"))
  201. (setf (epg-context-armor context) armor))
  202. (defun epg-context-set-textmode (context textmode)
  203. "Specify if canonical text mode should be used in CONTEXT."
  204. (declare (obsolete setf "25.1"))
  205. (setf (epg-context-textmode context) textmode))
  206. (defun epg-context-set-passphrase-callback (context
  207. passphrase-callback)
  208. "Set the function used to query passphrase.
  209. PASSPHRASE-CALLBACK is either a function, or a cons-cell whose
  210. car is a function and cdr is a callback data.
  211. The function gets three arguments: the context, the key-id in
  212. question, and the callback data (if any).
  213. The callback may not be called if you use GnuPG 2.x, which relies
  214. on the external program called `gpg-agent' for passphrase query.
  215. If you really want to intercept passphrase query, consider
  216. installing GnuPG 1.x _along with_ GnuPG 2.x, which does passphrase
  217. query by itself and Emacs can intercept them."
  218. ;; (declare (obsolete setf "25.1"))
  219. (setf (epg-context-passphrase-callback context)
  220. (if (functionp passphrase-callback)
  221. (list passphrase-callback)
  222. passphrase-callback)))
  223. (defun epg-context-set-progress-callback (context
  224. progress-callback)
  225. "Set the function which handles progress update.
  226. PROGRESS-CALLBACK is either a function, or a cons-cell whose
  227. car is a function and cdr is a callback data.
  228. The function gets six arguments: the context, the operation
  229. description, the character to display a progress unit, the
  230. current amount done, the total amount to be done, and the
  231. callback data (if any)."
  232. (setf (epg-context-progress-callback context)
  233. (if (functionp progress-callback)
  234. (list progress-callback)
  235. progress-callback)))
  236. (defun epg-context-set-signers (context signers)
  237. "Set the list of key-id for signing."
  238. (declare (obsolete setf "25.1"))
  239. (setf (epg-context-signers context) signers))
  240. (cl-defstruct (epg-signature
  241. (:constructor nil)
  242. (:constructor epg-make-signature
  243. (status &optional key-id))
  244. (:copier nil)
  245. (:predicate nil))
  246. status
  247. key-id
  248. validity
  249. fingerprint
  250. creation-time
  251. expiration-time
  252. pubkey-algorithm
  253. digest-algorithm
  254. class
  255. version
  256. notations)
  257. (cl-defstruct (epg-new-signature
  258. (:constructor nil)
  259. (:constructor epg-make-new-signature
  260. (type pubkey-algorithm digest-algorithm
  261. class creation-time fingerprint))
  262. (:copier nil)
  263. (:predicate nil))
  264. (type nil :read-only t)
  265. (pubkey-algorithm nil :read-only t)
  266. (digest-algorithm nil :read-only t)
  267. (class nil :read-only t)
  268. (creation-time nil :read-only t)
  269. (fingerprint nil :read-only t))
  270. (cl-defstruct (epg-key
  271. (:constructor nil)
  272. (:constructor epg-make-key (owner-trust))
  273. (:copier nil)
  274. (:predicate nil))
  275. (owner-trust nil :read-only t)
  276. sub-key-list user-id-list)
  277. (cl-defstruct (epg-sub-key
  278. (:constructor nil)
  279. (:constructor epg-make-sub-key
  280. (validity capability secret-p algorithm length id
  281. creation-time expiration-time))
  282. (:copier nil)
  283. (:predicate nil))
  284. validity capability secret-p algorithm length id
  285. creation-time expiration-time fingerprint)
  286. (cl-defstruct (epg-user-id
  287. (:constructor nil)
  288. (:constructor epg-make-user-id (validity string))
  289. (:copier nil)
  290. (:predicate nil))
  291. validity string signature-list)
  292. (cl-defstruct (epg-key-signature
  293. (:constructor nil)
  294. (:constructor epg-make-key-signature
  295. (validity pubkey-algorithm key-id creation-time
  296. expiration-time user-id class
  297. exportable-p))
  298. (:copier nil)
  299. (:predicate nil))
  300. validity pubkey-algorithm key-id creation-time
  301. expiration-time user-id class
  302. exportable-p)
  303. (cl-defstruct (epg-sig-notation
  304. (:constructor nil)
  305. (:constructor epg-make-sig-notation
  306. (name value &optional human-readable critical))
  307. (:copier nil)
  308. (:predicate nil))
  309. name value human-readable critical)
  310. (cl-defstruct (epg-import-status
  311. (:constructor nil)
  312. (:constructor epg-make-import-status
  313. (fingerprint
  314. &optional reason new user-id signature sub-key secret))
  315. (:copier nil)
  316. (:predicate nil))
  317. fingerprint reason new user-id signature sub-key secret)
  318. (cl-defstruct (epg-import-result
  319. (:constructor nil)
  320. (:constructor epg-make-import-result
  321. (considered no-user-id imported imported-rsa
  322. unchanged new-user-ids new-sub-keys
  323. new-signatures new-revocations
  324. secret-read secret-imported
  325. secret-unchanged not-imported
  326. imports))
  327. (:copier nil)
  328. (:predicate nil))
  329. considered no-user-id imported imported-rsa
  330. unchanged new-user-ids new-sub-keys
  331. new-signatures new-revocations
  332. secret-read secret-imported
  333. secret-unchanged not-imported
  334. imports)
  335. (defun epg-context-result-for (context name)
  336. "Return the result of CONTEXT associated with NAME."
  337. (cdr (assq name (epg-context-result context))))
  338. (defun epg-context-set-result-for (context name value)
  339. "Set the result of CONTEXT associated with NAME to VALUE."
  340. (let* ((result (epg-context-result context))
  341. (entry (assq name result)))
  342. (if entry
  343. (setcdr entry value)
  344. (setf (epg-context-result context) (cons (cons name value) result)))))
  345. (defun epg-signature-to-string (signature)
  346. "Convert SIGNATURE to a human readable string."
  347. (let* ((user-id (cdr (assoc (epg-signature-key-id signature)
  348. epg-user-id-alist)))
  349. (pubkey-algorithm (epg-signature-pubkey-algorithm signature))
  350. (key-id (epg-signature-key-id signature)))
  351. (concat
  352. (cond ((eq (epg-signature-status signature) 'good)
  353. "Good signature from ")
  354. ((eq (epg-signature-status signature) 'bad)
  355. "Bad signature from ")
  356. ((eq (epg-signature-status signature) 'expired)
  357. "Expired signature from ")
  358. ((eq (epg-signature-status signature) 'expired-key)
  359. "Signature made by expired key ")
  360. ((eq (epg-signature-status signature) 'revoked-key)
  361. "Signature made by revoked key ")
  362. ((eq (epg-signature-status signature) 'no-pubkey)
  363. "No public key for "))
  364. key-id
  365. (if user-id
  366. (concat " "
  367. (if (stringp user-id)
  368. user-id
  369. (epg-decode-dn user-id)))
  370. "")
  371. (if (epg-signature-validity signature)
  372. (format " (trust %s)" (epg-signature-validity signature))
  373. "")
  374. (if (epg-signature-creation-time signature)
  375. (format-time-string " created at %Y-%m-%dT%T%z"
  376. (epg-signature-creation-time signature))
  377. "")
  378. (if pubkey-algorithm
  379. (concat " using "
  380. (or (cdr (assq pubkey-algorithm epg-pubkey-algorithm-alist))
  381. (format "(unknown algorithm %d)" pubkey-algorithm)))
  382. ""))))
  383. (defun epg-verify-result-to-string (verify-result)
  384. "Convert VERIFY-RESULT to a human readable string."
  385. (mapconcat #'epg-signature-to-string verify-result "\n"))
  386. (defun epg-new-signature-to-string (new-signature)
  387. "Convert NEW-SIGNATURE to a human readable string."
  388. (concat
  389. (cond ((eq (epg-new-signature-type new-signature) 'detached)
  390. "Detached signature ")
  391. ((eq (epg-new-signature-type new-signature) 'clear)
  392. "Cleartext signature ")
  393. (t
  394. "Signature "))
  395. (cdr (assq (epg-new-signature-pubkey-algorithm new-signature)
  396. epg-pubkey-algorithm-alist))
  397. "/"
  398. (cdr (assq (epg-new-signature-digest-algorithm new-signature)
  399. epg-digest-algorithm-alist))
  400. " "
  401. (format "%02X " (epg-new-signature-class new-signature))
  402. (epg-new-signature-fingerprint new-signature)))
  403. (defun epg-import-result-to-string (import-result)
  404. "Convert IMPORT-RESULT to a human readable string."
  405. (concat (format "Total number processed: %d\n"
  406. (epg-import-result-considered import-result))
  407. (if (> (epg-import-result-not-imported import-result) 0)
  408. (format " skipped new keys: %d\n"
  409. (epg-import-result-not-imported import-result)))
  410. (if (> (epg-import-result-no-user-id import-result) 0)
  411. (format " w/o user IDs: %d\n"
  412. (epg-import-result-no-user-id import-result)))
  413. (if (> (epg-import-result-imported import-result) 0)
  414. (concat (format " imported: %d"
  415. (epg-import-result-imported import-result))
  416. (if (> (epg-import-result-imported-rsa import-result) 0)
  417. (format " (RSA: %d)"
  418. (epg-import-result-imported-rsa
  419. import-result)))
  420. "\n"))
  421. (if (> (epg-import-result-unchanged import-result) 0)
  422. (format " unchanged: %d\n"
  423. (epg-import-result-unchanged import-result)))
  424. (if (> (epg-import-result-new-user-ids import-result) 0)
  425. (format " new user IDs: %d\n"
  426. (epg-import-result-new-user-ids import-result)))
  427. (if (> (epg-import-result-new-sub-keys import-result) 0)
  428. (format " new subkeys: %d\n"
  429. (epg-import-result-new-sub-keys import-result)))
  430. (if (> (epg-import-result-new-signatures import-result) 0)
  431. (format " new signatures: %d\n"
  432. (epg-import-result-new-signatures import-result)))
  433. (if (> (epg-import-result-new-revocations import-result) 0)
  434. (format " new key revocations: %d\n"
  435. (epg-import-result-new-revocations import-result)))
  436. (if (> (epg-import-result-secret-read import-result) 0)
  437. (format " secret keys read: %d\n"
  438. (epg-import-result-secret-read import-result)))
  439. (if (> (epg-import-result-secret-imported import-result) 0)
  440. (format " secret keys imported: %d\n"
  441. (epg-import-result-secret-imported import-result)))
  442. (if (> (epg-import-result-secret-unchanged import-result) 0)
  443. (format " secret keys unchanged: %d\n"
  444. (epg-import-result-secret-unchanged import-result)))))
  445. (defun epg-error-to-string (error)
  446. (cond
  447. ((eq (car error) 'exit)
  448. "Exit")
  449. ((eq (car error) 'quit)
  450. "Canceled")
  451. ((eq (car error) 'no-data)
  452. (let ((entry (assq (cdr error) epg-no-data-reason-alist)))
  453. (if entry
  454. (format "No data (%s)" (downcase (cdr entry)))
  455. "No data")))
  456. ((eq (car error) 'unexpected)
  457. (let ((entry (assq (cdr error) epg-unexpected-reason-alist)))
  458. (if entry
  459. (format "Unexpected (%s)" (downcase (cdr entry)))
  460. "Unexpected")))
  461. ((eq (car error) 'bad-armor)
  462. "Bad armor")
  463. ((memq (car error) '(invalid-recipient invalid-signer))
  464. (concat
  465. (if (eq (car error) 'invalid-recipient)
  466. "Unusable public key"
  467. "Unusable secret key")
  468. (let ((entry (assq 'requested (cdr error))))
  469. (if entry
  470. (format ": %s" (cdr entry))
  471. ": <unknown>"))
  472. (let ((entry (assq 'reason (cdr error))))
  473. (if (and entry
  474. (> (cdr entry) 0) ;no specific reason given
  475. (setq entry (assq (cdr entry)
  476. epg-invalid-recipients-reason-alist)))
  477. (format " (%s)" (downcase (cdr entry)))
  478. ""))))
  479. ((eq (car error) 'no-pubkey)
  480. (format "No public key: %s" (cdr error)))
  481. ((eq (car error) 'no-seckey)
  482. (format "No secret key: %s" (cdr error)))
  483. ((eq (car error) 'no-recipients)
  484. "No recipients")
  485. ((eq (car error) 'no-signers)
  486. "No signers")
  487. ((eq (car error) 'delete-problem)
  488. (let ((entry (assq (cdr error) epg-delete-problem-reason-alist)))
  489. (if entry
  490. (format "Delete problem (%s)" (downcase (cdr entry)))
  491. "Delete problem")))
  492. ((eq (car error) 'key-not-created)
  493. "Key not created")))
  494. (defun epg-errors-to-string (errors)
  495. (mapconcat #'epg-error-to-string errors "; "))
  496. (declare-function pinentry-start "pinentry" (&optional quiet))
  497. (defun epg--start (context args)
  498. "Start `epg-gpg-program' in a subprocess with given ARGS."
  499. (if (and (epg-context-process context)
  500. (eq (process-status (epg-context-process context)) 'run))
  501. (error "%s is already running in this context"
  502. (epg-context-program context)))
  503. (let* ((agent-info (getenv "GPG_AGENT_INFO"))
  504. (args (append (list "--no-tty"
  505. "--status-fd" "1"
  506. "--yes")
  507. (if (and (not (eq (epg-context-protocol context) 'CMS))
  508. (string-match ":" (or agent-info "")))
  509. '("--use-agent"))
  510. (if (and (not (eq (epg-context-protocol context) 'CMS))
  511. (epg-context-progress-callback context))
  512. '("--enable-progress-filter"))
  513. (if (epg-context-home-directory context)
  514. (list "--homedir"
  515. (epg-context-home-directory context)))
  516. (unless (eq (epg-context-protocol context) 'CMS)
  517. '("--command-fd" "0"))
  518. (if (epg-context-armor context) '("--armor"))
  519. (if (epg-context-textmode context) '("--textmode"))
  520. (if (epg-context-output-file context)
  521. (list "--output" (epg-context-output-file context)))
  522. (if (epg-context-pinentry-mode context)
  523. (list "--pinentry-mode"
  524. (symbol-name (epg-context-pinentry-mode
  525. context))))
  526. args))
  527. (process-environment process-environment)
  528. (buffer (generate-new-buffer " *epg*"))
  529. error-process
  530. process
  531. terminal-name
  532. agent-file
  533. (agent-mtime '(0 0 0 0)))
  534. ;; Set GPG_TTY and TERM for pinentry-curses. Note that we can't
  535. ;; use `terminal-name' here to get the real pty name for the child
  536. ;; process, though /dev/fd/0" is not portable.
  537. (unless (memq system-type '(ms-dos windows-nt))
  538. (with-temp-buffer
  539. (condition-case nil
  540. (when (= (call-process "tty" "/dev/fd/0" t) 0)
  541. (delete-char -1)
  542. (setq terminal-name (buffer-string)))
  543. (file-error))))
  544. (when terminal-name
  545. (setq process-environment
  546. (cons (concat "GPG_TTY=" terminal-name)
  547. (cons "TERM=xterm" process-environment))))
  548. ;; Automatically start the Emacs Pinentry server if appropriate.
  549. (when (and (fboundp 'pinentry-start)
  550. ;; Emacs Pinentry is useless if Emacs has no interactive session.
  551. (not noninteractive)
  552. ;; Prefer pinentry-mode over Emacs Pinentry.
  553. (null (epg-context-pinentry-mode context))
  554. ;; Check if the allow-emacs-pinentry option is set.
  555. (executable-find epg-gpgconf-program)
  556. (with-temp-buffer
  557. (when (= (call-process epg-gpgconf-program nil t nil
  558. "--list-options" "gpg-agent")
  559. 0)
  560. (goto-char (point-min))
  561. (re-search-forward
  562. "^allow-emacs-pinentry:\\(?:.*:\\)\\{8\\}1"
  563. nil t))))
  564. (pinentry-start 'quiet))
  565. (setq process-environment
  566. (cons (format "INSIDE_EMACS=%s,epg" emacs-version)
  567. process-environment))
  568. ;; Record modified time of gpg-agent socket to restore the Emacs
  569. ;; frame on text terminal in `epg-wait-for-completion'.
  570. ;; See
  571. ;; <http://lists.gnu.org/archive/html/emacs-devel/2007-02/msg00755.html>
  572. ;; for more details.
  573. (when (and agent-info (string-match "\\(.*\\):[0-9]+:[0-9]+" agent-info))
  574. (setq agent-file (match-string 1 agent-info)
  575. agent-mtime (or (nth 5 (file-attributes agent-file)) '(0 0 0 0))))
  576. (if epg-debug
  577. (save-excursion
  578. (unless epg-debug-buffer
  579. (setq epg-debug-buffer (generate-new-buffer " *epg-debug*")))
  580. (set-buffer epg-debug-buffer)
  581. (goto-char (point-max))
  582. (insert (if agent-info
  583. (format "GPG_AGENT_INFO=%s\n" agent-info)
  584. "GPG_AGENT_INFO is not set\n")
  585. (format "%s %s\n"
  586. (epg-context-program context)
  587. (mapconcat #'identity args " ")))))
  588. (with-current-buffer buffer
  589. (if (fboundp 'set-buffer-multibyte)
  590. (set-buffer-multibyte nil))
  591. (make-local-variable 'epg-last-status)
  592. (setq epg-last-status nil)
  593. (make-local-variable 'epg-read-point)
  594. (setq epg-read-point (point-min))
  595. (make-local-variable 'epg-process-filter-running)
  596. (setq epg-process-filter-running nil)
  597. (make-local-variable 'epg-pending-status-list)
  598. (setq epg-pending-status-list nil)
  599. (make-local-variable 'epg-key-id)
  600. (setq epg-key-id nil)
  601. (make-local-variable 'epg-context)
  602. (setq epg-context context)
  603. (make-local-variable 'epg-agent-file)
  604. (setq epg-agent-file agent-file)
  605. (make-local-variable 'epg-agent-mtime)
  606. (setq epg-agent-mtime agent-mtime))
  607. (setq error-process
  608. (make-pipe-process :name "epg-error"
  609. :buffer (generate-new-buffer " *epg-error*")
  610. ;; Suppress "XXX finished" line.
  611. :sentinel #'ignore
  612. :noquery t))
  613. (setf (epg-context-error-buffer context) (process-buffer error-process))
  614. (with-file-modes 448
  615. (setq process (make-process :name "epg"
  616. :buffer buffer
  617. :command (cons (epg-context-program context)
  618. args)
  619. :connection-type 'pipe
  620. :coding '(binary . binary)
  621. :filter #'epg--process-filter
  622. :stderr error-process
  623. :noquery t)))
  624. (setf (epg-context-process context) process)))
  625. (defun epg--process-filter (process input)
  626. (if epg-debug
  627. (with-current-buffer
  628. (or epg-debug-buffer
  629. (setq epg-debug-buffer (generate-new-buffer " *epg-debug*")))
  630. (goto-char (point-max))
  631. (insert input)))
  632. (if (buffer-live-p (process-buffer process))
  633. (with-current-buffer (process-buffer process)
  634. (save-excursion
  635. (goto-char (point-max))
  636. (insert input)
  637. (unless epg-process-filter-running
  638. (let ((epg-process-filter-running t))
  639. (goto-char epg-read-point)
  640. (beginning-of-line)
  641. (while (looking-at ".*\n") ;the input line finished
  642. (if (looking-at "\\[GNUPG:] \\([A-Z_]+\\) ?\\(.*\\)")
  643. (let ((status (match-string 1))
  644. (string (match-string 2))
  645. symbol)
  646. (if (member status epg-pending-status-list)
  647. (setq epg-pending-status-list nil))
  648. ;; When editing a key, delegate all interaction
  649. ;; to edit-callback.
  650. (if (eq (epg-context-operation epg-context) 'edit-key)
  651. (funcall (car (epg-context-edit-callback
  652. epg-context))
  653. epg-context
  654. status
  655. string
  656. (cdr (epg-context-edit-callback
  657. epg-context)))
  658. ;; Otherwise call epg--status-STATUS function.
  659. (setq symbol (intern-soft (concat "epg--status-"
  660. status)))
  661. (if (and symbol
  662. (fboundp symbol))
  663. (funcall symbol epg-context string)))
  664. (setq epg-last-status (cons status string))))
  665. (forward-line)
  666. (setq epg-read-point (point)))))))))
  667. (defun epg-read-output (context)
  668. "Read the output file CONTEXT and return the content as a string."
  669. (with-temp-buffer
  670. (if (fboundp 'set-buffer-multibyte)
  671. (set-buffer-multibyte nil))
  672. (if (file-exists-p (epg-context-output-file context))
  673. (let ((coding-system-for-read 'binary))
  674. (insert-file-contents (epg-context-output-file context))
  675. (buffer-string)))))
  676. (defun epg-wait-for-status (context status-list)
  677. "Wait until one of elements in STATUS-LIST arrives."
  678. (with-current-buffer (process-buffer (epg-context-process context))
  679. (setq epg-pending-status-list status-list)
  680. (while (and (eq (process-status (epg-context-process context)) 'run)
  681. epg-pending-status-list)
  682. (accept-process-output (epg-context-process context) 1))
  683. (if epg-pending-status-list
  684. (epg-context-set-result-for
  685. context 'error
  686. (cons '(exit)
  687. (epg-context-result-for context 'error))))))
  688. (defun epg-wait-for-completion (context)
  689. "Wait until the `epg-gpg-program' process completes."
  690. (while (eq (process-status (epg-context-process context)) 'run)
  691. (accept-process-output (epg-context-process context) 1))
  692. ;; This line is needed to run the process-filter right now.
  693. (sleep-for 0.1)
  694. ;; Restore Emacs frame on text terminal, when pinentry-curses has terminated.
  695. (if (with-current-buffer (process-buffer (epg-context-process context))
  696. (and epg-agent-file
  697. (> (float-time (or (nth 5 (file-attributes epg-agent-file))
  698. '(0 0 0 0)))
  699. (float-time epg-agent-mtime))))
  700. (redraw-frame))
  701. (epg-context-set-result-for
  702. context 'error
  703. (nreverse (epg-context-result-for context 'error)))
  704. (setf (epg-context-error-output context)
  705. (with-current-buffer (epg-context-error-buffer context)
  706. (buffer-string))))
  707. (defun epg-reset (context)
  708. "Reset the CONTEXT."
  709. (if (and (epg-context-process context)
  710. (buffer-live-p (process-buffer (epg-context-process context))))
  711. (kill-buffer (process-buffer (epg-context-process context))))
  712. (if (buffer-live-p (epg-context-error-buffer context))
  713. (kill-buffer (epg-context-error-buffer context)))
  714. (setf (epg-context-process context) nil)
  715. (setf (epg-context-edit-callback context) nil))
  716. (defun epg-delete-output-file (context)
  717. "Delete the output file of CONTEXT."
  718. (if (and (epg-context-output-file context)
  719. (file-exists-p (epg-context-output-file context)))
  720. (delete-file (epg-context-output-file context))))
  721. (eval-and-compile
  722. (if (fboundp 'decode-coding-string)
  723. (defalias 'epg--decode-coding-string 'decode-coding-string)
  724. (defalias 'epg--decode-coding-string 'identity)))
  725. (defun epg--status-USERID_HINT (_context string)
  726. (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
  727. (let* ((key-id (match-string 1 string))
  728. (user-id (match-string 2 string))
  729. (entry (assoc key-id epg-user-id-alist)))
  730. (condition-case nil
  731. (setq user-id (epg--decode-coding-string
  732. (epg--decode-percent-escape user-id)
  733. 'utf-8))
  734. (error))
  735. (if entry
  736. (setcdr entry user-id)
  737. (setq epg-user-id-alist (cons (cons key-id user-id)
  738. epg-user-id-alist))))))
  739. (defun epg--status-NEED_PASSPHRASE (_context string)
  740. (if (string-match "\\`\\([^ ]+\\)" string)
  741. (setq epg-key-id (match-string 1 string))))
  742. (defun epg--status-NEED_PASSPHRASE_SYM (_context _string)
  743. (setq epg-key-id 'SYM))
  744. (defun epg--status-NEED_PASSPHRASE_PIN (_context _string)
  745. (setq epg-key-id 'PIN))
  746. (eval-and-compile
  747. (if (fboundp 'clear-string)
  748. (defalias 'epg--clear-string 'clear-string)
  749. (defun epg--clear-string (string)
  750. (fillarray string 0))))
  751. (eval-and-compile
  752. (if (fboundp 'encode-coding-string)
  753. (defalias 'epg--encode-coding-string 'encode-coding-string)
  754. (defalias 'epg--encode-coding-string 'identity)))
  755. (defun epg--status-GET_HIDDEN (context string)
  756. (when (and epg-key-id
  757. (string-match "\\`passphrase\\." string))
  758. (unless (epg-context-passphrase-callback context)
  759. (error "passphrase-callback not set"))
  760. (let (inhibit-quit
  761. passphrase
  762. passphrase-with-new-line
  763. encoded-passphrase-with-new-line)
  764. (unwind-protect
  765. (condition-case nil
  766. (progn
  767. (setq passphrase
  768. (funcall
  769. (car (epg-context-passphrase-callback context))
  770. context
  771. epg-key-id
  772. (cdr (epg-context-passphrase-callback context))))
  773. (when passphrase
  774. (setq passphrase-with-new-line (concat passphrase "\n"))
  775. (epg--clear-string passphrase)
  776. (setq passphrase nil)
  777. (if epg-passphrase-coding-system
  778. (progn
  779. (setq encoded-passphrase-with-new-line
  780. (epg--encode-coding-string
  781. passphrase-with-new-line
  782. (coding-system-change-eol-conversion
  783. epg-passphrase-coding-system 'unix)))
  784. (epg--clear-string passphrase-with-new-line)
  785. (setq passphrase-with-new-line nil))
  786. (setq encoded-passphrase-with-new-line
  787. passphrase-with-new-line
  788. passphrase-with-new-line nil))
  789. (process-send-string (epg-context-process context)
  790. encoded-passphrase-with-new-line)))
  791. (quit
  792. (epg-context-set-result-for
  793. context 'error
  794. (cons '(quit)
  795. (epg-context-result-for context 'error)))
  796. (delete-process (epg-context-process context))))
  797. (if passphrase
  798. (epg--clear-string passphrase))
  799. (if passphrase-with-new-line
  800. (epg--clear-string passphrase-with-new-line))
  801. (if encoded-passphrase-with-new-line
  802. (epg--clear-string encoded-passphrase-with-new-line))))))
  803. (defun epg--prompt-GET_BOOL (_context string)
  804. (let ((entry (assoc string epg-prompt-alist)))
  805. (y-or-n-p (if entry (cdr entry) (concat string "? ")))))
  806. (defun epg--prompt-GET_BOOL-untrusted_key.override (_context _string)
  807. (y-or-n-p (if (and (equal (car epg-last-status) "USERID_HINT")
  808. (string-match "\\`\\([^ ]+\\) \\(.*\\)"
  809. (cdr epg-last-status)))
  810. (let* ((key-id (match-string 1 (cdr epg-last-status)))
  811. (user-id (match-string 2 (cdr epg-last-status)))
  812. (entry (assoc key-id epg-user-id-alist)))
  813. (if entry
  814. (setq user-id (cdr entry)))
  815. (format "Untrusted key %s %s. Use anyway? " key-id user-id))
  816. "Use untrusted key anyway? ")))
  817. (defun epg--status-GET_BOOL (context string)
  818. (let (inhibit-quit)
  819. (condition-case nil
  820. (if (funcall (or (intern-soft (concat "epg--prompt-GET_BOOL-" string))
  821. #'epg--prompt-GET_BOOL)
  822. context string)
  823. (process-send-string (epg-context-process context) "y\n")
  824. (process-send-string (epg-context-process context) "n\n"))
  825. (quit
  826. (epg-context-set-result-for
  827. context 'error
  828. (cons '(quit)
  829. (epg-context-result-for context 'error)))
  830. (delete-process (epg-context-process context))))))
  831. (defun epg--status-GET_LINE (context string)
  832. (let ((entry (assoc string epg-prompt-alist))
  833. inhibit-quit)
  834. (condition-case nil
  835. (process-send-string (epg-context-process context)
  836. (concat (read-string
  837. (if entry
  838. (cdr entry)
  839. (concat string ": ")))
  840. "\n"))
  841. (quit
  842. (epg-context-set-result-for
  843. context 'error
  844. (cons '(quit)
  845. (epg-context-result-for context 'error)))
  846. (delete-process (epg-context-process context))))))
  847. (defun epg--status-*SIG (context status string)
  848. (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
  849. (let* ((key-id (match-string 1 string))
  850. (user-id (match-string 2 string))
  851. (entry (assoc key-id epg-user-id-alist)))
  852. (epg-context-set-result-for
  853. context
  854. 'verify
  855. (cons (epg-make-signature status key-id)
  856. (epg-context-result-for context 'verify)))
  857. (condition-case nil
  858. (if (eq (epg-context-protocol context) 'CMS)
  859. (setq user-id (epg-dn-from-string user-id))
  860. (setq user-id (epg--decode-coding-string
  861. (epg--decode-percent-escape user-id)
  862. 'utf-8)))
  863. (error))
  864. (if entry
  865. (setcdr entry user-id)
  866. (setq epg-user-id-alist
  867. (cons (cons key-id user-id) epg-user-id-alist))))
  868. (epg-context-set-result-for
  869. context
  870. 'verify
  871. (cons (epg-make-signature status)
  872. (epg-context-result-for context 'verify)))))
  873. (defun epg--status-GOODSIG (context string)
  874. (epg--status-*SIG context 'good string))
  875. (defun epg--status-EXPSIG (context string)
  876. (epg--status-*SIG context 'expired string))
  877. (defun epg--status-EXPKEYSIG (context string)
  878. (epg--status-*SIG context 'expired-key string))
  879. (defun epg--status-REVKEYSIG (context string)
  880. (epg--status-*SIG context 'revoked-key string))
  881. (defun epg--status-BADSIG (context string)
  882. (epg--status-*SIG context 'bad string))
  883. (defun epg--status-NO_PUBKEY (context string)
  884. (if (eq (epg-context-operation context) 'verify)
  885. (let ((signature (car (epg-context-result-for context 'verify))))
  886. (if (and signature
  887. (eq (epg-signature-status signature) 'error)
  888. (equal (epg-signature-key-id signature) string))
  889. (setf (epg-signature-status signature) 'no-pubkey)))
  890. (epg-context-set-result-for
  891. context 'error
  892. (cons (cons 'no-pubkey string)
  893. (epg-context-result-for context 'error)))))
  894. (defun epg--status-NO_SECKEY (context string)
  895. (epg-context-set-result-for
  896. context 'error
  897. (cons (cons 'no-seckey string)
  898. (epg-context-result-for context 'error))))
  899. (defun epg--time-from-seconds (seconds)
  900. (let ((number-seconds (string-to-number (concat seconds ".0"))))
  901. (cons (floor (/ number-seconds 65536))
  902. (floor (mod number-seconds 65536)))))
  903. (defun epg--status-ERRSIG (context string)
  904. (if (string-match "\\`\\([^ ]+\\) \\([0-9]+\\) \\([0-9]+\\) \
  905. \\([0-9A-Fa-f][0-9A-Fa-f]\\) \\([^ ]+\\) \\([0-9]+\\)"
  906. string)
  907. (let ((signature (epg-make-signature 'error)))
  908. (epg-context-set-result-for
  909. context
  910. 'verify
  911. (cons signature
  912. (epg-context-result-for context 'verify)))
  913. (setf (epg-signature-key-id signature)
  914. (match-string 1 string))
  915. (setf (epg-signature-pubkey-algorithm signature)
  916. (string-to-number (match-string 2 string)))
  917. (setf (epg-signature-digest-algorithm signature)
  918. (string-to-number (match-string 3 string)))
  919. (setf (epg-signature-class signature)
  920. (string-to-number (match-string 4 string) 16))
  921. (setf (epg-signature-creation-time signature)
  922. (epg--time-from-seconds (match-string 5 string))))))
  923. (defun epg--status-VALIDSIG (context string)
  924. (let ((signature (car (epg-context-result-for context 'verify))))
  925. (when (and signature
  926. (eq (epg-signature-status signature) 'good)
  927. (string-match "\\`\\([^ ]+\\) [^ ]+ \\([^ ]+\\) \\([^ ]+\\) \
  928. \\([0-9]+\\) [^ ]+ \\([0-9]+\\) \\([0-9]+\\) \\([0-9A-Fa-f][0-9A-Fa-f]\\) \
  929. \\(.*\\)"
  930. string))
  931. (setf (epg-signature-fingerprint signature)
  932. (match-string 1 string))
  933. (setf (epg-signature-creation-time signature)
  934. (epg--time-from-seconds (match-string 2 string)))
  935. (unless (equal (match-string 3 string) "0")
  936. (setf (epg-signature-expiration-time signature)
  937. (epg--time-from-seconds (match-string 3 string))))
  938. (setf (epg-signature-version signature)
  939. (string-to-number (match-string 4 string)))
  940. (setf (epg-signature-pubkey-algorithm signature)
  941. (string-to-number (match-string 5 string)))
  942. (setf (epg-signature-digest-algorithm signature)
  943. (string-to-number (match-string 6 string)))
  944. (setf (epg-signature-class signature)
  945. (string-to-number (match-string 7 string) 16)))))
  946. (defun epg--status-TRUST_UNDEFINED (context _string)
  947. (let ((signature (car (epg-context-result-for context 'verify))))
  948. (if (and signature
  949. (eq (epg-signature-status signature) 'good))
  950. (setf (epg-signature-validity signature) 'undefined))))
  951. (defun epg--status-TRUST_NEVER (context _string)
  952. (let ((signature (car (epg-context-result-for context 'verify))))
  953. (if (and signature
  954. (eq (epg-signature-status signature) 'good))
  955. (setf (epg-signature-validity signature) 'never))))
  956. (defun epg--status-TRUST_MARGINAL (context _string)
  957. (let ((signature (car (epg-context-result-for context 'verify))))
  958. (if (and signature
  959. (eq (epg-signature-status signature) 'good))
  960. (setf (epg-signature-validity signature) 'marginal))))
  961. (defun epg--status-TRUST_FULLY (context _string)
  962. (let ((signature (car (epg-context-result-for context 'verify))))
  963. (if (and signature
  964. (eq (epg-signature-status signature) 'good))
  965. (setf (epg-signature-validity signature) 'full))))
  966. (defun epg--status-TRUST_ULTIMATE (context _string)
  967. (let ((signature (car (epg-context-result-for context 'verify))))
  968. (if (and signature
  969. (eq (epg-signature-status signature) 'good))
  970. (setf (epg-signature-validity signature) 'ultimate))))
  971. (defun epg--status-NOTATION_NAME (context string)
  972. (let ((signature (car (epg-context-result-for context 'verify))))
  973. (if signature
  974. (push (epg-make-sig-notation string nil t nil)
  975. (epg-signature-notations signature)))))
  976. (defun epg--status-NOTATION_DATA (context string)
  977. (let ((signature (car (epg-context-result-for context 'verify)))
  978. notation)
  979. (if (and signature
  980. (setq notation (car (epg-signature-notations signature))))
  981. (setf (epg-sig-notation-value notation) string))))
  982. (defun epg--status-POLICY_URL (context string)
  983. (let ((signature (car (epg-context-result-for context 'verify))))
  984. (if signature
  985. (push (epg-make-sig-notation nil string t nil)
  986. (epg-signature-notations signature)))))
  987. (defun epg--status-PROGRESS (context string)
  988. (if (and (epg-context-progress-callback context)
  989. (string-match "\\`\\([^ ]+\\) \\([^ ]\\) \\([0-9]+\\) \\([0-9]+\\)"
  990. string))
  991. (funcall (car (epg-context-progress-callback context))
  992. context
  993. (match-string 1 string)
  994. (match-string 2 string)
  995. (string-to-number (match-string 3 string))
  996. (string-to-number (match-string 4 string))
  997. (cdr (epg-context-progress-callback context)))))
  998. (defun epg--status-ENC_TO (context string)
  999. (if (string-match "\\`\\([0-9A-Za-z]+\\) \\([0-9]+\\) \\([0-9]+\\)" string)
  1000. (epg-context-set-result-for
  1001. context 'encrypted-to
  1002. (cons (list (match-string 1 string)
  1003. (string-to-number (match-string 2 string))
  1004. (string-to-number (match-string 3 string)))
  1005. (epg-context-result-for context 'encrypted-to)))))
  1006. (defun epg--status-DECRYPTION_FAILED (context _string)
  1007. (epg-context-set-result-for context 'decryption-failed t))
  1008. (defun epg--status-DECRYPTION_OKAY (context _string)
  1009. (epg-context-set-result-for context 'decryption-okay t))
  1010. (defun epg--status-NODATA (context string)
  1011. (epg-context-set-result-for
  1012. context 'error
  1013. (cons (cons 'no-data (string-to-number string))
  1014. (epg-context-result-for context 'error))))
  1015. (defun epg--status-UNEXPECTED (context string)
  1016. (epg-context-set-result-for
  1017. context 'error
  1018. (cons (cons 'unexpected (string-to-number string))
  1019. (epg-context-result-for context 'error))))
  1020. (defun epg--status-KEYEXPIRED (context string)
  1021. (epg-context-set-result-for
  1022. context 'key
  1023. (cons (list 'key-expired (cons 'expiration-time
  1024. (epg--time-from-seconds string)))
  1025. (epg-context-result-for context 'key))))
  1026. (defun epg--status-KEYREVOKED (context _string)
  1027. (epg-context-set-result-for
  1028. context 'key
  1029. (cons '(key-revoked)
  1030. (epg-context-result-for context 'key))))
  1031. (defun epg--status-BADARMOR (context _string)
  1032. (epg-context-set-result-for
  1033. context 'error
  1034. (cons '(bad-armor)
  1035. (epg-context-result-for context 'error))))
  1036. (defun epg--status-INV_RECP (context string)
  1037. (if (string-match "\\`\\([0-9]+\\) \\(.*\\)" string)
  1038. (epg-context-set-result-for
  1039. context 'error
  1040. (cons (list 'invalid-recipient
  1041. (cons 'reason
  1042. (string-to-number (match-string 1 string)))
  1043. (cons 'requested
  1044. (match-string 2 string)))
  1045. (epg-context-result-for context 'error)))))
  1046. (defun epg--status-INV_SGNR (context string)
  1047. (if (string-match "\\`\\([0-9]+\\) \\(.*\\)" string)
  1048. (epg-context-set-result-for
  1049. context 'error
  1050. (cons (list 'invalid-signer
  1051. (cons 'reason
  1052. (string-to-number (match-string 1 string)))
  1053. (cons 'requested
  1054. (match-string 2 string)))
  1055. (epg-context-result-for context 'error)))))
  1056. (defun epg--status-NO_RECP (context _string)
  1057. (epg-context-set-result-for
  1058. context 'error
  1059. (cons '(no-recipients)
  1060. (epg-context-result-for context 'error))))
  1061. (defun epg--status-NO_SGNR (context _string)
  1062. (epg-context-set-result-for
  1063. context 'error
  1064. (cons '(no-signers)
  1065. (epg-context-result-for context 'error))))
  1066. (defun epg--status-DELETE_PROBLEM (context string)
  1067. (if (string-match "\\`\\([0-9]+\\)" string)
  1068. (epg-context-set-result-for
  1069. context 'error
  1070. (cons (cons 'delete-problem
  1071. (string-to-number (match-string 1 string)))
  1072. (epg-context-result-for context 'error)))))
  1073. (defun epg--status-SIG_CREATED (context string)
  1074. (if (string-match "\\`\\([DCS]\\) \\([0-9]+\\) \\([0-9]+\\) \
  1075. \\([0-9A-Fa-F][0-9A-Fa-F]\\) \\(.*\\) " string)
  1076. (epg-context-set-result-for
  1077. context 'sign
  1078. (cons (epg-make-new-signature
  1079. (cdr (assq (aref (match-string 1 string) 0)
  1080. epg-new-signature-type-alist))
  1081. (string-to-number (match-string 2 string))
  1082. (string-to-number (match-string 3 string))
  1083. (string-to-number (match-string 4 string) 16)
  1084. (epg--time-from-seconds (match-string 5 string))
  1085. (substring string (match-end 0)))
  1086. (epg-context-result-for context 'sign)))))
  1087. (defun epg--status-KEY_CREATED (context string)
  1088. (if (string-match "\\`\\([BPS]\\) \\([^ ]+\\)" string)
  1089. (epg-context-set-result-for
  1090. context 'generate-key
  1091. (cons (list (cons 'type (string-to-char (match-string 1 string)))
  1092. (cons 'fingerprint (match-string 2 string)))
  1093. (epg-context-result-for context 'generate-key)))))
  1094. (defun epg--status-KEY_NOT_CREATED (context _string)
  1095. (epg-context-set-result-for
  1096. context 'error
  1097. (cons '(key-not-created)
  1098. (epg-context-result-for context 'error))))
  1099. (defun epg--status-IMPORTED (_context string)
  1100. (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
  1101. (let* ((key-id (match-string 1 string))
  1102. (user-id (match-string 2 string))
  1103. (entry (assoc key-id epg-user-id-alist)))
  1104. (condition-case nil
  1105. (setq user-id (epg--decode-coding-string
  1106. (epg--decode-percent-escape user-id)
  1107. 'utf-8))
  1108. (error))
  1109. (if entry
  1110. (setcdr entry user-id)
  1111. (setq epg-user-id-alist (cons (cons key-id user-id)
  1112. epg-user-id-alist))))))
  1113. (defun epg--status-IMPORT_OK (context string)
  1114. (if (string-match "\\`\\([0-9]+\\)\\( \\(.+\\)\\)?" string)
  1115. (let ((reason (string-to-number (match-string 1 string))))
  1116. (epg-context-set-result-for
  1117. context 'import-status
  1118. (cons (epg-make-import-status (if (match-beginning 2)
  1119. (match-string 3 string))
  1120. nil
  1121. (/= (logand reason 1) 0)
  1122. (/= (logand reason 2) 0)
  1123. (/= (logand reason 4) 0)
  1124. (/= (logand reason 8) 0)
  1125. (/= (logand reason 16) 0))
  1126. (epg-context-result-for context 'import-status))))))
  1127. (defun epg--status-IMPORT_PROBLEM (context string)
  1128. (if (string-match "\\`\\([0-9]+\\)\\( \\(.+\\)\\)?" string)
  1129. (epg-context-set-result-for
  1130. context 'import-status
  1131. (cons (epg-make-import-status
  1132. (if (match-beginning 2)
  1133. (match-string 3 string))
  1134. (string-to-number (match-string 1 string)))
  1135. (epg-context-result-for context 'import-status)))))
  1136. (defun epg--status-IMPORT_RES (context string)
  1137. (when (string-match "\\`\\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\) \
  1138. \\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\) \
  1139. \\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\)" string)
  1140. (epg-context-set-result-for
  1141. context 'import
  1142. (epg-make-import-result (string-to-number (match-string 1 string))
  1143. (string-to-number (match-string 2 string))
  1144. (string-to-number (match-string 3 string))
  1145. (string-to-number (match-string 4 string))
  1146. (string-to-number (match-string 5 string))
  1147. (string-to-number (match-string 6 string))
  1148. (string-to-number (match-string 7 string))
  1149. (string-to-number (match-string 8 string))
  1150. (string-to-number (match-string 9 string))
  1151. (string-to-number (match-string 10 string))
  1152. (string-to-number (match-string 11 string))
  1153. (string-to-number (match-string 12 string))
  1154. (string-to-number (match-string 13 string))
  1155. (epg-context-result-for context 'import-status)))
  1156. (epg-context-set-result-for context 'import-status nil)))
  1157. (defun epg-passphrase-callback-function (context key-id _handback)
  1158. (declare (obsolete epa-passphrase-callback-function "23.1"))
  1159. (if (eq key-id 'SYM)
  1160. (read-passwd "Passphrase for symmetric encryption: "
  1161. (eq (epg-context-operation context) 'encrypt))
  1162. (read-passwd
  1163. (if (eq key-id 'PIN)
  1164. "Passphrase for PIN: "
  1165. (let ((entry (assoc key-id epg-user-id-alist)))
  1166. (if entry
  1167. (format "Passphrase for %s %s: " key-id (cdr entry))
  1168. (format "Passphrase for %s: " key-id)))))))
  1169. (defun epg--list-keys-1 (context name mode)
  1170. (let ((args (append (if (epg-context-home-directory context)
  1171. (list "--homedir"
  1172. (epg-context-home-directory context)))
  1173. '("--with-colons" "--no-greeting" "--batch"
  1174. "--with-fingerprint" "--with-fingerprint")
  1175. (unless (eq (epg-context-protocol context) 'CMS)
  1176. '("--fixed-list-mode"))))
  1177. (list-keys-option (if (memq mode '(t secret))
  1178. "--list-secret-keys"
  1179. (if (memq mode '(nil public))
  1180. "--list-keys"
  1181. "--list-sigs")))
  1182. (coding-system-for-read 'binary)
  1183. keys string field index)
  1184. (if name
  1185. (progn
  1186. (unless (listp name)
  1187. (setq name (list name)))
  1188. (while name
  1189. (setq args (append args (list list-keys-option (car name)))
  1190. name (cdr name))))
  1191. (setq args (append args (list list-keys-option))))
  1192. (with-temp-buffer
  1193. (apply #'call-process
  1194. (epg-context-program context)
  1195. nil (list t nil) nil args)
  1196. (goto-char (point-min))
  1197. (while (re-search-forward "^[a-z][a-z][a-z]:.*" nil t)
  1198. (setq keys (cons (make-vector 15 nil) keys)
  1199. string (match-string 0)
  1200. index 0
  1201. field 0)
  1202. (while (and (< field (length (car keys)))
  1203. (eq index
  1204. (string-match "\\([^:]+\\)?:" string index)))
  1205. (setq index (match-end 0))
  1206. (aset (car keys) field (match-string 1 string))
  1207. (setq field (1+ field))))
  1208. (nreverse keys))))
  1209. (defun epg--make-sub-key-1 (line)
  1210. (epg-make-sub-key
  1211. (if (aref line 1)
  1212. (cdr (assq (string-to-char (aref line 1)) epg-key-validity-alist)))
  1213. (delq nil
  1214. (mapcar (lambda (char) (cdr (assq char epg-key-capability-alist)))
  1215. (aref line 11)))
  1216. (member (aref line 0) '("sec" "ssb"))
  1217. (string-to-number (aref line 3))
  1218. (string-to-number (aref line 2))
  1219. (aref line 4)
  1220. (epg--time-from-seconds (aref line 5))
  1221. (if (aref line 6)
  1222. (epg--time-from-seconds (aref line 6)))))
  1223. (defun epg-list-keys (context &optional name mode)
  1224. "Return a list of epg-key objects matched with NAME.
  1225. If MODE is nil or `public', only public keyring should be searched.
  1226. If MODE is t or `secret', only secret keyring should be searched.
  1227. Otherwise, only public keyring should be searched and the key
  1228. signatures should be included.
  1229. NAME is either a string or a list of strings."
  1230. (let ((lines (epg--list-keys-1 context name mode))
  1231. keys cert pointer pointer-1 index string)
  1232. (while lines
  1233. (cond
  1234. ((member (aref (car lines) 0) '("pub" "sec" "crt" "crs"))
  1235. (setq cert (member (aref (car lines) 0) '("crt" "crs"))
  1236. keys (cons (epg-make-key
  1237. (if (aref (car lines) 8)
  1238. (cdr (assq (string-to-char (aref (car lines) 8))
  1239. epg-key-validity-alist))))
  1240. keys))
  1241. (push (epg--make-sub-key-1 (car lines))
  1242. (epg-key-sub-key-list (car keys))))
  1243. ((member (aref (car lines) 0) '("sub" "ssb"))
  1244. (push (epg--make-sub-key-1 (car lines))
  1245. (epg-key-sub-key-list (car keys))))
  1246. ((equal (aref (car lines) 0) "uid")
  1247. ;; Decode the UID name as a backslash escaped UTF-8 string,
  1248. ;; generated by GnuPG/GpgSM.
  1249. (setq string (copy-sequence (aref (car lines) 9))
  1250. index 0)
  1251. (while (string-match "\"" string index)
  1252. (setq string (replace-match "\\\"" t t string)
  1253. index (1+ (match-end 0))))
  1254. (condition-case nil
  1255. (setq string (epg--decode-coding-string
  1256. (car (read-from-string (concat "\"" string "\"")))
  1257. 'utf-8))
  1258. (error
  1259. (setq string (aref (car lines) 9))))
  1260. (push (epg-make-user-id
  1261. (if (aref (car lines) 1)
  1262. (cdr (assq (string-to-char (aref (car lines) 1))
  1263. epg-key-validity-alist)))
  1264. (if cert
  1265. (condition-case nil
  1266. (epg-dn-from-string string)
  1267. (error string))
  1268. string))
  1269. (epg-key-user-id-list (car keys))))
  1270. ((equal (aref (car lines) 0) "fpr")
  1271. (setf (epg-sub-key-fingerprint (car (epg-key-sub-key-list (car keys))))
  1272. (aref (car lines) 9)))
  1273. ((equal (aref (car lines) 0) "sig")
  1274. (push
  1275. (epg-make-key-signature
  1276. (if (aref (car lines) 1)
  1277. (cdr (assq (string-to-char (aref (car lines) 1))
  1278. epg-key-validity-alist)))
  1279. (string-to-number (aref (car lines) 3))
  1280. (aref (car lines) 4)
  1281. (epg--time-from-seconds (aref (car lines) 5))
  1282. (epg--time-from-seconds (aref (car lines) 6))
  1283. (aref (car lines) 9)
  1284. (string-to-number (aref (car lines) 10) 16)
  1285. (eq (aref (aref (car lines) 10) 2) ?x))
  1286. (epg-user-id-signature-list
  1287. (car (epg-key-user-id-list (car keys)))))))
  1288. (setq lines (cdr lines)))
  1289. (setq keys (nreverse keys)
  1290. pointer keys)
  1291. (while pointer
  1292. (epg--gv-nreverse (epg-key-sub-key-list (car pointer)))
  1293. (setq pointer-1 (epg--gv-nreverse (epg-key-user-id-list (car pointer))))
  1294. (while pointer-1
  1295. (epg--gv-nreverse (epg-user-id-signature-list (car pointer-1)))
  1296. (setq pointer-1 (cdr pointer-1)))
  1297. (setq pointer (cdr pointer)))
  1298. keys))
  1299. (eval-and-compile
  1300. (if (fboundp 'make-temp-file)
  1301. (defalias 'epg--make-temp-file 'make-temp-file)
  1302. (defvar temporary-file-directory)
  1303. ;; stolen from poe.el.
  1304. (defun epg--make-temp-file (prefix)
  1305. "Create a temporary file.
  1306. The returned file name (created by appending some random characters at the end
  1307. of PREFIX, and expanding against `temporary-file-directory' if necessary),
  1308. is guaranteed to point to a newly created empty file.
  1309. You can then use `write-region' to write new data into the file."
  1310. (let ((orig-modes (default-file-modes))
  1311. tempdir tempfile)
  1312. (setq prefix (expand-file-name prefix
  1313. (if (featurep 'xemacs)
  1314. (temp-directory)
  1315. temporary-file-directory)))
  1316. (unwind-protect
  1317. (let (file)
  1318. ;; First, create a temporary directory.
  1319. (set-default-file-modes #o700)
  1320. (while (condition-case ()
  1321. (progn
  1322. (setq tempdir (make-temp-name
  1323. (concat
  1324. (file-name-directory prefix)
  1325. "DIR")))
  1326. ;; return nil or signal an error.
  1327. (make-directory tempdir))
  1328. ;; let's try again.
  1329. (file-already-exists t)))
  1330. ;; Second, create a temporary file in the tempdir.
  1331. ;; There *is* a race condition between `make-temp-name'
  1332. ;; and `write-region', but we don't care it since we are
  1333. ;; in a private directory now.
  1334. (setq tempfile (make-temp-name (concat tempdir "/EMU")))
  1335. (write-region "" nil tempfile nil 'silent)
  1336. ;; Finally, make a hard-link from the tempfile.
  1337. (while (condition-case ()
  1338. (progn
  1339. (setq file (make-temp-name prefix))
  1340. ;; return nil or signal an error.
  1341. (add-name-to-file tempfile file))
  1342. ;; let's try again.
  1343. (file-already-exists t)))
  1344. file)
  1345. (set-default-file-modes orig-modes)
  1346. ;; Cleanup the tempfile.
  1347. (and tempfile
  1348. (file-exists-p tempfile)
  1349. (delete-file tempfile))
  1350. ;; Cleanup the tempdir.
  1351. (and tempdir
  1352. (file-directory-p tempdir)
  1353. (delete-directory tempdir)))))))
  1354. (defun epg--args-from-sig-notations (notations)
  1355. (apply #'nconc
  1356. (mapcar
  1357. (lambda (notation)
  1358. (if (and (epg-sig-notation-name notation)
  1359. (not (epg-sig-notation-human-readable notation)))
  1360. (error "Unreadable"))
  1361. (if (epg-sig-notation-name notation)
  1362. (list "--sig-notation"
  1363. (if (epg-sig-notation-critical notation)
  1364. (concat "!" (epg-sig-notation-name notation)
  1365. "=" (epg-sig-notation-value notation))
  1366. (concat (epg-sig-notation-name notation)
  1367. "=" (epg-sig-notation-value notation))))
  1368. (list "--sig-policy-url"
  1369. (if (epg-sig-notation-critical notation)
  1370. (concat "!" (epg-sig-notation-value notation))
  1371. (epg-sig-notation-value notation)))))
  1372. notations)))
  1373. (defun epg-cancel (context)
  1374. (if (buffer-live-p (process-buffer (epg-context-process context)))
  1375. (with-current-buffer (process-buffer (epg-context-process context))
  1376. (epg-context-set-result-for
  1377. epg-context 'error
  1378. (cons '(quit)
  1379. (epg-context-result-for epg-context 'error)))))
  1380. (if (eq (process-status (epg-context-process context)) 'run)
  1381. (delete-process (epg-context-process context))))
  1382. (defun epg-start-decrypt (context cipher)
  1383. "Initiate a decrypt operation on CIPHER.
  1384. CIPHER must be a file data object.
  1385. If you use this function, you will need to wait for the completion of
  1386. `epg-gpg-program' by using `epg-wait-for-completion' and call
  1387. `epg-reset' to clear a temporary output file.
  1388. If you are unsure, use synchronous version of this function
  1389. `epg-decrypt-file' or `epg-decrypt-string' instead."
  1390. (unless (epg-data-file cipher)
  1391. (error "Not a file"))
  1392. (setf (epg-context-operation context) 'decrypt)
  1393. (setf (epg-context-result context) nil)
  1394. (epg--start context (list "--decrypt" "--" (epg-data-file cipher)))
  1395. ;; `gpgsm' does not read passphrase from stdin, so waiting is not needed.
  1396. (unless (eq (epg-context-protocol context) 'CMS)
  1397. (epg-wait-for-status context '("BEGIN_DECRYPTION"))))
  1398. (defun epg--check-error-for-decrypt (context)
  1399. (let ((errors (epg-context-result-for context 'error)))
  1400. (if (epg-context-result-for context 'decryption-failed)
  1401. (signal 'epg-error
  1402. (list "Decryption failed" (epg-errors-to-string errors))))
  1403. (unless (epg-context-result-for context 'decryption-okay)
  1404. (signal 'epg-error
  1405. (list "Can't decrypt" (epg-errors-to-string errors))))))
  1406. (defun epg-decrypt-file (context cipher plain)
  1407. "Decrypt a file CIPHER and store the result to a file PLAIN.
  1408. If PLAIN is nil, it returns the result as a string."
  1409. (unwind-protect
  1410. (progn
  1411. (setf (epg-context-output-file context)
  1412. (or plain (epg--make-temp-file "epg-output")))
  1413. (epg-start-decrypt context (epg-make-data-from-file cipher))
  1414. (epg-wait-for-completion context)
  1415. (epg--check-error-for-decrypt context)
  1416. (unless plain
  1417. (epg-read-output context)))
  1418. (unless plain
  1419. (epg-delete-output-file context))
  1420. (epg-reset context)))
  1421. (defun epg-decrypt-string (context cipher)
  1422. "Decrypt a string CIPHER and return the plain text."
  1423. (let ((input-file (epg--make-temp-file "epg-input"))
  1424. (coding-system-for-write 'binary))
  1425. (unwind-protect
  1426. (progn
  1427. (write-region cipher nil input-file nil 'quiet)
  1428. (setf (epg-context-output-file context)
  1429. (epg--make-temp-file "epg-output"))
  1430. (epg-start-decrypt context (epg-make-data-from-file input-file))
  1431. (epg-wait-for-completion context)
  1432. (epg--check-error-for-decrypt context)
  1433. (epg-read-output context))
  1434. (epg-delete-output-file context)
  1435. (if (file-exists-p input-file)
  1436. (delete-file input-file))
  1437. (epg-reset context))))
  1438. (defun epg-start-verify (context signature &optional signed-text)
  1439. "Initiate a verify operation on SIGNATURE.
  1440. SIGNATURE and SIGNED-TEXT are a data object if they are specified.
  1441. For a detached signature, both SIGNATURE and SIGNED-TEXT should be set.
  1442. For a normal or a cleartext signature, SIGNED-TEXT should be nil.
  1443. If you use this function, you will need to wait for the completion of
  1444. `epg-gpg-program' by using `epg-wait-for-completion' and call
  1445. `epg-reset' to clear a temporary output file.
  1446. If you are unsure, use synchronous version of this function
  1447. `epg-verify-file' or `epg-verify-string' instead."
  1448. (setf (epg-context-operation context) 'verify)
  1449. (setf (epg-context-result context) nil)
  1450. (if signed-text
  1451. ;; Detached signature.
  1452. (if (epg-data-file signed-text)
  1453. (epg--start context (list "--verify" "--" (epg-data-file signature)
  1454. (epg-data-file signed-text)))
  1455. (epg--start context (list "--verify" "--" (epg-data-file signature)
  1456. "-"))
  1457. (if (eq (process-status (epg-context-process context)) 'run)
  1458. (process-send-string (epg-context-process context)
  1459. (epg-data-string signed-text)))
  1460. (if (eq (process-status (epg-context-process context)) 'run)
  1461. (process-send-eof (epg-context-process context))))
  1462. ;; Normal (or cleartext) signature.
  1463. (if (epg-data-file signature)
  1464. (epg--start context (if (eq (epg-context-protocol context) 'CMS)
  1465. (list "--verify" "--" (epg-data-file signature))
  1466. (list "--" (epg-data-file signature))))
  1467. (epg--start context (if (eq (epg-context-protocol context) 'CMS)
  1468. '("--verify" "-")
  1469. '("-")))
  1470. (if (eq (process-status (epg-context-process context)) 'run)
  1471. (process-send-string (epg-context-process context)
  1472. (epg-data-string signature)))
  1473. (if (eq (process-status (epg-context-process context)) 'run)
  1474. (process-send-eof (epg-context-process context))))))
  1475. (defun epg-verify-file (context signature &optional signed-text plain)
  1476. "Verify a file SIGNATURE.
  1477. SIGNED-TEXT and PLAIN are also a file if they are specified.
  1478. For a detached signature, both SIGNATURE and SIGNED-TEXT should be
  1479. string. For a normal or a cleartext signature, SIGNED-TEXT should be
  1480. nil. In the latter case, if PLAIN is specified, the plaintext is
  1481. stored into the file after successful verification.
  1482. Note that this function does not return verification result as t
  1483. or nil, nor signal error on failure. That's a design decision to
  1484. handle the case where SIGNATURE has multiple signature.
  1485. To check the verification results, use `epg-context-result-for' as follows:
  1486. \(epg-context-result-for context \\='verify)
  1487. which will return a list of `epg-signature' object."
  1488. (unwind-protect
  1489. (progn
  1490. (setf (epg-context-output-file context)
  1491. (or plain (epg--make-temp-file "epg-output")))
  1492. (if signed-text
  1493. (epg-start-verify context
  1494. (epg-make-data-from-file signature)
  1495. (epg-make-data-from-file signed-text))
  1496. (epg-start-verify context
  1497. (epg-make-data-from-file signature)))
  1498. (epg-wait-for-completion context)
  1499. (unless plain
  1500. (epg-read-output context)))
  1501. (unless plain
  1502. (epg-delete-output-file context))
  1503. (epg-reset context)))
  1504. (defun epg-verify-string (context signature &optional signed-text)
  1505. "Verify a string SIGNATURE.
  1506. SIGNED-TEXT is a string if it is specified.
  1507. For a detached signature, both SIGNATURE and SIGNED-TEXT should be
  1508. string. For a normal or a cleartext signature, SIGNED-TEXT should be
  1509. nil. In the latter case, this function returns the plaintext after
  1510. successful verification.
  1511. Note that this function does not return verification result as t
  1512. or nil, nor signal error on failure. That's a design decision to
  1513. handle the case where SIGNATURE has multiple signature.
  1514. To check the verification results, use `epg-context-result-for' as follows:
  1515. \(epg-context-result-for context \\='verify)
  1516. which will return a list of `epg-signature' object."
  1517. (let ((coding-system-for-write 'binary)
  1518. input-file)
  1519. (unwind-protect
  1520. (progn
  1521. (setf (epg-context-output-file context)
  1522. (epg--make-temp-file "epg-output"))
  1523. (if signed-text
  1524. (progn
  1525. (setq input-file (epg--make-temp-file "epg-signature"))
  1526. (write-region signature nil input-file nil 'quiet)
  1527. (epg-start-verify context
  1528. (epg-make-data-from-file input-file)
  1529. (epg-make-data-from-string signed-text)))
  1530. (epg-start-verify context (epg-make-data-from-string signature)))
  1531. (epg-wait-for-completion context)
  1532. (epg-read-output context))
  1533. (epg-delete-output-file context)
  1534. (if (and input-file
  1535. (file-exists-p input-file))
  1536. (delete-file input-file))
  1537. (epg-reset context))))
  1538. (defun epg-start-sign (context plain &optional mode)
  1539. "Initiate a sign operation on PLAIN.
  1540. PLAIN is a data object.
  1541. If optional 3rd argument MODE is t or `detached', it makes a detached signature.
  1542. If it is nil or `normal', it makes a normal signature.
  1543. Otherwise, it makes a cleartext signature.
  1544. If you use this function, you will need to wait for the completion of
  1545. `epg-gpg-program' by using `epg-wait-for-completion' and call
  1546. `epg-reset' to clear a temporary output file.
  1547. If you are unsure, use synchronous version of this function
  1548. `epg-sign-file' or `epg-sign-string' instead."
  1549. (setf (epg-context-operation context) 'sign)
  1550. (setf (epg-context-result context) nil)
  1551. (unless (memq mode '(t detached nil normal)) ;i.e. cleartext
  1552. (epg-context-set-armor context nil)
  1553. (epg-context-set-textmode context nil))
  1554. (epg--start context
  1555. (append (list (if (memq mode '(t detached))
  1556. "--detach-sign"
  1557. (if (memq mode '(nil normal))
  1558. "--sign"
  1559. "--clearsign")))
  1560. (apply #'nconc
  1561. (mapcar
  1562. (lambda (signer)
  1563. (list "-u"
  1564. (epg-sub-key-id
  1565. (car (epg-key-sub-key-list signer)))))
  1566. (epg-context-signers context)))
  1567. (epg--args-from-sig-notations
  1568. (epg-context-sig-notations context))
  1569. (if (epg-data-file plain)
  1570. (list "--" (epg-data-file plain)))))
  1571. ;; `gpgsm' does not read passphrase from stdin, so waiting is not needed.
  1572. (unless (eq (epg-context-protocol context) 'CMS)
  1573. (epg-wait-for-status context '("BEGIN_SIGNING")))
  1574. (when (epg-data-string plain)
  1575. (if (eq (process-status (epg-context-process context)) 'run)
  1576. (process-send-string (epg-context-process context)
  1577. (epg-data-string plain)))
  1578. (if (eq (process-status (epg-context-process context)) 'run)
  1579. (process-send-eof (epg-context-process context)))))
  1580. (defun epg-sign-file (context plain signature &optional mode)
  1581. "Sign a file PLAIN and store the result to a file SIGNATURE.
  1582. If SIGNATURE is nil, it returns the result as a string.
  1583. If optional 3rd argument MODE is t or `detached', it makes a detached signature.
  1584. If it is nil or `normal', it makes a normal signature.
  1585. Otherwise, it makes a cleartext signature."
  1586. (unwind-protect
  1587. (progn
  1588. (setf (epg-context-output-file context)
  1589. (or signature (epg--make-temp-file "epg-output")))
  1590. (epg-start-sign context (epg-make-data-from-file plain) mode)
  1591. (epg-wait-for-completion context)
  1592. (unless (epg-context-result-for context 'sign)
  1593. (let ((errors (epg-context-result-for context 'error)))
  1594. (signal 'epg-error
  1595. (list "Sign failed" (epg-errors-to-string errors)))))
  1596. (unless signature
  1597. (epg-read-output context)))
  1598. (unless signature
  1599. (epg-delete-output-file context))
  1600. (epg-reset context)))
  1601. (defun epg-sign-string (context plain &optional mode)
  1602. "Sign a string PLAIN and return the output as string.
  1603. If optional 3rd argument MODE is t or `detached', it makes a detached signature.
  1604. If it is nil or `normal', it makes a normal signature.
  1605. Otherwise, it makes a cleartext signature."
  1606. (let ((input-file
  1607. (unless (eq (epg-context-protocol context) 'CMS)
  1608. (epg--make-temp-file "epg-input")))
  1609. (coding-system-for-write 'binary))
  1610. (unwind-protect
  1611. (progn
  1612. (setf (epg-context-output-file context)
  1613. (epg--make-temp-file "epg-output"))
  1614. (if input-file
  1615. (write-region plain nil input-file nil 'quiet))
  1616. (epg-start-sign context
  1617. (if input-file
  1618. (epg-make-data-from-file input-file)
  1619. (epg-make-data-from-string plain))
  1620. mode)
  1621. (epg-wait-for-completion context)
  1622. (unless (epg-context-result-for context 'sign)
  1623. (if (epg-context-result-for context 'error)
  1624. (let ((errors (epg-context-result-for context 'error)))
  1625. (signal 'epg-error
  1626. (list "Sign failed" (epg-errors-to-string errors))))))
  1627. (epg-read-output context))
  1628. (epg-delete-output-file context)
  1629. (if input-file
  1630. (delete-file input-file))
  1631. (epg-reset context))))
  1632. (defun epg-start-encrypt (context plain recipients
  1633. &optional sign always-trust)
  1634. "Initiate an encrypt operation on PLAIN.
  1635. PLAIN is a data object.
  1636. If RECIPIENTS is nil, it performs symmetric encryption.
  1637. If you use this function, you will need to wait for the completion of
  1638. `epg-gpg-program' by using `epg-wait-for-completion' and call
  1639. `epg-reset' to clear a temporary output file.
  1640. If you are unsure, use synchronous version of this function
  1641. `epg-encrypt-file' or `epg-encrypt-string' instead."
  1642. (setf (epg-context-operation context) 'encrypt)
  1643. (setf (epg-context-result context) nil)
  1644. (epg--start context
  1645. (append (if always-trust '("--always-trust"))
  1646. (if recipients '("--encrypt") '("--symmetric"))
  1647. (if sign '("--sign"))
  1648. (if sign
  1649. (apply #'nconc
  1650. (mapcar
  1651. (lambda (signer)
  1652. (list "-u"
  1653. (epg-sub-key-id
  1654. (car (epg-key-sub-key-list
  1655. signer)))))
  1656. (epg-context-signers context))))
  1657. (if sign
  1658. (epg--args-from-sig-notations
  1659. (epg-context-sig-notations context)))
  1660. (apply #'nconc
  1661. (mapcar
  1662. (lambda (recipient)
  1663. (list "-r"
  1664. (epg-sub-key-id
  1665. (car (epg-key-sub-key-list recipient)))))
  1666. recipients))
  1667. (if (epg-data-file plain)
  1668. (list "--" (epg-data-file plain)))))
  1669. ;; `gpgsm' does not read passphrase from stdin, so waiting is not needed.
  1670. (unless (eq (epg-context-protocol context) 'CMS)
  1671. (epg-wait-for-status context
  1672. (if sign '("BEGIN_SIGNING") '("BEGIN_ENCRYPTION"))))
  1673. (when (epg-data-string plain)
  1674. (if (eq (process-status (epg-context-process context)) 'run)
  1675. (process-send-string (epg-context-process context)
  1676. (epg-data-string plain)))
  1677. (if (eq (process-status (epg-context-process context)) 'run)
  1678. (process-send-eof (epg-context-process context)))))
  1679. (defun epg-encrypt-file (context plain recipients
  1680. cipher &optional sign always-trust)
  1681. "Encrypt a file PLAIN and store the result to a file CIPHER.
  1682. If CIPHER is nil, it returns the result as a string.
  1683. If RECIPIENTS is nil, it performs symmetric encryption."
  1684. (unwind-protect
  1685. (progn
  1686. (setf (epg-context-output-file context)
  1687. (or cipher (epg--make-temp-file "epg-output")))
  1688. (epg-start-encrypt context (epg-make-data-from-file plain)
  1689. recipients sign always-trust)
  1690. (epg-wait-for-completion context)
  1691. (let ((errors (epg-context-result-for context 'error)))
  1692. (if (and sign
  1693. (not (epg-context-result-for context 'sign)))
  1694. (signal 'epg-error
  1695. (list "Sign failed" (epg-errors-to-string errors))))
  1696. (if errors
  1697. (signal 'epg-error
  1698. (list "Encrypt failed" (epg-errors-to-string errors)))))
  1699. (unless cipher
  1700. (epg-read-output context)))
  1701. (unless cipher
  1702. (epg-delete-output-file context))
  1703. (epg-reset context)))
  1704. (defun epg-encrypt-string (context plain recipients
  1705. &optional sign always-trust)
  1706. "Encrypt a string PLAIN.
  1707. If RECIPIENTS is nil, it performs symmetric encryption."
  1708. (let ((input-file
  1709. (unless (or (not sign)
  1710. (eq (epg-context-protocol context) 'CMS))
  1711. (epg--make-temp-file "epg-input")))
  1712. (coding-system-for-write 'binary))
  1713. (unwind-protect
  1714. (progn
  1715. (setf (epg-context-output-file context)
  1716. (epg--make-temp-file "epg-output"))
  1717. (if input-file
  1718. (write-region plain nil input-file nil 'quiet))
  1719. (epg-start-encrypt context
  1720. (if input-file
  1721. (epg-make-data-from-file input-file)
  1722. (epg-make-data-from-string plain))
  1723. recipients sign always-trust)
  1724. (epg-wait-for-completion context)
  1725. (let ((errors (epg-context-result-for context 'error)))
  1726. (if (and sign
  1727. (not (epg-context-result-for context 'sign)))
  1728. (signal 'epg-error
  1729. (list "Sign failed" (epg-errors-to-string errors))))
  1730. (if errors
  1731. (signal 'epg-error
  1732. (list "Encrypt failed" (epg-errors-to-string errors)))))
  1733. (epg-read-output context))
  1734. (epg-delete-output-file context)
  1735. (if input-file
  1736. (delete-file input-file))
  1737. (epg-reset context))))
  1738. (defun epg-start-export-keys (context keys)
  1739. "Initiate an export keys operation.
  1740. If you use this function, you will need to wait for the completion of
  1741. `epg-gpg-program' by using `epg-wait-for-completion' and call
  1742. `epg-reset' to clear a temporary output file.
  1743. If you are unsure, use synchronous version of this function
  1744. `epg-export-keys-to-file' or `epg-export-keys-to-string' instead."
  1745. (setf (epg-context-operation context) 'export-keys)
  1746. (setf (epg-context-result context) nil)
  1747. (epg--start context (cons "--export"
  1748. (mapcar
  1749. (lambda (key)
  1750. (epg-sub-key-id
  1751. (car (epg-key-sub-key-list key))))
  1752. keys))))
  1753. (defun epg-export-keys-to-file (context keys file)
  1754. "Extract public KEYS."
  1755. (unwind-protect
  1756. (progn
  1757. (setf (epg-context-output-file context)
  1758. (or file (epg--make-temp-file "epg-output")))
  1759. (epg-start-export-keys context keys)
  1760. (epg-wait-for-completion context)
  1761. (let ((errors (epg-context-result-for context 'error)))
  1762. (if errors
  1763. (signal 'epg-error
  1764. (list "Export keys failed"
  1765. (epg-errors-to-string errors)))))
  1766. (unless file
  1767. (epg-read-output context)))
  1768. (unless file
  1769. (epg-delete-output-file context))
  1770. (epg-reset context)))
  1771. (defun epg-export-keys-to-string (context keys)
  1772. "Extract public KEYS and return them as a string."
  1773. (epg-export-keys-to-file context keys nil))
  1774. (defun epg-start-import-keys (context keys)
  1775. "Initiate an import keys operation.
  1776. KEYS is a data object.
  1777. If you use this function, you will need to wait for the completion of
  1778. `epg-gpg-program' by using `epg-wait-for-completion' and call
  1779. `epg-reset' to clear a temporary output file.
  1780. If you are unsure, use synchronous version of this function
  1781. `epg-import-keys-from-file' or `epg-import-keys-from-string' instead."
  1782. (setf (epg-context-operation context) 'import-keys)
  1783. (setf (epg-context-result context) nil)
  1784. (epg--start context (if (epg-data-file keys)
  1785. (list "--import" "--" (epg-data-file keys))
  1786. (list "--import")))
  1787. (when (epg-data-string keys)
  1788. (if (eq (process-status (epg-context-process context)) 'run)
  1789. (process-send-string (epg-context-process context)
  1790. (epg-data-string keys)))
  1791. (if (eq (process-status (epg-context-process context)) 'run)
  1792. (process-send-eof (epg-context-process context)))))
  1793. (defun epg--import-keys-1 (context keys)
  1794. (unwind-protect
  1795. (progn
  1796. (epg-start-import-keys context keys)
  1797. (epg-wait-for-completion context)
  1798. (let ((errors (epg-context-result-for context 'error)))
  1799. (if errors
  1800. (signal 'epg-error
  1801. (list "Import keys failed"
  1802. (epg-errors-to-string errors))))))
  1803. (epg-reset context)))
  1804. (defun epg-import-keys-from-file (context keys)
  1805. "Add keys from a file KEYS."
  1806. (epg--import-keys-1 context (epg-make-data-from-file keys)))
  1807. (defun epg-import-keys-from-string (context keys)
  1808. "Add keys from a string KEYS."
  1809. (epg--import-keys-1 context (epg-make-data-from-string keys)))
  1810. (defun epg-start-receive-keys (context key-id-list)
  1811. "Initiate a receive key operation.
  1812. KEY-ID-LIST is a list of key IDs.
  1813. If you use this function, you will need to wait for the completion of
  1814. `epg-gpg-program' by using `epg-wait-for-completion' and call
  1815. `epg-reset' to clear a temporary output file.
  1816. If you are unsure, use synchronous version of this function
  1817. `epg-receive-keys' instead."
  1818. (setf (epg-context-operation context) 'receive-keys)
  1819. (setf (epg-context-result context) nil)
  1820. (epg--start context (cons "--recv-keys" key-id-list)))
  1821. (defun epg-receive-keys (context keys)
  1822. "Add keys from server.
  1823. KEYS is a list of key IDs"
  1824. (unwind-protect
  1825. (progn
  1826. (epg-start-receive-keys context keys)
  1827. (epg-wait-for-completion context)
  1828. (let ((errors (epg-context-result-for context 'error)))
  1829. (if errors
  1830. (signal 'epg-error
  1831. (list "Receive keys failed"
  1832. (epg-errors-to-string errors))))))
  1833. (epg-reset context)))
  1834. (defalias 'epg-import-keys-from-server 'epg-receive-keys)
  1835. (defun epg-start-delete-keys (context keys &optional allow-secret)
  1836. "Initiate a delete keys operation.
  1837. If you use this function, you will need to wait for the completion of
  1838. `epg-gpg-program' by using `epg-wait-for-completion' and call
  1839. `epg-reset' to clear a temporary output file.
  1840. If you are unsure, use synchronous version of this function
  1841. `epg-delete-keys' instead."
  1842. (setf (epg-context-operation context) 'delete-keys)
  1843. (setf (epg-context-result context) nil)
  1844. (epg--start context (cons (if allow-secret
  1845. "--delete-secret-key"
  1846. "--delete-key")
  1847. (mapcar
  1848. (lambda (key)
  1849. (epg-sub-key-id
  1850. (car (epg-key-sub-key-list key))))
  1851. keys))))
  1852. (defun epg-delete-keys (context keys &optional allow-secret)
  1853. "Delete KEYS from the key ring."
  1854. (unwind-protect
  1855. (progn
  1856. (epg-start-delete-keys context keys allow-secret)
  1857. (epg-wait-for-completion context)
  1858. (let ((errors (epg-context-result-for context 'error)))
  1859. (if errors
  1860. (signal 'epg-error
  1861. (list "Delete keys failed"
  1862. (epg-errors-to-string errors))))))
  1863. (epg-reset context)))
  1864. (defun epg-start-sign-keys (context keys &optional local)
  1865. "Initiate a sign keys operation.
  1866. If you use this function, you will need to wait for the completion of
  1867. `epg-gpg-program' by using `epg-wait-for-completion' and call
  1868. `epg-reset' to clear a temporary output file.
  1869. If you are unsure, use synchronous version of this function
  1870. `epg-sign-keys' instead."
  1871. (declare (obsolete nil "23.1"))
  1872. (setf (epg-context-operation context) 'sign-keys)
  1873. (setf (epg-context-result context) nil)
  1874. (epg--start context (cons (if local
  1875. "--lsign-key"
  1876. "--sign-key")
  1877. (mapcar
  1878. (lambda (key)
  1879. (epg-sub-key-id
  1880. (car (epg-key-sub-key-list key))))
  1881. keys))))
  1882. (defun epg-sign-keys (context keys &optional local)
  1883. "Sign KEYS from the key ring."
  1884. (declare (obsolete nil "23.1"))
  1885. (unwind-protect
  1886. (progn
  1887. (epg-start-sign-keys context keys local)
  1888. (epg-wait-for-completion context)
  1889. (let ((errors (epg-context-result-for context 'error)))
  1890. (if errors
  1891. (signal 'epg-error
  1892. (list "Sign keys failed"
  1893. (epg-errors-to-string errors))))))
  1894. (epg-reset context)))
  1895. (defun epg-start-generate-key (context parameters)
  1896. "Initiate a key generation.
  1897. PARAMETERS is a string which specifies parameters of the generated key.
  1898. See Info node `(gnupg) Unattended GPG key generation' in the
  1899. GnuPG manual for the format.
  1900. If you use this function, you will need to wait for the completion of
  1901. `epg-gpg-program' by using `epg-wait-for-completion' and call
  1902. `epg-reset' to clear a temporary output file.
  1903. If you are unsure, use synchronous version of this function
  1904. `epg-generate-key-from-file' or `epg-generate-key-from-string' instead."
  1905. (setf (epg-context-operation context) 'generate-key)
  1906. (setf (epg-context-result context) nil)
  1907. (if (epg-data-file parameters)
  1908. (epg--start context (list "--batch" "--gen-key" "--"
  1909. (epg-data-file parameters)))
  1910. (epg--start context '("--batch" "--gen-key"))
  1911. (if (eq (process-status (epg-context-process context)) 'run)
  1912. (process-send-string (epg-context-process context)
  1913. (epg-data-string parameters)))
  1914. (if (eq (process-status (epg-context-process context)) 'run)
  1915. (process-send-eof (epg-context-process context)))))
  1916. (defun epg-generate-key-from-file (context parameters)
  1917. "Generate a new key pair.
  1918. PARAMETERS is a file which tells how to create the key."
  1919. (unwind-protect
  1920. (progn
  1921. (epg-start-generate-key context (epg-make-data-from-file parameters))
  1922. (epg-wait-for-completion context)
  1923. (let ((errors (epg-context-result-for context 'error)))
  1924. (if errors
  1925. (signal 'epg-error
  1926. (list "Generate key failed"
  1927. (epg-errors-to-string errors))))))
  1928. (epg-reset context)))
  1929. (defun epg-generate-key-from-string (context parameters)
  1930. "Generate a new key pair.
  1931. PARAMETERS is a string which tells how to create the key."
  1932. (unwind-protect
  1933. (progn
  1934. (epg-start-generate-key context (epg-make-data-from-string parameters))
  1935. (epg-wait-for-completion context)
  1936. (let ((errors (epg-context-result-for context 'error)))
  1937. (if errors
  1938. (signal 'epg-error
  1939. (list "Generate key failed"
  1940. (epg-errors-to-string errors))))))
  1941. (epg-reset context)))
  1942. (defun epg-start-edit-key (context key edit-callback handback)
  1943. "Initiate an edit operation on KEY.
  1944. EDIT-CALLBACK is called from process filter and takes 3
  1945. arguments: the context, a status, an argument string, and the
  1946. handback argument.
  1947. If you use this function, you will need to wait for the completion of
  1948. `epg-gpg-program' by using `epg-wait-for-completion' and call
  1949. `epg-reset' to clear a temporary output file.
  1950. If you are unsure, use synchronous version of this function
  1951. `epg-edit-key' instead."
  1952. (setf (epg-context-operation context) 'edit-key)
  1953. (setf (epg-context-result context) nil)
  1954. (setf (epg-context-edit-callback context) (cons edit-callback handback))
  1955. (epg--start context (list "--edit-key"
  1956. (epg-sub-key-id
  1957. (car (epg-key-sub-key-list key))))))
  1958. (defun epg-edit-key (context key edit-callback handback)
  1959. "Edit KEY in the keyring."
  1960. (unwind-protect
  1961. (progn
  1962. (epg-start-edit-key context key edit-callback handback)
  1963. (epg-wait-for-completion context)
  1964. (let ((errors (epg-context-result-for context 'error)))
  1965. (if errors
  1966. (signal 'epg-error
  1967. (list "Edit key failed"
  1968. (epg-errors-to-string errors))))))
  1969. (epg-reset context)))
  1970. (defun epg--decode-percent-escape (string)
  1971. (let ((index 0))
  1972. (while (string-match "%\\(\\(%\\)\\|\\([0-9A-Fa-f][0-9A-Fa-f]\\)\\)"
  1973. string index)
  1974. (if (match-beginning 2)
  1975. (setq string (replace-match "%" t t string)
  1976. index (1- (match-end 0)))
  1977. (setq string (replace-match
  1978. (string (string-to-number (match-string 3 string) 16))
  1979. t t string)
  1980. index (- (match-end 0) 2))))
  1981. string))
  1982. (defun epg--decode-hexstring (string)
  1983. (let ((index 0))
  1984. (while (eq index (string-match "[0-9A-Fa-f][0-9A-Fa-f]" string index))
  1985. (setq string (replace-match (string (string-to-number
  1986. (match-string 0 string) 16))
  1987. t t string)
  1988. index (1- (match-end 0))))
  1989. string))
  1990. (defun epg--decode-quotedstring (string)
  1991. (let ((index 0))
  1992. (while (string-match "\\\\\\(\\([,=+<>#;\\\"]\\)\\|\
  1993. \\([0-9A-Fa-f][0-9A-Fa-f]\\)\\)"
  1994. string index)
  1995. (if (match-beginning 2)
  1996. (setq string (replace-match "\\2" t nil string)
  1997. index (1- (match-end 0)))
  1998. (if (match-beginning 3)
  1999. (setq string (replace-match (string (string-to-number
  2000. (match-string 0 string) 16))
  2001. t t string)
  2002. index (- (match-end 0) 2)))))
  2003. string))
  2004. (defun epg-dn-from-string (string)
  2005. "Parse STRING as LADPv3 Distinguished Names (RFC2253).
  2006. The return value is an alist mapping from types to values."
  2007. (let ((index 0)
  2008. (length (length string))
  2009. alist type value group)
  2010. (while (< index length)
  2011. (if (eq index (string-match "[ \t\n\r]*" string index))
  2012. (setq index (match-end 0)))
  2013. (if (eq index (string-match
  2014. "\\([0-9]+\\(\\.[0-9]+\\)*\\)[ \t\n\r]*=[ \t\n\r]*"
  2015. string index))
  2016. (setq type (match-string 1 string)
  2017. index (match-end 0))
  2018. (if (eq index (string-match "\\([0-9A-Za-z]+\\)[ \t\n\r]*=[ \t\n\r]*"
  2019. string index))
  2020. (setq type (match-string 1 string)
  2021. index (match-end 0))))
  2022. (unless type
  2023. (error "Invalid type"))
  2024. (if (eq index (string-match
  2025. "\\([^,=+<>#;\\\"]\\|\\\\.\\)+"
  2026. string index))
  2027. (setq index (match-end 0)
  2028. value (epg--decode-quotedstring (match-string 0 string)))
  2029. (if (eq index (string-match "#\\([0-9A-Fa-f]+\\)" string index))
  2030. (setq index (match-end 0)
  2031. value (epg--decode-hexstring (match-string 1 string)))
  2032. (if (eq index (string-match "\"\\([^\\\"]\\|\\\\.\\)*\""
  2033. string index))
  2034. (setq index (match-end 0)
  2035. value (epg--decode-quotedstring
  2036. (match-string 0 string))))))
  2037. (if group
  2038. (if (stringp (car (car alist)))
  2039. (setcar alist (list (cons type value) (car alist)))
  2040. (setcar alist (cons (cons type value) (car alist))))
  2041. (if (consp (car (car alist)))
  2042. (setcar alist (nreverse (car alist))))
  2043. (setq alist (cons (cons type value) alist)
  2044. type nil
  2045. value nil))
  2046. (if (eq index (string-match "[ \t\n\r]*\\([,;+]\\)" string index))
  2047. (setq index (match-end 0)
  2048. group (eq (aref string (match-beginning 1)) ?+))))
  2049. (nreverse alist)))
  2050. (defun epg-decode-dn (alist)
  2051. "Convert ALIST returned by `epg-dn-from-string' to a human readable form.
  2052. Type names are resolved using `epg-dn-type-alist'."
  2053. (mapconcat
  2054. (lambda (rdn)
  2055. (if (stringp (car rdn))
  2056. (let ((entry (assoc (car rdn) epg-dn-type-alist)))
  2057. (if entry
  2058. (format "%s=%s" (cdr entry) (cdr rdn))
  2059. (format "%s=%s" (car rdn) (cdr rdn))))
  2060. (concat "(" (epg-decode-dn rdn) ")")))
  2061. alist
  2062. ", "))
  2063. (provide 'epg)
  2064. ;;; epg.el ends here