ReactOS 0.4.17-dev-381-g2ec7c03
unixlib.c File Reference
#include "config.h"
#include <stdarg.h>
#include <stdlib.h>
#include <dirent.h>
#include <fcntl.h>
#include <unistd.h>
#include <dlfcn.h>
#include <sys/stat.h>
#include "ntstatus.h"
#include "windef.h"
#include "winbase.h"
#include "winternl.h"
#include "wincrypt.h"
#include "crypt32_private.h"
#include "wine/debug.h"
Include dependency graph for unixlib.c:

Go to the source code of this file.

Classes

struct  root_cert
 
struct  DynamicBuffer
 

Macros

#define WIN32_NO_STATUS
 
#define BASE64_DECODE_PADDING   0x100
 
#define BASE64_DECODE_WHITESPACE   0x200
 
#define BASE64_DECODE_INVALID   0x300
 

Functions

 WINE_DEFAULT_DEBUG_CHANNEL (crypt)
 
static NTSTATUS process_attach (void *args)
 
static NTSTATUS process_detach (void *args)
 
static NTSTATUS open_cert_store (void *args)
 
static NTSTATUS import_store_key (void *args)
 
static NTSTATUS import_store_cert (void *args)
 
static NTSTATUS close_cert_store (void *args)
 
static BYTEadd_cert (SIZE_T size)
 
static void reset_buffer (struct DynamicBuffer *buffer)
 
static void add_line_to_buffer (struct DynamicBuffer *buffer, LPCSTR line)
 
static int decodeBase64Byte (char c)
 
static BOOL base64_to_cert (const char *str)
 
static void import_certs_from_file (int fd)
 
static void import_certs_from_path (LPCSTR path, BOOL allow_dir)
 
static BOOL check_buffer_resize (char **ptr_buf, size_t *buf_size, size_t check_size)
 
static void import_certs_from_dir (LPCSTR path)
 
static void load_root_certs (void)
 
static NTSTATUS enum_root_certs (void *args)
 
 C_ASSERT (ARRAYSIZE(__wine_unix_call_funcs)==unix_funcs_count)
 

Variables

static struct list root_cert_list = LIST_INIT(root_cert_list)
 
static const char *const CRYPT_knownLocations []
 
const unixlib_entry_t __wine_unix_call_funcs []
 

Macro Definition Documentation

◆ BASE64_DECODE_INVALID

#define BASE64_DECODE_INVALID   0x300

Definition at line 419 of file unixlib.c.

◆ BASE64_DECODE_PADDING

#define BASE64_DECODE_PADDING   0x100

Definition at line 417 of file unixlib.c.

◆ BASE64_DECODE_WHITESPACE

#define BASE64_DECODE_WHITESPACE   0x200

Definition at line 418 of file unixlib.c.

◆ WIN32_NO_STATUS

#define WIN32_NO_STATUS

Definition at line 40 of file unixlib.c.

Function Documentation

◆ add_cert()

static BYTE * add_cert ( SIZE_T  size)
static

Definition at line 379 of file unixlib.c.

380{
381 struct root_cert *cert = malloc( offsetof( struct root_cert, data[size] ));
382
383 if (!cert) return NULL;
384 cert->size = size;
385 list_add_tail( &root_cert_list, &cert->entry );
386 return cert->data;
387}
static void list_add_tail(struct list_entry *head, struct list_entry *entry)
Definition: list.h:83
#define malloc
Definition: debug_ros.c:4
#define NULL
Definition: types.h:112
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: gl.h:1950
GLsizeiptr size
Definition: glext.h:5919
static BYTE cert[]
Definition: msg.c:1374
#define offsetof(TYPE, MEMBER)
static struct list root_cert_list
Definition: unixlib.c:377

Referenced by base64_to_cert(), and load_root_certs().

◆ add_line_to_buffer()

static void add_line_to_buffer ( struct DynamicBuffer buffer,
LPCSTR  line 
)
static

Definition at line 402 of file unixlib.c.

