Пример #1
0
    public static void Initialise(bool optimise = true)
    {
        Optimise = optimise;

        // Our executabe needs to know how to find the native libraries
        // For Windows, we can set this to be x86 or x64 directory at runtime (below)
        // For Linux we need to move our native libraries to 'netcoredeps' which is set by .net core
        // For OSX we need to set an environment variable (DYLD_LIBRARY_PATH) outside of the process by a script
        if (Environment.OSVersion.Platform == PlatformID.Win32NT)
        {
            try
            {
                SetDefaultDllDirectories(LOAD_LIBRARY_SEARCH_DEFAULT_DIRS);
                AddDllDirectory(Path.Combine(
                                    AppDomain.CurrentDomain.BaseDirectory,
                                    Environment.Is64BitProcess ? "x64" : "x86"
                                    ));
            }
            catch
            {
                // Pre-Windows 7, KB2533623
                SetDllDirectory(Path.Combine(
                                    AppDomain.CurrentDomain.BaseDirectory,
                                    Environment.Is64BitProcess ? "x64" : "x86"
                                    ));
            }
        }

        // .NET Core also doesn't use DllImport but we can replicate this using NativeLibrary as per below
        // Uses FNA.dll.config to dictate what the name of the native library is per platform and architecture
        var fnaAssembly = Assembly.GetAssembly(typeof(Microsoft.Xna.Framework.Graphics.ColorWriteChannels));

        DllMap.Register(fnaAssembly);
    }
Пример #2
0
    public static void Initialise(bool optimise = true)
    {
        Optimise = optimise;

        // Our executabe needs to know how to find the native libraries
        // For Windows, we can set this to be x86 or x64 directory at runtime (below)
        // For Linux we need to move our native libraries to 'netcoredeps' which is set by .net core
        // For OSX we need to set an environment variable (DYLD_LIBRARY_PATH) outside of the process by a script
        if (Environment.OSVersion.Platform == PlatformID.Win32NT)
        {
            try
            {
                SetDefaultDllDirectories(LOAD_LIBRARY_SEARCH_DEFAULT_DIRS);
                AddDllDirectory(Path.Combine(
                                    AppDomain.CurrentDomain.BaseDirectory,
                                    Environment.Is64BitProcess ? "x64" : "x86"
                                    ));
            }
            catch
            {
                // Pre-Windows 7, KB2533623
                SetDllDirectory(Path.Combine(
                                    AppDomain.CurrentDomain.BaseDirectory,
                                    Environment.Is64BitProcess ? "x64" : "x86"
                                    ));
            }
        }

        // .NET Core also doesn't use DllImport but we can replicate this using NativeLibrary as per below
        // Uses FNA.dll.config to dictate what the name of the native library is per platform and architecture
        var fnaAssembly = Assembly.GetAssembly(typeof(Microsoft.Xna.Framework.Graphics.ColorWriteChannels));

        DllMap.Register(fnaAssembly);

        // OTHER STUFF
        // Required to allow window to be resized - must be set before XAGE engine started
        Environment.SetEnvironmentVariable("FNA_WORKAROUND_WINDOW_RESIZABLE", "1");
        Environment.SetEnvironmentVariable("FNA_WORKAROUND_WINDOW_COLOR", 0x000000.ToString());

        // Invoke some activities asynchronously to reduce the startup time
        if (Optimise)
        {
            Task.Run(() => WarmupFAudio());
        }

        // Make console a decent size
        try
        {
            if (Console.WindowWidth > 0)
            {
                Console.SetWindowSize(140, 35);
            }
        }
        catch (IOException) // Swallow exception - cannot be done if project OutputType is 'Windows Application', not sure how to detect this
        {
        }
    }
Пример #3
0
 public static void Main()
 {
     try
     {
         DllMap.Register(Assembly.GetExecutingAssembly());
         int thirty = NativeSum(10, 20);
         Console.WriteLine($"OldLib.NativeSum(10,20) = {thirty}");
     }
     catch (Exception e)
     {
         Console.WriteLine($"Error: {e.Message} Line: {e.Source}");
     }
 }
