| 1883 | } |
| 1884 | |
| 1885 | static HCERTSTORE open_cert_db(const char *store_name, const char *passwd, int *error) { |
| 1886 | *error = 0; |
| 1887 | DWORD sys_store_type = 0; |
| 1888 | HCERTSTORE cert_store = 0; |
| 1889 | |
| 1890 | if (store_name) { |
| 1891 | if (strncmp(store_name, "ss:", 3) == 0) { |
| 1892 | store_name += 3; |
| 1893 | sys_store_type = CERT_SYSTEM_STORE_CURRENT_USER; |
| 1894 | } |
| 1895 | else if (strncmp(store_name, "lmss:", 5) == 0) { |
| 1896 | store_name += 5; |
| 1897 | sys_store_type = CERT_SYSTEM_STORE_LOCAL_MACHINE; |
| 1898 | } |
| 1899 | } |
| 1900 | |
| 1901 | if (sys_store_type) { |
| 1902 | // Opening a system store, names are not case sensitive. |
| 1903 | // Map confusing GUI name to actual registry store name. |
| 1904 | if (!pn_strcasecmp(store_name, "personal")) store_name= "my"; |
| 1905 | cert_store = CertOpenStore(CERT_STORE_PROV_SYSTEM_A, 0, NULL, |
| 1906 | CERT_STORE_OPEN_EXISTING_FLAG | CERT_STORE_READONLY_FLAG | |
| 1907 | sys_store_type, store_name); |
| 1908 | if (!cert_store) { |
| 1909 | ssl_log_error_status(GetLastError(), "Failed to open system certificate store %s", store_name); |
| 1910 | *error = -3; |
| 1911 | return NULL; |
| 1912 | } |
| 1913 | } else { |
| 1914 | // PKCS#12 file |
| 1915 | HANDLE cert_file = CreateFile(store_name, GENERIC_READ, 0, NULL, OPEN_EXISTING, |
| 1916 | FILE_ATTRIBUTE_NORMAL, NULL); |
| 1917 | if (INVALID_HANDLE_VALUE == cert_file) { |
| 1918 | HRESULT status = GetLastError(); |
| 1919 | ssl_log_error_status(status, "Failed to open the file holding the private key: %s", store_name); |
| 1920 | *error = -4; |
| 1921 | return NULL; |
| 1922 | } |
| 1923 | DWORD nread = 0L; |
| 1924 | const DWORD file_size = GetFileSize(cert_file, NULL); |
| 1925 | char *buf = NULL; |
| 1926 | if (INVALID_FILE_SIZE != file_size) |
| 1927 | buf = (char *) malloc(file_size); |
| 1928 | if (!buf || !ReadFile(cert_file, buf, file_size, &nread, NULL) |
| 1929 | || file_size != nread) { |
| 1930 | HRESULT status = GetLastError(); |
| 1931 | CloseHandle(cert_file); |
| 1932 | free(buf); |
| 1933 | ssl_log_error_status(status, "Reading the private key from file failed %s", store_name); |
| 1934 | *error = -5; |
| 1935 | return NULL; |
| 1936 | } |
| 1937 | CloseHandle(cert_file); |
| 1938 | |
| 1939 | CRYPT_DATA_BLOB blob; |
| 1940 | blob.cbData = nread; |
| 1941 | blob.pbData = (BYTE *) buf; |
| 1942 |
no test coverage detected