403{
404 if (buffer->used + strlen(line) + 1 > buffer->allocated)
405 {
406 DWORD new_size = max( max( buffer->allocated * 2, 1024 ), buffer->used + strlen(line) + 1 );
407 void *ptr = realloc( buffer->data, new_size );
408 if (!ptr) return;
409 buffer->data = ptr;
410 buffer->allocated = new_size;
411 if (!buffer->used) buffer->data[0] = 0;
412 }
413 strcpy( buffer->data + buffer->used, line );
414 buffer->used += strlen(line);
415}
#define realloc
Definition: debug_ros.c:6
_ACRTIMP size_t __cdecl strlen(const char *)
Definition: string.c:1597
size_t const new_size
Definition: expand.cpp:66
unsigned long DWORD
Definition: ntddk_ex.h:95
GLuint buffer
Definition: glext.h:5915
static PVOID ptr
Definition: dispmode.c:27
strcpy
Definition: string.h:131
Definition: parser.c:49
#define max(a, b)
Definition: svc.c:63

Referenced by import_certs_from_file().

◆ base64_to_cert()

static BOOL base64_to_cert ( const char str)
static

Definition at line 442 of file unixlib.c.

443{
444 DWORD i, valid, out, hasPadding;
445 BYTE block[4], *data;
446
447 for (i = valid = out = hasPadding = 0; str[i]; i++)
448 {
449 int d = decodeBase64Byte( str[i] );
450 if (d == BASE64_DECODE_INVALID) return FALSE;
451 if (d == BASE64_DECODE_WHITESPACE) continue;
452
453 /* When padding starts, data is not acceptable */
454 if (hasPadding && d != BASE64_DECODE_PADDING) return FALSE;
455
456 /* Padding after a full block (like "VVVV=") is ok and stops decoding */
457 if (d == BASE64_DECODE_PADDING && (valid & 3) == 0) break;
458
459 valid++;
461 {
462 hasPadding = 1;
463 /* When padding reaches a full block, stop decoding */
464 if ((valid & 3) == 0) break;
465 continue;
466 }
467
468 /* out is incremented in the 4-char block as follows: "1-23" */
469 if ((valid & 3) != 2) out++;
470 }
471 /* Fail if the block has bad padding; omitting padding is fine */
472 if ((valid & 3) != 0 && hasPadding) return FALSE;
473
474 if (!(data = add_cert( out ))) return FALSE;
475 for (i = valid = out = 0; str[i]; i++)
476 {
477 int d = decodeBase64Byte( str[i] );
478 if (d == BASE64_DECODE_WHITESPACE) continue;
479 if (d == BASE64_DECODE_PADDING) break;
480 block[valid & 3] = d;
481 valid += 1;
482 switch (valid & 3)
483 {
484 case 1:
485 data[out++] = (block[0] << 2);
486 break;
487 case 2:
488 data[out-1] = (block[0] << 2) | (block[1] >> 4);
489 break;
490 case 3:
491 data[out++] = (block[1] << 4) | (block[2] >> 2);
492 break;
493 case 0:
494 data[out++] = (block[2] << 6) | (block[3] >> 0);
495 break;
496 }
497 }
498 return TRUE;
499}
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
BOOLEAN valid
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
#define d
Definition: ke_i.h:81
const WCHAR * str
static BYTE * add_cert(SIZE_T size)
Definition: unixlib.c:379
#define BASE64_DECODE_PADDING
Definition: unixlib.c:417
#define BASE64_DECODE_WHITESPACE
Definition: unixlib.c:418
#define BASE64_DECODE_INVALID
Definition: unixlib.c:419
static int decodeBase64Byte(char c)
Definition: unixlib.c:421
wchar_t tm const _CrtWcstime_Writes_and_advances_ptr_ count wchar_t ** out
Definition: wcsftime.cpp:383
static unsigned int block
Definition: xmlmemory.c:101
unsigned char BYTE
Definition: xxhash.c:193

Referenced by import_certs_from_file().

◆ C_ASSERT()

◆ check_buffer_resize()

static BOOL check_buffer_resize ( char **  ptr_buf,
size_t buf_size,
size_t  check_size 
)
static

Definition at line 538 of file unixlib.c.

539{
540 if (check_size > *buf_size)
541 {
542 void *ptr = realloc(*ptr_buf, check_size);
543
544 if (!ptr) return FALSE;
545 *buf_size = check_size;
546 *ptr_buf = ptr;
547 }
548 return TRUE;
549}

Referenced by import_certs_from_dir().

◆ close_cert_store()

