des.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. /** BEGIN COPYRIGHT BLOCK
  2. * This Program is free software; you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation; version 2 of the License.
  5. *
  6. * This Program is distributed in the hope that it will be useful, but WITHOUT
  7. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  9. *
  10. * You should have received a copy of the GNU General Public License along with
  11. * this Program; if not, write to the Free Software Foundation, Inc., 59 Temple
  12. * Place, Suite 330, Boston, MA 02111-1307 USA.
  13. *
  14. * In addition, as a special exception, Red Hat, Inc. gives You the additional
  15. * right to link the code of this Program with code not covered under the GNU
  16. * General Public License ("Non-GPL Code") and to distribute linked combinations
  17. * including the two, subject to the limitations in this paragraph. Non-GPL Code
  18. * permitted under this exception must only link to the code of this Program
  19. * through those well defined interfaces identified in the file named EXCEPTION
  20. * found in the source code files (the "Approved Interfaces"). The files of
  21. * Non-GPL Code may instantiate templates or use macros or inline functions from
  22. * the Approved Interfaces without causing the resulting work to be covered by
  23. * the GNU General Public License. Only Red Hat, Inc. may make changes or
  24. * additions to the list of Approved Interfaces. You must obey the GNU General
  25. * Public License in all respects for all of the Program code and other code used
  26. * in conjunction with the Program except the Non-GPL Code covered by this
  27. * exception. If you modify this file, you may extend this exception to your
  28. * version of the file, but you are not obligated to do so. If you do not wish to
  29. * provide this exception without modification, you must delete this exception
  30. * statement from your version and license this file solely under the GPL without
  31. * exception.
  32. *
  33. *
  34. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  35. * Copyright (C) 2005 Red Hat, Inc.
  36. * All rights reserved.
  37. * END COPYRIGHT BLOCK **/
  38. #ifdef HAVE_CONFIG_H
  39. # include <config.h>
  40. #endif
  41. /* from /usr/project/iplanet/ws/ds5.ke/ns/svrcore/pkcs7/tstarchive.c */
  42. #include <string.h>
  43. #include <stdio.h>
  44. #include <ldap.h>
  45. #include <nspr.h>
  46. #include <nss.h>
  47. #include <secmod.h>
  48. /*
  49. #include <secasn1.h>
  50. #include <secpkcs7.h>
  51. */
  52. #include <key.h>
  53. #include <certdb.h>
  54. #include <cert.h>
  55. #include <svrcore.h>
  56. #include <secmodt.h>
  57. #include <prtypes.h>
  58. #include <seccomon.h>
  59. #include <pk11func.h>
  60. #define NEED_TOK_DES /* see slap.h - defines tokDes and ptokDes */
  61. #include "rever.h"
  62. #include <slap.h>
  63. #include "slapi-plugin.h"
  64. #include <uuid.h>
  65. struct pk11MechItem
  66. {
  67. CK_MECHANISM_TYPE type;
  68. const char *mechName;
  69. };
  70. static const struct pk11MechItem mymech = { CKM_DES_CBC, "DES CBC encryption" };
  71. static Slapi_Mutex *mylock = NULL;
  72. struct pk11ContextStore
  73. {
  74. PK11SlotInfo *slot;
  75. const struct pk11MechItem *mech;
  76. PK11SymKey *key;
  77. SECItem *params;
  78. int length;
  79. unsigned char *crypt;
  80. };
  81. static int encode_path(char *inPlain, char **outCipher, char *path);
  82. static int decode_path(char *inCipher, char **outPlain, char *path);
  83. static SVRCOREError genKey(struct pk11ContextStore **out, const char *token, char *path);
  84. static SVRCOREError cryptPassword(struct pk11ContextStore *store, char * clear, unsigned char **out);
  85. static SVRCOREError decryptPassword(struct pk11ContextStore *store, unsigned char *cipher, char **out, int len);
  86. static void freeDes(struct pk11ContextStore *out);
  87. void
  88. init_des_plugin()
  89. {
  90. mylock = slapi_new_mutex();
  91. }
  92. int
  93. encode(char *inPlain, char **outCipher)
  94. {
  95. return encode_path(inPlain, outCipher, NULL);
  96. }
  97. static int
  98. encode_path(char *inPlain, char **outCipher, char *path)
  99. {
  100. struct pk11ContextStore *context = NULL;
  101. int err;
  102. unsigned char *cipher = NULL;
  103. char *tmp = NULL;
  104. char *base = NULL;
  105. *outCipher = NULL;
  106. err = 1;
  107. if ( genKey(&context, tokDes, path) == SVRCORE_Success )
  108. {
  109. /* Try an encryption */
  110. if ( cryptPassword(context, inPlain, &cipher) == SVRCORE_Success )
  111. {
  112. base = BTOA_DataToAscii(cipher, context->length);
  113. if ( base != NULL )
  114. {
  115. tmp = slapi_ch_malloc( 3 + strlen(REVER_SCHEME_NAME) + strlen(base));
  116. if ( tmp != NULL )
  117. {
  118. sprintf( tmp, "%c%s%c%s", PWD_HASH_PREFIX_START, REVER_SCHEME_NAME, PWD_HASH_PREFIX_END, base);
  119. *outCipher = tmp;
  120. tmp = NULL;
  121. err = 0;
  122. }
  123. PORT_Free(base);
  124. }
  125. }
  126. }
  127. freeDes(context);
  128. slapi_ch_free((void **) &context);
  129. return(err);
  130. }
  131. int
  132. decode(char *inCipher, char **outPlain)
  133. {
  134. return decode_path(inCipher, outPlain, NULL);
  135. }
  136. static int
  137. decode_path(char *inCipher, char **outPlain, char *path)
  138. {
  139. struct pk11ContextStore *context = NULL;
  140. char *plain= NULL;
  141. int err;
  142. unsigned char *base = NULL;
  143. int len = 0;
  144. *outPlain = NULL;
  145. err = 1;
  146. if ( genKey(&context, tokDes, path) == SVRCORE_Success )
  147. {
  148. /* it seems that there is memory leak in that function: bug 400170 */
  149. base = ATOB_AsciiToData(inCipher, (unsigned int*)&len);
  150. if ( base != NULL )
  151. {
  152. if ( decryptPassword(context, base, &plain, len) == SVRCORE_Success )
  153. {
  154. *outPlain = plain;
  155. err = 0;
  156. }
  157. }
  158. }
  159. PORT_Free(base);
  160. freeDes(context);
  161. slapi_ch_free((void **) &context);
  162. return(err);
  163. }
  164. static void freeDes(struct pk11ContextStore *out)
  165. {
  166. if (out)
  167. {
  168. if (out->slot)
  169. slapd_pk11_freeSlot(out->slot);
  170. if (out->key)
  171. slapd_pk11_freeSymKey(out->key);
  172. if (out->params)
  173. SECITEM_FreeItem(out->params,PR_TRUE);
  174. if (out->crypt)
  175. free(out->crypt);
  176. }
  177. }
  178. static SVRCOREError genKey(struct pk11ContextStore **out, const char *token, char *path)
  179. {
  180. SVRCOREError err = SVRCORE_Success;
  181. struct pk11ContextStore *store = NULL;
  182. SECItem *pwitem = NULL;
  183. SECItem *result = NULL;
  184. SECAlgorithmID *algid = NULL;
  185. SECOidTag algoid;
  186. SECItem *salt = NULL;
  187. CK_MECHANISM pbeMech;
  188. CK_MECHANISM cryptoMech;
  189. char *configdir = NULL;
  190. char *iv = NULL;
  191. store = (struct pk11ContextStore*)slapi_ch_malloc(sizeof(*store));
  192. if (store == NULL)
  193. {
  194. err = SVRCORE_NoMemory_Error;
  195. goto done;
  196. }
  197. *out = store;
  198. /* Low-level init */
  199. store->slot = NULL;
  200. store->key = NULL;
  201. store->params = NULL;
  202. store->crypt = NULL;
  203. /* Use the tokenName to find a PKCS11 slot */
  204. store->slot = slapd_pk11_findSlotByName((char *)token);
  205. if (store->slot == NULL)
  206. {
  207. err = SVRCORE_NoSuchToken_Error;
  208. goto done;
  209. }
  210. /* Generate a key and parameters to do the encryption */
  211. store->mech = &mymech;
  212. /* Generate a unique id, used as salt for the key generation */
  213. if ( path == NULL )
  214. {
  215. configdir = config_get_configdir();
  216. if ( configdir == NULL )
  217. {
  218. err = SVRCORE_System_Error;
  219. goto done;
  220. }
  221. }
  222. else
  223. {
  224. configdir = slapi_ch_strdup(path);
  225. }
  226. if ( slapi_uniqueIDGenerateFromNameString (&iv, NULL, configdir, strlen(configdir)) != UID_SUCCESS )
  227. {
  228. slapi_ch_free((void**)&configdir);
  229. err = SVRCORE_System_Error;
  230. goto done;
  231. }
  232. slapi_ch_free((void**)&configdir);
  233. pwitem = (SECItem *) PORT_Alloc(sizeof(SECItem));
  234. if (pwitem == NULL)
  235. {
  236. err = SVRCORE_NoMemory_Error;
  237. goto done;
  238. }
  239. pwitem->type = siBuffer;
  240. pwitem->data = (unsigned char *)PORT_Alloc(strlen(iv)+1);
  241. if (pwitem->data == NULL)
  242. {
  243. err = SVRCORE_NoMemory_Error;
  244. goto done;
  245. }
  246. strcpy((char*)pwitem->data, iv);
  247. pwitem->len = strlen(iv) + 1;
  248. algoid = SEC_OID_PKCS5_PBE_WITH_MD2_AND_DES_CBC;
  249. salt = (SECItem *) PORT_Alloc(sizeof(SECItem));
  250. if (salt == NULL)
  251. {
  252. err = SVRCORE_NoMemory_Error;
  253. goto done;
  254. }
  255. salt->type = siBuffer;
  256. salt->data = (unsigned char *)PORT_Alloc(strlen(iv)+1);
  257. if ( salt->data == NULL )
  258. {
  259. err = SVRCORE_NoMemory_Error;
  260. goto done;
  261. }
  262. strcpy((char*)salt->data, iv);
  263. salt->len = strlen(iv) + 1;
  264. algid = slapd_pk11_createPBEAlgorithmID(algoid, 2, salt);
  265. slapi_lock_mutex(mylock);
  266. store->key = slapd_pk11_pbeKeyGen(store->slot, algid, pwitem, 0, 0);
  267. if (store->key == 0)
  268. {
  269. slapi_unlock_mutex(mylock);
  270. err = SVRCORE_System_Error;
  271. goto done;
  272. }
  273. slapi_unlock_mutex(mylock);
  274. pbeMech.mechanism = slapd_pk11_algtagToMechanism(algoid);
  275. result = slapd_pk11_paramFromAlgid(algid);
  276. secoid_destroyAlgorithmID(algid, PR_TRUE);
  277. pbeMech.pParameter = result->data;
  278. pbeMech.ulParameterLen = result->len;
  279. if(slapd_pk11_mapPBEMechanismToCryptoMechanism(&pbeMech, &cryptoMech, pwitem,
  280. PR_FALSE) != CKR_OK)
  281. {
  282. SECITEM_FreeItem(result, PR_TRUE);
  283. err = SVRCORE_System_Error;
  284. goto done;
  285. }
  286. SECITEM_FreeItem(result, PR_TRUE);
  287. SECITEM_FreeItem(pwitem, PR_TRUE);
  288. SECITEM_FreeItem(salt, PR_TRUE);
  289. store->params = (SECItem *) PORT_Alloc(sizeof(SECItem));
  290. if (store->params == NULL)
  291. {
  292. err = SVRCORE_System_Error;
  293. goto done;
  294. }
  295. store->params->type = store->mech->type;
  296. store->params->data = (unsigned char *)PORT_Alloc(cryptoMech.ulParameterLen);
  297. if (store->params->data == NULL)
  298. {
  299. err = SVRCORE_System_Error;
  300. goto done;
  301. }
  302. memcpy(store->params->data, (unsigned char *)cryptoMech.pParameter, cryptoMech.ulParameterLen);
  303. store->params->len = cryptoMech.ulParameterLen;
  304. PORT_Free(cryptoMech.pParameter);
  305. done:
  306. slapi_ch_free((void**)&iv);
  307. return (err);
  308. }
  309. static SVRCOREError decryptPassword(struct pk11ContextStore *store, unsigned char *cipher, char **out, int len)
  310. {
  311. SVRCOREError err = SVRCORE_Success;
  312. unsigned char *plain = NULL;
  313. unsigned char *cipher_with_padding = NULL;
  314. SECStatus rv;
  315. PK11Context *ctx = 0;
  316. int outLen = 0;
  317. int blocksize = 0;
  318. blocksize = slapd_pk11_getBlockSize(store->mech->type, 0);
  319. store->length = len;
  320. /* store->length is the max. length of the returned clear text -
  321. must be >= length of crypted bytes - also must be a multiple
  322. of blocksize */
  323. if (blocksize != 0)
  324. {
  325. store->length += blocksize - (store->length % blocksize);
  326. }
  327. /* plain will hold the returned clear text */
  328. plain = (unsigned char *)slapi_ch_calloc(sizeof(unsigned char),
  329. store->length+1);
  330. if (!plain)
  331. {
  332. err = SVRCORE_NoMemory_Error;
  333. goto done;
  334. }
  335. /* create a buffer holding the original cipher bytes, padded with
  336. zeros to a multiple of blocksize - do not need +1 since buffer is not
  337. a string */
  338. cipher_with_padding = (unsigned char *)slapi_ch_calloc(sizeof(unsigned char),
  339. store->length);
  340. if (!cipher_with_padding)
  341. {
  342. err = SVRCORE_NoMemory_Error;
  343. goto done;
  344. }
  345. memcpy(cipher_with_padding, cipher, len);
  346. ctx = slapd_pk11_createContextBySymKey(store->mech->type, CKA_DECRYPT,
  347. store->key, store->params);
  348. if (!ctx)
  349. {
  350. err = SVRCORE_System_Error;
  351. goto done;
  352. }
  353. /* warning - there is a purify UMR in the NSS des code - you may see it when the
  354. password is not a multiple of 8 bytes long */
  355. rv = slapd_pk11_cipherOp(ctx, plain, &outLen, store->length,
  356. cipher_with_padding, store->length);
  357. if (rv)
  358. {
  359. err = SVRCORE_System_Error;
  360. }
  361. rv = slapd_pk11_finalize(ctx);
  362. /* we must do the finalize, but we only want to set the err return
  363. code if it is not already set */
  364. if (rv && (SVRCORE_Success == err))
  365. err = SVRCORE_System_Error;
  366. done:
  367. if (err == SVRCORE_Success)
  368. {
  369. *out = (char *)plain;
  370. }
  371. else
  372. {
  373. slapi_ch_free((void **)&plain);
  374. }
  375. slapi_ch_free((void **)&cipher_with_padding);
  376. /* We should free the PK11Context... Something like : */
  377. if (ctx) slapd_pk11_destroyContext(ctx, PR_TRUE);
  378. return err;
  379. }
  380. static SVRCOREError cryptPassword(struct pk11ContextStore *store, char * clear, unsigned char **out)
  381. {
  382. SVRCOREError err = SVRCORE_Success;
  383. SECStatus rv;
  384. PK11Context *ctx = 0;
  385. int outLen = 0;
  386. int blocksize = 0;
  387. unsigned char *clear_with_padding = NULL; /* clear with padding up to blocksize */
  388. blocksize = slapd_pk11_getBlockSize(store->mech->type, 0);
  389. store->length = strlen(clear);
  390. /* the size of the clear text buffer passed to the des encryption functions
  391. must be a multiple of blocksize (usually 8 bytes) - we allocate a buffer
  392. of this size, copy the clear text password into it, and pad the rest with
  393. zeros */
  394. if (blocksize != 0)
  395. {
  396. store->length += blocksize - (store->length % blocksize);
  397. }
  398. /* store->crypt will hold the crypted password - it must be >= clear length */
  399. /* store->crypt is freed in NSS; let's not use slapi_ch_calloc */
  400. store->crypt = (unsigned char *)calloc(sizeof(unsigned char),
  401. store->length+1);
  402. if (!store->crypt)
  403. {
  404. err = SVRCORE_NoMemory_Error;
  405. goto done;
  406. }
  407. /* create a buffer big enough to hold the clear text password and padding */
  408. clear_with_padding = (unsigned char *)slapi_ch_calloc(sizeof(unsigned char),
  409. store->length+1);
  410. if (!clear_with_padding)
  411. {
  412. err = SVRCORE_NoMemory_Error;
  413. goto done;
  414. }
  415. /* copy the clear text password into the buffer - the calloc insures the
  416. remainder is zero padded */
  417. strcpy((char *)clear_with_padding, clear);
  418. ctx = slapd_pk11_createContextBySymKey(store->mech->type, CKA_ENCRYPT,
  419. store->key, store->params);
  420. if (!ctx)
  421. {
  422. err = SVRCORE_System_Error;
  423. goto done;
  424. }
  425. rv = slapd_pk11_cipherOp(ctx, store->crypt, &outLen, store->length,
  426. clear_with_padding, store->length);
  427. if (rv)
  428. {
  429. err = SVRCORE_System_Error;
  430. }
  431. rv = slapd_pk11_finalize(ctx);
  432. /* we must do the finalize, but we only want to set the err return
  433. code if it is not already set */
  434. if (rv && (SVRCORE_Success == err))
  435. err = SVRCORE_System_Error;
  436. done:
  437. if (err == SVRCORE_Success)
  438. *out = store->crypt;
  439. slapi_ch_free((void **)&clear_with_padding);
  440. /* We should free the PK11Context... Something like : */
  441. if (ctx) slapd_pk11_destroyContext(ctx, PR_TRUE);
  442. return err;
  443. }
  444. /*
  445. The UUID name based generator was broken on x86 platforms. We use
  446. this to generate the password encryption key. During migration,
  447. we have to fix this so we can use the fixed generator. The env.
  448. var USE_BROKEN_UUID tells the uuid generator to use the old
  449. broken method to create the UUID. That will allow us to decrypt
  450. the password to the correct clear text, then we can turn off
  451. the broken method and use the fixed method to encrypt the
  452. password.
  453. */
  454. char *
  455. migrateCredentials(char *oldpath, char *newpath, char *oldcred)
  456. {
  457. static char *useBrokenUUID = "USE_BROKEN_UUID=1";
  458. static char *disableBrokenUUID = "USE_BROKEN_UUID=0";
  459. char *plain = NULL;
  460. char *cipher = NULL;
  461. init_des_plugin();
  462. slapd_pk11_configurePKCS11(NULL, NULL, tokDes, ptokDes, NULL, NULL, NULL, NULL, 0, 0 );
  463. NSS_NoDB_Init(NULL);
  464. if (getenv("MIGRATE_BROKEN_PWD")) {
  465. putenv(useBrokenUUID);
  466. }
  467. if ( decode_path(oldcred, &plain, oldpath) == 0 )
  468. {
  469. if (getenv("MIGRATE_BROKEN_PWD")) {
  470. putenv(disableBrokenUUID);
  471. }
  472. if ( encode_path(plain, &cipher, newpath) != 0 )
  473. return(NULL);
  474. else
  475. return(cipher);
  476. }
  477. else
  478. return(NULL);
  479. }