2 * Copyright (C) 2010-2012 Free Software Foundation, Inc.
4 * Author: Nikos Mavrogiannopoulos
6 * This file is part of GnuTLS.
8 * The GnuTLS is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public License
10 * as published by the Free Software Foundation; either version 3 of
11 * the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>
24 #include <gnutls_int.h>
25 #include <gnutls_errors.h>
29 #include <sys/types.h>
33 # include <wincrypt.h>
34 # if defined(__MINGW32__) && !defined(__MINGW64__) && __MINGW32_MAJOR_VERSION <= 3 && __MINGW32_MINOR_VERSION <= 20
35 typedef PCCRL_CONTEXT
WINAPI (*Type_CertEnumCRLsInStore
) (HCERTSTORE hCertStore
, PCCRL_CONTEXT pPrevCrlContext
);
36 static Type_CertEnumCRLsInStore Loaded_CertEnumCRLsInStore
;
37 static HMODULE Crypt32_dll
;
41 # ifdef HAVE_PTHREAD_LOCKS
45 # if defined(HAVE_GETPWUID_R)
50 /* We need to disable gnulib's replacement wrappers to get native
51 Windows interfaces. */
56 /* System specific function wrappers.
61 system_errno (gnutls_transport_ptr p
)
63 int tmperr
= WSAGetLastError ();
83 WSASetLastError (tmperr
);
89 system_write (gnutls_transport_ptr ptr
, const void *data
, size_t data_size
)
91 return send (GNUTLS_POINTER_TO_INT (ptr
), data
, data_size
, 0);
95 system_errno (gnutls_transport_ptr_t ptr
)
97 #if defined(_AIX) || defined(AIX)
98 if (errno
== 0) errno
= EAGAIN
;
105 system_writev (gnutls_transport_ptr_t ptr
, const giovec_t
* iovec
,
108 return writev (GNUTLS_POINTER_TO_INT (ptr
), (struct iovec
*) iovec
,
115 system_read (gnutls_transport_ptr_t ptr
, void *data
, size_t data_size
)
117 return recv (GNUTLS_POINTER_TO_INT (ptr
), data
, data_size
, 0);
120 /* Wait for data to be received within a timeout period in milliseconds.
121 * To catch a termination it will also try to receive 0 bytes from the
122 * socket if select reports to proceed.
124 * Returns -1 on error, 0 on timeout, positive value if data are available for reading.
126 int system_recv_timeout(gnutls_transport_ptr_t ptr
, unsigned int ms
)
131 int fd
= GNUTLS_POINTER_TO_INT(ptr
);
137 tv
.tv_usec
= ms
* 1000;
139 while(tv
.tv_usec
>= 1000000)
141 tv
.tv_usec
-= 1000000;
145 ret
= select(fd
+1, &rfds
, NULL
, NULL
, &tv
);
154 #ifdef HAVE_WIN32_LOCKS
156 gnutls_system_mutex_init (void **priv
)
158 CRITICAL_SECTION
*lock
= malloc (sizeof (CRITICAL_SECTION
));
161 return GNUTLS_E_MEMORY_ERROR
;
163 InitializeCriticalSection (lock
);
171 gnutls_system_mutex_deinit (void **priv
)
173 DeleteCriticalSection ((CRITICAL_SECTION
*) * priv
);
180 gnutls_system_mutex_lock (void **priv
)
182 EnterCriticalSection ((CRITICAL_SECTION
*) * priv
);
187 gnutls_system_mutex_unlock (void **priv
)
189 LeaveCriticalSection ((CRITICAL_SECTION
*) * priv
);
193 #endif /* WIN32_LOCKS */
195 #ifdef HAVE_PTHREAD_LOCKS
198 gnutls_system_mutex_init (void **priv
)
200 pthread_mutex_t
*lock
= malloc (sizeof (pthread_mutex_t
));
204 return GNUTLS_E_MEMORY_ERROR
;
206 ret
= pthread_mutex_init (lock
, NULL
);
211 return GNUTLS_E_LOCKING_ERROR
;
220 gnutls_system_mutex_deinit (void **priv
)
222 pthread_mutex_destroy ((pthread_mutex_t
*) * priv
);
228 gnutls_system_mutex_lock (void **priv
)
230 if (pthread_mutex_lock ((pthread_mutex_t
*) * priv
))
233 return GNUTLS_E_LOCKING_ERROR
;
240 gnutls_system_mutex_unlock (void **priv
)
242 if (pthread_mutex_unlock ((pthread_mutex_t
*) * priv
))
245 return GNUTLS_E_LOCKING_ERROR
;
251 #endif /* PTHREAD_LOCKS */
256 gnutls_system_mutex_init (void **priv
)
262 gnutls_system_mutex_deinit (void **priv
)
268 gnutls_system_mutex_lock (void **priv
)
274 gnutls_system_mutex_unlock (void **priv
)
279 #endif /* NO_LOCKS */
281 gnutls_time_func gnutls_time
= time
;
282 mutex_init_func gnutls_mutex_init
= gnutls_system_mutex_init
;
283 mutex_deinit_func gnutls_mutex_deinit
= gnutls_system_mutex_deinit
;
284 mutex_lock_func gnutls_mutex_lock
= gnutls_system_mutex_lock
;
285 mutex_unlock_func gnutls_mutex_unlock
= gnutls_system_mutex_unlock
;
288 gnutls_system_global_init ()
291 # if defined(__MINGW32__) && !defined(__MINGW64__) && __MINGW32_MAJOR_VERSION <= 3 && __MINGW32_MINOR_VERSION <= 20
293 crypto
= LoadLibraryA ("Crypt32.dll");
296 return GNUTLS_E_CRYPTO_INIT_FAILED
;
298 Loaded_CertEnumCRLsInStore
= (Type_CertEnumCRLsInStore
) GetProcAddress (crypto
, "CertEnumCRLsInStore");
299 if (Loaded_CertEnumCRLsInStore
== NULL
)
301 FreeLibrary (crypto
);
302 return GNUTLS_E_CRYPTO_INIT_FAILED
;
305 Crypt32_dll
= crypto
;
312 gnutls_system_global_deinit ()
315 # if defined(__MINGW32__) && !defined(__MINGW64__) && __MINGW32_MAJOR_VERSION <= 3 && __MINGW32_MINOR_VERSION <= 20
316 FreeLibrary (Crypt32_dll
);
322 #define CONFIG_PATH ".gnutls"
324 /* Returns a path to store user-specific configuration
327 int _gnutls_find_config_path(char* path
, size_t max_size
)
329 char tmp_home_dir
[1024];
330 const char *home_dir
= getenv ("HOME");
333 if (home_dir
== NULL
|| home_dir
[0] == '\0')
335 const char *home_drive
= getenv ("HOMEDRIVE");
336 const char *home_path
= getenv ("HOMEPATH");
338 if (home_drive
!= NULL
&& home_path
!= NULL
)
340 snprintf(tmp_home_dir
, sizeof(tmp_home_dir
), "%s%s", home_drive
, home_path
);
347 home_dir
= tmp_home_dir
;
349 #elif defined(HAVE_GETPWUID_R)
350 if (home_dir
== NULL
|| home_dir
[0] == '\0')
356 getpwuid_r(getuid(), &_pwd
, buf
, sizeof(buf
), &pwd
);
359 snprintf(tmp_home_dir
, sizeof(tmp_home_dir
), "%s", pwd
->pw_dir
);
366 home_dir
= tmp_home_dir
;
369 if (home_dir
== NULL
|| home_dir
[0] == '\0')
372 home_dir
= tmp_home_dir
;
376 if (home_dir
== NULL
|| home_dir
[0] == 0)
379 snprintf(path
, max_size
, "%s/"CONFIG_PATH
, home_dir
);
385 * gnutls_x509_trust_list_add_system_trust:
386 * @list: The structure of the list
387 * @tl_flags: GNUTLS_TL_*
388 * @tl_vflags: gnutls_certificate_verify_flags if flags specifies GNUTLS_TL_VERIFY_CRL
390 * This function adds the system's default trusted certificate
391 * authorities to the trusted list. Note that on unsupported system
392 * this function returns %GNUTLS_E_UNIMPLEMENTED_FEATURE.
394 * Returns: The number of added elements or a negative error code on error.
399 gnutls_x509_trust_list_add_system_trust(gnutls_x509_trust_list_t list
,
400 unsigned int tl_flags
, unsigned int tl_vflags
)
402 #if !defined(DEFAULT_TRUST_STORE_PKCS11) && !defined(DEFAULT_TRUST_STORE_FILE) && !defined(_WIN32)
403 return GNUTLS_E_UNIMPLEMENTED_FEATURE
;
406 const char* crl_file
=
407 # ifdef DEFAULT_CRL_FILE
419 const CERT_CONTEXT
*cert
;
420 const CRL_CONTEXT
*crl
;
423 if (i
==0) store
= CertOpenSystemStore(0, "ROOT");
424 else store
= CertOpenSystemStore(0, "CA");
426 if (store
== NULL
) return GNUTLS_E_FILE_ERROR
;
428 cert
= CertEnumCertificatesInStore(store
, NULL
);
429 crl
= Loaded_CertEnumCRLsInStore(store
, NULL
);
433 if (cert
->dwCertEncodingType
== X509_ASN_ENCODING
)
435 data
.data
= cert
->pbCertEncoded
;
436 data
.size
= cert
->cbCertEncoded
;
437 if (gnutls_x509_trust_list_add_trust_mem(list
, &data
, NULL
, GNUTLS_X509_FMT_DER
, tl_flags
, tl_vflags
) > 0)
440 cert
= CertEnumCertificatesInStore(store
, cert
);
445 if (crl
->dwCertEncodingType
== X509_ASN_ENCODING
)
447 data
.data
= crl
->pbCrlEncoded
;
448 data
.size
= crl
->cbCrlEncoded
;
449 gnutls_x509_trust_list_add_trust_mem(list
, NULL
, &data
, GNUTLS_X509_FMT_DER
, tl_flags
, tl_vflags
);
451 crl
= Loaded_CertEnumCRLsInStore(store
, crl
);
453 CertCloseStore(store
, 0);
457 # if defined(ENABLE_PKCS11) && defined(DEFAULT_TRUST_STORE_PKCS11)
458 ret
= gnutls_x509_trust_list_add_trust_file(list
, DEFAULT_TRUST_STORE_PKCS11
, crl_file
,
459 GNUTLS_X509_FMT_DER
, tl_flags
, tl_vflags
);
464 # ifdef DEFAULT_TRUST_STORE_FILE
465 ret
= gnutls_x509_trust_list_add_trust_file(list
, DEFAULT_TRUST_STORE_FILE
, crl_file
,
466 GNUTLS_X509_FMT_PEM
, tl_flags
, tl_vflags
);