Пример #4
0
    public static int Main()
    {
        try
        {
            DllMap.Register(Assembly.GetExecutingAssembly());
            int thirty = NativeSum(10, 20);

            if (thirty != 30)
            {
                Console.WriteLine("Wrong result from native call");
                return(101);
            }
        }
        catch (Exception e)
        {
            Console.WriteLine("Unexpected exception: {0}", e.Message);
            return(102);
        }

        return(100);
    }
Пример #5
0
        static int Main(string[] args)
        {
            DllMap.Register(Assembly.GetAssembly(typeof(Pkcs11Uri)));

            CommandLineOptions options;

            try
            {
                options          = CommandLineParser.Parse(args);
                _serviceProvider = Startup.Configure(options.LogFilePath, options.NativeLibraryPath);
            }
            catch (AppMustBeClosedException ex)
            {
                return(ex.RetCode);
            }

            var core              = _serviceProvider.GetService <TokenSlot>();
            var pinsStore         = _serviceProvider.GetService <PinsStorage>();
            var configLinesStore  = _serviceProvider.GetService <ConfigLinesStorage>();
            var logMessageBuilder = _serviceProvider.GetService <LogMessageBuilder>();

            try
            {
                if (!string.IsNullOrWhiteSpace(options.PinFilePath))
                {
                    pinsStore.Load(options.PinFilePath);
                }

                if (!string.IsNullOrWhiteSpace(options.ConfigurationFilePath))
                {
                    configLinesStore.Load(options.ConfigurationFilePath);
                    return(Main(configLinesStore.GetNext().Trim().Split(" ")));
                }

                var initialSlots = core.GetInitialSlots();

                if (!initialSlots.Any())
                {
                    Console.WriteLine(Resources.WaitingNextToken);
                }

                while (true)
                {
                    if (!initialSlots.TryPop(out var slot))
                    {
                        var cts   = new CancellationTokenSource();
                        var token = cts.Token;

                        var waitTokenTask = Task.Run(() =>
                        {
                            try
                            {
                                slot = core.WaitToken();
                            }
                            catch (Pkcs11Exception e) when(e.RV == CKR.CKR_CRYPTOKI_NOT_INITIALIZED)
                            {
                            }
                            finally
                            {
                                cts.Cancel();
                            }
                        });
                        var waitExitKeyTask = Task.Run(() =>
                        {
                            while (Console.KeyAvailable)
                            {
                                Console.ReadKey(true);
                            }

                            while (Console.ReadKey(true).Key != ConsoleKey.Q && !token.IsCancellationRequested)
                            {
                                ;
                            }
                        });

                        var completedTask = Task.WhenAny(waitTokenTask, waitExitKeyTask).Result;
                        if (completedTask == waitExitKeyTask && !token.IsCancellationRequested)
                        {
                            throw new AppMustBeClosedException(0);
                        }
                    }

                    if (options.SerialNumber != null && !string.Equals(slot.GetTokenInfo().SerialNumber, options.SerialNumber, StringComparison.OrdinalIgnoreCase))
                    {
                        continue;
                    }

                    var commandHandlerBuilder = _serviceProvider.GetService <CommandHandlerBuilder>()
                                                .ConfigureWith(slot, options);

                    foreach (var(key, value) in _optionsMapping)
                    {
                        if (key.Invoke(options))
                        {
                            value.Invoke(commandHandlerBuilder);
                        }
                    }

                    try
                    {
                        commandHandlerBuilder.Execute();
                    }
                    catch (TokenMustBeChangedException)
                    {
                        // ignored (перейти к следующему токену)
                    }

                    if (options.OneIterationOnly)
                    {
                        break;
                    }

                    Console.WriteLine(Resources.WaitingNextToken);
                }

                _retCode = (int)CKR.CKR_OK;
            }
            catch (Pkcs11Exception ex)
            {
                Console.Error.WriteLine(logMessageBuilder.WithPKCS11Error(ex.RV));
                _retCode = (int)ex.RV;
            }
            catch (CKRException ex)
            {
                Console.Error.WriteLine(ex.Message);
                _retCode = (int)ex.ReturnCode;
            }
            catch (AppMustBeClosedException ex)
            {
                return(ex.RetCode);
            }
            catch (Exception ex)
            {
                Console.Error.WriteLine(logMessageBuilder.WithUnhandledError(ex.Message));
                _retCode = -1;
            }
            finally
            {
                DisposeServices();
            }

            return(_retCode);
        }