예제 #1
0
        public ImageRepository()
        {
            string dir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\ZetSwitch";

            try {
                collectionPath = new DirectoryPath(dir + Properties.Settings.Default.ImagesPath);
                collectionPath.CreateDirectory();
            }
            catch (Exception) {
            }
        }
예제 #2
0
        public CertVaultCertificate(CertVault vault, DirectoryPath dirName, CertVaultCertType certType)
        {
            this.Vault = vault;

            if (certType.EqualsAny(CertVaultCertType.Acme, CertVaultCertType.Static, CertVaultCertType.AutoGenerated) == false)
            {
                throw new ArgumentOutOfRangeException("certType");
            }

            try
            {
                dirName.CreateDirectory();
            }
            catch { }

            CertificateStore?store = null;

            this.CertType = certType;
            this.DirName  = dirName;

            if (certType == CertVaultCertType.Static || certType == CertVaultCertType.AutoGenerated)
            {
                // Static / auto generated cert
                var files = DirName.EnumDirectory().Where(x => x.IsDirectory == false);

                string?p12file = files.Where(x => x.Name._IsExtensionMatch(Consts.Extensions.Filter_Pkcs12s)).SingleOrDefault()?.FullPath;

                string?certfile = files.Where(x => x.Name._IsExtensionMatch(Consts.Extensions.Filter_Certificates)).SingleOrDefault()?.FullPath;
                string?keyfile  = files.Where(x => x.Name._IsExtensionMatch(Consts.Extensions.Filter_Keys)).SingleOrDefault()?.FullPath;

                string?passwordfile = files.Where(x => x.Name._IsSamei(Consts.FileNames.CertVault_Password)).SingleOrDefault()?.FullPath;
                string?password     = null;

                if (passwordfile != null)
                {
                    password = FileSystem !.ReadStringFromFile(passwordfile, oneLine: true);

                    if (password._IsEmpty())
                    {
                        password = null;
                    }
                }

                if (p12file != null)
                {
                    store = new CertificateStore(FileSystem !.ReadDataFromFile(p12file).Span, password);
                }
                else if (certfile != null && keyfile != null)
                {
                    store = new CertificateStore(FileSystem !.ReadDataFromFile(certfile).Span, FileSystem.ReadDataFromFile(keyfile).Span, password);
                }
                else
                {
                    store = null;
                }
            }
            else
            {
                // ACME cert
                FilePath fileName = DirName.Combine(DirName.GetThisDirectoryName() + Consts.Extensions.Certificate_Acme);

                if (fileName.IsFileExists())
                {
                    store = new CertificateStore(fileName.ReadDataFromFile().Span, this.Vault.AcmeCertKey !);
                }
                else
                {
                    store = null;
                }
            }

            Certificate?test = store?.PrimaryContainer.CertificateList[0];

            if (test != null && store != null)
            {
                if (test.PublicKey.Equals(store.PrimaryContainer.PrivateKey.PublicKey) == false)
                {
                    Con.WriteDebug($"CertVault: The public key certificate in the directory '{dirName}' doesn't match to the private key.");
                    store = null;
                }
            }

            this.Store = store;
        }
예제 #3
0
    public void Win32FileTest()
    {
        if (Env.IsWindows == false)
        {
            return;
        }

        Async(async() =>
        {
            const string dirAcl    = "D:PAI(A;OICI;FA;;;BU)(A;OICI;FA;;;SY)(A;OICI;FA;;;BA)(A;OICI;FA;;;S-1-5-21-2439965180-1288029102-2284794580-1001)";
            const string fileAcl   = "D:PAI(A;;FA;;;WD)(A;;FA;;;SY)(A;;FA;;;BA)(A;;FA;;;BU)(A;;FA;;;S-1-5-21-2439965180-1288029102-2284794580-1001)";
            const string altStream = "W1pvbmVUcmFuc2Zlcl0NClpvbmVJZD0zDQpSZWZlcnJlclVybD1odHRwczovL3N0YXRpYy5sdHMuZG4uaXBhbnR0Lm5ldC9kLzIxMDExMV8wMDNfdWJ1bnR1X3NldHVwX3NjcmlwdHNfNTk4NjcvDQpIb3N0VXJsPWh0dHBzOi8vc3RhdGljLmx0cy5kbi5pcGFudHQubmV0L2QvMjEwMTExXzAwM191YnVudHVfc2V0dXBfc2NyaXB0c181OTg2Ny8yMTAxMTFfYXB0X3VidW50dV9qYXBhbl9zZXJ2ZXIuc2gNCg==";

            DirectoryPath dirPath = Env.MyLocalTempDir._CombinePath(Str.GenRandStr());

            try
            {
                dirPath.DeleteDirectory(true);
            }
            catch { }

            dirPath.CreateDirectory();

            FileMetadata dirMeta1 = new FileMetadata(
                securityData: new FileSecurityMetadata()
            {
                Acl = new FileSecurityAcl
                {
                    Win32AclSddl = dirAcl
                },
            }
                );

            dirPath.SetDirectoryMetadata(dirMeta1);

            FilePath filePath1 = dirPath.Combine("test1.txt");
            FilePath filePath2 = dirPath.Combine("test2.txt");

            await filePath1.WriteDataToFileAsync("Hello"._GetBytes_Ascii(), FileFlags.Async | FileFlags.AutoCreateDirectory);

            await filePath2.WriteDataToFileAsync("Hello"._GetBytes_Ascii(), FileFlags.Async | FileFlags.AutoCreateDirectory);

            FileMetadata fileMeta1 = new FileMetadata(specialOperation: FileSpecialOperationFlags.SetCompressionFlag,
                                                      securityData: new FileSecurityMetadata()
            {
                Acl = new FileSecurityAcl
                {
                    Win32AclSddl = fileAcl
                },
            },
                                                      alternateStream: new FileAlternateStreamMetadata {
                Items = new FileAlternateStreamItemMetadata {
                    Name = ":Zone.Identifier:$DATA", Data = altStream._Base64Decode()
                }._SingleArray()
            }
                                                      );

            await filePath1.SetFileMetadataAsync(fileMeta1);

            await Lfs.CopyFileAsync(filePath1, filePath2, new CopyFileParams(flags: FileFlags.Async, metadataCopier: new FileMetadataCopier(FileMetadataCopyMode.All)));

            FileMetadata fileMeta2 = await filePath2.GetFileMetadataAsync();

            if (fileMeta1.Security !.Acl !.Win32AclSddl._IsSamei(fileMeta2.Security !.Acl !.Win32AclSddl) == false)
            {
                throw new CoresException($"Different ACL. '{fileMeta1.Security!.Acl!.Win32AclSddl}' != '{fileMeta2.Security!.Acl!.Win32AclSddl}'");
            }

            if (fileMeta2.AlternateStream !.Items ![0].Data !._MemEquals(altStream._Base64Decode()) == false)
            {
                throw new CoresException("Different alternative stream.");
            }

            var fileMeta1_2 = await filePath1.GetFileMetadataAsync();
            if (fileMeta1_2.Attributes !.Value.Bit(FileAttributes.Compressed) == false)
            {
                throw new CoresException("FileAttributes.Compressed not set.");
            }
        });