static NTSTATUS close_cert_store ( void args)
static

Definition at line 366 of file unixlib.c.

366{ return STATUS_DLL_NOT_FOUND; }
#define STATUS_DLL_NOT_FOUND
Definition: ntstatus.h:639

Referenced by PFXImportCertStore().

◆ decodeBase64Byte()

static int decodeBase64Byte ( char  c)
inlinestatic

Definition at line 421 of file unixlib.c.

422{
424
425 if (c >= 'A' && c <= 'Z')
426 ret = c - 'A';
427 else if (c >= 'a' && c <= 'z')
428 ret = c - 'a' + 26;
429 else if (c >= '0' && c <= '9')
430 ret = c - '0' + 52;
431 else if (c == '+')
432 ret = 62;
433 else if (c == '/')
434 ret = 63;
435 else if (c == '=')
437 else if (c == ' ' || c == '\t' || c == '\r' || c == '\n')
439 return ret;
440}
return ret
Definition: mutex.c:146
const GLubyte * c
Definition: glext.h:8905

Referenced by base64_to_cert().

◆ enum_root_certs()

static NTSTATUS enum_root_certs ( void args)
static

Definition at line 667 of file unixlib.c.

668{
670 static BOOL loaded;
671 struct list *ptr;
672 struct root_cert *cert;
673
674 if (!loaded) load_root_certs();
675 loaded = TRUE;
676
678 cert = LIST_ENTRY( ptr, struct root_cert, entry );
679 *params->needed = cert->size;
680 if (cert->size <= params->size)
681 {
682 memcpy( params->buffer, cert->data, cert->size );
683 list_remove( &cert->entry );
684 free( cert );
685 }
686 return STATUS_SUCCESS;
687}
static void list_remove(struct list_entry *entry)
Definition: list.h:90
Definition: list.h:37
#define free
Definition: debug_ros.c:5
unsigned int BOOL
Definition: ntddk_ex.h:94
GLenum const GLfloat * params
Definition: glext.h:5645
uint32_t entry
Definition: isohybrid.c:63
#define memcpy(s1, s2, n)
Definition: mkisofs.h:878
BOOL loaded
Definition: xmlview.c:54
#define STATUS_NO_MORE_ENTRIES
Definition: ntstatus.h:285
#define args
Definition: format.c:66
#define STATUS_SUCCESS
Definition: shellext.h:65
Definition: list.h:15
#define LIST_ENTRY(type)
Definition: queue.h:175
static void load_root_certs(void)
Definition: unixlib.c:626

Referenced by read_trusted_roots_from_known_locations().

◆ import_certs_from_dir()

static void import_certs_from_dir ( LPCSTR  path)
static

Definition at line 555 of file unixlib.c.

556{
557 DIR *dir;
558
559 dir = opendir(path);
560 if (dir)
561 {
562 size_t path_len = strlen(path), bufsize = 0;
563 char *filebuf = NULL;
564
565 struct dirent *entry;
566 while ((entry = readdir(dir)))
567 {
568 if (strcmp(entry->d_name, ".") && strcmp(entry->d_name, ".."))
569 {
570 size_t name_len = strlen(entry->d_name);
571
572 if (!check_buffer_resize(&filebuf, &bufsize, path_len + 1 + name_len + 1)) break;
573 snprintf(filebuf, bufsize, "%s/%s", path, entry->d_name);
575 }
576 }
577 free(filebuf);
578 closedir(dir);
579 }
580}
unsigned int dir
Definition: maze.c:112
_ACRTIMP int __cdecl strcmp(const char *, const char *)
Definition: string.c:3324
GLenum GLuint GLsizei bufsize
Definition: glext.h:7473
static DWORD path_len
Definition: batch.c:31
int __cdecl closedir(DIR *)
DIR *__cdecl opendir(const char *)
struct dirent *__cdecl readdir(DIR *)
Definition: dirent.h:40
Definition: dirent.h:25
static void import_certs_from_path(LPCSTR path, BOOL allow_dir)
Definition: unixlib.c:586
static BOOL check_buffer_resize(char **ptr_buf, size_t *buf_size, size_t check_size)
Definition: unixlib.c:538
#define snprintf
Definition: wintirpc.h:48

Referenced by import_certs_from_path().

◆ import_certs_from_file()

