rand.c 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /* Licensed to the Apache Software Foundation (ASF) under one or more
  2. * contributor license agreements. See the NOTICE file distributed with
  3. * this work for additional information regarding copyright ownership.
  4. * The ASF licenses this file to You under the Apache License, Version 2.0
  5. * (the "License"); you may not use this file except in compliance with
  6. * the License. You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "fspr.h"
  17. #include "fspr_private.h"
  18. #include "fspr_general.h"
  19. #include "fspr_portable.h"
  20. #include "fspr_arch_misc.h"
  21. #include <wincrypt.h>
  22. APR_DECLARE(fspr_status_t) fspr_generate_random_bytes(unsigned char * buf,
  23. fspr_size_t length)
  24. {
  25. HCRYPTPROV hProv;
  26. fspr_status_t res = APR_SUCCESS;
  27. /* 0x40 bit = CRYPT_SILENT, only introduced in more recent PSDKs
  28. * and will only work for Win2K and later.
  29. */
  30. DWORD flags = CRYPT_VERIFYCONTEXT
  31. | ((fspr_os_level >= APR_WIN_2000) ? 0x40 : 0);
  32. if (!CryptAcquireContext(&hProv, NULL, NULL, PROV_RSA_FULL, flags)) {
  33. return fspr_get_os_error();
  34. }
  35. /* XXX: An ugly hack for Win64, randomness is such that noone should
  36. * ever expect > 2^31 bytes of data at once without the prng
  37. * coming to a complete halt.
  38. */
  39. if (!CryptGenRandom(hProv, (DWORD)length, buf)) {
  40. res = fspr_get_os_error();
  41. }
  42. CryptReleaseContext(hProv, 0);
  43. return res;
  44. }
  45. APR_DECLARE(fspr_status_t) fspr_os_uuid_get(unsigned char *uuid_data)
  46. {
  47. /* Note: this call doesn't actually require CoInitialize() first
  48. *
  49. * XXX: we should scramble the bytes or some such to eliminate the
  50. * possible misuse/abuse since uuid is based on the NIC address, and
  51. * is therefore not only a uniqifier, but an identity (which might not
  52. * be appropriate in all cases.
  53. *
  54. * Note that Win2000, XP and later no longer suffer from this problem,
  55. * a scrambling fix is only needed for (fspr_os_level < APR_WIN_2000)
  56. */
  57. if (FAILED(UuidCreate((UUID *)uuid_data))) {
  58. return APR_EGENERAL;
  59. }
  60. return APR_SUCCESS;
  61. }