ssh-pkcs11.c 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244
  1. /* $OpenBSD: ssh-pkcs11.c,v 1.50 2020/05/29 03:14:02 djm Exp $ */
  2. /*
  3. * Copyright (c) 2010 Markus Friedl. All rights reserved.
  4. * Copyright (c) 2014 Pedro Martelletto. All rights reserved.
  5. *
  6. * Permission to use, copy, modify, and distribute this software for any
  7. * purpose with or without fee is hereby granted, provided that the above
  8. * copyright notice and this permission notice appear in all copies.
  9. *
  10. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  11. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  12. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  13. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  14. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  15. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  16. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  17. */
  18. #include "includes.h"
  19. #ifdef ENABLE_PKCS11
  20. #ifdef HAVE_SYS_TIME_H
  21. # include <sys/time.h>
  22. #endif
  23. #include <sys/types.h>
  24. #include <stdarg.h>
  25. #include <stdio.h>
  26. #include <ctype.h>
  27. #include <string.h>
  28. #include <dlfcn.h>
  29. #include "openbsd-compat/sys-queue.h"
  30. #include "openbsd-compat/openssl-compat.h"
  31. #include <openssl/ecdsa.h>
  32. #include <openssl/x509.h>
  33. #include <openssl/err.h>
  34. #define CRYPTOKI_COMPAT
  35. #include "pkcs11.h"
  36. #include "log.h"
  37. #include "misc.h"
  38. #include "sshkey.h"
  39. #include "ssh-pkcs11.h"
  40. #include "digest.h"
  41. #include "xmalloc.h"
  42. struct pkcs11_slotinfo {
  43. CK_TOKEN_INFO token;
  44. CK_SESSION_HANDLE session;
  45. int logged_in;
  46. };
  47. struct pkcs11_module {
  48. char *module_path;
  49. void *handle;
  50. CK_FUNCTION_LIST *function_list;
  51. CK_INFO info;
  52. CK_ULONG nslots;
  53. CK_SLOT_ID *slotlist;
  54. struct pkcs11_slotinfo *slotinfo;
  55. int valid;
  56. int refcount;
  57. };
  58. struct pkcs11_provider {
  59. char *name;
  60. struct pkcs11_module *module; /* can be shared between various providers */
  61. int refcount;
  62. int valid;
  63. TAILQ_ENTRY(pkcs11_provider) next;
  64. };
  65. TAILQ_HEAD(, pkcs11_provider) pkcs11_providers;
  66. struct pkcs11_key {
  67. struct pkcs11_provider *provider;
  68. CK_ULONG slotidx;
  69. char *keyid;
  70. int keyid_len;
  71. char *label;
  72. };
  73. int pkcs11_interactive = 0;
  74. #ifdef HAVE_EC_KEY_METHOD_NEW
  75. static void
  76. ossl_error(const char *msg)
  77. {
  78. unsigned long e;
  79. error("%s: %s", __func__, msg);
  80. while ((e = ERR_get_error()) != 0)
  81. error("%s: libcrypto error: %.100s", __func__,
  82. ERR_error_string(e, NULL));
  83. }
  84. #endif /* HAVE_EC_KEY_METHOD_NEW */
  85. int
  86. pkcs11_init(int interactive)
  87. {
  88. pkcs11_interactive = interactive;
  89. TAILQ_INIT(&pkcs11_providers);
  90. return (0);
  91. }
  92. /*
  93. * finalize a provider shared library, it's no longer usable.
  94. * however, there might still be keys referencing this provider,
  95. * so the actual freeing of memory is handled by pkcs11_provider_unref().
  96. * this is called when a provider gets unregistered.
  97. */
  98. static void
  99. pkcs11_module_finalize(struct pkcs11_module *m)
  100. {
  101. CK_RV rv;
  102. CK_ULONG i;
  103. debug("%s: %p refcount %d valid %d", __func__,
  104. m, m->refcount, m->valid);
  105. if (!m->valid)
  106. return;
  107. for (i = 0; i < m->nslots; i++) {
  108. if (m->slotinfo[i].session &&
  109. (rv = m->function_list->C_CloseSession(
  110. m->slotinfo[i].session)) != CKR_OK)
  111. error("C_CloseSession failed: %lu", rv);
  112. }
  113. if ((rv = m->function_list->C_Finalize(NULL)) != CKR_OK)
  114. error("C_Finalize failed: %lu", rv);
  115. m->valid = 0;
  116. m->function_list = NULL;
  117. dlclose(m->handle);
  118. }
  119. /*
  120. * remove a reference to the pkcs11 module.
  121. * called when a provider is unregistered.
  122. */
  123. static void
  124. pkcs11_module_unref(struct pkcs11_module *m)
  125. {
  126. debug("%s: %p refcount %d", __func__, m, m->refcount);
  127. if (--m->refcount <= 0) {
  128. pkcs11_module_finalize(m);
  129. if (m->valid)
  130. error("%s: %p still valid", __func__, m);
  131. free(m->slotlist);
  132. free(m->slotinfo);
  133. free(m->module_path);
  134. free(m);
  135. }
  136. }
  137. /*
  138. * finalize a provider shared libarary, it's no longer usable.
  139. * however, there might still be keys referencing this provider,
  140. * so the actuall freeing of memory is handled by pkcs11_provider_unref().
  141. * this is called when a provider gets unregistered.
  142. */
  143. static void
  144. pkcs11_provider_finalize(struct pkcs11_provider *p)
  145. {
  146. debug("%s: %p refcount %d valid %d", __func__,
  147. p, p->refcount, p->valid);
  148. if (!p->valid)
  149. return;
  150. pkcs11_module_unref(p->module);
  151. p->module = NULL;
  152. p->valid = 0;
  153. }
  154. /*
  155. * remove a reference to the provider.
  156. * called when a key gets destroyed or when the provider is unregistered.
  157. */
  158. static void
  159. pkcs11_provider_unref(struct pkcs11_provider *p)
  160. {
  161. debug("%s: %p refcount %d", __func__, p, p->refcount);
  162. if (--p->refcount <= 0) {
  163. free(p->name);
  164. if (p->module)
  165. pkcs11_module_unref(p->module);
  166. free(p);
  167. }
  168. }
  169. /* unregister all providers, keys might still point to the providers */
  170. void
  171. pkcs11_terminate(void)
  172. {
  173. struct pkcs11_provider *p;
  174. while ((p = TAILQ_FIRST(&pkcs11_providers)) != NULL) {
  175. TAILQ_REMOVE(&pkcs11_providers, p, next);
  176. pkcs11_provider_finalize(p);
  177. pkcs11_provider_unref(p);
  178. }
  179. }
  180. /* lookup provider by module path */
  181. static struct pkcs11_module *
  182. pkcs11_provider_lookup_module(char *module_path)
  183. {
  184. struct pkcs11_provider *p;
  185. TAILQ_FOREACH(p, &pkcs11_providers, next) {
  186. debug("check %p %s (%s)", p, p->name, p->module->module_path);
  187. if (!strcmp(module_path, p->module->module_path))
  188. return (p->module);
  189. }
  190. return (NULL);
  191. }
  192. /* lookup provider by name */
  193. static struct pkcs11_provider *
  194. pkcs11_provider_lookup(char *provider_id)
  195. {
  196. struct pkcs11_provider *p;
  197. TAILQ_FOREACH(p, &pkcs11_providers, next) {
  198. debug("check %p %s", p, p->name);
  199. if (!strcmp(provider_id, p->name))
  200. return (p);
  201. }
  202. return (NULL);
  203. }
  204. int pkcs11_del_provider_by_uri(struct pkcs11_uri *);
  205. /* unregister provider by name */
  206. int
  207. pkcs11_del_provider(char *provider_id)
  208. {
  209. int rv;
  210. struct pkcs11_uri *uri;
  211. debug("%s: called, provider_id = %s", __func__, provider_id);
  212. uri = pkcs11_uri_init();
  213. if (uri == NULL)
  214. fatal("Failed to init PKCS#11 URI");
  215. if (strlen(provider_id) >= strlen(PKCS11_URI_SCHEME) &&
  216. strncmp(provider_id, PKCS11_URI_SCHEME, strlen(PKCS11_URI_SCHEME)) == 0) {
  217. if (pkcs11_uri_parse(provider_id, uri) != 0)
  218. fatal("Failed to parse PKCS#11 URI");
  219. } else {
  220. uri->module_path = strdup(provider_id);
  221. }
  222. rv = pkcs11_del_provider_by_uri(uri);
  223. pkcs11_uri_cleanup(uri);
  224. return rv;
  225. }
  226. /* unregister provider by PKCS#11 URI */
  227. int
  228. pkcs11_del_provider_by_uri(struct pkcs11_uri *uri)
  229. {
  230. struct pkcs11_provider *p;
  231. int rv = -1;
  232. char *provider_uri = pkcs11_uri_get(uri);
  233. debug3("%s(%s): called", __func__, provider_uri);
  234. if ((p = pkcs11_provider_lookup(provider_uri)) != NULL) {
  235. TAILQ_REMOVE(&pkcs11_providers, p, next);
  236. pkcs11_provider_finalize(p);
  237. pkcs11_provider_unref(p);
  238. rv = 0;
  239. }
  240. free(provider_uri);
  241. return rv;
  242. }
  243. static RSA_METHOD *rsa_method;
  244. static int rsa_idx = 0;
  245. #ifdef HAVE_EC_KEY_METHOD_NEW
  246. static EC_KEY_METHOD *ec_key_method;
  247. static int ec_key_idx = 0;
  248. #endif
  249. /*
  250. * This can't be in the ssh-pkcs11-uri, becase we can not depend on
  251. * PKCS#11 structures in ssh-agent (using client-helper communication)
  252. */
  253. int
  254. pkcs11_uri_write(const struct sshkey *key, FILE *f)
  255. {
  256. char *p = NULL;
  257. struct pkcs11_uri uri;
  258. struct pkcs11_key *k11;
  259. /* sanity - is it a RSA key with associated app_data? */
  260. switch (key->type) {
  261. case KEY_RSA:
  262. k11 = RSA_get_ex_data(key->rsa, rsa_idx);
  263. break;
  264. #ifdef HAVE_EC_KEY_METHOD_NEW
  265. case KEY_ECDSA:
  266. k11 = EC_KEY_get_ex_data(key->ecdsa, ec_key_idx);
  267. break;
  268. #endif
  269. default:
  270. error("Unknown key type %d", key->type);
  271. return -1;
  272. }
  273. if (k11 == NULL) {
  274. error("Failed to get ex_data for key type %d", key->type);
  275. return (-1);
  276. }
  277. /* omit type -- we are looking for private-public or private-certificate pairs */
  278. uri.id = k11->keyid;
  279. uri.id_len = k11->keyid_len;
  280. uri.token = k11->provider->module->slotinfo[k11->slotidx].token.label;
  281. uri.object = k11->label;
  282. uri.module_path = k11->provider->module->module_path;
  283. uri.lib_manuf = k11->provider->module->info.manufacturerID;
  284. uri.manuf = k11->provider->module->slotinfo[k11->slotidx].token.manufacturerID;
  285. p = pkcs11_uri_get(&uri);
  286. /* do not cleanup -- we do not allocate here, only reference */
  287. if (p == NULL)
  288. return -1;
  289. fprintf(f, " %s", p);
  290. free(p);
  291. return 0;
  292. }
  293. /* release a wrapped object */
  294. static void
  295. pkcs11_k11_free(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx,
  296. long argl, void *argp)
  297. {
  298. struct pkcs11_key *k11 = ptr;
  299. debug("%s: parent %p ptr %p idx %d", __func__, parent, ptr, idx);
  300. if (k11 == NULL)
  301. return;
  302. if (k11->provider)
  303. pkcs11_provider_unref(k11->provider);
  304. free(k11->keyid);
  305. free(k11->label);
  306. free(k11);
  307. }
  308. /* find a single 'obj' for given attributes */
  309. static int
  310. pkcs11_find(struct pkcs11_provider *p, CK_ULONG slotidx, CK_ATTRIBUTE *attr,
  311. CK_ULONG nattr, CK_OBJECT_HANDLE *obj)
  312. {
  313. CK_FUNCTION_LIST *f;
  314. CK_SESSION_HANDLE session;
  315. CK_ULONG nfound = 0;
  316. CK_RV rv;
  317. int ret = -1;
  318. f = p->module->function_list;
  319. session = p->module->slotinfo[slotidx].session;
  320. if ((rv = f->C_FindObjectsInit(session, attr, nattr)) != CKR_OK) {
  321. error("C_FindObjectsInit failed (nattr %lu): %lu", nattr, rv);
  322. return (-1);
  323. }
  324. if ((rv = f->C_FindObjects(session, obj, 1, &nfound)) != CKR_OK ||
  325. nfound != 1) {
  326. debug("C_FindObjects failed (nfound %lu nattr %lu): %lu",
  327. nfound, nattr, rv);
  328. } else
  329. ret = 0;
  330. if ((rv = f->C_FindObjectsFinal(session)) != CKR_OK)
  331. error("C_FindObjectsFinal failed: %lu", rv);
  332. return (ret);
  333. }
  334. static int
  335. pkcs11_login_slot(struct pkcs11_provider *provider, struct pkcs11_slotinfo *si,
  336. CK_USER_TYPE type)
  337. {
  338. char *pin = NULL, prompt[1024];
  339. CK_RV rv;
  340. if (provider == NULL || si == NULL || !provider->valid) {
  341. error("no pkcs11 (valid) provider found");
  342. return (-1);
  343. }
  344. if (!pkcs11_interactive) {
  345. error("need pin entry%s",
  346. (si->token.flags & CKF_PROTECTED_AUTHENTICATION_PATH) ?
  347. " on reader keypad" : "");
  348. return (-1);
  349. }
  350. if (si->token.flags & CKF_PROTECTED_AUTHENTICATION_PATH)
  351. verbose("Deferring PIN entry to reader keypad.");
  352. else {
  353. snprintf(prompt, sizeof(prompt), "Enter PIN for '%s': ",
  354. si->token.label);
  355. if ((pin = read_passphrase(prompt, RP_ALLOW_EOF|RP_ALLOW_STDIN)) == NULL) {
  356. debug("%s: no pin specified", __func__);
  357. return (-1); /* bail out */
  358. }
  359. }
  360. rv = provider->module->function_list->C_Login(si->session, type, (u_char *)pin,
  361. (pin != NULL) ? strlen(pin) : 0);
  362. if (pin != NULL)
  363. freezero(pin, strlen(pin));
  364. switch (rv) {
  365. case CKR_OK:
  366. case CKR_USER_ALREADY_LOGGED_IN:
  367. /* success */
  368. break;
  369. case CKR_PIN_LEN_RANGE:
  370. error("PKCS#11 login failed: PIN length out of range");
  371. return -1;
  372. case CKR_PIN_INCORRECT:
  373. error("PKCS#11 login failed: PIN incorrect");
  374. return -1;
  375. case CKR_PIN_LOCKED:
  376. error("PKCS#11 login failed: PIN locked");
  377. return -1;
  378. default:
  379. error("PKCS#11 login failed: error %lu", rv);
  380. return -1;
  381. }
  382. si->logged_in = 1;
  383. return (0);
  384. }
  385. static int
  386. pkcs11_login(struct pkcs11_key *k11, CK_USER_TYPE type)
  387. {
  388. if (k11 == NULL || k11->provider == NULL || !k11->provider->valid ||
  389. k11->provider->module == NULL || !k11->provider->module->valid) {
  390. error("no pkcs11 (valid) provider found");
  391. return (-1);
  392. }
  393. return pkcs11_login_slot(k11->provider,
  394. &k11->provider->module->slotinfo[k11->slotidx], type);
  395. }
  396. static int
  397. pkcs11_check_obj_bool_attrib(struct pkcs11_key *k11, CK_OBJECT_HANDLE obj,
  398. CK_ATTRIBUTE_TYPE type, int *val)
  399. {
  400. struct pkcs11_slotinfo *si;
  401. CK_FUNCTION_LIST *f;
  402. CK_BBOOL flag = 0;
  403. CK_ATTRIBUTE attr;
  404. CK_RV rv;
  405. *val = 0;
  406. if (!k11->provider || !k11->provider->valid ||
  407. !k11->provider->module || !k11->provider->module->valid) {
  408. error("no pkcs11 (valid) provider found");
  409. return (-1);
  410. }
  411. f = k11->provider->module->function_list;
  412. si = &k11->provider->module->slotinfo[k11->slotidx];
  413. attr.type = type;
  414. attr.pValue = &flag;
  415. attr.ulValueLen = sizeof(flag);
  416. rv = f->C_GetAttributeValue(si->session, obj, &attr, 1);
  417. if (rv != CKR_OK) {
  418. error("C_GetAttributeValue failed: %lu", rv);
  419. return (-1);
  420. }
  421. *val = flag != 0;
  422. debug("%s: provider %p slot %lu object %lu: attrib %lu = %d",
  423. __func__, k11->provider, k11->slotidx, obj, type, *val);
  424. return (0);
  425. }
  426. static int
  427. pkcs11_get_key(struct pkcs11_key *k11, CK_MECHANISM_TYPE mech_type)
  428. {
  429. struct pkcs11_slotinfo *si;
  430. CK_FUNCTION_LIST *f;
  431. CK_OBJECT_HANDLE obj;
  432. CK_RV rv;
  433. CK_OBJECT_CLASS private_key_class;
  434. CK_BBOOL true_val;
  435. CK_MECHANISM mech;
  436. CK_ATTRIBUTE key_filter[3];
  437. int always_auth = 0;
  438. int did_login = 0;
  439. if (!k11->provider || !k11->provider->valid ||
  440. !k11->provider->module || !k11->provider->module->valid) {
  441. error("no pkcs11 (valid) provider found");
  442. return (-1);
  443. }
  444. f = k11->provider->module->function_list;
  445. si = &k11->provider->module->slotinfo[k11->slotidx];
  446. if ((si->token.flags & CKF_LOGIN_REQUIRED) && !si->logged_in) {
  447. if (pkcs11_login(k11, CKU_USER) < 0) {
  448. error("login failed");
  449. return (-1);
  450. }
  451. did_login = 1;
  452. }
  453. memset(&key_filter, 0, sizeof(key_filter));
  454. private_key_class = CKO_PRIVATE_KEY;
  455. key_filter[0].type = CKA_CLASS;
  456. key_filter[0].pValue = &private_key_class;
  457. key_filter[0].ulValueLen = sizeof(private_key_class);
  458. key_filter[1].type = CKA_ID;
  459. key_filter[1].pValue = k11->keyid;
  460. key_filter[1].ulValueLen = k11->keyid_len;
  461. true_val = CK_TRUE;
  462. key_filter[2].type = CKA_SIGN;
  463. key_filter[2].pValue = &true_val;
  464. key_filter[2].ulValueLen = sizeof(true_val);
  465. /* try to find object w/CKA_SIGN first, retry w/o */
  466. if (pkcs11_find(k11->provider, k11->slotidx, key_filter, 3, &obj) < 0 &&
  467. pkcs11_find(k11->provider, k11->slotidx, key_filter, 2, &obj) < 0) {
  468. error("cannot find private key");
  469. return (-1);
  470. }
  471. memset(&mech, 0, sizeof(mech));
  472. mech.mechanism = mech_type;
  473. mech.pParameter = NULL_PTR;
  474. mech.ulParameterLen = 0;
  475. if ((rv = f->C_SignInit(si->session, &mech, obj)) != CKR_OK) {
  476. error("C_SignInit failed: %lu", rv);
  477. return (-1);
  478. }
  479. pkcs11_check_obj_bool_attrib(k11, obj, CKA_ALWAYS_AUTHENTICATE,
  480. &always_auth); /* ignore errors here */
  481. if (always_auth && !did_login) {
  482. debug("%s: always-auth key", __func__);
  483. if (pkcs11_login(k11, CKU_CONTEXT_SPECIFIC) < 0) {
  484. error("login failed for always-auth key");
  485. return (-1);
  486. }
  487. }
  488. return (0);
  489. }
  490. /* openssl callback doing the actual signing operation */
  491. static int
  492. pkcs11_rsa_private_encrypt(int flen, const u_char *from, u_char *to, RSA *rsa,
  493. int padding)
  494. {
  495. struct pkcs11_key *k11;
  496. struct pkcs11_slotinfo *si;
  497. CK_FUNCTION_LIST *f;
  498. CK_ULONG tlen = 0;
  499. CK_RV rv;
  500. int rval = -1;
  501. if ((k11 = RSA_get_ex_data(rsa, rsa_idx)) == NULL) {
  502. error("RSA_get_ex_data failed for rsa %p", rsa);
  503. return (-1);
  504. }
  505. if (pkcs11_get_key(k11, CKM_RSA_PKCS) == -1) {
  506. error("pkcs11_get_key failed");
  507. return (-1);
  508. }
  509. f = k11->provider->module->function_list;
  510. si = &k11->provider->module->slotinfo[k11->slotidx];
  511. tlen = RSA_size(rsa);
  512. /* XXX handle CKR_BUFFER_TOO_SMALL */
  513. rv = f->C_Sign(si->session, (CK_BYTE *)from, flen, to, &tlen);
  514. if (rv == CKR_OK)
  515. rval = tlen;
  516. else
  517. error("C_Sign failed: %lu", rv);
  518. return (rval);
  519. }
  520. static int
  521. pkcs11_rsa_private_decrypt(int flen, const u_char *from, u_char *to, RSA *rsa,
  522. int padding)
  523. {
  524. return (-1);
  525. }
  526. static int
  527. pkcs11_rsa_start_wrapper(void)
  528. {
  529. if (rsa_method != NULL)
  530. return (0);
  531. rsa_method = RSA_meth_dup(RSA_get_default_method());
  532. if (rsa_method == NULL)
  533. return (-1);
  534. rsa_idx = RSA_get_ex_new_index(0, "ssh-pkcs11-rsa",
  535. NULL, NULL, pkcs11_k11_free);
  536. if (rsa_idx == -1)
  537. return (-1);
  538. if (!RSA_meth_set1_name(rsa_method, "pkcs11") ||
  539. !RSA_meth_set_priv_enc(rsa_method, pkcs11_rsa_private_encrypt) ||
  540. !RSA_meth_set_priv_dec(rsa_method, pkcs11_rsa_private_decrypt)) {
  541. error("%s: setup pkcs11 method failed", __func__);
  542. return (-1);
  543. }
  544. return (0);
  545. }
  546. /* redirect private key operations for rsa key to pkcs11 token */
  547. static int
  548. pkcs11_rsa_wrap(struct pkcs11_provider *provider, CK_ULONG slotidx,
  549. CK_ATTRIBUTE *keyid_attrib, CK_ATTRIBUTE *label_attrib, RSA *rsa)
  550. {
  551. struct pkcs11_key *k11;
  552. if (pkcs11_rsa_start_wrapper() == -1)
  553. return (-1);
  554. k11 = xcalloc(1, sizeof(*k11));
  555. k11->provider = provider;
  556. provider->refcount++; /* provider referenced by RSA key */
  557. k11->slotidx = slotidx;
  558. /* identify key object on smartcard */
  559. k11->keyid_len = keyid_attrib->ulValueLen;
  560. if (k11->keyid_len > 0) {
  561. k11->keyid = xmalloc(k11->keyid_len);
  562. memcpy(k11->keyid, keyid_attrib->pValue, k11->keyid_len);
  563. }
  564. if (label_attrib->ulValueLen > 0 ) {
  565. k11->label = xmalloc(label_attrib->ulValueLen+1);
  566. memcpy(k11->label, label_attrib->pValue, label_attrib->ulValueLen);
  567. k11->label[label_attrib->ulValueLen] = 0;
  568. }
  569. RSA_set_method(rsa, rsa_method);
  570. RSA_set_ex_data(rsa, rsa_idx, k11);
  571. return (0);
  572. }
  573. #ifdef HAVE_EC_KEY_METHOD_NEW
  574. /* openssl callback doing the actual signing operation */
  575. static ECDSA_SIG *
  576. ecdsa_do_sign(const unsigned char *dgst, int dgst_len, const BIGNUM *inv,
  577. const BIGNUM *rp, EC_KEY *ec)
  578. {
  579. struct pkcs11_key *k11;
  580. struct pkcs11_slotinfo *si;
  581. CK_FUNCTION_LIST *f;
  582. CK_ULONG siglen = 0, bnlen;
  583. CK_RV rv;
  584. ECDSA_SIG *ret = NULL;
  585. u_char *sig;
  586. BIGNUM *r = NULL, *s = NULL;
  587. if ((k11 = EC_KEY_get_ex_data(ec, ec_key_idx)) == NULL) {
  588. ossl_error("EC_KEY_get_key_method_data failed for ec");
  589. return (NULL);
  590. }
  591. if (pkcs11_get_key(k11, CKM_ECDSA) == -1) {
  592. error("pkcs11_get_key failed");
  593. return (NULL);
  594. }
  595. f = k11->provider->module->function_list;
  596. si = &k11->provider->module->slotinfo[k11->slotidx];
  597. siglen = ECDSA_size(ec);
  598. sig = xmalloc(siglen);
  599. /* XXX handle CKR_BUFFER_TOO_SMALL */
  600. rv = f->C_Sign(si->session, (CK_BYTE *)dgst, dgst_len, sig, &siglen);
  601. if (rv != CKR_OK) {
  602. error("C_Sign failed: %lu", rv);
  603. goto done;
  604. }
  605. if (siglen < 64 || siglen > 132 || siglen % 2) {
  606. ossl_error("d2i_ECDSA_SIG failed");
  607. goto done;
  608. }
  609. bnlen = siglen/2;
  610. if ((ret = ECDSA_SIG_new()) == NULL) {
  611. error("ECDSA_SIG_new failed");
  612. goto done;
  613. }
  614. if ((r = BN_bin2bn(sig, bnlen, NULL)) == NULL ||
  615. (s = BN_bin2bn(sig+bnlen, bnlen, NULL)) == NULL) {
  616. ossl_error("d2i_ECDSA_SIG failed");
  617. ECDSA_SIG_free(ret);
  618. ret = NULL;
  619. goto done;
  620. }
  621. if (!ECDSA_SIG_set0(ret, r, s)) {
  622. error("%s: ECDSA_SIG_set0 failed", __func__);
  623. ECDSA_SIG_free(ret);
  624. ret = NULL;
  625. goto done;
  626. }
  627. r = s = NULL; /* now owned by ret */
  628. /* success */
  629. done:
  630. BN_free(r);
  631. BN_free(s);
  632. free(sig);
  633. return (ret);
  634. }
  635. static int
  636. pkcs11_ecdsa_start_wrapper(void)
  637. {
  638. int (*orig_sign)(int, const unsigned char *, int, unsigned char *,
  639. unsigned int *, const BIGNUM *, const BIGNUM *, EC_KEY *) = NULL;
  640. if (ec_key_method != NULL)
  641. return (0);
  642. ec_key_idx = EC_KEY_get_ex_new_index(0, "ssh-pkcs11-ecdsa",
  643. NULL, NULL, pkcs11_k11_free);
  644. if (ec_key_idx == -1)
  645. return (-1);
  646. ec_key_method = EC_KEY_METHOD_new(EC_KEY_OpenSSL());
  647. if (ec_key_method == NULL)
  648. return (-1);
  649. EC_KEY_METHOD_get_sign(ec_key_method, &orig_sign, NULL, NULL);
  650. EC_KEY_METHOD_set_sign(ec_key_method, orig_sign, NULL, ecdsa_do_sign);
  651. return (0);
  652. }
  653. static int
  654. pkcs11_ecdsa_wrap(struct pkcs11_provider *provider, CK_ULONG slotidx,
  655. CK_ATTRIBUTE *keyid_attrib, CK_ATTRIBUTE *label_attrib, EC_KEY *ec)
  656. {
  657. struct pkcs11_key *k11;
  658. if (pkcs11_ecdsa_start_wrapper() == -1)
  659. return (-1);
  660. k11 = xcalloc(1, sizeof(*k11));
  661. k11->provider = provider;
  662. provider->refcount++; /* provider referenced by ECDSA key */
  663. k11->slotidx = slotidx;
  664. /* identify key object on smartcard */
  665. k11->keyid_len = keyid_attrib->ulValueLen;
  666. k11->keyid = xmalloc(k11->keyid_len);
  667. memcpy(k11->keyid, keyid_attrib->pValue, k11->keyid_len);
  668. if (label_attrib->ulValueLen > 0 ) {
  669. k11->label = xmalloc(label_attrib->ulValueLen+1);
  670. memcpy(k11->label, label_attrib->pValue, label_attrib->ulValueLen);
  671. k11->label[label_attrib->ulValueLen] = 0;
  672. }
  673. EC_KEY_set_method(ec, ec_key_method);
  674. EC_KEY_set_ex_data(ec, ec_key_idx, k11);
  675. return (0);
  676. }
  677. #endif /* HAVE_EC_KEY_METHOD_NEW */
  678. /* remove trailing spaces */
  679. static void
  680. rmspace(u_char *buf, size_t len)
  681. {
  682. size_t i;
  683. if (!len)
  684. return;
  685. for (i = len - 1; i > 0; i--)
  686. if (i == len - 1 || buf[i] == ' ')
  687. buf[i] = '\0';
  688. else
  689. break;
  690. }
  691. /*
  692. * open a pkcs11 session and login if required.
  693. * if pin == NULL we delay login until key use
  694. */
  695. static int
  696. pkcs11_open_session(struct pkcs11_provider *p, CK_ULONG slotidx, char *pin,
  697. CK_ULONG user)
  698. {
  699. struct pkcs11_slotinfo *si;
  700. CK_FUNCTION_LIST *f;
  701. CK_RV rv;
  702. CK_SESSION_HANDLE session;
  703. int login_required, ret;
  704. f = p->module->function_list;
  705. si = &p->module->slotinfo[slotidx];
  706. login_required = si->token.flags & CKF_LOGIN_REQUIRED;
  707. /* fail early before opening session */
  708. if (login_required && !pkcs11_interactive &&
  709. (pin == NULL || strlen(pin) == 0)) {
  710. error("pin required");
  711. return (-SSH_PKCS11_ERR_PIN_REQUIRED);
  712. }
  713. if ((rv = f->C_OpenSession(p->module->slotlist[slotidx], CKF_RW_SESSION|
  714. CKF_SERIAL_SESSION, NULL, NULL, &session)) != CKR_OK) {
  715. error("C_OpenSession failed for slot %lu: %lu", slotidx, rv);
  716. return (-1);
  717. }
  718. if (login_required && pin != NULL && strlen(pin) != 0) {
  719. rv = f->C_Login(session, user, (u_char *)pin, strlen(pin));
  720. if (rv != CKR_OK && rv != CKR_USER_ALREADY_LOGGED_IN) {
  721. error("C_Login failed: %lu", rv);
  722. ret = (rv == CKR_PIN_LOCKED) ?
  723. -SSH_PKCS11_ERR_PIN_LOCKED :
  724. -SSH_PKCS11_ERR_LOGIN_FAIL;
  725. if ((rv = f->C_CloseSession(session)) != CKR_OK)
  726. error("C_CloseSession failed: %lu", rv);
  727. return (ret);
  728. }
  729. si->logged_in = 1;
  730. }
  731. si->session = session;
  732. return (0);
  733. }
  734. static int
  735. pkcs11_key_included(struct sshkey ***keysp, int *nkeys, struct sshkey *key)
  736. {
  737. int i;
  738. for (i = 0; i < *nkeys; i++)
  739. if (sshkey_equal(key, (*keysp)[i]))
  740. return (1);
  741. return (0);
  742. }
  743. #ifdef HAVE_EC_KEY_METHOD_NEW
  744. static struct sshkey *
  745. pkcs11_fetch_ecdsa_pubkey(struct pkcs11_provider *p, CK_ULONG slotidx,
  746. CK_OBJECT_HANDLE *obj)
  747. {
  748. CK_ATTRIBUTE key_attr[4];
  749. int nattr = 4;
  750. CK_SESSION_HANDLE session;
  751. CK_FUNCTION_LIST *f = NULL;
  752. CK_RV rv;
  753. ASN1_OCTET_STRING *octet = NULL;
  754. EC_KEY *ec = NULL;
  755. EC_GROUP *group = NULL;
  756. struct sshkey *key = NULL;
  757. const unsigned char *attrp = NULL;
  758. int i;
  759. int nid;
  760. memset(&key_attr, 0, sizeof(key_attr));
  761. key_attr[0].type = CKA_ID;
  762. key_attr[1].type = CKA_LABEL;
  763. key_attr[2].type = CKA_EC_POINT;
  764. key_attr[3].type = CKA_EC_PARAMS;
  765. session = p->module->slotinfo[slotidx].session;
  766. f = p->module->function_list;
  767. /* figure out size of the attributes */
  768. rv = f->C_GetAttributeValue(session, *obj, key_attr, nattr);
  769. if (rv != CKR_OK) {
  770. error("C_GetAttributeValue failed: %lu", rv);
  771. return (NULL);
  772. }
  773. /*
  774. * Allow CKA_ID (always first attribute) to be empty, but
  775. * ensure that none of the others are zero length.
  776. * XXX assumes CKA_ID is always first.
  777. */
  778. if (key_attr[2].ulValueLen == 0 ||
  779. key_attr[3].ulValueLen == 0) {
  780. error("invalid attribute length");
  781. return (NULL);
  782. }
  783. /* allocate buffers for attributes */
  784. for (i = 0; i < nattr; i++)
  785. if (key_attr[i].ulValueLen > 0)
  786. key_attr[i].pValue = xcalloc(1, key_attr[i].ulValueLen);
  787. /* retrieve ID, public point and curve parameters of EC key */
  788. rv = f->C_GetAttributeValue(session, *obj, key_attr, nattr);
  789. if (rv != CKR_OK) {
  790. error("C_GetAttributeValue failed: %lu", rv);
  791. goto fail;
  792. }
  793. ec = EC_KEY_new();
  794. if (ec == NULL) {
  795. error("EC_KEY_new failed");
  796. goto fail;
  797. }
  798. attrp = key_attr[3].pValue;
  799. group = d2i_ECPKParameters(NULL, &attrp, key_attr[3].ulValueLen);
  800. if (group == NULL) {
  801. ossl_error("d2i_ECPKParameters failed");
  802. goto fail;
  803. }
  804. if (EC_KEY_set_group(ec, group) == 0) {
  805. ossl_error("EC_KEY_set_group failed");
  806. goto fail;
  807. }
  808. if (key_attr[2].ulValueLen <= 2) {
  809. error("CKA_EC_POINT too small");
  810. goto fail;
  811. }
  812. attrp = key_attr[2].pValue;
  813. octet = d2i_ASN1_OCTET_STRING(NULL, &attrp, key_attr[2].ulValueLen);
  814. if (octet == NULL) {
  815. ossl_error("d2i_ASN1_OCTET_STRING failed");
  816. goto fail;
  817. }
  818. attrp = octet->data;
  819. if (o2i_ECPublicKey(&ec, &attrp, octet->length) == NULL) {
  820. ossl_error("o2i_ECPublicKey failed");
  821. goto fail;
  822. }
  823. nid = sshkey_ecdsa_key_to_nid(ec);
  824. if (nid < 0) {
  825. error("couldn't get curve nid");
  826. goto fail;
  827. }
  828. if (pkcs11_ecdsa_wrap(p, slotidx, &key_attr[0], &key_attr[1], ec))
  829. goto fail;
  830. key = sshkey_new(KEY_UNSPEC);
  831. if (key == NULL) {
  832. error("sshkey_new failed");
  833. goto fail;
  834. }
  835. key->ecdsa = ec;
  836. key->ecdsa_nid = nid;
  837. key->type = KEY_ECDSA;
  838. key->flags |= SSHKEY_FLAG_EXT;
  839. ec = NULL; /* now owned by key */
  840. fail:
  841. for (i = 0; i < nattr; i++)
  842. free(key_attr[i].pValue);
  843. if (ec)
  844. EC_KEY_free(ec);
  845. if (group)
  846. EC_GROUP_free(group);
  847. if (octet)
  848. ASN1_OCTET_STRING_free(octet);
  849. return (key);
  850. }
  851. #endif /* HAVE_EC_KEY_METHOD_NEW */
  852. static struct sshkey *
  853. pkcs11_fetch_rsa_pubkey(struct pkcs11_provider *p, CK_ULONG slotidx,
  854. CK_OBJECT_HANDLE *obj)
  855. {
  856. CK_ATTRIBUTE key_attr[4];
  857. int nattr = 4;
  858. CK_SESSION_HANDLE session;
  859. CK_FUNCTION_LIST *f = NULL;
  860. CK_RV rv;
  861. RSA *rsa = NULL;
  862. BIGNUM *rsa_n, *rsa_e;
  863. struct sshkey *key = NULL;
  864. int i;
  865. memset(&key_attr, 0, sizeof(key_attr));
  866. key_attr[0].type = CKA_ID;
  867. key_attr[1].type = CKA_LABEL;
  868. key_attr[2].type = CKA_MODULUS;
  869. key_attr[3].type = CKA_PUBLIC_EXPONENT;
  870. session = p->module->slotinfo[slotidx].session;
  871. f = p->module->function_list;
  872. /* figure out size of the attributes */
  873. rv = f->C_GetAttributeValue(session, *obj, key_attr, nattr);
  874. if (rv != CKR_OK) {
  875. error("C_GetAttributeValue failed: %lu", rv);
  876. return (NULL);
  877. }
  878. /*
  879. * Allow CKA_ID (always first attribute) to be empty, but
  880. * ensure that none of the others are zero length.
  881. * XXX assumes CKA_ID is always first.
  882. */
  883. if (key_attr[2].ulValueLen == 0 ||
  884. key_attr[3].ulValueLen == 0) {
  885. error("invalid attribute length");
  886. return (NULL);
  887. }
  888. /* allocate buffers for attributes */
  889. for (i = 0; i < nattr; i++)
  890. if (key_attr[i].ulValueLen > 0)
  891. key_attr[i].pValue = xcalloc(1, key_attr[i].ulValueLen);
  892. /* retrieve ID, modulus and public exponent of RSA key */
  893. rv = f->C_GetAttributeValue(session, *obj, key_attr, nattr);
  894. if (rv != CKR_OK) {
  895. error("C_GetAttributeValue failed: %lu", rv);
  896. goto fail;
  897. }
  898. rsa = RSA_new();
  899. if (rsa == NULL) {
  900. error("RSA_new failed");
  901. goto fail;
  902. }
  903. rsa_n = BN_bin2bn(key_attr[2].pValue, key_attr[2].ulValueLen, NULL);
  904. rsa_e = BN_bin2bn(key_attr[3].pValue, key_attr[3].ulValueLen, NULL);
  905. if (rsa_n == NULL || rsa_e == NULL) {
  906. error("BN_bin2bn failed");
  907. goto fail;
  908. }
  909. if (!RSA_set0_key(rsa, rsa_n, rsa_e, NULL))
  910. fatal("%s: set key", __func__);
  911. rsa_n = rsa_e = NULL; /* transferred */
  912. if (pkcs11_rsa_wrap(p, slotidx, &key_attr[0], &key_attr[1], rsa))
  913. goto fail;
  914. key = sshkey_new(KEY_UNSPEC);
  915. if (key == NULL) {
  916. error("sshkey_new failed");
  917. goto fail;
  918. }
  919. key->rsa = rsa;
  920. key->type = KEY_RSA;
  921. key->flags |= SSHKEY_FLAG_EXT;
  922. rsa = NULL; /* now owned by key */
  923. fail:
  924. for (i = 0; i < nattr; i++)
  925. free(key_attr[i].pValue);
  926. RSA_free(rsa);
  927. return (key);
  928. }
  929. static int
  930. pkcs11_fetch_x509_pubkey(struct pkcs11_provider *p, CK_ULONG slotidx,
  931. CK_OBJECT_HANDLE *obj, struct sshkey **keyp, char **labelp)
  932. {
  933. CK_ATTRIBUTE cert_attr[4];
  934. int nattr = 4;
  935. CK_SESSION_HANDLE session;
  936. CK_FUNCTION_LIST *f = NULL;
  937. CK_RV rv;
  938. X509 *x509 = NULL;
  939. X509_NAME *x509_name = NULL;
  940. EVP_PKEY *evp;
  941. RSA *rsa = NULL;
  942. #ifdef OPENSSL_HAS_ECC
  943. EC_KEY *ec = NULL;
  944. #endif
  945. struct sshkey *key = NULL;
  946. int i;
  947. #ifdef HAVE_EC_KEY_METHOD_NEW
  948. int nid;
  949. #endif
  950. const u_char *cp;
  951. char *subject = NULL;
  952. *keyp = NULL;
  953. *labelp = NULL;
  954. memset(&cert_attr, 0, sizeof(cert_attr));
  955. cert_attr[0].type = CKA_ID;
  956. cert_attr[1].type = CKA_LABEL;
  957. cert_attr[2].type = CKA_SUBJECT;
  958. cert_attr[3].type = CKA_VALUE;
  959. session = p->module->slotinfo[slotidx].session;
  960. f = p->module->function_list;
  961. /* figure out size of the attributes */
  962. rv = f->C_GetAttributeValue(session, *obj, cert_attr, nattr);
  963. if (rv != CKR_OK) {
  964. error("C_GetAttributeValue failed: %lu", rv);
  965. return -1;
  966. }
  967. /*
  968. * Allow CKA_ID (always first attribute) to be empty, but
  969. * ensure that none of the others are zero length.
  970. * XXX assumes CKA_ID is always first.
  971. */
  972. if (cert_attr[1].ulValueLen == 0 ||
  973. cert_attr[2].ulValueLen == 0 ||
  974. cert_attr[3].ulValueLen == 0) {
  975. error("invalid attribute length");
  976. return -1;
  977. }
  978. /* allocate buffers for attributes */
  979. for (i = 0; i < nattr; i++)
  980. if (cert_attr[i].ulValueLen > 0)
  981. cert_attr[i].pValue = xcalloc(1, cert_attr[i].ulValueLen);
  982. /* retrieve ID, subject and value of certificate */
  983. rv = f->C_GetAttributeValue(session, *obj, cert_attr, nattr);
  984. if (rv != CKR_OK) {
  985. error("C_GetAttributeValue failed: %lu", rv);
  986. goto out;
  987. }
  988. /* Decode DER-encoded cert subject */
  989. cp = cert_attr[2].pValue;
  990. if ((x509_name = d2i_X509_NAME(NULL, &cp,
  991. cert_attr[1].ulValueLen)) == NULL ||
  992. (subject = X509_NAME_oneline(x509_name, NULL, 0)) == NULL)
  993. subject = xstrdup("invalid subject");
  994. X509_NAME_free(x509_name);
  995. cp = cert_attr[3].pValue;
  996. if ((x509 = d2i_X509(NULL, &cp, cert_attr[3].ulValueLen)) == NULL) {
  997. error("d2i_x509 failed");
  998. goto out;
  999. }
  1000. if ((evp = X509_get_pubkey(x509)) == NULL) {
  1001. error("X509_get_pubkey failed");
  1002. goto out;
  1003. }
  1004. if (EVP_PKEY_base_id(evp) == EVP_PKEY_RSA) {
  1005. if (EVP_PKEY_get0_RSA(evp) == NULL) {
  1006. error("invalid x509; no rsa key");
  1007. goto out;
  1008. }
  1009. if ((rsa = RSAPublicKey_dup(EVP_PKEY_get0_RSA(evp))) == NULL) {
  1010. error("RSAPublicKey_dup failed");
  1011. goto out;
  1012. }
  1013. if (pkcs11_rsa_wrap(p, slotidx, &cert_attr[0], &cert_attr[1], rsa))
  1014. goto out;
  1015. key = sshkey_new(KEY_UNSPEC);
  1016. if (key == NULL) {
  1017. error("sshkey_new failed");
  1018. goto out;
  1019. }
  1020. key->rsa = rsa;
  1021. key->type = KEY_RSA;
  1022. key->flags |= SSHKEY_FLAG_EXT;
  1023. rsa = NULL; /* now owned by key */
  1024. #ifdef HAVE_EC_KEY_METHOD_NEW
  1025. } else if (EVP_PKEY_base_id(evp) == EVP_PKEY_EC) {
  1026. if (EVP_PKEY_get0_EC_KEY(evp) == NULL) {
  1027. error("invalid x509; no ec key");
  1028. goto out;
  1029. }
  1030. if ((ec = EC_KEY_dup(EVP_PKEY_get0_EC_KEY(evp))) == NULL) {
  1031. error("EC_KEY_dup failed");
  1032. goto out;
  1033. }
  1034. nid = sshkey_ecdsa_key_to_nid(ec);
  1035. if (nid < 0) {
  1036. error("couldn't get curve nid");
  1037. goto out;
  1038. }
  1039. if (pkcs11_ecdsa_wrap(p, slotidx, &cert_attr[0], &cert_attr[1], ec))
  1040. goto out;
  1041. key = sshkey_new(KEY_UNSPEC);
  1042. if (key == NULL) {
  1043. error("sshkey_new failed");
  1044. goto out;
  1045. }
  1046. key->ecdsa = ec;
  1047. key->ecdsa_nid = nid;
  1048. key->type = KEY_ECDSA;
  1049. key->flags |= SSHKEY_FLAG_EXT;
  1050. ec = NULL; /* now owned by key */
  1051. #endif /* HAVE_EC_KEY_METHOD_NEW */
  1052. } else {
  1053. error("unknown certificate key type");
  1054. goto out;
  1055. }
  1056. out:
  1057. for (i = 0; i < nattr; i++)
  1058. free(cert_attr[i].pValue);
  1059. X509_free(x509);
  1060. RSA_free(rsa);
  1061. #ifdef OPENSSL_HAS_ECC
  1062. EC_KEY_free(ec);
  1063. #endif
  1064. if (key == NULL) {
  1065. free(subject);
  1066. return -1;
  1067. }
  1068. /* success */
  1069. *keyp = key;
  1070. *labelp = subject;
  1071. return 0;
  1072. }
  1073. #if 0
  1074. static int
  1075. have_rsa_key(const RSA *rsa)
  1076. {
  1077. const BIGNUM *rsa_n, *rsa_e;
  1078. RSA_get0_key(rsa, &rsa_n, &rsa_e, NULL);
  1079. return rsa_n != NULL && rsa_e != NULL;
  1080. }
  1081. #endif
  1082. static void
  1083. note_key(struct pkcs11_provider *p, CK_ULONG slotidx, const char *context,
  1084. struct sshkey *key)
  1085. {
  1086. char *fp;
  1087. if ((fp = sshkey_fingerprint(key, SSH_FP_HASH_DEFAULT,
  1088. SSH_FP_DEFAULT)) == NULL) {
  1089. error("sshkey_fingerprint failed");
  1090. return;
  1091. }
  1092. debug2("%s: provider %s slot %lu: %s %s", context, p->name,
  1093. (u_long)slotidx, sshkey_type(key), fp);
  1094. free(fp);
  1095. }
  1096. /*
  1097. * lookup certificates for token in slot identified by slotidx,
  1098. * add 'wrapped' public keys to the 'keysp' array and increment nkeys.
  1099. * keysp points to an (possibly empty) array with *nkeys keys.
  1100. */
  1101. static int
  1102. pkcs11_fetch_certs(struct pkcs11_provider *p, CK_ULONG slotidx,
  1103. struct sshkey ***keysp, char ***labelsp, int *nkeys, struct pkcs11_uri *uri)
  1104. {
  1105. struct sshkey *key = NULL;
  1106. CK_OBJECT_CLASS key_class;
  1107. CK_ATTRIBUTE key_attr[3];
  1108. int nattr = 1;
  1109. CK_SESSION_HANDLE session;
  1110. CK_FUNCTION_LIST *f = NULL;
  1111. CK_RV rv;
  1112. CK_OBJECT_HANDLE obj;
  1113. CK_ULONG n = 0;
  1114. int ret = -1;
  1115. char *label;
  1116. memset(&key_attr, 0, sizeof(key_attr));
  1117. memset(&obj, 0, sizeof(obj));
  1118. key_class = CKO_CERTIFICATE;
  1119. key_attr[0].type = CKA_CLASS;
  1120. key_attr[0].pValue = &key_class;
  1121. key_attr[0].ulValueLen = sizeof(key_class);
  1122. if (uri->id != NULL) {
  1123. key_attr[nattr].type = CKA_ID;
  1124. key_attr[nattr].pValue = uri->id;
  1125. key_attr[nattr].ulValueLen = uri->id_len;
  1126. nattr++;
  1127. }
  1128. if (uri->object != NULL) {
  1129. key_attr[nattr].type = CKA_LABEL;
  1130. key_attr[nattr].pValue = uri->object;
  1131. key_attr[nattr].ulValueLen = strlen(uri->object);
  1132. nattr++;
  1133. }
  1134. session = p->module->slotinfo[slotidx].session;
  1135. f = p->module->function_list;
  1136. rv = f->C_FindObjectsInit(session, key_attr, nattr);
  1137. if (rv != CKR_OK) {
  1138. error("C_FindObjectsInit failed: %lu", rv);
  1139. goto fail;
  1140. }
  1141. while (1) {
  1142. CK_CERTIFICATE_TYPE ck_cert_type;
  1143. rv = f->C_FindObjects(session, &obj, 1, &n);
  1144. if (rv != CKR_OK) {
  1145. error("C_FindObjects failed: %lu", rv);
  1146. goto fail;
  1147. }
  1148. if (n == 0)
  1149. break;
  1150. memset(&ck_cert_type, 0, sizeof(ck_cert_type));
  1151. memset(&key_attr, 0, sizeof(key_attr));
  1152. key_attr[0].type = CKA_CERTIFICATE_TYPE;
  1153. key_attr[0].pValue = &ck_cert_type;
  1154. key_attr[0].ulValueLen = sizeof(ck_cert_type);
  1155. rv = f->C_GetAttributeValue(session, obj, key_attr, 1);
  1156. if (rv != CKR_OK) {
  1157. error("C_GetAttributeValue failed: %lu", rv);
  1158. goto fail;
  1159. }
  1160. key = NULL;
  1161. label = NULL;
  1162. switch (ck_cert_type) {
  1163. case CKC_X_509:
  1164. if (pkcs11_fetch_x509_pubkey(p, slotidx, &obj,
  1165. &key, &label) != 0) {
  1166. error("failed to fetch key");
  1167. continue;
  1168. }
  1169. break;
  1170. default:
  1171. error("skipping unsupported certificate type %lu",
  1172. ck_cert_type);
  1173. continue;
  1174. }
  1175. note_key(p, slotidx, __func__, key);
  1176. if (pkcs11_key_included(keysp, nkeys, key)) {
  1177. debug2("key already included");;
  1178. sshkey_free(key);
  1179. } else {
  1180. /* expand key array and add key */
  1181. *keysp = xrecallocarray(*keysp, *nkeys,
  1182. *nkeys + 1, sizeof(struct sshkey *));
  1183. (*keysp)[*nkeys] = key;
  1184. if (labelsp != NULL) {
  1185. *labelsp = xrecallocarray(*labelsp, *nkeys,
  1186. *nkeys + 1, sizeof(char *));
  1187. (*labelsp)[*nkeys] = xstrdup((char *)label);
  1188. }
  1189. *nkeys = *nkeys + 1;
  1190. debug("have %d keys", *nkeys);
  1191. }
  1192. }
  1193. ret = 0;
  1194. fail:
  1195. rv = f->C_FindObjectsFinal(session);
  1196. if (rv != CKR_OK) {
  1197. error("C_FindObjectsFinal failed: %lu", rv);
  1198. ret = -1;
  1199. }
  1200. return (ret);
  1201. }
  1202. /*
  1203. * lookup public keys for token in slot identified by slotidx,
  1204. * add 'wrapped' public keys to the 'keysp' array and increment nkeys.
  1205. * keysp points to an (possibly empty) array with *nkeys keys.
  1206. */
  1207. static int
  1208. pkcs11_fetch_keys(struct pkcs11_provider *p, CK_ULONG slotidx,
  1209. struct sshkey ***keysp, char ***labelsp, int *nkeys, struct pkcs11_uri *uri)
  1210. {
  1211. struct sshkey *key = NULL;
  1212. CK_OBJECT_CLASS key_class;
  1213. CK_ATTRIBUTE key_attr[3];
  1214. int nattr = 1;
  1215. CK_SESSION_HANDLE session;
  1216. CK_FUNCTION_LIST *f = NULL;
  1217. CK_RV rv;
  1218. CK_OBJECT_HANDLE obj;
  1219. CK_ULONG n = 0;
  1220. int ret = -1;
  1221. memset(&key_attr, 0, sizeof(key_attr));
  1222. memset(&obj, 0, sizeof(obj));
  1223. key_class = CKO_PUBLIC_KEY;
  1224. key_attr[0].type = CKA_CLASS;
  1225. key_attr[0].pValue = &key_class;
  1226. key_attr[0].ulValueLen = sizeof(key_class);
  1227. if (uri->id != NULL) {
  1228. key_attr[nattr].type = CKA_ID;
  1229. key_attr[nattr].pValue = uri->id;
  1230. key_attr[nattr].ulValueLen = uri->id_len;
  1231. nattr++;
  1232. }
  1233. if (uri->object != NULL) {
  1234. key_attr[nattr].type = CKA_LABEL;
  1235. key_attr[nattr].pValue = uri->object;
  1236. key_attr[nattr].ulValueLen = strlen(uri->object);
  1237. nattr++;
  1238. }
  1239. session = p->module->slotinfo[slotidx].session;
  1240. f = p->module->function_list;
  1241. rv = f->C_FindObjectsInit(session, key_attr, nattr);
  1242. if (rv != CKR_OK) {
  1243. error("C_FindObjectsInit failed: %lu", rv);
  1244. goto fail;
  1245. }
  1246. while (1) {
  1247. CK_KEY_TYPE ck_key_type;
  1248. CK_UTF8CHAR label[256];
  1249. rv = f->C_FindObjects(session, &obj, 1, &n);
  1250. if (rv != CKR_OK) {
  1251. error("C_FindObjects failed: %lu", rv);
  1252. goto fail;
  1253. }
  1254. if (n == 0)
  1255. break;
  1256. memset(&ck_key_type, 0, sizeof(ck_key_type));
  1257. memset(&key_attr, 0, sizeof(key_attr));
  1258. key_attr[0].type = CKA_KEY_TYPE;
  1259. key_attr[0].pValue = &ck_key_type;
  1260. key_attr[0].ulValueLen = sizeof(ck_key_type);
  1261. key_attr[1].type = CKA_LABEL;
  1262. key_attr[1].pValue = &label;
  1263. key_attr[1].ulValueLen = sizeof(label) - 1;
  1264. rv = f->C_GetAttributeValue(session, obj, key_attr, 2);
  1265. if (rv != CKR_OK) {
  1266. error("C_GetAttributeValue failed: %lu", rv);
  1267. goto fail;
  1268. }
  1269. label[key_attr[1].ulValueLen] = '\0';
  1270. switch (ck_key_type) {
  1271. case CKK_RSA:
  1272. key = pkcs11_fetch_rsa_pubkey(p, slotidx, &obj);
  1273. break;
  1274. #ifdef HAVE_EC_KEY_METHOD_NEW
  1275. case CKK_ECDSA:
  1276. key = pkcs11_fetch_ecdsa_pubkey(p, slotidx, &obj);
  1277. break;
  1278. #endif /* HAVE_EC_KEY_METHOD_NEW */
  1279. default:
  1280. /* XXX print key type? */
  1281. key = NULL;
  1282. error("skipping unsupported key type");
  1283. }
  1284. if (key == NULL) {
  1285. error("failed to fetch key");
  1286. continue;
  1287. }
  1288. note_key(p, slotidx, __func__, key);
  1289. if (pkcs11_key_included(keysp, nkeys, key)) {
  1290. debug2("key already included");;
  1291. sshkey_free(key);
  1292. } else {
  1293. /* expand key array and add key */
  1294. *keysp = xrecallocarray(*keysp, *nkeys,
  1295. *nkeys + 1, sizeof(struct sshkey *));
  1296. (*keysp)[*nkeys] = key;
  1297. if (labelsp != NULL) {
  1298. *labelsp = xrecallocarray(*labelsp, *nkeys,
  1299. *nkeys + 1, sizeof(char *));
  1300. (*labelsp)[*nkeys] = xstrdup((char *)label);
  1301. }
  1302. *nkeys = *nkeys + 1;
  1303. debug("have %d keys", *nkeys);
  1304. }
  1305. }
  1306. ret = 0;
  1307. fail:
  1308. rv = f->C_FindObjectsFinal(session);
  1309. if (rv != CKR_OK) {
  1310. error("C_FindObjectsFinal failed: %lu", rv);
  1311. ret = -1;
  1312. }
  1313. return (ret);
  1314. }
  1315. #ifdef WITH_PKCS11_KEYGEN
  1316. #define FILL_ATTR(attr, idx, typ, val, len) \
  1317. { (attr[idx]).type=(typ); (attr[idx]).pValue=(val); (attr[idx]).ulValueLen=len; idx++; }
  1318. static struct sshkey *
  1319. pkcs11_rsa_generate_private_key(struct pkcs11_provider *p, CK_ULONG slotidx,
  1320. char *label, CK_ULONG bits, CK_BYTE keyid, u_int32_t *err)
  1321. {
  1322. struct pkcs11_slotinfo *si;
  1323. char *plabel = label ? label : "";
  1324. int npub = 0, npriv = 0;
  1325. CK_RV rv;
  1326. CK_FUNCTION_LIST *f;
  1327. CK_SESSION_HANDLE session;
  1328. CK_BBOOL true_val = CK_TRUE, false_val = CK_FALSE;
  1329. CK_OBJECT_HANDLE pubKey, privKey;
  1330. CK_ATTRIBUTE tpub[16], tpriv[16];
  1331. CK_MECHANISM mech = {
  1332. CKM_RSA_PKCS_KEY_PAIR_GEN, NULL_PTR, 0
  1333. };
  1334. CK_BYTE pubExponent[] = {
  1335. 0x01, 0x00, 0x01 /* RSA_F4 in bytes */
  1336. };
  1337. pubkey_filter[0].pValue = &pubkey_class;
  1338. cert_filter[0].pValue = &cert_class;
  1339. *err = 0;
  1340. FILL_ATTR(tpub, npub, CKA_TOKEN, &true_val, sizeof(true_val));
  1341. FILL_ATTR(tpub, npub, CKA_LABEL, plabel, strlen(plabel));
  1342. FILL_ATTR(tpub, npub, CKA_ENCRYPT, &false_val, sizeof(false_val));
  1343. FILL_ATTR(tpub, npub, CKA_VERIFY, &true_val, sizeof(true_val));
  1344. FILL_ATTR(tpub, npub, CKA_VERIFY_RECOVER, &false_val,
  1345. sizeof(false_val));
  1346. FILL_ATTR(tpub, npub, CKA_WRAP, &false_val, sizeof(false_val));
  1347. FILL_ATTR(tpub, npub, CKA_DERIVE, &false_val, sizeof(false_val));
  1348. FILL_ATTR(tpub, npub, CKA_MODULUS_BITS, &bits, sizeof(bits));
  1349. FILL_ATTR(tpub, npub, CKA_PUBLIC_EXPONENT, pubExponent,
  1350. sizeof(pubExponent));
  1351. FILL_ATTR(tpub, npub, CKA_ID, &keyid, sizeof(keyid));
  1352. FILL_ATTR(tpriv, npriv, CKA_TOKEN, &true_val, sizeof(true_val));
  1353. FILL_ATTR(tpriv, npriv, CKA_LABEL, plabel, strlen(plabel));
  1354. FILL_ATTR(tpriv, npriv, CKA_PRIVATE, &true_val, sizeof(true_val));
  1355. FILL_ATTR(tpriv, npriv, CKA_SENSITIVE, &true_val, sizeof(true_val));
  1356. FILL_ATTR(tpriv, npriv, CKA_DECRYPT, &false_val, sizeof(false_val));
  1357. FILL_ATTR(tpriv, npriv, CKA_SIGN, &true_val, sizeof(true_val));
  1358. FILL_ATTR(tpriv, npriv, CKA_SIGN_RECOVER, &false_val,
  1359. sizeof(false_val));
  1360. FILL_ATTR(tpriv, npriv, CKA_UNWRAP, &false_val, sizeof(false_val));
  1361. FILL_ATTR(tpriv, npriv, CKA_DERIVE, &false_val, sizeof(false_val));
  1362. FILL_ATTR(tpriv, npriv, CKA_ID, &keyid, sizeof(keyid));
  1363. f = p->function_list;
  1364. si = &p->slotinfo[slotidx];
  1365. session = si->session;
  1366. if ((rv = f->C_GenerateKeyPair(session, &mech, tpub, npub, tpriv, npriv,
  1367. &pubKey, &privKey)) != CKR_OK) {
  1368. error("%s: key generation failed: error 0x%lx", __func__, rv);
  1369. *err = rv;
  1370. return NULL;
  1371. }
  1372. return pkcs11_fetch_rsa_pubkey(p, slotidx, &pubKey);
  1373. }
  1374. static int
  1375. pkcs11_decode_hex(const char *hex, unsigned char **dest, size_t *rlen)
  1376. {
  1377. size_t i, len;
  1378. char ptr[3];
  1379. if (dest)
  1380. *dest = NULL;
  1381. if (rlen)
  1382. *rlen = 0;
  1383. if ((len = strlen(hex)) % 2)
  1384. return -1;
  1385. len /= 2;
  1386. *dest = xmalloc(len);
  1387. ptr[2] = '\0';
  1388. for (i = 0; i < len; i++) {
  1389. ptr[0] = hex[2 * i];
  1390. ptr[1] = hex[(2 * i) + 1];
  1391. if (!isxdigit(ptr[0]) || !isxdigit(ptr[1]))
  1392. return -1;
  1393. (*dest)[i] = (unsigned char)strtoul(ptr, NULL, 16);
  1394. }
  1395. if (rlen)
  1396. *rlen = len;
  1397. return 0;
  1398. }
  1399. static struct ec_curve_info {
  1400. const char *name;
  1401. const char *oid;
  1402. const char *oid_encoded;
  1403. size_t size;
  1404. } ec_curve_infos[] = {
  1405. {"prime256v1", "1.2.840.10045.3.1.7", "06082A8648CE3D030107", 256},
  1406. {"secp384r1", "1.3.132.0.34", "06052B81040022", 384},
  1407. {"secp521r1", "1.3.132.0.35", "06052B81040023", 521},
  1408. {NULL, NULL, NULL, 0},
  1409. };
  1410. static struct sshkey *
  1411. pkcs11_ecdsa_generate_private_key(struct pkcs11_provider *p, CK_ULONG slotidx,
  1412. char *label, CK_ULONG bits, CK_BYTE keyid, u_int32_t *err)
  1413. {
  1414. struct pkcs11_slotinfo *si;
  1415. char *plabel = label ? label : "";
  1416. int i;
  1417. size_t ecparams_size;
  1418. unsigned char *ecparams = NULL;
  1419. int npub = 0, npriv = 0;
  1420. CK_RV rv;
  1421. CK_FUNCTION_LIST *f;
  1422. CK_SESSION_HANDLE session;
  1423. CK_BBOOL true_val = CK_TRUE, false_val = CK_FALSE;
  1424. CK_OBJECT_HANDLE pubKey, privKey;
  1425. CK_MECHANISM mech = {
  1426. CKM_EC_KEY_PAIR_GEN, NULL_PTR, 0
  1427. };
  1428. CK_ATTRIBUTE tpub[16], tpriv[16];
  1429. *err = 0;
  1430. for (i = 0; ec_curve_infos[i].name; i++) {
  1431. if (ec_curve_infos[i].size == bits)
  1432. break;
  1433. }
  1434. if (!ec_curve_infos[i].name) {
  1435. error("%s: invalid key size %lu", __func__, bits);
  1436. return NULL;
  1437. }
  1438. if (pkcs11_decode_hex(ec_curve_infos[i].oid_encoded, &ecparams,
  1439. &ecparams_size) == -1) {
  1440. error("%s: invalid oid", __func__);
  1441. return NULL;
  1442. }
  1443. FILL_ATTR(tpub, npub, CKA_TOKEN, &true_val, sizeof(true_val));
  1444. FILL_ATTR(tpub, npub, CKA_LABEL, plabel, strlen(plabel));
  1445. FILL_ATTR(tpub, npub, CKA_ENCRYPT, &false_val, sizeof(false_val));
  1446. FILL_ATTR(tpub, npub, CKA_VERIFY, &true_val, sizeof(true_val));
  1447. FILL_ATTR(tpub, npub, CKA_VERIFY_RECOVER, &false_val,
  1448. sizeof(false_val));
  1449. FILL_ATTR(tpub, npub, CKA_WRAP, &false_val, sizeof(false_val));
  1450. FILL_ATTR(tpub, npub, CKA_DERIVE, &false_val, sizeof(false_val));
  1451. FILL_ATTR(tpub, npub, CKA_EC_PARAMS, ecparams, ecparams_size);
  1452. FILL_ATTR(tpub, npub, CKA_ID, &keyid, sizeof(keyid));
  1453. FILL_ATTR(tpriv, npriv, CKA_TOKEN, &true_val, sizeof(true_val));
  1454. FILL_ATTR(tpriv, npriv, CKA_LABEL, plabel, strlen(plabel));
  1455. FILL_ATTR(tpriv, npriv, CKA_PRIVATE, &true_val, sizeof(true_val));
  1456. FILL_ATTR(tpriv, npriv, CKA_SENSITIVE, &true_val, sizeof(true_val));
  1457. FILL_ATTR(tpriv, npriv, CKA_DECRYPT, &false_val, sizeof(false_val));
  1458. FILL_ATTR(tpriv, npriv, CKA_SIGN, &true_val, sizeof(true_val));
  1459. FILL_ATTR(tpriv, npriv, CKA_SIGN_RECOVER, &false_val,
  1460. sizeof(false_val));
  1461. FILL_ATTR(tpriv, npriv, CKA_UNWRAP, &false_val, sizeof(false_val));
  1462. FILL_ATTR(tpriv, npriv, CKA_DERIVE, &false_val, sizeof(false_val));
  1463. FILL_ATTR(tpriv, npriv, CKA_ID, &keyid, sizeof(keyid));
  1464. f = p->function_list;
  1465. si = &p->slotinfo[slotidx];
  1466. session = si->session;
  1467. if ((rv = f->C_GenerateKeyPair(session, &mech, tpub, npub, tpriv, npriv,
  1468. &pubKey, &privKey)) != CKR_OK) {
  1469. error("%s: key generation failed: error 0x%lx", __func__, rv);
  1470. *err = rv;
  1471. return NULL;
  1472. }
  1473. return pkcs11_fetch_ecdsa_pubkey(p, slotidx, &pubKey);
  1474. }
  1475. #endif /* WITH_PKCS11_KEYGEN */
  1476. static int
  1477. pkcs11_initialize_provider(struct pkcs11_uri *uri, struct pkcs11_provider **providerp)
  1478. {
  1479. int need_finalize = 0;
  1480. int ret = -1;
  1481. struct pkcs11_provider *p = NULL;
  1482. void *handle = NULL;
  1483. CK_RV (*getfunctionlist)(CK_FUNCTION_LIST **);
  1484. CK_RV rv;
  1485. CK_FUNCTION_LIST *f = NULL;
  1486. CK_TOKEN_INFO *token;
  1487. CK_ULONG i;
  1488. char *provider_module = NULL;
  1489. struct pkcs11_module *m = NULL;
  1490. /* if no provider specified, fallback to p11-kit */
  1491. if (uri->module_path == NULL) {
  1492. #ifdef PKCS11_DEFAULT_PROVIDER
  1493. provider_module = strdup(PKCS11_DEFAULT_PROVIDER);
  1494. #else
  1495. error("%s: No module path provided", __func__);
  1496. goto fail;
  1497. #endif
  1498. } else {
  1499. provider_module = strdup(uri->module_path);
  1500. }
  1501. p = xcalloc(1, sizeof(*p));
  1502. p->name = pkcs11_uri_get(uri);
  1503. if ((m = pkcs11_provider_lookup_module(provider_module)) != NULL
  1504. && m->valid) {
  1505. debug("%s: provider module already initialized: %s",
  1506. __func__, provider_module);
  1507. free(provider_module);
  1508. /* Skip the initialization of PKCS#11 module */
  1509. m->refcount++;
  1510. p->module = m;
  1511. p->valid = 1;
  1512. TAILQ_INSERT_TAIL(&pkcs11_providers, p, next);
  1513. p->refcount++; /* add to provider list */
  1514. *providerp = p;
  1515. return 0;
  1516. } else {
  1517. m = xcalloc(1, sizeof(*m));
  1518. p->module = m;
  1519. m->refcount++;
  1520. }
  1521. /* open shared pkcs11-library */
  1522. if ((handle = dlopen(provider_module, RTLD_NOW)) == NULL) {
  1523. error("dlopen %s failed: %s", provider_module, dlerror());
  1524. goto fail;
  1525. }
  1526. if ((getfunctionlist = dlsym(handle, "C_GetFunctionList")) == NULL) {
  1527. error("dlsym(C_GetFunctionList) failed: %s", dlerror());
  1528. goto fail;
  1529. }
  1530. p->module->handle = handle;
  1531. /* setup the pkcs11 callbacks */
  1532. if ((rv = (*getfunctionlist)(&f)) != CKR_OK) {
  1533. error("C_GetFunctionList for provider %s failed: %lu",
  1534. provider_module, rv);
  1535. goto fail;
  1536. }
  1537. m->function_list = f;
  1538. if ((rv = f->C_Initialize(NULL)) != CKR_OK) {
  1539. error("C_Initialize for provider %s failed: %lu",
  1540. provider_module, rv);
  1541. goto fail;
  1542. }
  1543. need_finalize = 1;
  1544. if ((rv = f->C_GetInfo(&m->info)) != CKR_OK) {
  1545. error("C_GetInfo for provider %s failed: %lu",
  1546. provider_module, rv);
  1547. goto fail;
  1548. }
  1549. rmspace(m->info.manufacturerID, sizeof(m->info.manufacturerID));
  1550. if (uri->lib_manuf != NULL &&
  1551. strcmp(uri->lib_manuf, m->info.manufacturerID)) {
  1552. debug("%s: Skipping provider %s not matching library_manufacturer",
  1553. __func__, m->info.manufacturerID);
  1554. goto fail;
  1555. }
  1556. rmspace(m->info.libraryDescription, sizeof(m->info.libraryDescription));
  1557. debug("provider %s: manufacturerID <%s> cryptokiVersion %d.%d"
  1558. " libraryDescription <%s> libraryVersion %d.%d",
  1559. provider_module,
  1560. m->info.manufacturerID,
  1561. m->info.cryptokiVersion.major,
  1562. m->info.cryptokiVersion.minor,
  1563. m->info.libraryDescription,
  1564. m->info.libraryVersion.major,
  1565. m->info.libraryVersion.minor);
  1566. if ((rv = f->C_GetSlotList(CK_TRUE, NULL, &m->nslots)) != CKR_OK) {
  1567. error("C_GetSlotList failed: %lu", rv);
  1568. goto fail;
  1569. }
  1570. if (m->nslots == 0) {
  1571. debug("%s: provider %s returned no slots", __func__,
  1572. provider_module);
  1573. ret = -SSH_PKCS11_ERR_NO_SLOTS;
  1574. goto fail;
  1575. }
  1576. m->slotlist = xcalloc(m->nslots, sizeof(CK_SLOT_ID));
  1577. if ((rv = f->C_GetSlotList(CK_TRUE, m->slotlist, &m->nslots))
  1578. != CKR_OK) {
  1579. error("C_GetSlotList for provider %s failed: %lu",
  1580. provider_module, rv);
  1581. goto fail;
  1582. }
  1583. p->valid = 1;
  1584. m->slotinfo = xcalloc(m->nslots, sizeof(struct pkcs11_slotinfo));
  1585. m->valid = 1;
  1586. for (i = 0; i < m->nslots; i++) {
  1587. token = &m->slotinfo[i].token;
  1588. if ((rv = f->C_GetTokenInfo(m->slotlist[i], token))
  1589. != CKR_OK) {
  1590. error("C_GetTokenInfo for provider %s slot %lu "
  1591. "failed: %lu", provider_module, (unsigned long)i, rv);
  1592. token->flags = 0;
  1593. continue;
  1594. }
  1595. rmspace(token->label, sizeof(token->label));
  1596. rmspace(token->manufacturerID, sizeof(token->manufacturerID));
  1597. rmspace(token->model, sizeof(token->model));
  1598. rmspace(token->serialNumber, sizeof(token->serialNumber));
  1599. }
  1600. m->module_path = provider_module;
  1601. provider_module = NULL;
  1602. /* insert unconditionally -- remove if there will be no keys later */
  1603. TAILQ_INSERT_TAIL(&pkcs11_providers, p, next);
  1604. p->refcount++; /* add to provider list */
  1605. *providerp = p;
  1606. return 0;
  1607. fail:
  1608. if (need_finalize && (rv = f->C_Finalize(NULL)) != CKR_OK)
  1609. error("C_Finalize for provider %s failed: %lu",
  1610. provider_module, rv);
  1611. free(provider_module);
  1612. if (m) {
  1613. free(m->slotlist);
  1614. free(m);
  1615. }
  1616. if (p) {
  1617. free(p->name);
  1618. free(p);
  1619. }
  1620. if (handle)
  1621. dlclose(handle);
  1622. return ret;
  1623. }
  1624. /*
  1625. * register a new provider, fails if provider already exists. if
  1626. * keyp is provided, fetch keys.
  1627. */
  1628. static int
  1629. pkcs11_register_provider_by_uri(struct pkcs11_uri *uri, char *pin,
  1630. struct sshkey ***keyp, char ***labelsp, struct pkcs11_provider **providerp,
  1631. CK_ULONG user)
  1632. {
  1633. int nkeys;
  1634. int ret = -1;
  1635. struct pkcs11_provider *p = NULL;
  1636. CK_ULONG i;
  1637. CK_TOKEN_INFO *token;
  1638. char *provider_uri = NULL;
  1639. if (providerp == NULL)
  1640. goto fail;
  1641. *providerp = NULL;
  1642. if (keyp != NULL)
  1643. *keyp = NULL;
  1644. if ((ret = pkcs11_initialize_provider(uri, &p)) != 0) {
  1645. goto fail;
  1646. }
  1647. provider_uri = pkcs11_uri_get(uri);
  1648. if (pin == NULL && uri->pin != NULL) {
  1649. pin = uri->pin;
  1650. }
  1651. nkeys = 0;
  1652. for (i = 0; i < p->module->nslots; i++) {
  1653. token = &p->module->slotinfo[i].token;
  1654. if ((token->flags & CKF_TOKEN_INITIALIZED) == 0) {
  1655. debug2("%s: ignoring uninitialised token in "
  1656. "provider %s slot %lu", __func__,
  1657. provider_uri, (unsigned long)i);
  1658. continue;
  1659. }
  1660. if (uri->token != NULL &&
  1661. strcmp(token->label, uri->token) != 0) {
  1662. debug2("%s: ignoring token not matching label (%s) "
  1663. "specified by PKCS#11 URI in slot %lu", __func__,
  1664. token->label, (unsigned long)i);
  1665. continue;
  1666. }
  1667. if (uri->manuf != NULL &&
  1668. strcmp(token->manufacturerID, uri->manuf) != 0) {
  1669. debug2("%s: ignoring token not matching requrested "
  1670. "manufacturerID (%s) specified by PKCS#11 URI in "
  1671. "slot %lu", __func__,
  1672. token->manufacturerID, (unsigned long)i);
  1673. continue;
  1674. }
  1675. debug("provider %s slot %lu: label <%s> manufacturerID <%s> "
  1676. "model <%s> serial <%s> flags 0x%lx",
  1677. provider_uri, (unsigned long)i,
  1678. token->label, token->manufacturerID, token->model,
  1679. token->serialNumber, token->flags);
  1680. /*
  1681. * open session if not yet openend, login with pin and
  1682. * retrieve public keys (if keyp is provided)
  1683. */
  1684. if ((p->module->slotinfo[i].session != 0 ||
  1685. (ret = pkcs11_open_session(p, i, pin, user)) != 0) && /* ??? */
  1686. keyp == NULL)
  1687. continue;
  1688. pkcs11_fetch_keys(p, i, keyp, labelsp, &nkeys, uri);
  1689. pkcs11_fetch_certs(p, i, keyp, labelsp, &nkeys, uri);
  1690. if (nkeys == 0 && !p->module->slotinfo[i].logged_in &&
  1691. pkcs11_interactive) {
  1692. /*
  1693. * Some tokens require login before they will
  1694. * expose keys.
  1695. */
  1696. debug3("%s: Trying to login as there were no keys found",
  1697. __func__);
  1698. if (pkcs11_login_slot(p, &p->module->slotinfo[i],
  1699. CKU_USER) < 0) {
  1700. error("login failed");
  1701. continue;
  1702. }
  1703. pkcs11_fetch_keys(p, i, keyp, labelsp, &nkeys, uri);
  1704. pkcs11_fetch_certs(p, i, keyp, labelsp, &nkeys, uri);
  1705. }
  1706. if (nkeys == 0 && uri->object != NULL) {
  1707. debug3("%s: No keys found. Retrying without label (%s) ",
  1708. __func__, uri->object);
  1709. /* Try once more without the label filter */
  1710. char *label = uri->object;
  1711. uri->object = NULL; /* XXX clone uri? */
  1712. pkcs11_fetch_keys(p, i, keyp, labelsp, &nkeys, uri);
  1713. pkcs11_fetch_certs(p, i, keyp, labelsp, &nkeys, uri);
  1714. uri->object = label;
  1715. }
  1716. }
  1717. pin = NULL; /* Will be cleaned up with URI */
  1718. /* now owned by caller */
  1719. *providerp = p;
  1720. free(provider_uri);
  1721. return (nkeys);
  1722. fail:
  1723. if (p) {
  1724. TAILQ_REMOVE(&pkcs11_providers, p, next);
  1725. pkcs11_provider_unref(p);
  1726. }
  1727. if (ret > 0)
  1728. ret = -1;
  1729. return (ret);
  1730. }
  1731. static int
  1732. pkcs11_register_provider(char *provider_id, char *pin, struct sshkey ***keyp,
  1733. char ***labelsp, struct pkcs11_provider **providerp, CK_ULONG user)
  1734. {
  1735. struct pkcs11_uri *uri = NULL;
  1736. int r;
  1737. debug("%s: called, provider_id = %s", __func__, provider_id);
  1738. uri = pkcs11_uri_init();
  1739. if (uri == NULL)
  1740. fatal("failed to init PKCS#11 URI");
  1741. if (strlen(provider_id) >= strlen(PKCS11_URI_SCHEME) &&
  1742. strncmp(provider_id, PKCS11_URI_SCHEME, strlen(PKCS11_URI_SCHEME)) == 0) {
  1743. if (pkcs11_uri_parse(provider_id, uri) != 0)
  1744. fatal("Failed to parse PKCS#11 URI");
  1745. } else {
  1746. uri->module_path = strdup(provider_id);
  1747. }
  1748. r = pkcs11_register_provider_by_uri(uri, pin, keyp, labelsp, providerp, user);
  1749. pkcs11_uri_cleanup(uri);
  1750. return r;
  1751. }
  1752. int
  1753. pkcs11_add_provider_by_uri(struct pkcs11_uri *uri, char *pin,
  1754. struct sshkey ***keyp, char ***labelsp)
  1755. {
  1756. int nkeys;
  1757. struct pkcs11_provider *p = NULL;
  1758. char *provider_uri = pkcs11_uri_get(uri);
  1759. debug("%s: called, provider_uri = %s", __func__, provider_uri);
  1760. nkeys = pkcs11_register_provider_by_uri(uri, pin, keyp, labelsp, &p, CKU_USER);
  1761. /* no keys found or some other error, de-register provider */
  1762. if (nkeys <= 0 && p != NULL) {
  1763. TAILQ_REMOVE(&pkcs11_providers, p, next);
  1764. pkcs11_provider_finalize(p);
  1765. pkcs11_provider_unref(p);
  1766. }
  1767. if (nkeys == 0)
  1768. debug("%s: provider %s returned no keys", __func__,
  1769. provider_uri);
  1770. free(provider_uri);
  1771. return nkeys;
  1772. }
  1773. /*
  1774. * register a new provider and get number of keys hold by the token,
  1775. * fails if provider already exists
  1776. */
  1777. int
  1778. pkcs11_add_provider(char *provider_id, char *pin,
  1779. struct sshkey ***keyp, char ***labelsp)
  1780. {
  1781. struct pkcs11_uri *uri;
  1782. int nkeys;
  1783. uri = pkcs11_uri_init();
  1784. if (uri == NULL)
  1785. fatal("Failed to init PKCS#11 URI");
  1786. if (strlen(provider_id) >= strlen(PKCS11_URI_SCHEME) &&
  1787. strncmp(provider_id, PKCS11_URI_SCHEME, strlen(PKCS11_URI_SCHEME)) == 0) {
  1788. if (pkcs11_uri_parse(provider_id, uri) != 0)
  1789. fatal("Failed to parse PKCS#11 URI");
  1790. } else {
  1791. uri->module_path = strdup(provider_id);
  1792. }
  1793. nkeys = pkcs11_add_provider_by_uri(uri, pin, keyp, labelsp);
  1794. pkcs11_uri_cleanup(uri);
  1795. return (nkeys);
  1796. }
  1797. #ifdef WITH_PKCS11_KEYGEN
  1798. struct sshkey *
  1799. pkcs11_gakp(char *provider_id, char *pin, unsigned int slotidx, char *label,
  1800. unsigned int type, unsigned int bits, unsigned char keyid, u_int32_t *err)
  1801. {
  1802. struct pkcs11_provider *p = NULL;
  1803. struct pkcs11_slotinfo *si;
  1804. CK_FUNCTION_LIST *f;
  1805. CK_SESSION_HANDLE session;
  1806. struct sshkey *k = NULL;
  1807. int ret = -1, reset_pin = 0, reset_provider = 0;
  1808. CK_RV rv;
  1809. *err = 0;
  1810. if ((p = pkcs11_provider_lookup(provider_id)) != NULL)
  1811. debug("%s: provider \"%s\" available", __func__, provider_id);
  1812. else if ((rv = pkcs11_register_provider(provider_id, pin, NULL, NULL,
  1813. &p, CKU_SO)) < 0) {
  1814. debug("%s: could not register provider %s", __func__,
  1815. provider_id);
  1816. goto out;
  1817. } else
  1818. reset_provider = 1;
  1819. f = p->function_list;
  1820. si = &p->slotinfo[slotidx];
  1821. session = si->session;
  1822. if ((rv = f->C_SetOperationState(session , pin, strlen(pin),
  1823. CK_INVALID_HANDLE, CK_INVALID_HANDLE)) != CKR_OK) {
  1824. debug("%s: could not supply SO pin: %lu", __func__, rv);
  1825. reset_pin = 0;
  1826. } else
  1827. reset_pin = 1;
  1828. switch (type) {
  1829. case KEY_RSA:
  1830. if ((k = pkcs11_rsa_generate_private_key(p, slotidx, label,
  1831. bits, keyid, err)) == NULL) {
  1832. debug("%s: failed to generate RSA key", __func__);
  1833. goto out;
  1834. }
  1835. break;
  1836. case KEY_ECDSA:
  1837. if ((k = pkcs11_ecdsa_generate_private_key(p, slotidx, label,
  1838. bits, keyid, err)) == NULL) {
  1839. debug("%s: failed to generate ECDSA key", __func__);
  1840. goto out;
  1841. }
  1842. break;
  1843. default:
  1844. *err = SSH_PKCS11_ERR_GENERIC;
  1845. debug("%s: unknown type %d", __func__, type);
  1846. goto out;
  1847. }
  1848. out:
  1849. if (reset_pin)
  1850. f->C_SetOperationState(session , NULL, 0, CK_INVALID_HANDLE,
  1851. CK_INVALID_HANDLE);
  1852. if (reset_provider)
  1853. pkcs11_del_provider(provider_id);
  1854. return (k);
  1855. }
  1856. struct sshkey *
  1857. pkcs11_destroy_keypair(char *provider_id, char *pin, unsigned long slotidx,
  1858. unsigned char keyid, u_int32_t *err)
  1859. {
  1860. struct pkcs11_provider *p = NULL;
  1861. struct pkcs11_slotinfo *si;
  1862. struct sshkey *k = NULL;
  1863. int reset_pin = 0, reset_provider = 0;
  1864. CK_ULONG nattrs;
  1865. CK_FUNCTION_LIST *f;
  1866. CK_SESSION_HANDLE session;
  1867. CK_ATTRIBUTE attrs[16];
  1868. CK_OBJECT_CLASS key_class;
  1869. CK_KEY_TYPE key_type;
  1870. CK_OBJECT_HANDLE obj = CK_INVALID_HANDLE;
  1871. CK_RV rv;
  1872. *err = 0;
  1873. if ((p = pkcs11_provider_lookup(provider_id)) != NULL) {
  1874. debug("%s: using provider \"%s\"", __func__, provider_id);
  1875. } else if ((rv = pkcs11_register_provider(provider_id, pin, NULL, NULL,
  1876. &p, CKU_SO)) < 0) {
  1877. debug("%s: could not register provider %s", __func__,
  1878. provider_id);
  1879. goto out;
  1880. } else
  1881. reset_provider = 1;
  1882. f = p->function_list;
  1883. si = &p->slotinfo[slotidx];
  1884. session = si->session;
  1885. if ((rv = f->C_SetOperationState(session , pin, strlen(pin),
  1886. CK_INVALID_HANDLE, CK_INVALID_HANDLE)) != CKR_OK) {
  1887. debug("%s: could not supply SO pin: %lu", __func__, rv);
  1888. reset_pin = 0;
  1889. } else
  1890. reset_pin = 1;
  1891. /* private key */
  1892. nattrs = 0;
  1893. key_class = CKO_PRIVATE_KEY;
  1894. FILL_ATTR(attrs, nattrs, CKA_CLASS, &key_class, sizeof(key_class));
  1895. FILL_ATTR(attrs, nattrs, CKA_ID, &keyid, sizeof(keyid));
  1896. if (pkcs11_find(p, slotidx, attrs, nattrs, &obj) == 0 &&
  1897. obj != CK_INVALID_HANDLE) {
  1898. if ((rv = f->C_DestroyObject(session, obj)) != CKR_OK) {
  1899. debug("%s: could not destroy private key 0x%hhx",
  1900. __func__, keyid);
  1901. *err = rv;
  1902. goto out;
  1903. }
  1904. }
  1905. /* public key */
  1906. nattrs = 0;
  1907. key_class = CKO_PUBLIC_KEY;
  1908. FILL_ATTR(attrs, nattrs, CKA_CLASS, &key_class, sizeof(key_class));
  1909. FILL_ATTR(attrs, nattrs, CKA_ID, &keyid, sizeof(keyid));
  1910. if (pkcs11_find(p, slotidx, attrs, nattrs, &obj) == 0 &&
  1911. obj != CK_INVALID_HANDLE) {
  1912. /* get key type */
  1913. nattrs = 0;
  1914. FILL_ATTR(attrs, nattrs, CKA_KEY_TYPE, &key_type,
  1915. sizeof(key_type));
  1916. rv = f->C_GetAttributeValue(session, obj, attrs, nattrs);
  1917. if (rv != CKR_OK) {
  1918. debug("%s: could not get key type of public key 0x%hhx",
  1919. __func__, keyid);
  1920. *err = rv;
  1921. key_type = -1;
  1922. }
  1923. if (key_type == CKK_RSA)
  1924. k = pkcs11_fetch_rsa_pubkey(p, slotidx, &obj);
  1925. else if (key_type == CKK_ECDSA)
  1926. k = pkcs11_fetch_ecdsa_pubkey(p, slotidx, &obj);
  1927. if ((rv = f->C_DestroyObject(session, obj)) != CKR_OK) {
  1928. debug("%s: could not destroy public key 0x%hhx",
  1929. __func__, keyid);
  1930. *err = rv;
  1931. goto out;
  1932. }
  1933. }
  1934. out:
  1935. if (reset_pin)
  1936. f->C_SetOperationState(session , NULL, 0, CK_INVALID_HANDLE,
  1937. CK_INVALID_HANDLE);
  1938. if (reset_provider)
  1939. pkcs11_del_provider(provider_id);
  1940. return (k);
  1941. }
  1942. #endif /* WITH_PKCS11_KEYGEN */
  1943. #else /* ENABLE_PKCS11 */
  1944. #include <sys/types.h>
  1945. #include <stdarg.h>
  1946. #include <stdio.h>
  1947. #include "log.h"
  1948. #include "sshkey.h"
  1949. int
  1950. pkcs11_init(int interactive)
  1951. {
  1952. error("%s: dlopen() not supported", __func__);
  1953. return (-1);
  1954. }
  1955. int
  1956. pkcs11_add_provider(char *provider_id, char *pin, struct sshkey ***keyp,
  1957. char ***labelsp)
  1958. {
  1959. error("%s: dlopen() not supported", __func__);
  1960. return (-1);
  1961. }
  1962. void
  1963. pkcs11_terminate(void)
  1964. {
  1965. error("%s: dlopen() not supported", __func__);
  1966. }
  1967. #endif /* ENABLE_PKCS11 */