static void import_certs_from_file ( int  fd)
static

Definition at line 502 of file unixlib.c.

503{
504 FILE *fp = fdopen(fd, "r");
505 char line[1024];
506 BOOL in_cert = FALSE;
507 struct DynamicBuffer saved_cert = { 0, 0, NULL };
508 int num_certs = 0;
509
510 if (!fp) return;
511 TRACE("\n");
512 while (fgets(line, sizeof(line), fp))
513 {
514 static const char header[] = "-----BEGIN CERTIFICATE-----";
515 static const char trailer[] = "-----END CERTIFICATE-----";
516
518 {
519 TRACE("begin new certificate\n");
520 in_cert = TRUE;
521 reset_buffer(&saved_cert);
522 }
523 else if (!strncmp(line, trailer, strlen(trailer)))
524 {
525 TRACE("end of certificate, adding cert\n");
526 in_cert = FALSE;
527 if (base64_to_cert( saved_cert.data )) num_certs++;
528 }
529 else if (in_cert) add_line_to_buffer(&saved_cert, line);
530 }
531 free( saved_cert.data );
532 TRACE("Read %d certs\n", num_certs);
533 fclose(fp);
534}
int CDECL fclose(FILE *file)
Definition: file.c:3757
char *CDECL fgets(char *s, int size, FILE *file)
Definition: file.c:3903
_ACRTIMP int __cdecl _ACRTIMP int __cdecl _ACRTIMP int __cdecl _ACRTIMP int __cdecl _ACRTIMP int __cdecl _ACRTIMP int __cdecl _ACRTIMP int __cdecl _ACRTIMP int __cdecl static FILE * fdopen(int fd, const char *mode)
Definition: stdio.h:480
_ACRTIMP int __cdecl strncmp(const char *, const char *, size_t)
Definition: string.c:3335
static int fd
Definition: io.c:51
#define TRACE(s)
Definition: solgame.cpp:4
char * data
Definition: unixlib.c:393
static BOOL base64_to_cert(const char *str)
Definition: unixlib.c:442
static void add_line_to_buffer(struct DynamicBuffer *buffer, LPCSTR line)
Definition: unixlib.c:402
static void reset_buffer(struct DynamicBuffer *buffer)
Definition: unixlib.c:396

Referenced by import_certs_from_path().

◆ import_certs_from_path()

static void import_certs_from_path ( LPCSTR  path,
BOOL  allow_dir 
)
static

Definition at line 586 of file unixlib.c.

