Commit cf43882e9fb55b66776b9fc6c812487d772d37ca
1 parent
857bb208
Handle Microsoft crypt provider without prior keys
As reported in issue #40, a call to CryptAcquireContext in SecureRandomDataProvider fails in a fresh windows install prior to any user keys being created in AppData\Roaming\Microsoft\Crypto\RSA. Thanks michalrames.
Showing
2 changed files
with
37 additions
and
2 deletions
ChangeLog
| 1 | 1 | 2015-05-24 Jay Berkenbilt <ejb@ql.org> |
| 2 | 2 | |
| 3 | + * Handle Microsoft crypt provider initialization properly for case | |
| 4 | + where no keys have been previously created, such as in a fresh | |
| 5 | + Windows installation. | |
| 6 | + | |
| 3 | 7 | * Include time.h in QUtil.hh for time_t |
| 4 | 8 | |
| 5 | 9 | 2015-02-21 Jay Berkenbilt <ejb@ql.org> | ... | ... |
libqpdf/SecureRandomDataProvider.cc
| ... | ... | @@ -42,9 +42,40 @@ class WindowsCryptProvider |
| 42 | 42 | public: |
| 43 | 43 | WindowsCryptProvider() |
| 44 | 44 | { |
| 45 | - if (! CryptAcquireContext(&crypt_prov, NULL, NULL, PROV_RSA_FULL, 0)) | |
| 45 | + if (!CryptAcquireContext(&crypt_prov, | |
| 46 | + "Container", | |
| 47 | + NULL, | |
| 48 | + PROV_RSA_FULL, | |
| 49 | + 0)) | |
| 46 | 50 | { |
| 47 | - throw std::runtime_error("unable to acquire crypt context"); | |
| 51 | +#ifdef __GNUC__ | |
| 52 | +# if ((__GNUC__ * 100) + __GNUC_MINOR__) >= 406 | |
| 53 | +# pragma GCC diagnostic push | |
| 54 | +# pragma GCC diagnostic ignored "-Wold-style-cast" | |
| 55 | +# pragma GCC diagnostic ignored "-Wsign-compare" | |
| 56 | +# endif | |
| 57 | +#endif | |
| 58 | + if (GetLastError() == NTE_BAD_KEYSET) | |
| 59 | +#ifdef __GNUC__ | |
| 60 | +# if ((__GNUC__ * 100) + __GNUC_MINOR__) >= 406 | |
| 61 | +# pragma GCC diagnostic pop | |
| 62 | +# endif | |
| 63 | +#endif | |
| 64 | + { | |
| 65 | + if (! CryptAcquireContext(&crypt_prov, | |
| 66 | + "Container", | |
| 67 | + NULL, | |
| 68 | + PROV_RSA_FULL, | |
| 69 | + CRYPT_NEWKEYSET)) | |
| 70 | + { | |
| 71 | + throw std::runtime_error( | |
| 72 | + "unable to acquire crypt context with new keyset"); | |
| 73 | + } | |
| 74 | + } | |
| 75 | + else | |
| 76 | + { | |
| 77 | + throw std::runtime_error("unable to acquire crypt context"); | |
| 78 | + } | |
| 48 | 79 | } |
| 49 | 80 | } |
| 50 | 81 | ~WindowsCryptProvider() | ... | ... |