patch-lib_hx509_softp11_c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. $OpenBSD: patch-lib_hx509_softp11_c,v 1.2 2016/12/17 14:58:31 ajacoutot Exp $
  2. Use HAVE_ARC4RANDOM and choose to call arc4random() instead of srand()
  3. and rand() and friends.
  4. --- lib/hx509/softp11.c.orig Tue Nov 29 02:35:27 2016
  5. +++ lib/hx509/softp11.c Thu Dec 15 11:29:38 2016
  6. @@ -33,6 +33,7 @@
  7. #define CRYPTOKI_EXPORTS 1
  8. +#include <config.h>
  9. #include "hx_locl.h"
  10. #include "ref/pkcs11.h"
  11. @@ -304,6 +305,7 @@ static struct st_object *
  12. add_st_object(void)
  13. {
  14. struct st_object *o, **objs;
  15. + u_int32_t rnd;
  16. int i;
  17. o = calloc(1, sizeof(*o));
  18. @@ -326,8 +328,14 @@ add_st_object(void)
  19. soft_token.object.objs = objs;
  20. soft_token.object.objs[soft_token.object.num_objs++] = o;
  21. }
  22. +#ifdef HAVE_ARC4RANDOM
  23. + rnd = arc4random();
  24. +#else
  25. + rnd = random();
  26. +#endif
  27. +
  28. soft_token.object.objs[i]->object_handle =
  29. - (random() & (~OBJECT_ID_MASK)) | i;
  30. + (rnd & (~OBJECT_ID_MASK)) | i;
  31. return o;
  32. }
  33. @@ -878,7 +886,9 @@ C_Initialize(CK_VOID_PTR a)
  34. OpenSSL_add_all_algorithms();
  35. +#ifndef HAVE_ARC4RANDOM
  36. srandom(getpid() ^ (int) time(NULL));
  37. +#endif
  38. for (i = 0; i < MAX_NUM_SESSION; i++) {
  39. soft_token.state[i].session_handle = CK_INVALID_HANDLE;
  40. @@ -1124,6 +1134,7 @@ C_OpenSession(CK_SLOT_ID slotID,
  41. CK_SESSION_HANDLE_PTR phSession)
  42. {
  43. size_t i;
  44. + u_int32_t rnd;
  45. INIT_CONTEXT();
  46. st_logf("OpenSession: slot: %d\n", (int)slotID);
  47. @@ -1139,10 +1150,15 @@ C_OpenSession(CK_SLOT_ID slotID,
  48. if (i == MAX_NUM_SESSION)
  49. abort();
  50. - soft_token.open_sessions++;
  51. +#ifdef HAVE_ARC4RANDOM
  52. + rnd = arc4random();
  53. +#else
  54. + rnd = random();
  55. +#endif
  56. + soft_token.open_sessions++;
  57. soft_token.state[i].session_handle =
  58. - (CK_SESSION_HANDLE)(random() & 0xfffff);
  59. + (CK_SESSION_HANDLE)(rnd & 0xfffff);
  60. *phSession = soft_token.state[i].session_handle;
  61. return CKR_OK;