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
;
39 # define Loaded_CertEnumCRLsInStore CertEnumCRLsInStore
42 # ifdef HAVE_PTHREAD_LOCKS
46 # if defined(HAVE_GETPWUID_R)
51 /* We need to disable gnulib's replacement wrappers to get native
52 Windows interfaces. */
57 /* System specific function wrappers.
62 system_errno (gnutls_transport_ptr p
)
64 int tmperr
= WSAGetLastError ();
84 WSASetLastError (tmperr
);
90 system_write (gnutls_transport_ptr ptr
, const void *data
, size_t data_size
)
92 return send (GNUTLS_POINTER_TO_INT (ptr
), data
, data_size
, 0);
96 system_errno (gnutls_transport_ptr_t ptr
)
98 #if defined(_AIX) || defined(AIX)
99 if (errno
== 0) errno
= EAGAIN
;
106 system_writev (gnutls_transport_ptr_t ptr
, const giovec_t
* iovec
,
109 return writev (GNUTLS_POINTER_TO_INT (ptr
), (struct iovec
*) iovec
,
116 system_read (gnutls_transport_ptr_t ptr
, void *data
, size_t data_size
)
118 return recv (GNUTLS_POINTER_TO_INT (ptr
), data
, data_size
, 0);
121 /* Wait for data to be received within a timeout period in milliseconds.
122 * To catch a termination it will also try to receive 0 bytes from the
123 * socket if select reports to proceed.
125 * Returns -1 on error, 0 on timeout, positive value if data are available for reading.
127 int system_recv_timeout(gnutls_transport_ptr_t ptr
, unsigned int ms
)
132 int fd
= GNUTLS_POINTER_TO_INT(ptr
);
138 tv
.tv_usec
= ms
* 1000;
140 while(tv
.tv_usec
>= 1000000)
142 tv
.tv_usec
-= 1000000;
146 ret
= select(fd
+1, &rfds
, NULL
, NULL
, &tv
);
155 #ifdef HAVE_WIN32_LOCKS
157 gnutls_system_mutex_init (void **priv
)
159 CRITICAL_SECTION
*lock
= malloc (sizeof (CRITICAL_SECTION
));
162 return GNUTLS_E_MEMORY_ERROR
;
164 InitializeCriticalSection (lock
);
172 gnutls_system_mutex_deinit (void **priv
)
174 DeleteCriticalSection ((CRITICAL_SECTION
*) * priv
);
181 gnutls_system_mutex_lock (void **priv
)
183 EnterCriticalSection ((CRITICAL_SECTION
*) * priv
);
188 gnutls_system_mutex_unlock (void **priv
)
190 LeaveCriticalSection ((CRITICAL_SECTION
*) * priv
);
194 #endif /* WIN32_LOCKS */
196 #ifdef HAVE_PTHREAD_LOCKS
199 gnutls_system_mutex_init (void **priv
)
201 pthread_mutex_t
*lock
= malloc (sizeof (pthread_mutex_t
));
205 return GNUTLS_E_MEMORY_ERROR
;
207 ret
= pthread_mutex_init (lock
, NULL
);
212 return GNUTLS_E_LOCKING_ERROR
;
221 gnutls_system_mutex_deinit (void **priv
)
223 pthread_mutex_destroy ((pthread_mutex_t
*) * priv
);
229 gnutls_system_mutex_lock (void **priv
)
231 if (pthread_mutex_lock ((pthread_mutex_t
*) * priv
))
234 return GNUTLS_E_LOCKING_ERROR
;
241 gnutls_system_mutex_unlock (void **priv
)
243 if (pthread_mutex_unlock ((pthread_mutex_t
*) * priv
))
246 return GNUTLS_E_LOCKING_ERROR
;
252 #endif /* PTHREAD_LOCKS */
257 gnutls_system_mutex_init (void **priv
)
263 gnutls_system_mutex_deinit (void **priv
)
269 gnutls_system_mutex_lock (void **priv
)
275 gnutls_system_mutex_unlock (void **priv
)
280 #endif /* NO_LOCKS */
282 gnutls_time_func gnutls_time
= time
;
283 mutex_init_func gnutls_mutex_init
= gnutls_system_mutex_init
;
284 mutex_deinit_func gnutls_mutex_deinit
= gnutls_system_mutex_deinit
;
285 mutex_lock_func gnutls_mutex_lock
= gnutls_system_mutex_lock
;
286 mutex_unlock_func gnutls_mutex_unlock
= gnutls_system_mutex_unlock
;
289 gnutls_system_global_init ()
292 # if defined(__MINGW32__) && !defined(__MINGW64__) && __MINGW32_MAJOR_VERSION <= 3 && __MINGW32_MINOR_VERSION <= 20
294 crypto
= LoadLibraryA ("Crypt32.dll");
297 return GNUTLS_E_CRYPTO_INIT_FAILED
;
299 Loaded_CertEnumCRLsInStore
= (Type_CertEnumCRLsInStore
) GetProcAddress (crypto
, "CertEnumCRLsInStore");
300 if (Loaded_CertEnumCRLsInStore
== NULL
)
302 FreeLibrary (crypto
);
303 return GNUTLS_E_CRYPTO_INIT_FAILED
;
306 Crypt32_dll
= crypto
;
313 gnutls_system_global_deinit ()
316 # if defined(__MINGW32__) && !defined(__MINGW64__) && __MINGW32_MAJOR_VERSION <= 3 && __MINGW32_MINOR_VERSION <= 20
317 FreeLibrary (Crypt32_dll
);
323 #define CONFIG_PATH ".gnutls"
325 /* Returns a path to store user-specific configuration
328 int _gnutls_find_config_path(char* path
, size_t max_size
)
330 char tmp_home_dir
[1024];
331 const char *home_dir
= getenv ("HOME");
334 if (home_dir
== NULL
|| home_dir
[0] == '\0')
336 const char *home_drive
= getenv ("HOMEDRIVE");
337 const char *home_path
= getenv ("HOMEPATH");
339 if (home_drive
!= NULL
&& home_path
!= NULL
)
341 snprintf(tmp_home_dir
, sizeof(tmp_home_dir
), "%s%s", home_drive
, home_path
);
348 home_dir
= tmp_home_dir
;
350 #elif defined(HAVE_GETPWUID_R)
351 if (home_dir
== NULL
|| home_dir
[0] == '\0')
357 getpwuid_r(getuid(), &_pwd
, buf
, sizeof(buf
), &pwd
);
360 snprintf(tmp_home_dir
, sizeof(tmp_home_dir
), "%s", pwd
->pw_dir
);
367 home_dir
= tmp_home_dir
;
370 if (home_dir
== NULL
|| home_dir
[0] == '\0')
373 home_dir
= tmp_home_dir
;
377 if (home_dir
== NULL
|| home_dir
[0] == 0)
380 snprintf(path
, max_size
, "%s/"CONFIG_PATH
, home_dir
);
386 * gnutls_x509_trust_list_add_system_trust:
387 * @list: The structure of the list
388 * @tl_flags: GNUTLS_TL_*
389 * @tl_vflags: gnutls_certificate_verify_flags if flags specifies GNUTLS_TL_VERIFY_CRL
391 * This function adds the system's default trusted certificate
392 * authorities to the trusted list. Note that on unsupported system
393 * this function returns %GNUTLS_E_UNIMPLEMENTED_FEATURE.
395 * Returns: The number of added elements or a negative error code on error.
400 gnutls_x509_trust_list_add_system_trust(gnutls_x509_trust_list_t list
,
401 unsigned int tl_flags
, unsigned int tl_vflags
)
403 #if !defined(DEFAULT_TRUST_STORE_PKCS11) && !defined(DEFAULT_TRUST_STORE_FILE) && !defined(_WIN32)
404 return GNUTLS_E_UNIMPLEMENTED_FEATURE
;
407 const char* crl_file
=
408 # ifdef DEFAULT_CRL_FILE
420 const CERT_CONTEXT
*cert
;
421 const CRL_CONTEXT
*crl
;
424 if (i
==0) store
= CertOpenSystemStore(0, "ROOT");
425 else store
= CertOpenSystemStore(0, "CA");
427 if (store
== NULL
) return GNUTLS_E_FILE_ERROR
;
429 cert
= CertEnumCertificatesInStore(store
, NULL
);
430 crl
= Loaded_CertEnumCRLsInStore(store
, NULL
);
434 if (cert
->dwCertEncodingType
== X509_ASN_ENCODING
)
436 data
.data
= cert
->pbCertEncoded
;
437 data
.size
= cert
->cbCertEncoded
;
438 if (gnutls_x509_trust_list_add_trust_mem(list
, &data
, NULL
, GNUTLS_X509_FMT_DER
, tl_flags
, tl_vflags
) > 0)
441 cert
= CertEnumCertificatesInStore(store
, cert
);
446 if (crl
->dwCertEncodingType
== X509_ASN_ENCODING
)
448 data
.data
= crl
->pbCrlEncoded
;
449 data
.size
= crl
->cbCrlEncoded
;
450 gnutls_x509_trust_list_add_trust_mem(list
, NULL
, &data
, GNUTLS_X509_FMT_DER
, tl_flags
, tl_vflags
);
452 crl
= Loaded_CertEnumCRLsInStore(store
, crl
);
454 CertCloseStore(store
, 0);
458 # if defined(ENABLE_PKCS11) && defined(DEFAULT_TRUST_STORE_PKCS11)
459 ret
= gnutls_x509_trust_list_add_trust_file(list
, DEFAULT_TRUST_STORE_PKCS11
, crl_file
,
460 GNUTLS_X509_FMT_DER
, tl_flags
, tl_vflags
);
465 # ifdef DEFAULT_TRUST_STORE_FILE
466 ret
= gnutls_x509_trust_list_add_trust_file(list
, DEFAULT_TRUST_STORE_FILE
, crl_file
,
467 GNUTLS_X509_FMT_PEM
, tl_flags
, tl_vflags
);