epg.el 99 KB

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