ac.c 74 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302
  1. /* ac.c - Alternative interface for asymmetric cryptography.
  2. Copyright (C) 2003, 2004, 2005, 2006
  3. 2007, 2008 Free Software Foundation, Inc.
  4. This file is part of Libgcrypt.
  5. Libgcrypt is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU Lesser general Public License as
  7. published by the Free Software Foundation; either version 2.1 of
  8. the License, or (at your option) any later version.
  9. Libgcrypt is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with this program; if not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include <config.h>
  17. #include <errno.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <stdio.h>
  21. #include <stddef.h>
  22. #include "g10lib.h"
  23. #include "cipher.h"
  24. #include "mpi.h"
  25. /* At the moment the ac interface is a wrapper around the pk
  26. interface, but this might change somewhen in the future, depending
  27. on how many people prefer the ac interface. */
  28. /* Mapping of flag numbers to the according strings as it is expected
  29. for S-expressions. */
  30. static struct number_string
  31. {
  32. int number;
  33. const char *string;
  34. } ac_flags[] =
  35. {
  36. { GCRY_AC_FLAG_NO_BLINDING, "no-blinding" },
  37. };
  38. /* The positions in this list correspond to the values contained in
  39. the gcry_ac_key_type_t enumeration list. */
  40. static const char *ac_key_identifiers[] =
  41. {
  42. "private-key",
  43. "public-key"
  44. };
  45. /* These specifications are needed for key-pair generation; the caller
  46. is allowed to pass additional, algorithm-specific `specs' to
  47. gcry_ac_key_pair_generate. This list is used for decoding the
  48. provided values according to the selected algorithm. */
  49. struct gcry_ac_key_generate_spec
  50. {
  51. int algorithm; /* Algorithm for which this flag is
  52. relevant. */
  53. const char *name; /* Name of this flag. */
  54. size_t offset; /* Offset in the cipher-specific spec
  55. structure at which the MPI value
  56. associated with this flag is to be
  57. found. */
  58. } ac_key_generate_specs[] =
  59. {
  60. { GCRY_AC_RSA, "rsa-use-e", offsetof (gcry_ac_key_spec_rsa_t, e) },
  61. { 0 }
  62. };
  63. /* Handle structure. */
  64. struct gcry_ac_handle
  65. {
  66. int algorithm; /* Algorithm ID associated with this
  67. handle. */
  68. const char *algorithm_name; /* Name of the algorithm. */
  69. unsigned int flags; /* Flags, not used yet. */
  70. gcry_module_t module; /* Reference to the algorithm
  71. module. */
  72. };
  73. /* A named MPI value. */
  74. typedef struct gcry_ac_mpi
  75. {
  76. char *name; /* Self-maintained copy of name. */
  77. gcry_mpi_t mpi; /* MPI value. */
  78. unsigned int flags; /* Flags. */
  79. } gcry_ac_mpi_t;
  80. /* A data set, that is simply a list of named MPI values. */
  81. struct gcry_ac_data
  82. {
  83. gcry_ac_mpi_t *data; /* List of named values. */
  84. unsigned int data_n; /* Number of values in DATA. */
  85. };
  86. /* A single key. */
  87. struct gcry_ac_key
  88. {
  89. gcry_ac_data_t data; /* Data in native ac structure. */
  90. gcry_ac_key_type_t type; /* Type of the key. */
  91. };
  92. /* A key pair. */
  93. struct gcry_ac_key_pair
  94. {
  95. gcry_ac_key_t public;
  96. gcry_ac_key_t secret;
  97. };
  98. /*
  99. * Functions for working with data sets.
  100. */
  101. /* Creates a new, empty data set and store it in DATA. */
  102. gcry_error_t
  103. _gcry_ac_data_new (gcry_ac_data_t *data)
  104. {
  105. gcry_ac_data_t data_new;
  106. gcry_error_t err;
  107. if (fips_mode ())
  108. return gpg_error (GPG_ERR_NOT_SUPPORTED);
  109. data_new = gcry_malloc (sizeof (*data_new));
  110. if (! data_new)
  111. {
  112. err = gcry_error_from_errno (errno);
  113. goto out;
  114. }
  115. data_new->data = NULL;
  116. data_new->data_n = 0;
  117. *data = data_new;
  118. err = 0;
  119. out:
  120. return err;
  121. }
  122. /* Destroys all the entries in DATA, but not DATA itself. */
  123. static void
  124. ac_data_values_destroy (gcry_ac_data_t data)
  125. {
  126. unsigned int i;
  127. for (i = 0; i < data->data_n; i++)
  128. if (data->data[i].flags & GCRY_AC_FLAG_DEALLOC)
  129. {
  130. gcry_mpi_release (data->data[i].mpi);
  131. gcry_free (data->data[i].name);
  132. }
  133. }
  134. /* Destroys the data set DATA. */
  135. void
  136. _gcry_ac_data_destroy (gcry_ac_data_t data)
  137. {
  138. if (data)
  139. {
  140. ac_data_values_destroy (data);
  141. gcry_free (data->data);
  142. gcry_free (data);
  143. }
  144. }
  145. /* This function creates a copy of the array of named MPIs DATA_MPIS,
  146. which is of length DATA_MPIS_N; the copy is stored in
  147. DATA_MPIS_CP. */
  148. static gcry_error_t
  149. ac_data_mpi_copy (gcry_ac_mpi_t *data_mpis, unsigned int data_mpis_n,
  150. gcry_ac_mpi_t **data_mpis_cp)
  151. {
  152. gcry_ac_mpi_t *data_mpis_new;
  153. gcry_error_t err;
  154. unsigned int i;
  155. gcry_mpi_t mpi;
  156. char *label;
  157. data_mpis_new = gcry_calloc (data_mpis_n, sizeof (*data_mpis_new));
  158. if (! data_mpis_new)
  159. {
  160. err = gcry_error_from_errno (errno);
  161. goto out;
  162. }
  163. memset (data_mpis_new, 0, sizeof (*data_mpis_new) * data_mpis_n);
  164. err = 0;
  165. for (i = 0; i < data_mpis_n; i++)
  166. {
  167. /* Copy values. */
  168. label = gcry_strdup (data_mpis[i].name);
  169. mpi = gcry_mpi_copy (data_mpis[i].mpi);
  170. if (! (label && mpi))
  171. {
  172. err = gcry_error_from_errno (errno);
  173. gcry_mpi_release (mpi);
  174. gcry_free (label);
  175. break;
  176. }
  177. data_mpis_new[i].flags = GCRY_AC_FLAG_DEALLOC;
  178. data_mpis_new[i].name = label;
  179. data_mpis_new[i].mpi = mpi;
  180. }
  181. if (err)
  182. goto out;
  183. *data_mpis_cp = data_mpis_new;
  184. err = 0;
  185. out:
  186. if (err)
  187. if (data_mpis_new)
  188. {
  189. for (i = 0; i < data_mpis_n; i++)
  190. {
  191. gcry_mpi_release (data_mpis_new[i].mpi);
  192. gcry_free (data_mpis_new[i].name);
  193. }
  194. gcry_free (data_mpis_new);
  195. }
  196. return err;
  197. }
  198. /* Create a copy of the data set DATA and store it in DATA_CP. */
  199. gcry_error_t
  200. _gcry_ac_data_copy (gcry_ac_data_t *data_cp, gcry_ac_data_t data)
  201. {
  202. gcry_ac_mpi_t *data_mpis = NULL;
  203. gcry_ac_data_t data_new;
  204. gcry_error_t err;
  205. if (fips_mode ())
  206. return gpg_error (GPG_ERR_NOT_SUPPORTED);
  207. /* Allocate data set. */
  208. data_new = gcry_malloc (sizeof (*data_new));
  209. if (! data_new)
  210. {
  211. err = gcry_error_from_errno (errno);
  212. goto out;
  213. }
  214. err = ac_data_mpi_copy (data->data, data->data_n, &data_mpis);
  215. if (err)
  216. goto out;
  217. data_new->data_n = data->data_n;
  218. data_new->data = data_mpis;
  219. *data_cp = data_new;
  220. out:
  221. if (err)
  222. gcry_free (data_new);
  223. return err;
  224. }
  225. /* Returns the number of named MPI values inside of the data set
  226. DATA. */
  227. unsigned int
  228. _gcry_ac_data_length (gcry_ac_data_t data)
  229. {
  230. return data->data_n;
  231. }
  232. /* Add the value MPI to DATA with the label NAME. If FLAGS contains
  233. GCRY_AC_FLAG_COPY, the data set will contain copies of NAME
  234. and MPI. If FLAGS contains GCRY_AC_FLAG_DEALLOC or
  235. GCRY_AC_FLAG_COPY, the values contained in the data set will
  236. be deallocated when they are to be removed from the data set. */
  237. gcry_error_t
  238. _gcry_ac_data_set (gcry_ac_data_t data, unsigned int flags,
  239. const char *name, gcry_mpi_t mpi)
  240. {
  241. gcry_error_t err;
  242. gcry_mpi_t mpi_cp;
  243. char *name_cp;
  244. unsigned int i;
  245. name_cp = NULL;
  246. mpi_cp = NULL;
  247. if (fips_mode ())
  248. return gpg_error (GPG_ERR_NOT_SUPPORTED);
  249. if (flags & ~(GCRY_AC_FLAG_DEALLOC | GCRY_AC_FLAG_COPY))
  250. {
  251. err = gcry_error (GPG_ERR_INV_ARG);
  252. goto out;
  253. }
  254. if (flags & GCRY_AC_FLAG_COPY)
  255. {
  256. /* Create copies. */
  257. flags |= GCRY_AC_FLAG_DEALLOC;
  258. name_cp = gcry_strdup (name);
  259. mpi_cp = gcry_mpi_copy (mpi);
  260. if (! (name_cp && mpi_cp))
  261. {
  262. err = gcry_error_from_errno (errno);
  263. goto out;
  264. }
  265. }
  266. /* Search for existing entry. */
  267. for (i = 0; i < data->data_n; i++)
  268. if (! strcmp (name, data->data[i].name))
  269. break;
  270. if (i < data->data_n)
  271. {
  272. /* An entry for NAME does already exist. */
  273. if (data->data[i].flags & GCRY_AC_FLAG_DEALLOC)
  274. {
  275. gcry_mpi_release (data->data[i].mpi);
  276. gcry_free (data->data[i].name);
  277. }
  278. }
  279. else
  280. {
  281. /* Create a new entry. */
  282. gcry_ac_mpi_t *ac_mpis;
  283. ac_mpis = gcry_realloc (data->data,
  284. sizeof (*data->data) * (data->data_n + 1));
  285. if (! ac_mpis)
  286. {
  287. err = gcry_error_from_errno (errno);
  288. goto out;
  289. }
  290. if (data->data != ac_mpis)
  291. data->data = ac_mpis;
  292. data->data_n++;
  293. }
  294. data->data[i].name = name_cp ? name_cp : ((char *) name);
  295. data->data[i].mpi = mpi_cp ? mpi_cp : mpi;
  296. data->data[i].flags = flags;
  297. err = 0;
  298. out:
  299. if (err)
  300. {
  301. gcry_mpi_release (mpi_cp);
  302. gcry_free (name_cp);
  303. }
  304. return err;
  305. }
  306. /* Stores the value labelled with NAME found in the data set DATA in
  307. MPI. The returned MPI value will be released in case
  308. gcry_ac_data_set is used to associate the label NAME with a
  309. different MPI value. */
  310. gcry_error_t
  311. _gcry_ac_data_get_name (gcry_ac_data_t data, unsigned int flags,
  312. const char *name, gcry_mpi_t *mpi)
  313. {
  314. gcry_mpi_t mpi_return;
  315. gcry_error_t err;
  316. unsigned int i;
  317. if (fips_mode ())
  318. return gpg_error (GPG_ERR_NOT_SUPPORTED);
  319. if (flags & ~(GCRY_AC_FLAG_COPY))
  320. {
  321. err = gcry_error (GPG_ERR_INV_ARG);
  322. goto out;
  323. }
  324. for (i = 0; i < data->data_n; i++)
  325. if (! strcmp (name, data->data[i].name))
  326. break;
  327. if (i == data->data_n)
  328. {
  329. err = gcry_error (GPG_ERR_NOT_FOUND);
  330. goto out;
  331. }
  332. if (flags & GCRY_AC_FLAG_COPY)
  333. {
  334. mpi_return = gcry_mpi_copy (data->data[i].mpi);
  335. if (! mpi_return)
  336. {
  337. err = gcry_error_from_errno (errno); /* FIXME? */
  338. goto out;
  339. }
  340. }
  341. else
  342. mpi_return = data->data[i].mpi;
  343. *mpi = mpi_return;
  344. err = 0;
  345. out:
  346. return err;
  347. }
  348. /* Stores in NAME and MPI the named MPI value contained in the data
  349. set DATA with the index IDX. NAME or MPI may be NULL. The
  350. returned MPI value will be released in case gcry_ac_data_set is
  351. used to associate the label NAME with a different MPI value. */
  352. gcry_error_t
  353. _gcry_ac_data_get_index (gcry_ac_data_t data, unsigned int flags,
  354. unsigned int idx,
  355. const char **name, gcry_mpi_t *mpi)
  356. {
  357. gcry_error_t err;
  358. gcry_mpi_t mpi_cp;
  359. char *name_cp;
  360. name_cp = NULL;
  361. mpi_cp = NULL;
  362. if (fips_mode ())
  363. return gpg_error (GPG_ERR_NOT_SUPPORTED);
  364. if (flags & ~(GCRY_AC_FLAG_COPY))
  365. {
  366. err = gcry_error (GPG_ERR_INV_ARG);
  367. goto out;
  368. }
  369. if (idx >= data->data_n)
  370. {
  371. err = gcry_error (GPG_ERR_INV_ARG);
  372. goto out;
  373. }
  374. if (flags & GCRY_AC_FLAG_COPY)
  375. {
  376. /* Return copies to the user. */
  377. if (name)
  378. {
  379. name_cp = gcry_strdup (data->data[idx].name);
  380. if (! name_cp)
  381. {
  382. err = gcry_error_from_errno (errno);
  383. goto out;
  384. }
  385. }
  386. if (mpi)
  387. {
  388. mpi_cp = gcry_mpi_copy (data->data[idx].mpi);
  389. if (! mpi_cp)
  390. {
  391. err = gcry_error_from_errno (errno);
  392. goto out;
  393. }
  394. }
  395. }
  396. if (name)
  397. *name = name_cp ? name_cp : data->data[idx].name;
  398. if (mpi)
  399. *mpi = mpi_cp ? mpi_cp : data->data[idx].mpi;
  400. err = 0;
  401. out:
  402. if (err)
  403. {
  404. gcry_mpi_release (mpi_cp);
  405. gcry_free (name_cp);
  406. }
  407. return err;
  408. }
  409. /* Convert the data set DATA into a new S-Expression, which is to be
  410. stored in SEXP, according to the identifiers contained in
  411. IDENTIFIERS. */
  412. gcry_error_t
  413. _gcry_ac_data_to_sexp (gcry_ac_data_t data, gcry_sexp_t *sexp,
  414. const char **identifiers)
  415. {
  416. gcry_sexp_t sexp_new;
  417. gcry_error_t err;
  418. char *sexp_buffer;
  419. size_t sexp_buffer_n;
  420. size_t identifiers_n;
  421. const char *label;
  422. gcry_mpi_t mpi;
  423. void **arg_list;
  424. size_t data_n;
  425. unsigned int i;
  426. sexp_buffer_n = 1;
  427. sexp_buffer = NULL;
  428. arg_list = NULL;
  429. err = 0;
  430. if (fips_mode ())
  431. return gpg_error (GPG_ERR_NOT_SUPPORTED);
  432. /* Calculate size of S-expression representation. */
  433. i = 0;
  434. if (identifiers)
  435. while (identifiers[i])
  436. {
  437. /* For each identifier, we add "(<IDENTIFIER>)". */
  438. sexp_buffer_n += 1 + strlen (identifiers[i]) + 1;
  439. i++;
  440. }
  441. identifiers_n = i;
  442. if (! identifiers_n)
  443. /* If there are NO identifiers, we still add surrounding braces so
  444. that we have a list of named MPI value lists. Otherwise it
  445. wouldn't be too much fun to process these lists. */
  446. sexp_buffer_n += 2;
  447. data_n = _gcry_ac_data_length (data);
  448. for (i = 0; i < data_n; i++)
  449. {
  450. err = gcry_ac_data_get_index (data, 0, i, &label, NULL);
  451. if (err)
  452. break;
  453. /* For each MPI we add "(<LABEL> %m)". */
  454. sexp_buffer_n += 1 + strlen (label) + 4;
  455. }
  456. if (err)
  457. goto out;
  458. /* Allocate buffer. */
  459. sexp_buffer = gcry_malloc (sexp_buffer_n);
  460. if (! sexp_buffer)
  461. {
  462. err = gcry_error_from_errno (errno);
  463. goto out;
  464. }
  465. /* Fill buffer. */
  466. *sexp_buffer = 0;
  467. sexp_buffer_n = 0;
  468. /* Add identifiers: (<IDENTIFIER0>(<IDENTIFIER1>...)). */
  469. if (identifiers_n)
  470. {
  471. /* Add nested identifier lists as usual. */
  472. for (i = 0; i < identifiers_n; i++)
  473. sexp_buffer_n += sprintf (sexp_buffer + sexp_buffer_n, "(%s",
  474. identifiers[i]);
  475. }
  476. else
  477. {
  478. /* Add special list. */
  479. sexp_buffer_n += sprintf (sexp_buffer + sexp_buffer_n, "(");
  480. }
  481. /* Add MPI list. */
  482. arg_list = gcry_calloc (data_n + 1, sizeof (*arg_list));
  483. if (! arg_list)
  484. {
  485. err = gcry_error_from_errno (errno);
  486. goto out;
  487. }
  488. for (i = 0; i < data_n; i++)
  489. {
  490. err = gcry_ac_data_get_index (data, 0, i, &label, &mpi);
  491. if (err)
  492. break;
  493. sexp_buffer_n += sprintf (sexp_buffer + sexp_buffer_n,
  494. "(%s %%m)", label);
  495. arg_list[i] = &data->data[i].mpi;
  496. }
  497. if (err)
  498. goto out;
  499. if (identifiers_n)
  500. {
  501. /* Add closing braces for identifier lists as usual. */
  502. for (i = 0; i < identifiers_n; i++)
  503. sexp_buffer_n += sprintf (sexp_buffer + sexp_buffer_n, ")");
  504. }
  505. else
  506. {
  507. /* Add closing braces for special list. */
  508. sexp_buffer_n += sprintf (sexp_buffer + sexp_buffer_n, ")");
  509. }
  510. /* Construct. */
  511. err = gcry_sexp_build_array (&sexp_new, NULL, sexp_buffer, arg_list);
  512. if (err)
  513. goto out;
  514. *sexp = sexp_new;
  515. out:
  516. gcry_free (sexp_buffer);
  517. gcry_free (arg_list);
  518. return err;
  519. }
  520. /* Create a new data set, which is to be stored in DATA_SET, from the
  521. S-Expression SEXP, according to the identifiers contained in
  522. IDENTIFIERS. */
  523. gcry_error_t
  524. _gcry_ac_data_from_sexp (gcry_ac_data_t *data_set, gcry_sexp_t sexp,
  525. const char **identifiers)
  526. {
  527. gcry_ac_data_t data_set_new;
  528. gcry_error_t err;
  529. gcry_sexp_t sexp_cur;
  530. gcry_sexp_t sexp_tmp;
  531. gcry_mpi_t mpi;
  532. char *string;
  533. const char *data;
  534. size_t data_n;
  535. size_t sexp_n;
  536. unsigned int i;
  537. int skip_name;
  538. data_set_new = NULL;
  539. sexp_cur = sexp;
  540. sexp_tmp = NULL;
  541. string = NULL;
  542. mpi = NULL;
  543. err = 0;
  544. if (fips_mode ())
  545. return gpg_error (GPG_ERR_NOT_SUPPORTED);
  546. /* Process S-expression/identifiers. */
  547. if (identifiers)
  548. {
  549. for (i = 0; identifiers[i]; i++)
  550. {
  551. /* Next identifier. Extract first data item from
  552. SEXP_CUR. */
  553. data = gcry_sexp_nth_data (sexp_cur, 0, &data_n);
  554. if (! ((data_n == strlen (identifiers[i]))
  555. && (! strncmp (data, identifiers[i], data_n))))
  556. {
  557. /* Identifier mismatch -> error. */
  558. err = gcry_error (GPG_ERR_INV_SEXP);
  559. break;
  560. }
  561. /* Identifier matches. Now we have to distinguish two
  562. cases:
  563. (i) we are at the last identifier:
  564. leave loop
  565. (ii) we are not at the last identifier:
  566. extract next element, which is supposed to be a
  567. sublist. */
  568. if (! identifiers[i + 1])
  569. /* Last identifier. */
  570. break;
  571. else
  572. {
  573. /* Not the last identifier, extract next sublist. */
  574. sexp_tmp = gcry_sexp_nth (sexp_cur, 1);
  575. if (! sexp_tmp)
  576. {
  577. /* Missing sublist. */
  578. err = gcry_error (GPG_ERR_INV_SEXP);
  579. break;
  580. }
  581. /* Release old SEXP_CUR, in case it is not equal to the
  582. original SEXP. */
  583. if (sexp_cur != sexp)
  584. gcry_sexp_release (sexp_cur);
  585. /* Make SEXP_CUR point to the new current sublist. */
  586. sexp_cur = sexp_tmp;
  587. sexp_tmp = NULL;
  588. }
  589. }
  590. if (err)
  591. goto out;
  592. if (i)
  593. {
  594. /* We have at least one identifier in the list, this means
  595. the the list of named MPI values is prefixed, this means
  596. that we need to skip the first item (the list name), when
  597. processing the MPI values. */
  598. skip_name = 1;
  599. }
  600. else
  601. {
  602. /* Since there is no identifiers list, the list of named MPI
  603. values is not prefixed with a list name, therefore the
  604. offset to use is zero. */
  605. skip_name = 0;
  606. }
  607. }
  608. else
  609. /* Since there is no identifiers list, the list of named MPI
  610. values is not prefixed with a list name, therefore the offset
  611. to use is zero. */
  612. skip_name = 0;
  613. /* Create data set from S-expression data. */
  614. err = gcry_ac_data_new (&data_set_new);
  615. if (err)
  616. goto out;
  617. /* Figure out amount of named MPIs in SEXP_CUR. */
  618. if (sexp_cur)
  619. sexp_n = gcry_sexp_length (sexp_cur) - skip_name;
  620. else
  621. sexp_n = 0;
  622. /* Extracte the named MPIs sequentially. */
  623. for (i = 0; i < sexp_n; i++)
  624. {
  625. /* Store next S-Expression pair, which is supposed to consist of
  626. a name and an MPI value, in SEXP_TMP. */
  627. sexp_tmp = gcry_sexp_nth (sexp_cur, i + skip_name);
  628. if (! sexp_tmp)
  629. {
  630. err = gcry_error (GPG_ERR_INV_SEXP);
  631. break;
  632. }
  633. /* Extract name from current S-Expression pair. */
  634. data = gcry_sexp_nth_data (sexp_tmp, 0, &data_n);
  635. string = gcry_malloc (data_n + 1);
  636. if (! string)
  637. {
  638. err = gcry_error_from_errno (errno);
  639. break;
  640. }
  641. memcpy (string, data, data_n);
  642. string[data_n] = 0;
  643. /* Extract MPI value. */
  644. mpi = gcry_sexp_nth_mpi (sexp_tmp, 1, 0);
  645. if (! mpi)
  646. {
  647. err = gcry_error (GPG_ERR_INV_SEXP); /* FIXME? */
  648. break;
  649. }
  650. /* Store named MPI in data_set_new. */
  651. err = gcry_ac_data_set (data_set_new, GCRY_AC_FLAG_DEALLOC, string, mpi);
  652. if (err)
  653. break;
  654. /* gcry_free (string); */
  655. string = NULL;
  656. /* gcry_mpi_release (mpi); */
  657. mpi = NULL;
  658. gcry_sexp_release (sexp_tmp);
  659. sexp_tmp = NULL;
  660. }
  661. if (err)
  662. goto out;
  663. *data_set = data_set_new;
  664. out:
  665. if (sexp_cur != sexp)
  666. gcry_sexp_release (sexp_cur);
  667. gcry_sexp_release (sexp_tmp);
  668. gcry_mpi_release (mpi);
  669. gcry_free (string);
  670. if (err)
  671. gcry_ac_data_destroy (data_set_new);
  672. return err;
  673. }
  674. static void
  675. _gcry_ac_data_dump (const char *prefix, gcry_ac_data_t data)
  676. {
  677. unsigned char *mpi_buffer;
  678. size_t mpi_buffer_n;
  679. unsigned int data_n;
  680. gcry_error_t err;
  681. const char *name;
  682. gcry_mpi_t mpi;
  683. unsigned int i;
  684. if (! data)
  685. return;
  686. if (fips_mode ())
  687. return;
  688. mpi_buffer = NULL;
  689. data_n = _gcry_ac_data_length (data);
  690. for (i = 0; i < data_n; i++)
  691. {
  692. err = gcry_ac_data_get_index (data, 0, i, &name, &mpi);
  693. if (err)
  694. {
  695. log_error ("failed to dump data set");
  696. break;
  697. }
  698. err = gcry_mpi_aprint (GCRYMPI_FMT_HEX, &mpi_buffer, &mpi_buffer_n, mpi);
  699. if (err)
  700. {
  701. log_error ("failed to dump data set");
  702. break;
  703. }
  704. log_printf ("%s%s%s: %s\n",
  705. prefix ? prefix : "",
  706. prefix ? ": " : ""
  707. , name, mpi_buffer);
  708. gcry_free (mpi_buffer);
  709. mpi_buffer = NULL;
  710. }
  711. gcry_free (mpi_buffer);
  712. }
  713. /* Dump the named MPI values contained in the data set DATA to
  714. Libgcrypt's logging stream. */
  715. void
  716. gcry_ac_data_dump (const char *prefix, gcry_ac_data_t data)
  717. {
  718. _gcry_ac_data_dump (prefix, data);
  719. }
  720. /* Destroys any values contained in the data set DATA. */
  721. void
  722. _gcry_ac_data_clear (gcry_ac_data_t data)
  723. {
  724. ac_data_values_destroy (data);
  725. gcry_free (data->data);
  726. data->data = NULL;
  727. data->data_n = 0;
  728. }
  729. /*
  730. * Implementation of `ac io' objects.
  731. */
  732. /* Initialize AC_IO according to MODE, TYPE and the variable list of
  733. arguments AP. The list of variable arguments to specify depends on
  734. the given TYPE. */
  735. void
  736. _gcry_ac_io_init_va (gcry_ac_io_t *ac_io,
  737. gcry_ac_io_mode_t mode, gcry_ac_io_type_t type, va_list ap)
  738. {
  739. memset (ac_io, 0, sizeof (*ac_io));
  740. if (fips_mode ())
  741. return;
  742. gcry_assert ((mode == GCRY_AC_IO_READABLE) || (mode == GCRY_AC_IO_WRITABLE));
  743. gcry_assert ((type == GCRY_AC_IO_STRING) || (type == GCRY_AC_IO_STRING));
  744. ac_io->mode = mode;
  745. ac_io->type = type;
  746. switch (mode)
  747. {
  748. case GCRY_AC_IO_READABLE:
  749. switch (type)
  750. {
  751. case GCRY_AC_IO_STRING:
  752. ac_io->io.readable.string.data = va_arg (ap, unsigned char *);
  753. ac_io->io.readable.string.data_n = va_arg (ap, size_t);
  754. break;
  755. case GCRY_AC_IO_CALLBACK:
  756. ac_io->io.readable.callback.cb = va_arg (ap, gcry_ac_data_read_cb_t);
  757. ac_io->io.readable.callback.opaque = va_arg (ap, void *);
  758. break;
  759. }
  760. break;
  761. case GCRY_AC_IO_WRITABLE:
  762. switch (type)
  763. {
  764. case GCRY_AC_IO_STRING:
  765. ac_io->io.writable.string.data = va_arg (ap, unsigned char **);
  766. ac_io->io.writable.string.data_n = va_arg (ap, size_t *);
  767. break;
  768. case GCRY_AC_IO_CALLBACK:
  769. ac_io->io.writable.callback.cb = va_arg (ap, gcry_ac_data_write_cb_t);
  770. ac_io->io.writable.callback.opaque = va_arg (ap, void *);
  771. break;
  772. }
  773. break;
  774. }
  775. }
  776. /* Initialize AC_IO according to MODE, TYPE and the variable list of
  777. arguments. The list of variable arguments to specify depends on
  778. the given TYPE. */
  779. void
  780. _gcry_ac_io_init (gcry_ac_io_t *ac_io,
  781. gcry_ac_io_mode_t mode, gcry_ac_io_type_t type, ...)
  782. {
  783. va_list ap;
  784. va_start (ap, type);
  785. _gcry_ac_io_init_va (ac_io, mode, type, ap);
  786. va_end (ap);
  787. }
  788. /* Write to the IO object AC_IO BUFFER_N bytes from BUFFER. Return
  789. zero on success or error code. */
  790. static gcry_error_t
  791. _gcry_ac_io_write (gcry_ac_io_t *ac_io, unsigned char *buffer, size_t buffer_n)
  792. {
  793. gcry_error_t err;
  794. gcry_assert (ac_io->mode == GCRY_AC_IO_WRITABLE);
  795. err = 0;
  796. switch (ac_io->type)
  797. {
  798. case GCRY_AC_IO_STRING:
  799. {
  800. unsigned char *p;
  801. if (*ac_io->io.writable.string.data)
  802. {
  803. p = gcry_realloc (*ac_io->io.writable.string.data,
  804. *ac_io->io.writable.string.data_n + buffer_n);
  805. if (! p)
  806. err = gcry_error_from_errno (errno);
  807. else
  808. {
  809. if (*ac_io->io.writable.string.data != p)
  810. *ac_io->io.writable.string.data = p;
  811. memcpy (p + *ac_io->io.writable.string.data_n, buffer, buffer_n);
  812. *ac_io->io.writable.string.data_n += buffer_n;
  813. }
  814. }
  815. else
  816. {
  817. if (gcry_is_secure (buffer))
  818. p = gcry_malloc_secure (buffer_n);
  819. else
  820. p = gcry_malloc (buffer_n);
  821. if (! p)
  822. err = gcry_error_from_errno (errno);
  823. else
  824. {
  825. memcpy (p, buffer, buffer_n);
  826. *ac_io->io.writable.string.data = p;
  827. *ac_io->io.writable.string.data_n = buffer_n;
  828. }
  829. }
  830. }
  831. break;
  832. case GCRY_AC_IO_CALLBACK:
  833. err = (*ac_io->io.writable.callback.cb) (ac_io->io.writable.callback.opaque,
  834. buffer, buffer_n);
  835. break;
  836. }
  837. return err;
  838. }
  839. /* Read *BUFFER_N bytes from the IO object AC_IO into BUFFER; NREAD
  840. bytes have already been read from the object; on success, store the
  841. amount of bytes read in *BUFFER_N; zero bytes read means EOF.
  842. Return zero on success or error code. */
  843. static gcry_error_t
  844. _gcry_ac_io_read (gcry_ac_io_t *ac_io,
  845. unsigned int nread, unsigned char *buffer, size_t *buffer_n)
  846. {
  847. gcry_error_t err;
  848. gcry_assert (ac_io->mode == GCRY_AC_IO_READABLE);
  849. err = 0;
  850. switch (ac_io->type)
  851. {
  852. case GCRY_AC_IO_STRING:
  853. {
  854. size_t bytes_available;
  855. size_t bytes_to_read;
  856. size_t bytes_wanted;
  857. bytes_available = ac_io->io.readable.string.data_n - nread;
  858. bytes_wanted = *buffer_n;
  859. if (bytes_wanted > bytes_available)
  860. bytes_to_read = bytes_available;
  861. else
  862. bytes_to_read = bytes_wanted;
  863. memcpy (buffer, ac_io->io.readable.string.data + nread, bytes_to_read);
  864. *buffer_n = bytes_to_read;
  865. err = 0;
  866. break;
  867. }
  868. case GCRY_AC_IO_CALLBACK:
  869. err = (*ac_io->io.readable.callback.cb)
  870. (ac_io->io.readable.callback.opaque, buffer, buffer_n);
  871. break;
  872. }
  873. return err;
  874. }
  875. /* Read all data available from the IO object AC_IO into newly
  876. allocated memory, storing an appropriate pointer in *BUFFER and the
  877. amount of bytes read in *BUFFER_N. Return zero on success or error
  878. code. */
  879. static gcry_error_t
  880. _gcry_ac_io_read_all (gcry_ac_io_t *ac_io, unsigned char **buffer, size_t *buffer_n)
  881. {
  882. unsigned char *buffer_new;
  883. size_t buffer_new_n;
  884. unsigned char buf[BUFSIZ];
  885. size_t buf_n;
  886. unsigned char *p;
  887. gcry_error_t err;
  888. buffer_new = NULL;
  889. buffer_new_n = 0;
  890. while (1)
  891. {
  892. buf_n = sizeof (buf);
  893. err = _gcry_ac_io_read (ac_io, buffer_new_n, buf, &buf_n);
  894. if (err)
  895. break;
  896. if (buf_n)
  897. {
  898. p = gcry_realloc (buffer_new, buffer_new_n + buf_n);
  899. if (! p)
  900. {
  901. err = gcry_error_from_errno (errno);
  902. break;
  903. }
  904. if (buffer_new != p)
  905. buffer_new = p;
  906. memcpy (buffer_new + buffer_new_n, buf, buf_n);
  907. buffer_new_n += buf_n;
  908. }
  909. else
  910. break;
  911. }
  912. if (err)
  913. goto out;
  914. *buffer_n = buffer_new_n;
  915. *buffer = buffer_new;
  916. out:
  917. if (err)
  918. gcry_free (buffer_new);
  919. return err;
  920. }
  921. /* Read data chunks from the IO object AC_IO until EOF, feeding them
  922. to the callback function CB. Return zero on success or error
  923. code. */
  924. static gcry_error_t
  925. _gcry_ac_io_process (gcry_ac_io_t *ac_io,
  926. gcry_ac_data_write_cb_t cb, void *opaque)
  927. {
  928. unsigned char buffer[BUFSIZ];
  929. unsigned int nread;
  930. size_t buffer_n;
  931. gcry_error_t err;
  932. nread = 0;
  933. while (1)
  934. {
  935. buffer_n = sizeof (buffer);
  936. err = _gcry_ac_io_read (ac_io, nread, buffer, &buffer_n);
  937. if (err)
  938. break;
  939. if (buffer_n)
  940. {
  941. err = (*cb) (opaque, buffer, buffer_n);
  942. if (err)
  943. break;
  944. nread += buffer_n;
  945. }
  946. else
  947. break;
  948. }
  949. return err;
  950. }
  951. /*
  952. * Functions for converting data between the native ac and the
  953. * S-expression structure used by the pk interface.
  954. */
  955. /* Extract the S-Expression DATA_SEXP into DATA under the control of
  956. TYPE and NAME. This function assumes that S-Expressions are of the
  957. following structure:
  958. (IDENTIFIER [...]
  959. (ALGORITHM <list of named MPI values>)) */
  960. static gcry_error_t
  961. ac_data_extract (const char *identifier, const char *algorithm,
  962. gcry_sexp_t sexp, gcry_ac_data_t *data)
  963. {
  964. gcry_error_t err;
  965. gcry_sexp_t value_sexp;
  966. gcry_sexp_t data_sexp;
  967. size_t data_sexp_n;
  968. gcry_mpi_t value_mpi;
  969. char *value_name;
  970. const char *data_raw;
  971. size_t data_raw_n;
  972. gcry_ac_data_t data_new;
  973. unsigned int i;
  974. value_sexp = NULL;
  975. data_sexp = NULL;
  976. value_name = NULL;
  977. value_mpi = NULL;
  978. data_new = NULL;
  979. /* Verify that the S-expression contains the correct identifier. */
  980. data_raw = gcry_sexp_nth_data (sexp, 0, &data_raw_n);
  981. if ((! data_raw) || strncmp (identifier, data_raw, data_raw_n))
  982. {
  983. err = gcry_error (GPG_ERR_INV_SEXP);
  984. goto out;
  985. }
  986. /* Extract inner S-expression. */
  987. data_sexp = gcry_sexp_find_token (sexp, algorithm, 0);
  988. if (! data_sexp)
  989. {
  990. err = gcry_error (GPG_ERR_INV_SEXP);
  991. goto out;
  992. }
  993. /* Count data elements. */
  994. data_sexp_n = gcry_sexp_length (data_sexp);
  995. data_sexp_n--;
  996. /* Allocate new data set. */
  997. err = _gcry_ac_data_new (&data_new);
  998. if (err)
  999. goto out;
  1000. /* Iterate through list of data elements and add them to the data
  1001. set. */
  1002. for (i = 0; i < data_sexp_n; i++)
  1003. {
  1004. /* Get the S-expression of the named MPI, that contains the name
  1005. and the MPI value. */
  1006. value_sexp = gcry_sexp_nth (data_sexp, i + 1);
  1007. if (! value_sexp)
  1008. {
  1009. err = gcry_error (GPG_ERR_INV_SEXP);
  1010. break;
  1011. }
  1012. /* Extract the name. */
  1013. data_raw = gcry_sexp_nth_data (value_sexp, 0, &data_raw_n);
  1014. if (! data_raw)
  1015. {
  1016. err = gcry_error (GPG_ERR_INV_SEXP);
  1017. break;
  1018. }
  1019. /* Extract the MPI value. */
  1020. value_mpi = gcry_sexp_nth_mpi (value_sexp, 1, GCRYMPI_FMT_USG);
  1021. if (! value_mpi)
  1022. {
  1023. err = gcry_error (GPG_ERR_INTERNAL); /* FIXME? */
  1024. break;
  1025. }
  1026. /* Duplicate the name. */
  1027. value_name = gcry_malloc (data_raw_n + 1);
  1028. if (! value_name)
  1029. {
  1030. err = gcry_error_from_errno (errno);
  1031. break;
  1032. }
  1033. strncpy (value_name, data_raw, data_raw_n);
  1034. value_name[data_raw_n] = 0;
  1035. err = _gcry_ac_data_set (data_new, GCRY_AC_FLAG_DEALLOC, value_name, value_mpi);
  1036. if (err)
  1037. break;
  1038. gcry_sexp_release (value_sexp);
  1039. value_sexp = NULL;
  1040. value_name = NULL;
  1041. value_mpi = NULL;
  1042. }
  1043. if (err)
  1044. goto out;
  1045. /* Copy out. */
  1046. *data = data_new;
  1047. out:
  1048. /* Deallocate resources. */
  1049. if (err)
  1050. {
  1051. _gcry_ac_data_destroy (data_new);
  1052. gcry_mpi_release (value_mpi);
  1053. gcry_free (value_name);
  1054. gcry_sexp_release (value_sexp);
  1055. }
  1056. gcry_sexp_release (data_sexp);
  1057. return err;
  1058. }
  1059. /* Construct an S-expression from the DATA and store it in
  1060. DATA_SEXP. The S-expression will be of the following structure:
  1061. (IDENTIFIER [(flags [...])]
  1062. (ALGORITHM <list of named MPI values>)) */
  1063. static gcry_error_t
  1064. ac_data_construct (const char *identifier, int include_flags,
  1065. unsigned int flags, const char *algorithm,
  1066. gcry_ac_data_t data, gcry_sexp_t *sexp)
  1067. {
  1068. unsigned int data_length;
  1069. gcry_sexp_t sexp_new;
  1070. gcry_error_t err;
  1071. size_t sexp_format_n;
  1072. char *sexp_format;
  1073. void **arg_list;
  1074. unsigned int i;
  1075. arg_list = NULL;
  1076. sexp_new = NULL;
  1077. sexp_format = NULL;
  1078. /* We build a list of arguments to pass to
  1079. gcry_sexp_build_array(). */
  1080. data_length = _gcry_ac_data_length (data);
  1081. arg_list = gcry_calloc (data_length, sizeof (*arg_list) * 2);
  1082. if (! arg_list)
  1083. {
  1084. err = gcry_error_from_errno (errno);
  1085. goto out;
  1086. }
  1087. /* Fill list with MPIs. */
  1088. for (i = 0; i < data_length; i++)
  1089. {
  1090. char **nameaddr = &data->data[i].name;
  1091. arg_list[(i * 2) + 0] = nameaddr;
  1092. arg_list[(i * 2) + 1] = &data->data[i].mpi;
  1093. }
  1094. /* Calculate size of format string. */
  1095. sexp_format_n = (3
  1096. + (include_flags ? 7 : 0)
  1097. + (algorithm ? (2 + strlen (algorithm)) : 0)
  1098. + strlen (identifier));
  1099. for (i = 0; i < data_length; i++)
  1100. /* Per-element sizes. */
  1101. sexp_format_n += 6;
  1102. if (include_flags)
  1103. /* Add flags. */
  1104. for (i = 0; i < DIM (ac_flags); i++)
  1105. if (flags & ac_flags[i].number)
  1106. sexp_format_n += strlen (ac_flags[i].string) + 1;
  1107. /* Done. */
  1108. sexp_format = gcry_malloc (sexp_format_n);
  1109. if (! sexp_format)
  1110. {
  1111. err = gcry_error_from_errno (errno);
  1112. goto out;
  1113. }
  1114. /* Construct the format string. */
  1115. *sexp_format = 0;
  1116. strcat (sexp_format, "(");
  1117. strcat (sexp_format, identifier);
  1118. if (include_flags)
  1119. {
  1120. strcat (sexp_format, "(flags");
  1121. for (i = 0; i < DIM (ac_flags); i++)
  1122. if (flags & ac_flags[i].number)
  1123. {
  1124. strcat (sexp_format, " ");
  1125. strcat (sexp_format, ac_flags[i].string);
  1126. }
  1127. strcat (sexp_format, ")");
  1128. }
  1129. if (algorithm)
  1130. {
  1131. strcat (sexp_format, "(");
  1132. strcat (sexp_format, algorithm);
  1133. }
  1134. for (i = 0; i < data_length; i++)
  1135. strcat (sexp_format, "(%s%m)");
  1136. if (algorithm)
  1137. strcat (sexp_format, ")");
  1138. strcat (sexp_format, ")");
  1139. /* Create final S-expression. */
  1140. err = gcry_sexp_build_array (&sexp_new, NULL, sexp_format, arg_list);
  1141. if (err)
  1142. goto out;
  1143. *sexp = sexp_new;
  1144. out:
  1145. /* Deallocate resources. */
  1146. gcry_free (sexp_format);
  1147. gcry_free (arg_list);
  1148. if (err)
  1149. gcry_sexp_release (sexp_new);
  1150. return err;
  1151. }
  1152. /*
  1153. * Handle management.
  1154. */
  1155. /* Creates a new handle for the algorithm ALGORITHM and stores it in
  1156. HANDLE. FLAGS is not used yet. */
  1157. gcry_error_t
  1158. _gcry_ac_open (gcry_ac_handle_t *handle,
  1159. gcry_ac_id_t algorithm, unsigned int flags)
  1160. {
  1161. gcry_ac_handle_t handle_new;
  1162. const char *algorithm_name;
  1163. gcry_module_t module;
  1164. gcry_error_t err;
  1165. *handle = NULL;
  1166. module = NULL;
  1167. if (fips_mode ())
  1168. return gpg_error (GPG_ERR_NOT_SUPPORTED);
  1169. /* Get name. */
  1170. algorithm_name = _gcry_pk_aliased_algo_name (algorithm);
  1171. if (! algorithm_name)
  1172. {
  1173. err = gcry_error (GPG_ERR_PUBKEY_ALGO);
  1174. goto out;
  1175. }
  1176. /* Acquire reference to the pubkey module. */
  1177. err = _gcry_pk_module_lookup (algorithm, &module);
  1178. if (err)
  1179. goto out;
  1180. /* Allocate. */
  1181. handle_new = gcry_malloc (sizeof (*handle_new));
  1182. if (! handle_new)
  1183. {
  1184. err = gcry_error_from_errno (errno);
  1185. goto out;
  1186. }
  1187. /* Done. */
  1188. handle_new->algorithm = algorithm;
  1189. handle_new->algorithm_name = algorithm_name;
  1190. handle_new->flags = flags;
  1191. handle_new->module = module;
  1192. *handle = handle_new;
  1193. out:
  1194. /* Deallocate resources. */
  1195. if (err)
  1196. _gcry_pk_module_release (module);
  1197. return err;
  1198. }
  1199. /* Destroys the handle HANDLE. */
  1200. void
  1201. _gcry_ac_close (gcry_ac_handle_t handle)
  1202. {
  1203. /* Release reference to pubkey module. */
  1204. if (handle)
  1205. {
  1206. _gcry_pk_module_release (handle->module);
  1207. gcry_free (handle);
  1208. }
  1209. }
  1210. /*
  1211. * Key management.
  1212. */
  1213. /* Initialize a key from a given data set. */
  1214. /* FIXME/Damn: the argument HANDLE is not only unnecessary, it is
  1215. completely WRONG here. */
  1216. gcry_error_t
  1217. _gcry_ac_key_init (gcry_ac_key_t *key, gcry_ac_handle_t handle,
  1218. gcry_ac_key_type_t type, gcry_ac_data_t data)
  1219. {
  1220. gcry_ac_data_t data_new;
  1221. gcry_ac_key_t key_new;
  1222. gcry_error_t err;
  1223. (void)handle;
  1224. if (fips_mode ())
  1225. return gpg_error (GPG_ERR_NOT_SUPPORTED);
  1226. /* Allocate. */
  1227. key_new = gcry_malloc (sizeof (*key_new));
  1228. if (! key_new)
  1229. {
  1230. err = gcry_error_from_errno (errno);
  1231. goto out;
  1232. }
  1233. /* Copy data set. */
  1234. err = _gcry_ac_data_copy (&data_new, data);
  1235. if (err)
  1236. goto out;
  1237. /* Done. */
  1238. key_new->data = data_new;
  1239. key_new->type = type;
  1240. *key = key_new;
  1241. out:
  1242. if (err)
  1243. /* Deallocate resources. */
  1244. gcry_free (key_new);
  1245. return err;
  1246. }
  1247. /* Generates a new key pair via the handle HANDLE of NBITS bits and
  1248. stores it in KEY_PAIR. In case non-standard settings are wanted, a
  1249. pointer to a structure of type gcry_ac_key_spec_<algorithm>_t,
  1250. matching the selected algorithm, can be given as KEY_SPEC.
  1251. MISC_DATA is not used yet. */
  1252. gcry_error_t
  1253. _gcry_ac_key_pair_generate (gcry_ac_handle_t handle, unsigned int nbits,
  1254. void *key_spec,
  1255. gcry_ac_key_pair_t *key_pair,
  1256. gcry_mpi_t **misc_data)
  1257. {
  1258. gcry_sexp_t genkey_sexp_request;
  1259. gcry_sexp_t genkey_sexp_reply;
  1260. gcry_ac_data_t key_data_secret;
  1261. gcry_ac_data_t key_data_public;
  1262. gcry_ac_key_pair_t key_pair_new;
  1263. gcry_ac_key_t key_secret;
  1264. gcry_ac_key_t key_public;
  1265. gcry_sexp_t key_sexp;
  1266. gcry_error_t err;
  1267. char *genkey_format;
  1268. size_t genkey_format_n;
  1269. void **arg_list;
  1270. size_t arg_list_n;
  1271. unsigned int i;
  1272. unsigned int j;
  1273. (void)misc_data;
  1274. if (fips_mode ())
  1275. return gpg_error (GPG_ERR_NOT_SUPPORTED);
  1276. key_data_secret = NULL;
  1277. key_data_public = NULL;
  1278. key_secret = NULL;
  1279. key_public = NULL;
  1280. genkey_format = NULL;
  1281. arg_list = NULL;
  1282. genkey_sexp_request = NULL;
  1283. genkey_sexp_reply = NULL;
  1284. key_sexp = NULL;
  1285. /* Allocate key pair. */
  1286. key_pair_new = gcry_malloc (sizeof (struct gcry_ac_key_pair));
  1287. if (! key_pair_new)
  1288. {
  1289. err = gcry_error_from_errno (errno);
  1290. goto out;
  1291. }
  1292. /* Allocate keys. */
  1293. key_secret = gcry_malloc (sizeof (*key_secret));
  1294. if (! key_secret)
  1295. {
  1296. err = gcry_error_from_errno (errno);
  1297. goto out;
  1298. }
  1299. key_public = gcry_malloc (sizeof (*key_public));
  1300. if (! key_public)
  1301. {
  1302. err = gcry_error_from_errno (errno);
  1303. goto out;
  1304. }
  1305. /* Calculate size of the format string, that is used for creating
  1306. the request S-expression. */
  1307. genkey_format_n = 22;
  1308. /* Respect any relevant algorithm specific commands. */
  1309. if (key_spec)
  1310. for (i = 0; i < DIM (ac_key_generate_specs); i++)
  1311. if (handle->algorithm == ac_key_generate_specs[i].algorithm)
  1312. genkey_format_n += 6;
  1313. /* Create format string. */
  1314. genkey_format = gcry_malloc (genkey_format_n);
  1315. if (! genkey_format)
  1316. {
  1317. err = gcry_error_from_errno (errno);
  1318. goto out;
  1319. }
  1320. /* Fill format string. */
  1321. *genkey_format = 0;
  1322. strcat (genkey_format, "(genkey(%s(nbits%d)");
  1323. if (key_spec)
  1324. for (i = 0; i < DIM (ac_key_generate_specs); i++)
  1325. if (handle->algorithm == ac_key_generate_specs[i].algorithm)
  1326. strcat (genkey_format, "(%s%m)");
  1327. strcat (genkey_format, "))");
  1328. /* Build list of argument pointers, the algorithm name and the nbits
  1329. are always needed. */
  1330. arg_list_n = 2;
  1331. /* Now the algorithm specific arguments. */
  1332. if (key_spec)
  1333. for (i = 0; i < DIM (ac_key_generate_specs); i++)
  1334. if (handle->algorithm == ac_key_generate_specs[i].algorithm)
  1335. arg_list_n += 2;
  1336. /* Allocate list. */
  1337. arg_list = gcry_calloc (arg_list_n, sizeof (*arg_list));
  1338. if (! arg_list)
  1339. {
  1340. err = gcry_error_from_errno (errno);
  1341. goto out;
  1342. }
  1343. arg_list[0] = (void *) &handle->algorithm_name;
  1344. arg_list[1] = (void *) &nbits;
  1345. if (key_spec)
  1346. for (j = 2, i = 0; i < DIM (ac_key_generate_specs); i++)
  1347. if (handle->algorithm == ac_key_generate_specs[i].algorithm)
  1348. {
  1349. /* Add name of this specification flag and the
  1350. according member of the spec strucuture. */
  1351. arg_list[j++] = (void *)(&ac_key_generate_specs[i].name);
  1352. arg_list[j++] = (void *)
  1353. (((char *) key_spec)
  1354. + ac_key_generate_specs[i].offset);
  1355. /* FIXME: above seems to suck. */
  1356. }
  1357. /* Construct final request S-expression. */
  1358. err = gcry_sexp_build_array (&genkey_sexp_request,
  1359. NULL, genkey_format, arg_list);
  1360. if (err)
  1361. goto out;
  1362. /* Perform genkey operation. */
  1363. err = gcry_pk_genkey (&genkey_sexp_reply, genkey_sexp_request);
  1364. if (err)
  1365. goto out;
  1366. key_sexp = gcry_sexp_find_token (genkey_sexp_reply, "private-key", 0);
  1367. if (! key_sexp)
  1368. {
  1369. err = gcry_error (GPG_ERR_INTERNAL);
  1370. goto out;
  1371. }
  1372. err = ac_data_extract ("private-key", handle->algorithm_name,
  1373. key_sexp, &key_data_secret);
  1374. if (err)
  1375. goto out;
  1376. gcry_sexp_release (key_sexp);
  1377. key_sexp = gcry_sexp_find_token (genkey_sexp_reply, "public-key", 0);
  1378. if (! key_sexp)
  1379. {
  1380. err = gcry_error (GPG_ERR_INTERNAL);
  1381. goto out;
  1382. }
  1383. err = ac_data_extract ("public-key", handle->algorithm_name,
  1384. key_sexp, &key_data_public);
  1385. if (err)
  1386. goto out;
  1387. /* Done. */
  1388. key_secret->type = GCRY_AC_KEY_SECRET;
  1389. key_secret->data = key_data_secret;
  1390. key_public->type = GCRY_AC_KEY_PUBLIC;
  1391. key_public->data = key_data_public;
  1392. key_pair_new->secret = key_secret;
  1393. key_pair_new->public = key_public;
  1394. *key_pair = key_pair_new;
  1395. out:
  1396. /* Deallocate resources. */
  1397. gcry_free (genkey_format);
  1398. gcry_free (arg_list);
  1399. gcry_sexp_release (genkey_sexp_request);
  1400. gcry_sexp_release (genkey_sexp_reply);
  1401. gcry_sexp_release (key_sexp);
  1402. if (err)
  1403. {
  1404. _gcry_ac_data_destroy (key_data_secret);
  1405. _gcry_ac_data_destroy (key_data_public);
  1406. gcry_free (key_secret);
  1407. gcry_free (key_public);
  1408. gcry_free (key_pair_new);
  1409. }
  1410. return err;
  1411. }
  1412. /* Returns the key of type WHICH out of the key pair KEY_PAIR. */
  1413. gcry_ac_key_t
  1414. _gcry_ac_key_pair_extract (gcry_ac_key_pair_t key_pair,
  1415. gcry_ac_key_type_t which)
  1416. {
  1417. gcry_ac_key_t key;
  1418. if (fips_mode ())
  1419. return NULL;
  1420. switch (which)
  1421. {
  1422. case GCRY_AC_KEY_SECRET:
  1423. key = key_pair->secret;
  1424. break;
  1425. case GCRY_AC_KEY_PUBLIC:
  1426. key = key_pair->public;
  1427. break;
  1428. default:
  1429. key = NULL;
  1430. break;
  1431. }
  1432. return key;
  1433. }
  1434. /* Destroys the key KEY. */
  1435. void
  1436. _gcry_ac_key_destroy (gcry_ac_key_t key)
  1437. {
  1438. unsigned int i;
  1439. if (key)
  1440. {
  1441. if (key->data)
  1442. {
  1443. for (i = 0; i < key->data->data_n; i++)
  1444. {
  1445. if (key->data->data[i].mpi)
  1446. gcry_mpi_release (key->data->data[i].mpi);
  1447. if (key->data->data[i].name)
  1448. gcry_free (key->data->data[i].name);
  1449. }
  1450. gcry_free (key->data->data);
  1451. gcry_free (key->data);
  1452. }
  1453. gcry_free (key);
  1454. }
  1455. }
  1456. /* Destroys the key pair KEY_PAIR. */
  1457. void
  1458. _gcry_ac_key_pair_destroy (gcry_ac_key_pair_t key_pair)
  1459. {
  1460. if (key_pair)
  1461. {
  1462. gcry_ac_key_destroy (key_pair->secret);
  1463. gcry_ac_key_destroy (key_pair->public);
  1464. gcry_free (key_pair);
  1465. }
  1466. }
  1467. /* Returns the data set contained in the key KEY. */
  1468. gcry_ac_data_t
  1469. _gcry_ac_key_data_get (gcry_ac_key_t key)
  1470. {
  1471. if (fips_mode ())
  1472. return NULL;
  1473. return key->data;
  1474. }
  1475. /* Verifies that the key KEY is sane via HANDLE. */
  1476. gcry_error_t
  1477. _gcry_ac_key_test (gcry_ac_handle_t handle, gcry_ac_key_t key)
  1478. {
  1479. gcry_sexp_t key_sexp;
  1480. gcry_error_t err;
  1481. if (fips_mode ())
  1482. return gpg_error (GPG_ERR_NOT_SUPPORTED);
  1483. key_sexp = NULL;
  1484. err = ac_data_construct (ac_key_identifiers[key->type], 0, 0,
  1485. handle->algorithm_name, key->data, &key_sexp);
  1486. if (err)
  1487. goto out;
  1488. err = gcry_pk_testkey (key_sexp);
  1489. out:
  1490. gcry_sexp_release (key_sexp);
  1491. return gcry_error (err);
  1492. }
  1493. /* Stores the number of bits of the key KEY in NBITS via HANDLE. */
  1494. gcry_error_t
  1495. _gcry_ac_key_get_nbits (gcry_ac_handle_t handle,
  1496. gcry_ac_key_t key, unsigned int *nbits)
  1497. {
  1498. gcry_sexp_t key_sexp;
  1499. gcry_error_t err;
  1500. unsigned int n;
  1501. if (fips_mode ())
  1502. return gpg_error (GPG_ERR_NOT_SUPPORTED);
  1503. key_sexp = NULL;
  1504. err = ac_data_construct (ac_key_identifiers[key->type],
  1505. 0, 0, handle->algorithm_name, key->data, &key_sexp);
  1506. if (err)
  1507. goto out;
  1508. n = gcry_pk_get_nbits (key_sexp);
  1509. if (! n)
  1510. {
  1511. err = gcry_error (GPG_ERR_PUBKEY_ALGO);
  1512. goto out;
  1513. }
  1514. *nbits = n;
  1515. out:
  1516. gcry_sexp_release (key_sexp);
  1517. return err;
  1518. }
  1519. /* Writes the 20 byte long key grip of the key KEY to KEY_GRIP via
  1520. HANDLE. */
  1521. gcry_error_t
  1522. _gcry_ac_key_get_grip (gcry_ac_handle_t handle,
  1523. gcry_ac_key_t key, unsigned char *key_grip)
  1524. {
  1525. gcry_sexp_t key_sexp;
  1526. gcry_error_t err;
  1527. unsigned char *ret;
  1528. if (fips_mode ())
  1529. return gpg_error (GPG_ERR_NOT_SUPPORTED);
  1530. key_sexp = NULL;
  1531. err = ac_data_construct (ac_key_identifiers[key->type], 0, 0,
  1532. handle->algorithm_name, key->data, &key_sexp);
  1533. if (err)
  1534. goto out;
  1535. ret = gcry_pk_get_keygrip (key_sexp, key_grip);
  1536. if (! ret)
  1537. {
  1538. err = gcry_error (GPG_ERR_INV_OBJ);
  1539. goto out;
  1540. }
  1541. err = 0;
  1542. out:
  1543. gcry_sexp_release (key_sexp);
  1544. return err;
  1545. }
  1546. /*
  1547. * Functions performing cryptographic operations.
  1548. */
  1549. /* Encrypts the plain text MPI value DATA_PLAIN with the key public
  1550. KEY under the control of the flags FLAGS and stores the resulting
  1551. data set into DATA_ENCRYPTED. */
  1552. gcry_error_t
  1553. _gcry_ac_data_encrypt (gcry_ac_handle_t handle,
  1554. unsigned int flags,
  1555. gcry_ac_key_t key,
  1556. gcry_mpi_t data_plain,
  1557. gcry_ac_data_t *data_encrypted)
  1558. {
  1559. gcry_ac_data_t data_encrypted_new;
  1560. gcry_ac_data_t data_value;
  1561. gcry_sexp_t sexp_request;
  1562. gcry_sexp_t sexp_reply;
  1563. gcry_sexp_t sexp_key;
  1564. gcry_error_t err;
  1565. if (fips_mode ())
  1566. return gpg_error (GPG_ERR_NOT_SUPPORTED);
  1567. data_encrypted_new = NULL;
  1568. sexp_request = NULL;
  1569. sexp_reply = NULL;
  1570. data_value = NULL;
  1571. sexp_key = NULL;
  1572. if (key->type != GCRY_AC_KEY_PUBLIC)
  1573. {
  1574. err = gcry_error (GPG_ERR_WRONG_KEY_USAGE);
  1575. goto out;
  1576. }
  1577. err = ac_data_construct (ac_key_identifiers[key->type], 0, 0,
  1578. handle->algorithm_name, key->data, &sexp_key);
  1579. if (err)
  1580. goto out;
  1581. err = _gcry_ac_data_new (&data_value);
  1582. if (err)
  1583. goto out;
  1584. err = _gcry_ac_data_set (data_value, 0, "value", data_plain);
  1585. if (err)
  1586. goto out;
  1587. err = ac_data_construct ("data", 1, flags, handle->algorithm_name,
  1588. data_value, &sexp_request);
  1589. if (err)
  1590. goto out;
  1591. /* FIXME: error vs. errcode? */
  1592. err = gcry_pk_encrypt (&sexp_reply, sexp_request, sexp_key);
  1593. if (err)
  1594. goto out;
  1595. /* Extract data. */
  1596. err = ac_data_extract ("enc-val", handle->algorithm_name,
  1597. sexp_reply, &data_encrypted_new);
  1598. if (err)
  1599. goto out;
  1600. *data_encrypted = data_encrypted_new;
  1601. out:
  1602. /* Deallocate resources. */
  1603. gcry_sexp_release (sexp_request);
  1604. gcry_sexp_release (sexp_reply);
  1605. gcry_sexp_release (sexp_key);
  1606. _gcry_ac_data_destroy (data_value);
  1607. return err;
  1608. }
  1609. /* Decrypts the encrypted data contained in the data set
  1610. DATA_ENCRYPTED with the secret key KEY under the control of the
  1611. flags FLAGS and stores the resulting plain text MPI value in
  1612. DATA_PLAIN. */
  1613. gcry_error_t
  1614. _gcry_ac_data_decrypt (gcry_ac_handle_t handle,
  1615. unsigned int flags,
  1616. gcry_ac_key_t key,
  1617. gcry_mpi_t *data_plain,
  1618. gcry_ac_data_t data_encrypted)
  1619. {
  1620. gcry_mpi_t data_decrypted;
  1621. gcry_sexp_t sexp_request;
  1622. gcry_sexp_t sexp_reply;
  1623. gcry_sexp_t sexp_value;
  1624. gcry_sexp_t sexp_key;
  1625. gcry_error_t err;
  1626. if (fips_mode ())
  1627. return gpg_error (GPG_ERR_NOT_SUPPORTED);
  1628. sexp_request = NULL;
  1629. sexp_reply = NULL;
  1630. sexp_value = NULL;
  1631. sexp_key = NULL;
  1632. if (key->type != GCRY_AC_KEY_SECRET)
  1633. {
  1634. err = gcry_error (GPG_ERR_WRONG_KEY_USAGE);
  1635. goto out;
  1636. }
  1637. err = ac_data_construct (ac_key_identifiers[key->type], 0, 0,
  1638. handle->algorithm_name, key->data, &sexp_key);
  1639. if (err)
  1640. goto out;
  1641. /* Create S-expression from data. */
  1642. err = ac_data_construct ("enc-val", 1, flags, handle->algorithm_name,
  1643. data_encrypted, &sexp_request);
  1644. if (err)
  1645. goto out;
  1646. /* Decrypt. */
  1647. err = gcry_pk_decrypt (&sexp_reply, sexp_request, sexp_key);
  1648. if (err)
  1649. goto out;
  1650. /* Extract plain text. */
  1651. sexp_value = gcry_sexp_find_token (sexp_reply, "value", 0);
  1652. if (! sexp_value)
  1653. {
  1654. /* FIXME? */
  1655. err = gcry_error (GPG_ERR_GENERAL);
  1656. goto out;
  1657. }
  1658. data_decrypted = gcry_sexp_nth_mpi (sexp_value, 1, GCRYMPI_FMT_USG);
  1659. if (! data_decrypted)
  1660. {
  1661. err = gcry_error (GPG_ERR_GENERAL);
  1662. goto out;
  1663. }
  1664. *data_plain = data_decrypted;
  1665. out:
  1666. /* Deallocate resources. */
  1667. gcry_sexp_release (sexp_request);
  1668. gcry_sexp_release (sexp_reply);
  1669. gcry_sexp_release (sexp_value);
  1670. gcry_sexp_release (sexp_key);
  1671. return gcry_error (err);
  1672. }
  1673. /* Signs the data contained in DATA with the secret key KEY and stores
  1674. the resulting signature data set in DATA_SIGNATURE. */
  1675. gcry_error_t
  1676. _gcry_ac_data_sign (gcry_ac_handle_t handle,
  1677. gcry_ac_key_t key,
  1678. gcry_mpi_t data,
  1679. gcry_ac_data_t *data_signature)
  1680. {
  1681. gcry_ac_data_t data_signed;
  1682. gcry_ac_data_t data_value;
  1683. gcry_sexp_t sexp_request;
  1684. gcry_sexp_t sexp_reply;
  1685. gcry_sexp_t sexp_key;
  1686. gcry_error_t err;
  1687. if (fips_mode ())
  1688. return gpg_error (GPG_ERR_NOT_SUPPORTED);
  1689. data_signed = NULL;
  1690. data_value = NULL;
  1691. sexp_request = NULL;
  1692. sexp_reply = NULL;
  1693. sexp_key = NULL;
  1694. if (key->type != GCRY_AC_KEY_SECRET)
  1695. {
  1696. err = gcry_error (GPG_ERR_WRONG_KEY_USAGE);
  1697. goto out;
  1698. }
  1699. err = ac_data_construct (ac_key_identifiers[key->type], 0, 0,
  1700. handle->algorithm_name, key->data, &sexp_key);
  1701. if (err)
  1702. goto out;
  1703. err = _gcry_ac_data_new (&data_value);
  1704. if (err)
  1705. goto out;
  1706. err = _gcry_ac_data_set (data_value, 0, "value", data);
  1707. if (err)
  1708. goto out;
  1709. /* Create S-expression holding the data. */
  1710. err = ac_data_construct ("data", 1, 0, NULL, data_value, &sexp_request);
  1711. if (err)
  1712. goto out;
  1713. /* Sign. */
  1714. err = gcry_pk_sign (&sexp_reply, sexp_request, sexp_key);
  1715. if (err)
  1716. goto out;
  1717. /* Extract data. */
  1718. err = ac_data_extract ("sig-val", handle->algorithm_name,
  1719. sexp_reply, &data_signed);
  1720. if (err)
  1721. goto out;
  1722. /* Done. */
  1723. *data_signature = data_signed;
  1724. out:
  1725. gcry_sexp_release (sexp_request);
  1726. gcry_sexp_release (sexp_reply);
  1727. gcry_sexp_release (sexp_key);
  1728. _gcry_ac_data_destroy (data_value);
  1729. return gcry_error (err);
  1730. }
  1731. /* Verifies that the signature contained in the data set
  1732. DATA_SIGNATURE is indeed the result of signing the data contained
  1733. in DATA with the secret key belonging to the public key KEY. */
  1734. gcry_error_t
  1735. _gcry_ac_data_verify (gcry_ac_handle_t handle,
  1736. gcry_ac_key_t key,
  1737. gcry_mpi_t data,
  1738. gcry_ac_data_t data_signature)
  1739. {
  1740. gcry_sexp_t sexp_signature;
  1741. gcry_ac_data_t data_value;
  1742. gcry_sexp_t sexp_data;
  1743. gcry_sexp_t sexp_key;
  1744. gcry_error_t err;
  1745. if (fips_mode ())
  1746. return gpg_error (GPG_ERR_NOT_SUPPORTED);
  1747. sexp_signature = NULL;
  1748. data_value = NULL;
  1749. sexp_data = NULL;
  1750. sexp_key = NULL;
  1751. err = ac_data_construct ("public-key", 0, 0,
  1752. handle->algorithm_name, key->data, &sexp_key);
  1753. if (err)
  1754. goto out;
  1755. if (key->type != GCRY_AC_KEY_PUBLIC)
  1756. {
  1757. err = gcry_error (GPG_ERR_WRONG_KEY_USAGE);
  1758. goto out;
  1759. }
  1760. /* Construct S-expression holding the signature data. */
  1761. err = ac_data_construct ("sig-val", 1, 0, handle->algorithm_name,
  1762. data_signature, &sexp_signature);
  1763. if (err)
  1764. goto out;
  1765. err = _gcry_ac_data_new (&data_value);
  1766. if (err)
  1767. goto out;
  1768. err = _gcry_ac_data_set (data_value, 0, "value", data);
  1769. if (err)
  1770. goto out;
  1771. /* Construct S-expression holding the data. */
  1772. err = ac_data_construct ("data", 1, 0, NULL, data_value, &sexp_data);
  1773. if (err)
  1774. goto out;
  1775. /* Verify signature. */
  1776. err = gcry_pk_verify (sexp_signature, sexp_data, sexp_key);
  1777. out:
  1778. gcry_sexp_release (sexp_signature);
  1779. gcry_sexp_release (sexp_data);
  1780. gcry_sexp_release (sexp_key);
  1781. _gcry_ac_data_destroy (data_value);
  1782. return gcry_error (err);
  1783. }
  1784. /*
  1785. * Implementation of encoding methods (em).
  1786. */
  1787. /* Type for functions that encode or decode (hence the name) a
  1788. message. */
  1789. typedef gcry_error_t (*gcry_ac_em_dencode_t) (unsigned int flags,
  1790. void *options,
  1791. gcry_ac_io_t *ac_io_read,
  1792. gcry_ac_io_t *ac_io_write);
  1793. /* Fill the buffer BUFFER which is BUFFER_N bytes long with non-zero
  1794. random bytes of random level LEVEL. */
  1795. static void
  1796. em_randomize_nonzero (unsigned char *buffer, size_t buffer_n,
  1797. gcry_random_level_t level)
  1798. {
  1799. unsigned char *buffer_rand;
  1800. unsigned int buffer_rand_n;
  1801. unsigned int zeros;
  1802. unsigned int i;
  1803. unsigned int j;
  1804. for (i = 0; i < buffer_n; i++)
  1805. buffer[i] = 0;
  1806. do
  1807. {
  1808. /* Count zeros. */
  1809. for (i = zeros = 0; i < buffer_n; i++)
  1810. if (! buffer[i])
  1811. zeros++;
  1812. if (zeros)
  1813. {
  1814. /* Get random bytes. */
  1815. buffer_rand_n = zeros + (zeros / 128);
  1816. buffer_rand = gcry_random_bytes_secure (buffer_rand_n, level);
  1817. /* Substitute zeros with non-zero random bytes. */
  1818. for (i = j = 0; zeros && (i < buffer_n) && (j < buffer_rand_n); i++)
  1819. if (! buffer[i])
  1820. {
  1821. while ((j < buffer_rand_n) && (! buffer_rand[j]))
  1822. j++;
  1823. if (j < buffer_rand_n)
  1824. {
  1825. buffer[i] = buffer_rand[j++];
  1826. zeros--;
  1827. }
  1828. else
  1829. break;
  1830. }
  1831. gcry_free (buffer_rand);
  1832. }
  1833. }
  1834. while (zeros);
  1835. }
  1836. /* Encode a message according to the Encoding Method for Encryption
  1837. `PKCS-V1_5' (EME-PKCS-V1_5). */
  1838. static gcry_error_t
  1839. eme_pkcs_v1_5_encode (unsigned int flags, void *opts,
  1840. gcry_ac_io_t *ac_io_read,
  1841. gcry_ac_io_t *ac_io_write)
  1842. {
  1843. gcry_ac_eme_pkcs_v1_5_t *options;
  1844. gcry_error_t err;
  1845. unsigned char *buffer;
  1846. unsigned char *ps;
  1847. unsigned char *m;
  1848. size_t m_n;
  1849. unsigned int ps_n;
  1850. unsigned int k;
  1851. (void)flags;
  1852. options = opts;
  1853. buffer = NULL;
  1854. m = NULL;
  1855. err = _gcry_ac_io_read_all (ac_io_read, &m, &m_n);
  1856. if (err)
  1857. goto out;
  1858. /* Figure out key length in bytes. */
  1859. k = options->key_size / 8;
  1860. if (m_n > k - 11)
  1861. {
  1862. /* Key is too short for message. */
  1863. err = gcry_error (GPG_ERR_TOO_SHORT);
  1864. goto out;
  1865. }
  1866. /* According to this encoding method, the first byte of the encoded
  1867. message is zero. This byte will be lost anyway, when the encoded
  1868. message is to be converted into an MPI, that's why we skip
  1869. it. */
  1870. /* Allocate buffer. */
  1871. buffer = gcry_malloc (k - 1);
  1872. if (! buffer)
  1873. {
  1874. err = gcry_error_from_errno (errno);
  1875. goto out;
  1876. }
  1877. /* Generate an octet string PS of length k - mLen - 3 consisting
  1878. of pseudorandomly generated nonzero octets. The length of PS
  1879. will be at least eight octets. */
  1880. ps_n = k - m_n - 3;
  1881. ps = buffer + 1;
  1882. em_randomize_nonzero (ps, ps_n, GCRY_STRONG_RANDOM);
  1883. /* Concatenate PS, the message M, and other padding to form an
  1884. encoded message EM of length k octets as:
  1885. EM = 0x00 || 0x02 || PS || 0x00 || M. */
  1886. buffer[0] = 0x02;
  1887. buffer[ps_n + 1] = 0x00;
  1888. memcpy (buffer + ps_n + 2, m, m_n);
  1889. err = _gcry_ac_io_write (ac_io_write, buffer, k - 1);
  1890. out:
  1891. gcry_free (buffer);
  1892. gcry_free (m);
  1893. return err;
  1894. }
  1895. /* Decode a message according to the Encoding Method for Encryption
  1896. `PKCS-V1_5' (EME-PKCS-V1_5). */
  1897. static gcry_error_t
  1898. eme_pkcs_v1_5_decode (unsigned int flags, void *opts,
  1899. gcry_ac_io_t *ac_io_read,
  1900. gcry_ac_io_t *ac_io_write)
  1901. {
  1902. gcry_ac_eme_pkcs_v1_5_t *options;
  1903. unsigned char *buffer;
  1904. unsigned char *em;
  1905. size_t em_n;
  1906. gcry_error_t err;
  1907. unsigned int i;
  1908. unsigned int k;
  1909. (void)flags;
  1910. options = opts;
  1911. buffer = NULL;
  1912. em = NULL;
  1913. err = _gcry_ac_io_read_all (ac_io_read, &em, &em_n);
  1914. if (err)
  1915. goto out;
  1916. /* Figure out key size. */
  1917. k = options->key_size / 8;
  1918. /* Search for zero byte. */
  1919. for (i = 0; (i < em_n) && em[i]; i++);
  1920. /* According to this encoding method, the first byte of the encoded
  1921. message should be zero. This byte is lost. */
  1922. if (! ((em_n >= 10)
  1923. && (em_n == (k - 1))
  1924. && (em[0] == 0x02)
  1925. && (i < em_n)
  1926. && ((i - 1) >= 8)))
  1927. {
  1928. err = gcry_error (GPG_ERR_DECRYPT_FAILED);
  1929. goto out;
  1930. }
  1931. i++;
  1932. buffer = gcry_malloc (em_n - i);
  1933. if (! buffer)
  1934. {
  1935. err = gcry_error_from_errno (errno);
  1936. goto out;
  1937. }
  1938. memcpy (buffer, em + i, em_n - i);
  1939. err = _gcry_ac_io_write (ac_io_write, buffer, em_n - i);
  1940. out:
  1941. gcry_free (buffer);
  1942. gcry_free (em);
  1943. return err;
  1944. }
  1945. static gcry_error_t
  1946. emsa_pkcs_v1_5_encode_data_cb (void *opaque,
  1947. unsigned char *buffer, size_t buffer_n)
  1948. {
  1949. gcry_md_hd_t md_handle;
  1950. md_handle = opaque;
  1951. gcry_md_write (md_handle, buffer, buffer_n);
  1952. return 0;
  1953. }
  1954. /* Encode a message according to the Encoding Method for Signatures
  1955. with Appendix `PKCS-V1_5' (EMSA-PKCS-V1_5). */
  1956. static gcry_error_t
  1957. emsa_pkcs_v1_5_encode (unsigned int flags, void *opts,
  1958. gcry_ac_io_t *ac_io_read,
  1959. gcry_ac_io_t *ac_io_write)
  1960. {
  1961. gcry_ac_emsa_pkcs_v1_5_t *options;
  1962. gcry_error_t err;
  1963. gcry_md_hd_t md;
  1964. unsigned char *t;
  1965. size_t t_n;
  1966. unsigned char *h;
  1967. size_t h_n;
  1968. unsigned char *ps;
  1969. size_t ps_n;
  1970. unsigned char *buffer;
  1971. size_t buffer_n;
  1972. unsigned char asn[100]; /* FIXME, always enough? */
  1973. size_t asn_n;
  1974. unsigned int i;
  1975. (void)flags;
  1976. options = opts;
  1977. buffer = NULL;
  1978. md = NULL;
  1979. ps = NULL;
  1980. t = NULL;
  1981. /* Create hashing handle and get the necessary information. */
  1982. err = gcry_md_open (&md, options->md, 0);
  1983. if (err)
  1984. goto out;
  1985. asn_n = DIM (asn);
  1986. err = gcry_md_algo_info (options->md, GCRYCTL_GET_ASNOID, asn, &asn_n);
  1987. if (err)
  1988. goto out;
  1989. h_n = gcry_md_get_algo_dlen (options->md);
  1990. err = _gcry_ac_io_process (ac_io_read, emsa_pkcs_v1_5_encode_data_cb, md);
  1991. if (err)
  1992. goto out;
  1993. h = gcry_md_read (md, 0);
  1994. /* Encode the algorithm ID for the hash function and the hash value
  1995. into an ASN.1 value of type DigestInfo with the Distinguished
  1996. Encoding Rules (DER), where the type DigestInfo has the syntax:
  1997. DigestInfo ::== SEQUENCE {
  1998. digestAlgorithm AlgorithmIdentifier,
  1999. digest OCTET STRING
  2000. }
  2001. The first field identifies the hash function and the second
  2002. contains the hash value. Let T be the DER encoding of the
  2003. DigestInfo value and let tLen be the length in octets of T. */
  2004. t_n = asn_n + h_n;
  2005. t = gcry_malloc (t_n);
  2006. if (! t)
  2007. {
  2008. err = gcry_error_from_errno (errno);
  2009. goto out;
  2010. }
  2011. for (i = 0; i < asn_n; i++)
  2012. t[i] = asn[i];
  2013. for (i = 0; i < h_n; i++)
  2014. t[asn_n + i] = h[i];
  2015. /* If emLen < tLen + 11, output "intended encoded message length
  2016. too short" and stop. */
  2017. if (options->em_n < t_n + 11)
  2018. {
  2019. err = gcry_error (GPG_ERR_TOO_SHORT);
  2020. goto out;
  2021. }
  2022. /* Generate an octet string PS consisting of emLen - tLen - 3 octets
  2023. with hexadecimal value 0xFF. The length of PS will be at least 8
  2024. octets. */
  2025. ps_n = options->em_n - t_n - 3;
  2026. ps = gcry_malloc (ps_n);
  2027. if (! ps)
  2028. {
  2029. err = gcry_error_from_errno (errno);
  2030. goto out;
  2031. }
  2032. for (i = 0; i < ps_n; i++)
  2033. ps[i] = 0xFF;
  2034. /* Concatenate PS, the DER encoding T, and other padding to form the
  2035. encoded message EM as:
  2036. EM = 0x00 || 0x01 || PS || 0x00 || T. */
  2037. buffer_n = ps_n + t_n + 3;
  2038. buffer = gcry_malloc (buffer_n);
  2039. if (! buffer)
  2040. {
  2041. err = gcry_error_from_errno (errno);
  2042. goto out;
  2043. }
  2044. buffer[0] = 0x00;
  2045. buffer[1] = 0x01;
  2046. for (i = 0; i < ps_n; i++)
  2047. buffer[2 + i] = ps[i];
  2048. buffer[2 + ps_n] = 0x00;
  2049. for (i = 0; i < t_n; i++)
  2050. buffer[3 + ps_n + i] = t[i];
  2051. err = _gcry_ac_io_write (ac_io_write, buffer, buffer_n);
  2052. out:
  2053. gcry_md_close (md);
  2054. gcry_free (buffer);
  2055. gcry_free (ps);
  2056. gcry_free (t);
  2057. return err;
  2058. }
  2059. /* `Actions' for data_dencode(). */
  2060. typedef enum dencode_action
  2061. {
  2062. DATA_ENCODE,
  2063. DATA_DECODE,
  2064. }
  2065. dencode_action_t;
  2066. /* Encode or decode a message according to the the encoding method
  2067. METHOD; ACTION specifies whether the message that is contained in
  2068. BUFFER_IN and of length BUFFER_IN_N should be encoded or decoded.
  2069. The resulting message will be stored in a newly allocated buffer in
  2070. BUFFER_OUT and BUFFER_OUT_N. */
  2071. static gcry_error_t
  2072. ac_data_dencode (gcry_ac_em_t method, dencode_action_t action,
  2073. unsigned int flags, void *options,
  2074. gcry_ac_io_t *ac_io_read,
  2075. gcry_ac_io_t *ac_io_write)
  2076. {
  2077. struct
  2078. {
  2079. gcry_ac_em_t method;
  2080. gcry_ac_em_dencode_t encode;
  2081. gcry_ac_em_dencode_t decode;
  2082. } methods[] =
  2083. {
  2084. { GCRY_AC_EME_PKCS_V1_5,
  2085. eme_pkcs_v1_5_encode, eme_pkcs_v1_5_decode },
  2086. { GCRY_AC_EMSA_PKCS_V1_5,
  2087. emsa_pkcs_v1_5_encode, NULL },
  2088. };
  2089. size_t methods_n;
  2090. gcry_error_t err;
  2091. unsigned int i;
  2092. methods_n = sizeof (methods) / sizeof (*methods);
  2093. for (i = 0; i < methods_n; i++)
  2094. if (methods[i].method == method)
  2095. break;
  2096. if (i == methods_n)
  2097. {
  2098. err = gcry_error (GPG_ERR_NOT_FOUND); /* FIXME? */
  2099. goto out;
  2100. }
  2101. err = 0;
  2102. switch (action)
  2103. {
  2104. case DATA_ENCODE:
  2105. if (methods[i].encode)
  2106. /* FIXME? */
  2107. err = (*methods[i].encode) (flags, options, ac_io_read, ac_io_write);
  2108. break;
  2109. case DATA_DECODE:
  2110. if (methods[i].decode)
  2111. /* FIXME? */
  2112. err = (*methods[i].decode) (flags, options, ac_io_read, ac_io_write);
  2113. break;
  2114. default:
  2115. err = gcry_error (GPG_ERR_INV_ARG);
  2116. break;
  2117. }
  2118. out:
  2119. return err;
  2120. }
  2121. /* Encode a message according to the encoding method METHOD. OPTIONS
  2122. must be a pointer to a method-specific structure
  2123. (gcry_ac_em*_t). */
  2124. gcry_error_t
  2125. _gcry_ac_data_encode (gcry_ac_em_t method,
  2126. unsigned int flags, void *options,
  2127. gcry_ac_io_t *ac_io_read,
  2128. gcry_ac_io_t *ac_io_write)
  2129. {
  2130. if (fips_mode ())
  2131. return gpg_error (GPG_ERR_NOT_SUPPORTED);
  2132. return ac_data_dencode (method, DATA_ENCODE, flags, options,
  2133. ac_io_read, ac_io_write);
  2134. }
  2135. /* Dencode a message according to the encoding method METHOD. OPTIONS
  2136. must be a pointer to a method-specific structure
  2137. (gcry_ac_em*_t). */
  2138. gcry_error_t
  2139. _gcry_ac_data_decode (gcry_ac_em_t method,
  2140. unsigned int flags, void *options,
  2141. gcry_ac_io_t *ac_io_read,
  2142. gcry_ac_io_t *ac_io_write)
  2143. {
  2144. if (fips_mode ())
  2145. return gpg_error (GPG_ERR_NOT_SUPPORTED);
  2146. return ac_data_dencode (method, DATA_DECODE, flags, options,
  2147. ac_io_read, ac_io_write);
  2148. }
  2149. /* Convert an MPI into an octet string. */
  2150. void
  2151. _gcry_ac_mpi_to_os (gcry_mpi_t mpi, unsigned char *os, size_t os_n)
  2152. {
  2153. unsigned long digit;
  2154. gcry_mpi_t base;
  2155. unsigned int i;
  2156. unsigned int n;
  2157. gcry_mpi_t m;
  2158. gcry_mpi_t d;
  2159. if (fips_mode ())
  2160. return;
  2161. base = gcry_mpi_new (0);
  2162. gcry_mpi_set_ui (base, 256);
  2163. n = 0;
  2164. m = gcry_mpi_copy (mpi);
  2165. while (gcry_mpi_cmp_ui (m, 0))
  2166. {
  2167. n++;
  2168. gcry_mpi_div (m, NULL, m, base, 0);
  2169. }
  2170. gcry_mpi_set (m, mpi);
  2171. d = gcry_mpi_new (0);
  2172. for (i = 0; (i < n) && (i < os_n); i++)
  2173. {
  2174. gcry_mpi_mod (d, m, base);
  2175. _gcry_mpi_get_ui (d, &digit);
  2176. gcry_mpi_div (m, NULL, m, base, 0);
  2177. os[os_n - i - 1] = (digit & 0xFF);
  2178. }
  2179. for (; i < os_n; i++)
  2180. os[os_n - i - 1] = 0;
  2181. gcry_mpi_release (base);
  2182. gcry_mpi_release (d);
  2183. gcry_mpi_release (m);
  2184. }
  2185. /* Convert an MPI into an newly allocated octet string. */
  2186. gcry_error_t
  2187. _gcry_ac_mpi_to_os_alloc (gcry_mpi_t mpi, unsigned char **os, size_t *os_n)
  2188. {
  2189. unsigned char *buffer;
  2190. size_t buffer_n;
  2191. gcry_error_t err;
  2192. unsigned int nbits;
  2193. if (fips_mode ())
  2194. return gpg_error (GPG_ERR_NOT_SUPPORTED);
  2195. nbits = gcry_mpi_get_nbits (mpi);
  2196. buffer_n = (nbits + 7) / 8;
  2197. buffer = gcry_malloc (buffer_n);
  2198. if (! buffer)
  2199. {
  2200. err = gcry_error_from_errno (errno);
  2201. goto out;
  2202. }
  2203. _gcry_ac_mpi_to_os (mpi, buffer, buffer_n);
  2204. *os = buffer;
  2205. *os_n = buffer_n;
  2206. err = 0;
  2207. out:
  2208. return err;
  2209. }
  2210. /* Convert an octet string into an MPI. */
  2211. void
  2212. _gcry_ac_os_to_mpi (gcry_mpi_t mpi, unsigned char *os, size_t os_n)
  2213. {
  2214. unsigned int i;
  2215. gcry_mpi_t xi;
  2216. gcry_mpi_t x;
  2217. gcry_mpi_t a;
  2218. if (fips_mode ())
  2219. return;
  2220. a = gcry_mpi_new (0);
  2221. gcry_mpi_set_ui (a, 1);
  2222. x = gcry_mpi_new (0);
  2223. gcry_mpi_set_ui (x, 0);
  2224. xi = gcry_mpi_new (0);
  2225. for (i = 0; i < os_n; i++)
  2226. {
  2227. gcry_mpi_mul_ui (xi, a, os[os_n - i - 1]);
  2228. gcry_mpi_add (x, x, xi);
  2229. gcry_mpi_mul_ui (a, a, 256);
  2230. }
  2231. gcry_mpi_release (xi);
  2232. gcry_mpi_release (a);
  2233. gcry_mpi_set (mpi, x);
  2234. gcry_mpi_release (x); /* FIXME: correct? */
  2235. }
  2236. /*
  2237. * Implementation of Encryption Schemes (ES) and Signature Schemes
  2238. * with Appendix (SSA).
  2239. */
  2240. /* Schemes consist of two things: encoding methods and cryptographic
  2241. primitives.
  2242. Since encoding methods are accessible through a common API with
  2243. method-specific options passed as an anonymous struct, schemes have
  2244. to provide functions that construct this method-specific structure;
  2245. this is what the functions of type `gcry_ac_dencode_prepare_t' are
  2246. there for. */
  2247. typedef gcry_error_t (*gcry_ac_dencode_prepare_t) (gcry_ac_handle_t handle,
  2248. gcry_ac_key_t key,
  2249. void *opts,
  2250. void *opts_em);
  2251. /* The `dencode_prepare' function for ES-PKCS-V1_5. */
  2252. static gcry_error_t
  2253. ac_es_dencode_prepare_pkcs_v1_5 (gcry_ac_handle_t handle, gcry_ac_key_t key,
  2254. void *opts, void *opts_em)
  2255. {
  2256. gcry_ac_eme_pkcs_v1_5_t *options_em;
  2257. unsigned int nbits;
  2258. gcry_error_t err;
  2259. (void)opts;
  2260. err = _gcry_ac_key_get_nbits (handle, key, &nbits);
  2261. if (err)
  2262. goto out;
  2263. options_em = opts_em;
  2264. options_em->key_size = nbits;
  2265. out:
  2266. return err;
  2267. }
  2268. /* The `dencode_prepare' function for SSA-PKCS-V1_5. */
  2269. static gcry_error_t
  2270. ac_ssa_dencode_prepare_pkcs_v1_5 (gcry_ac_handle_t handle, gcry_ac_key_t key,
  2271. void *opts, void *opts_em)
  2272. {
  2273. gcry_ac_emsa_pkcs_v1_5_t *options_em;
  2274. gcry_ac_ssa_pkcs_v1_5_t *options;
  2275. gcry_error_t err;
  2276. unsigned int k;
  2277. options_em = opts_em;
  2278. options = opts;
  2279. err = _gcry_ac_key_get_nbits (handle, key, &k);
  2280. if (err)
  2281. goto out;
  2282. k = (k + 7) / 8;
  2283. options_em->md = options->md;
  2284. options_em->em_n = k;
  2285. out:
  2286. return err;
  2287. }
  2288. /* Type holding the information about each supported
  2289. Encryption/Signature Scheme. */
  2290. typedef struct ac_scheme
  2291. {
  2292. gcry_ac_scheme_t scheme;
  2293. gcry_ac_em_t scheme_encoding;
  2294. gcry_ac_dencode_prepare_t dencode_prepare;
  2295. size_t options_em_n;
  2296. } ac_scheme_t;
  2297. /* List of supported Schemes. */
  2298. static ac_scheme_t ac_schemes[] =
  2299. {
  2300. { GCRY_AC_ES_PKCS_V1_5, GCRY_AC_EME_PKCS_V1_5,
  2301. ac_es_dencode_prepare_pkcs_v1_5,
  2302. sizeof (gcry_ac_eme_pkcs_v1_5_t) },
  2303. { GCRY_AC_SSA_PKCS_V1_5, GCRY_AC_EMSA_PKCS_V1_5,
  2304. ac_ssa_dencode_prepare_pkcs_v1_5,
  2305. sizeof (gcry_ac_emsa_pkcs_v1_5_t) }
  2306. };
  2307. /* Lookup a scheme by it's ID. */
  2308. static ac_scheme_t *
  2309. ac_scheme_get (gcry_ac_scheme_t scheme)
  2310. {
  2311. ac_scheme_t *ac_scheme;
  2312. unsigned int i;
  2313. for (i = 0; i < DIM (ac_schemes); i++)
  2314. if (scheme == ac_schemes[i].scheme)
  2315. break;
  2316. if (i == DIM (ac_schemes))
  2317. ac_scheme = NULL;
  2318. else
  2319. ac_scheme = ac_schemes + i;
  2320. return ac_scheme;
  2321. }
  2322. /* Prepares the encoding/decoding by creating an according option
  2323. structure. */
  2324. static gcry_error_t
  2325. ac_dencode_prepare (gcry_ac_handle_t handle, gcry_ac_key_t key, void *opts,
  2326. ac_scheme_t scheme, void **opts_em)
  2327. {
  2328. gcry_error_t err;
  2329. void *options_em;
  2330. options_em = gcry_malloc (scheme.options_em_n);
  2331. if (! options_em)
  2332. {
  2333. err = gcry_error_from_errno (errno);
  2334. goto out;
  2335. }
  2336. err = (*scheme.dencode_prepare) (handle, key, opts, options_em);
  2337. if (err)
  2338. goto out;
  2339. *opts_em = options_em;
  2340. out:
  2341. if (err)
  2342. free (options_em);
  2343. return err;
  2344. }
  2345. /* Convert a data set into a single MPI; currently, this is only
  2346. supported for data sets containing a single MPI. */
  2347. static gcry_error_t
  2348. ac_data_set_to_mpi (gcry_ac_data_t data, gcry_mpi_t *mpi)
  2349. {
  2350. gcry_error_t err;
  2351. gcry_mpi_t mpi_new;
  2352. unsigned int elems;
  2353. elems = _gcry_ac_data_length (data);
  2354. if (elems != 1)
  2355. {
  2356. /* FIXME: I guess, we should be more flexible in this respect by
  2357. allowing the actual encryption/signature schemes to implement
  2358. this conversion mechanism. */
  2359. err = gcry_error (GPG_ERR_CONFLICT);
  2360. goto out;
  2361. }
  2362. err = _gcry_ac_data_get_index (data, GCRY_AC_FLAG_COPY, 0, NULL, &mpi_new);
  2363. if (err)
  2364. goto out;
  2365. *mpi = mpi_new;
  2366. out:
  2367. return err;
  2368. }
  2369. /* Encrypts the plain text message contained in M, which is of size
  2370. M_N, with the public key KEY_PUBLIC according to the Encryption
  2371. Scheme SCHEME_ID. HANDLE is used for accessing the low-level
  2372. cryptographic primitives. If OPTS is not NULL, it has to be an
  2373. anonymous structure specific to the chosen scheme (gcry_ac_es_*_t).
  2374. The encrypted message will be stored in C and C_N. */
  2375. gcry_error_t
  2376. _gcry_ac_data_encrypt_scheme (gcry_ac_handle_t handle,
  2377. gcry_ac_scheme_t scheme_id,
  2378. unsigned int flags, void *opts,
  2379. gcry_ac_key_t key,
  2380. gcry_ac_io_t *io_message,
  2381. gcry_ac_io_t *io_cipher)
  2382. {
  2383. gcry_error_t err;
  2384. gcry_ac_io_t io_em;
  2385. unsigned char *em;
  2386. size_t em_n;
  2387. gcry_mpi_t mpi_plain;
  2388. gcry_ac_data_t data_encrypted;
  2389. gcry_mpi_t mpi_encrypted;
  2390. unsigned char *buffer;
  2391. size_t buffer_n;
  2392. void *opts_em;
  2393. ac_scheme_t *scheme;
  2394. (void)flags;
  2395. if (fips_mode ())
  2396. return gpg_error (GPG_ERR_NOT_SUPPORTED);
  2397. data_encrypted = NULL;
  2398. mpi_encrypted = NULL;
  2399. mpi_plain = NULL;
  2400. opts_em = NULL;
  2401. buffer = NULL;
  2402. em = NULL;
  2403. scheme = ac_scheme_get (scheme_id);
  2404. if (! scheme)
  2405. {
  2406. err = gcry_error (GPG_ERR_NO_ENCRYPTION_SCHEME);
  2407. goto out;
  2408. }
  2409. if (key->type != GCRY_AC_KEY_PUBLIC)
  2410. {
  2411. err = gcry_error (GPG_ERR_WRONG_KEY_USAGE);
  2412. goto out;
  2413. }
  2414. err = ac_dencode_prepare (handle, key, opts, *scheme, &opts_em);
  2415. if (err)
  2416. goto out;
  2417. _gcry_ac_io_init (&io_em, GCRY_AC_IO_WRITABLE,
  2418. GCRY_AC_IO_STRING, &em, &em_n);
  2419. err = _gcry_ac_data_encode (scheme->scheme_encoding, 0, opts_em,
  2420. io_message, &io_em);
  2421. if (err)
  2422. goto out;
  2423. mpi_plain = gcry_mpi_snew (0);
  2424. gcry_ac_os_to_mpi (mpi_plain, em, em_n);
  2425. err = _gcry_ac_data_encrypt (handle, 0, key, mpi_plain, &data_encrypted);
  2426. if (err)
  2427. goto out;
  2428. err = ac_data_set_to_mpi (data_encrypted, &mpi_encrypted);
  2429. if (err)
  2430. goto out;
  2431. err = _gcry_ac_mpi_to_os_alloc (mpi_encrypted, &buffer, &buffer_n);
  2432. if (err)
  2433. goto out;
  2434. err = _gcry_ac_io_write (io_cipher, buffer, buffer_n);
  2435. out:
  2436. gcry_ac_data_destroy (data_encrypted);
  2437. gcry_mpi_release (mpi_encrypted);
  2438. gcry_mpi_release (mpi_plain);
  2439. gcry_free (opts_em);
  2440. gcry_free (buffer);
  2441. gcry_free (em);
  2442. return err;
  2443. }
  2444. /* Decryptes the cipher message contained in C, which is of size C_N,
  2445. with the secret key KEY_SECRET according to the Encryption Scheme
  2446. SCHEME_ID. Handle is used for accessing the low-level
  2447. cryptographic primitives. If OPTS is not NULL, it has to be an
  2448. anonymous structure specific to the chosen scheme (gcry_ac_es_*_t).
  2449. The decrypted message will be stored in M and M_N. */
  2450. gcry_error_t
  2451. _gcry_ac_data_decrypt_scheme (gcry_ac_handle_t handle,
  2452. gcry_ac_scheme_t scheme_id,
  2453. unsigned int flags, void *opts,
  2454. gcry_ac_key_t key,
  2455. gcry_ac_io_t *io_cipher,
  2456. gcry_ac_io_t *io_message)
  2457. {
  2458. gcry_ac_io_t io_em;
  2459. gcry_error_t err;
  2460. gcry_ac_data_t data_encrypted;
  2461. unsigned char *em;
  2462. size_t em_n;
  2463. gcry_mpi_t mpi_encrypted;
  2464. gcry_mpi_t mpi_decrypted;
  2465. void *opts_em;
  2466. ac_scheme_t *scheme;
  2467. char *elements_enc;
  2468. size_t elements_enc_n;
  2469. unsigned char *c;
  2470. size_t c_n;
  2471. (void)flags;
  2472. if (fips_mode ())
  2473. return gpg_error (GPG_ERR_NOT_SUPPORTED);
  2474. data_encrypted = NULL;
  2475. mpi_encrypted = NULL;
  2476. mpi_decrypted = NULL;
  2477. elements_enc = NULL;
  2478. opts_em = NULL;
  2479. em = NULL;
  2480. c = NULL;
  2481. scheme = ac_scheme_get (scheme_id);
  2482. if (! scheme)
  2483. {
  2484. err = gcry_error (GPG_ERR_NO_ENCRYPTION_SCHEME);
  2485. goto out;
  2486. }
  2487. if (key->type != GCRY_AC_KEY_SECRET)
  2488. {
  2489. err = gcry_error (GPG_ERR_WRONG_KEY_USAGE);
  2490. goto out;
  2491. }
  2492. err = _gcry_ac_io_read_all (io_cipher, &c, &c_n);
  2493. if (err)
  2494. goto out;
  2495. mpi_encrypted = gcry_mpi_snew (0);
  2496. gcry_ac_os_to_mpi (mpi_encrypted, c, c_n);
  2497. err = _gcry_pk_get_elements (handle->algorithm, &elements_enc, NULL);
  2498. if (err)
  2499. goto out;
  2500. elements_enc_n = strlen (elements_enc);
  2501. if (elements_enc_n != 1)
  2502. {
  2503. /* FIXME? */
  2504. err = gcry_error (GPG_ERR_CONFLICT);
  2505. goto out;
  2506. }
  2507. err = _gcry_ac_data_new (&data_encrypted);
  2508. if (err)
  2509. goto out;
  2510. err = _gcry_ac_data_set (data_encrypted, GCRY_AC_FLAG_COPY | GCRY_AC_FLAG_DEALLOC,
  2511. elements_enc, mpi_encrypted);
  2512. if (err)
  2513. goto out;
  2514. err = _gcry_ac_data_decrypt (handle, 0, key, &mpi_decrypted, data_encrypted);
  2515. if (err)
  2516. goto out;
  2517. err = _gcry_ac_mpi_to_os_alloc (mpi_decrypted, &em, &em_n);
  2518. if (err)
  2519. goto out;
  2520. err = ac_dencode_prepare (handle, key, opts, *scheme, &opts_em);
  2521. if (err)
  2522. goto out;
  2523. _gcry_ac_io_init (&io_em, GCRY_AC_IO_READABLE,
  2524. GCRY_AC_IO_STRING, em, em_n);
  2525. err = _gcry_ac_data_decode (scheme->scheme_encoding, 0, opts_em,
  2526. &io_em, io_message);
  2527. if (err)
  2528. goto out;
  2529. out:
  2530. _gcry_ac_data_destroy (data_encrypted);
  2531. gcry_mpi_release (mpi_encrypted);
  2532. gcry_mpi_release (mpi_decrypted);
  2533. free (elements_enc);
  2534. gcry_free (opts_em);
  2535. gcry_free (em);
  2536. gcry_free (c);
  2537. return err;
  2538. }
  2539. /* Signs the message contained in M, which is of size M_N, with the
  2540. secret key KEY according to the Signature Scheme SCHEME_ID. Handle
  2541. is used for accessing the low-level cryptographic primitives. If
  2542. OPTS is not NULL, it has to be an anonymous structure specific to
  2543. the chosen scheme (gcry_ac_ssa_*_t). The signed message will be
  2544. stored in S and S_N. */
  2545. gcry_error_t
  2546. _gcry_ac_data_sign_scheme (gcry_ac_handle_t handle,
  2547. gcry_ac_scheme_t scheme_id,
  2548. unsigned int flags, void *opts,
  2549. gcry_ac_key_t key,
  2550. gcry_ac_io_t *io_message,
  2551. gcry_ac_io_t *io_signature)
  2552. {
  2553. gcry_ac_io_t io_em;
  2554. gcry_error_t err;
  2555. gcry_ac_data_t data_signed;
  2556. unsigned char *em;
  2557. size_t em_n;
  2558. gcry_mpi_t mpi;
  2559. void *opts_em;
  2560. unsigned char *buffer;
  2561. size_t buffer_n;
  2562. gcry_mpi_t mpi_signed;
  2563. ac_scheme_t *scheme;
  2564. (void)flags;
  2565. if (fips_mode ())
  2566. return gpg_error (GPG_ERR_NOT_SUPPORTED);
  2567. data_signed = NULL;
  2568. mpi_signed = NULL;
  2569. opts_em = NULL;
  2570. buffer = NULL;
  2571. mpi = NULL;
  2572. em = NULL;
  2573. if (key->type != GCRY_AC_KEY_SECRET)
  2574. {
  2575. err = gcry_error (GPG_ERR_WRONG_KEY_USAGE);
  2576. goto out;
  2577. }
  2578. scheme = ac_scheme_get (scheme_id);
  2579. if (! scheme)
  2580. {
  2581. /* FIXME: adjust api of scheme_get in respect to err codes. */
  2582. err = gcry_error (GPG_ERR_NO_SIGNATURE_SCHEME);
  2583. goto out;
  2584. }
  2585. err = ac_dencode_prepare (handle, key, opts, *scheme, &opts_em);
  2586. if (err)
  2587. goto out;
  2588. _gcry_ac_io_init (&io_em, GCRY_AC_IO_WRITABLE,
  2589. GCRY_AC_IO_STRING, &em, &em_n);
  2590. err = _gcry_ac_data_encode (scheme->scheme_encoding, 0, opts_em,
  2591. io_message, &io_em);
  2592. if (err)
  2593. goto out;
  2594. mpi = gcry_mpi_new (0);
  2595. _gcry_ac_os_to_mpi (mpi, em, em_n);
  2596. err = _gcry_ac_data_sign (handle, key, mpi, &data_signed);
  2597. if (err)
  2598. goto out;
  2599. err = ac_data_set_to_mpi (data_signed, &mpi_signed);
  2600. if (err)
  2601. goto out;
  2602. err = _gcry_ac_mpi_to_os_alloc (mpi_signed, &buffer, &buffer_n);
  2603. if (err)
  2604. goto out;
  2605. err = _gcry_ac_io_write (io_signature, buffer, buffer_n);
  2606. out:
  2607. _gcry_ac_data_destroy (data_signed);
  2608. gcry_mpi_release (mpi_signed);
  2609. gcry_mpi_release (mpi);
  2610. gcry_free (opts_em);
  2611. gcry_free (buffer);
  2612. gcry_free (em);
  2613. return err;
  2614. }
  2615. /* Verifies that the signature contained in S, which is of length S_N,
  2616. is indeed the result of signing the message contained in M, which
  2617. is of size M_N, with the secret key belonging to the public key
  2618. KEY_PUBLIC. If OPTS is not NULL, it has to be an anonymous
  2619. structure (gcry_ac_ssa_*_t) specific to the Signature Scheme, whose
  2620. ID is contained in SCHEME_ID. */
  2621. gcry_error_t
  2622. _gcry_ac_data_verify_scheme (gcry_ac_handle_t handle,
  2623. gcry_ac_scheme_t scheme_id,
  2624. unsigned int flags, void *opts,
  2625. gcry_ac_key_t key,
  2626. gcry_ac_io_t *io_message,
  2627. gcry_ac_io_t *io_signature)
  2628. {
  2629. gcry_ac_io_t io_em;
  2630. gcry_error_t err;
  2631. gcry_ac_data_t data_signed;
  2632. unsigned char *em;
  2633. size_t em_n;
  2634. void *opts_em;
  2635. gcry_mpi_t mpi_signature;
  2636. gcry_mpi_t mpi_data;
  2637. ac_scheme_t *scheme;
  2638. char *elements_sig;
  2639. size_t elements_sig_n;
  2640. unsigned char *s;
  2641. size_t s_n;
  2642. (void)flags;
  2643. if (fips_mode ())
  2644. return gpg_error (GPG_ERR_NOT_SUPPORTED);
  2645. mpi_signature = NULL;
  2646. elements_sig = NULL;
  2647. data_signed = NULL;
  2648. mpi_data = NULL;
  2649. opts_em = NULL;
  2650. em = NULL;
  2651. s = NULL;
  2652. if (key->type != GCRY_AC_KEY_PUBLIC)
  2653. {
  2654. err = gcry_error (GPG_ERR_WRONG_KEY_USAGE);
  2655. goto out;
  2656. }
  2657. scheme = ac_scheme_get (scheme_id);
  2658. if (! scheme)
  2659. {
  2660. err = gcry_error (GPG_ERR_NO_SIGNATURE_SCHEME);
  2661. goto out;
  2662. }
  2663. err = ac_dencode_prepare (handle, key, opts, *scheme, &opts_em);
  2664. if (err)
  2665. goto out;
  2666. _gcry_ac_io_init (&io_em, GCRY_AC_IO_WRITABLE,
  2667. GCRY_AC_IO_STRING, &em, &em_n);
  2668. err = _gcry_ac_data_encode (scheme->scheme_encoding, 0, opts_em,
  2669. io_message, &io_em);
  2670. if (err)
  2671. goto out;
  2672. mpi_data = gcry_mpi_new (0);
  2673. _gcry_ac_os_to_mpi (mpi_data, em, em_n);
  2674. err = _gcry_ac_io_read_all (io_signature, &s, &s_n);
  2675. if (err)
  2676. goto out;
  2677. mpi_signature = gcry_mpi_new (0);
  2678. _gcry_ac_os_to_mpi (mpi_signature, s, s_n);
  2679. err = _gcry_pk_get_elements (handle->algorithm, NULL, &elements_sig);
  2680. if (err)
  2681. goto out;
  2682. elements_sig_n = strlen (elements_sig);
  2683. if (elements_sig_n != 1)
  2684. {
  2685. /* FIXME? */
  2686. err = gcry_error (GPG_ERR_CONFLICT);
  2687. goto out;
  2688. }
  2689. err = _gcry_ac_data_new (&data_signed);
  2690. if (err)
  2691. goto out;
  2692. err = _gcry_ac_data_set (data_signed, GCRY_AC_FLAG_COPY | GCRY_AC_FLAG_DEALLOC,
  2693. elements_sig, mpi_signature);
  2694. if (err)
  2695. goto out;
  2696. gcry_mpi_release (mpi_signature);
  2697. mpi_signature = NULL;
  2698. err = _gcry_ac_data_verify (handle, key, mpi_data, data_signed);
  2699. out:
  2700. _gcry_ac_data_destroy (data_signed);
  2701. gcry_mpi_release (mpi_signature);
  2702. gcry_mpi_release (mpi_data);
  2703. free (elements_sig);
  2704. gcry_free (opts_em);
  2705. gcry_free (em);
  2706. gcry_free (s);
  2707. return err;
  2708. }
  2709. /*
  2710. * General functions.
  2711. */
  2712. gcry_err_code_t
  2713. _gcry_ac_init (void)
  2714. {
  2715. if (fips_mode ())
  2716. return GPG_ERR_NOT_SUPPORTED;
  2717. return 0;
  2718. }