587{
588 int fd;
589
590 TRACE("(%s, %d)\n", debugstr_a(path), allow_dir);
591
592 fd = open(path, O_RDONLY);
593 if (fd != -1)
594 {
595 struct stat st;
596
597 if (fstat(fd, &st) == 0)
598 {
599 if (S_ISREG(st.st_mode))
601 else if (S_ISDIR(st.st_mode))
602 {
603 if (allow_dir)
605 else
606 WARN("%s is a directory and directories are disallowed\n",
608 }
609 else
610 ERR("%s: invalid file type\n", path);
611 }
612 close(fd);
613 }
614}
#define close
Definition: acwin.h:99
#define fstat
Definition: acwin.h:101
#define S_ISDIR(mode)
Definition: various.h:18
#define S_ISREG(mode)
Definition: various.h:17
#define WARN(fmt,...)
Definition: precomp.h:61
#define ERR(fmt,...)
Definition: precomp.h:57
#define O_RDONLY
Definition: fcntl.h:34
#define open
Definition: io.h:44
#define debugstr_a
Definition: kernel32.h:31
Definition: stat.h:66
static void import_certs_from_file(int fd)
Definition: unixlib.c:502
static void import_certs_from_dir(LPCSTR path)
Definition: unixlib.c:555

Referenced by import_certs_from_dir(), and load_root_certs().

◆ import_store_cert()

static NTSTATUS import_store_cert ( void args)
static

Definition at line 365 of file unixlib.c.

365{ return STATUS_DLL_NOT_FOUND; }

Referenced by PFXImportCertStore().

◆ import_store_key()

static NTSTATUS import_store_key ( void args)
static

Definition at line 364 of file unixlib.c.

364{ return STATUS_DLL_NOT_FOUND; }

Referenced by import_key().

◆ load_root_certs()

static void load_root_certs ( void  )
static

Definition at line 626 of file unixlib.c.

627{
628 unsigned int i;
629
630#ifdef __APPLE__
631 const SecTrustSettingsDomain domains[] = {
632 kSecTrustSettingsDomainSystem,
633 kSecTrustSettingsDomainAdmin,
634 kSecTrustSettingsDomainUser
635 };
636 OSStatus status;
637 CFArrayRef certs;
639
640 for (domain = 0; domain < ARRAY_SIZE(domains); domain++)
641 {
642 status = SecTrustSettingsCopyCertificates(domains[domain], &certs);
643 if (status == noErr)
644 {
645 for (i = 0; i < CFArrayGetCount(certs); i++)
646 {
647 SecCertificateRef cert = (SecCertificateRef)CFArrayGetValueAtIndex(certs, i);
648 CFDataRef certData;
649 if ((status = SecItemExport(cert, kSecFormatX509Cert, 0, NULL, &certData)) == noErr)
650 {
651 BYTE *data = add_cert( CFDataGetLength(certData) );
652 if (data) memcpy( data, CFDataGetBytePtr(certData), CFDataGetLength(certData) );
653 CFRelease(certData);
654 }
655 else
656 WARN("could not export certificate %u to X509 format: 0x%08x\n", i, (unsigned int)status);
657 }
658 CFRelease(certs);
659 }
660 }
661#endif
662
665}
#define ARRAY_SIZE(A)
Definition: main.h:20
static int list_empty(struct list_entry *head)
Definition: list.h:58
Definition: cookie.c:42
Definition: ps.c:97
static const char *const CRYPT_knownLocations[]
Definition: unixlib.c:616

Referenced by enum_root_certs().

◆ open_cert_store()

static NTSTATUS open_cert_store ( void args)
static

Definition at line 363 of file unixlib.c.

363{ return STATUS_DLL_NOT_FOUND; }

Referenced by PFXImportCertStore().

◆ process_attach()

static NTSTATUS process_attach ( void args)
static

Definition at line 361 of file unixlib.c.

361{ return STATUS_SUCCESS; }

Referenced by DllMain().

◆ process_detach()

static NTSTATUS process_detach ( void args)
static

Definition at line 362 of file unixlib.c.

362{ return STATUS_SUCCESS; }

◆ reset_buffer()

static void reset_buffer ( struct DynamicBuffer buffer)
inlinestatic

Definition at line 396 of file unixlib.c.

397{
398 buffer->used = 0;
399 if (buffer->data) buffer->data[0] = 0;
400}

Referenced by common_splitpath_internal(), import_certs_from_file(), and reset_buffers().

◆ WINE_DEFAULT_DEBUG_CHANNEL()

WINE_DEFAULT_DEBUG_CHANNEL ( crypt  )

Variable Documentation

◆ __wine_unix_call_funcs

const unixlib_entry_t __wine_unix_call_funcs[]
Initial value:
=
{
}
static void process_detach(void)
Definition: main.c:94
static NTSTATUS import_store_key(void *args)
Definition: unixlib.c:364
static NTSTATUS close_cert_store(void *args)
Definition: unixlib.c:366
static NTSTATUS enum_root_certs(void *args)
Definition: unixlib.c:667
static NTSTATUS import_store_cert(void *args)
Definition: unixlib.c:365
static NTSTATUS process_attach(void *args)
Definition: unixlib.c:361
static NTSTATUS open_cert_store(void *args)
Definition: unixlib.c:363

Definition at line 689 of file unixlib.c.

◆ CRYPT_knownLocations

const char* const CRYPT_knownLocations[]
static
Initial value:
= {
"/etc/ssl/certs/ca-certificates.crt",
"/etc/ssl/certs",
"/etc/pki/tls/certs/ca-bundle.crt",
"/usr/share/ca-certificates/ca-bundle.crt",
"/usr/local/share/certs/",
"/etc/sfw/openssl/certs",
"/etc/security/cacerts",
}

Definition at line 616 of file unixlib.c.

Referenced by load_root_certs().

◆ root_cert_list

struct list root_cert_list = LIST_INIT(root_cert_list)
static

Definition at line 377 of file unixlib.c.

Referenced by add_cert(), enum_root_certs(), and load_root_certs().