コード例 #1
0
        public async Task <ActionResult <ApiResultModel> > HttpsApiCall()
        {
            var model   = new ApiResultModel();
            var handler = new HttpClientHandler()
            {
                UseDefaultCredentials = true,
                ServerCertificateCustomValidationCallback = (sender, cert, chain, error) =>
                {
                    model.Thumbprint = cert.Thumbprint;
                    model.Subject    = cert.Subject;
                    model.Error      = error;
                    return(CertificateExtensions.CertificateValidationCallBack(sender, cert, chain, error, model));
                }
            };

            using (var httpClient = new HttpClient(handler))
            //using (var httpClient = new HttpClient())
            {
                try
                {
                    var result = await httpClient.GetAsync($"{service1Options.Https}/api/test");

                    model.Result = JsonSerializer.Deserialize <TestApiResultModel>(await result.Content.ReadAsStringAsync());
                    return(model);
                }
                catch (Exception e)
                {
                    model.Exception = $"{e.Message}, {e.InnerException?.Message}";
                }
            }

            return(model);
        }
コード例 #2
0
        public NSObject ObjectValueForTableColumn(NSTableView table, NSTableColumn col, int row)
        {
            try {
                if (Entries != null)
                {
                    X509Certificate2 cert = CertificateExtensions.GetX509Certificate2FromString(this.Entries [row].Certificate);
                    switch (col.Identifier)
                    {
                    case Constants.CERT_ISSUED_BY:
                        return((NSString)cert.Issuer);

                    case Constants.CERT_ISSUED_DATE:
                        return((NSString)cert.NotBefore.ToShortDateString());

                    case Constants.CERT_EXPIRATION_DATE:
                        return((NSString)cert.NotAfter.ToShortDateString());

                    case Constants.CERT_INTENDED_PURPOSES:
                        return((NSString)CertificateExtensions.GetKeyUsage(cert));

                    case Constants.CERT_STATUS:
                        break;

                    case Constants.CERT_ISSUED_TO:
                        return((NSString)cert.Subject);
                    }
                }
            } catch (Exception e) {
                System.Diagnostics.Debug.WriteLine("Error in fetching data : " + e.Message);
            }
            return(null);
        }
コード例 #3
0
 static AppInfo()
 {
     LoginUrl         = "/Account/Login";
     HangfirePath     = "/hangfire";
     HangfireLoginUrl = UriHelper.BuildRelative(path: new PathString(LoginUrl), query: QueryString.Create("ReturnUrl", HangfirePath));
     Instance         = new AppInfo
     {
         Certificate = CertificateExtensions.GetX509Certificate(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Certs", "IdentityServerCredential.pfx"))
     };
 }
コード例 #4
0
        public PkiCertificateSigningRequest(PkiEncodingFormat format, byte[] encoded,
                                            PkiHashAlgorithm hashAlgorithm)
        {
            Pkcs10CertificationRequest pkcs10;

            switch (format)
            {
            case PkiEncodingFormat.Pem:
                var encodedString = Encoding.UTF8.GetString(encoded);
                using (var sr = new StringReader(encodedString))
                {
                    var pemReader = new PemReader(sr);
                    pkcs10 = pemReader.ReadObject() as Pkcs10CertificationRequest;
                    if (pkcs10 == null)
                    {
                        throw new Exception("invalid PEM object is not PKCS#10 archive");
                    }
                }
                break;

            case PkiEncodingFormat.Der:
                pkcs10 = new Pkcs10CertificationRequest(encoded);
                break;

            default:
                throw new NotSupportedException();
            }

            var info            = pkcs10.GetCertificationRequestInfo();
            var nativePublicKey = pkcs10.GetPublicKey();
            var rsaKey          = nativePublicKey as RsaKeyParameters;
            var ecdsaKey        = nativePublicKey as ECPublicKeyParameters;

            if (rsaKey != null)
            {
                PublicKey = new PkiKey(nativePublicKey, PkiAsymmetricAlgorithm.Rsa);
            }
            else if (ecdsaKey != null)
            {
                PublicKey = new PkiKey(nativePublicKey, PkiAsymmetricAlgorithm.Ecdsa);
            }
            else
            {
                throw new NotSupportedException("unsupported asymmetric algorithm key");
            }
            SubjectName   = info.Subject.ToString();
            HashAlgorithm = hashAlgorithm;


            // // // Based on:
            // // //    http://forum.rebex.net/4284/pkcs10-certificate-request-example-provided-castle-working

            // // var extGen = new X509ExtensionsGenerator();
            // // foreach (var ext in CertificateExtensions)
            // // {
            // //     extGen.AddExtension(ext.Identifier, ext.IsCritical, ext.Value);
            // // }
            // // var attr = new AttributeX509(PkcsObjectIdentifiers.Pkcs9AtExtensionRequest,
            // //         new DerSet(extGen.Generate()));


            // Based on:
            //    http://unitstep.net/blog/2008/10/27/extracting-x509-extensions-from-a-csr-using-the-bouncy-castle-apis/
            //    https://stackoverflow.com/q/24448909/5428506
            foreach (var attr in info.Attributes.ToArray())
            {
                if (attr is DerSequence derSeq && derSeq.Count == 2)
                {
                    var attrX509 = AttributeX509.GetInstance(attr);
                    if (object.Equals(attrX509.AttrType, PkcsObjectIdentifiers.Pkcs9AtExtensionRequest))
                    {
                        // The `Extension Request` attribute is present.
                        // The X509Extensions are contained as a value of the ASN.1 Set.
                        // Assume that it is the first value of the set.
                        if (attrX509.AttrValues.Count >= 1)
                        {
                            var csrExts = X509Extensions.GetInstance(attrX509.AttrValues[0]);
                            foreach (var extOid in csrExts.GetExtensionOids())
                            {
                                if (object.Equals(extOid, X509Extensions.SubjectAlternativeName))
                                {
                                    var ext    = csrExts.GetExtension(extOid);
                                    var extVal = ext.Value;
                                    var der    = extVal.GetDerEncoded();
                                    // The ext value, which is an ASN.1 Octet String, **MIGHT** be tagged with
                                    // a leading indicator that it's an Octet String and its length, so we want
                                    // to remove it if that's the case to extract the GeneralNames collection
                                    if (der.Length > 2 && der[0] == 4 && der[1] == der.Length - 2)
                                    {
                                        der = der.Skip(2).ToArray();
                                    }
                                    var asn1obj = Asn1Object.FromByteArray(der);
                                    var gnames  = GeneralNames.GetInstance(asn1obj);
                                    CertificateExtensions.Add(new PkiCertificateExtension
                                    {
                                        Identifier = extOid,
                                        IsCritical = ext.IsCritical,
                                        Value      = gnames,
                                    });
                                }
                            }

                            // No need to search any more.
                            break;
                        }
                    }
                }
            }
        }
コード例 #5
0
 public static void Main()
 {
     CertificateExtensions t = new CertificateExtensions();
     t.Start();
 }
コード例 #6
0
    public static void Main()
    {
        CertificateExtensions t = new CertificateExtensions();

        t.Start();
    }
コード例 #7
0
 private void UpdateCertificate(string certFile, string privateKeyFile)
 {
     certificate = CertificateExtensions.GetSertificate(certFile, privateKeyFile);
 }
コード例 #8
0
ファイル: VMCATableView.cs プロジェクト: wfu8/lightwave
        public override NSMenu MenuForEvent(NSEvent theEvent)
        {
            UIErrorHelper.CheckedExec(delegate() {
                CGPoint pt   = this.ConvertPointFromView(theEvent.LocationInWindow, null);
                _selectedRow = this.GetRow(pt);
                //get datasource and node information
                NSTableViewDataSource ds = (NSTableViewDataSource)this.DataSource;
                NSMenu menu = new NSMenu();
                if (_selectedRow >= 0)
                {
                    if (ds is NodesListView)
                    {
                        string data = (ds as NodesListView).Entries [(int)_selectedRow].DisplayName;
                        switch (data)
                        {
                        case "Key Pairs":
                            NSMenuItem createKeyPair = new NSMenuItem("Create Key Pair", HandleKeyPairRequest);
                            menu.AddItem(createKeyPair);
                            break;

                        case "Certificates":
                            NSMenuItem createCertificate = new NSMenuItem("Create Certificate", HandleCreateSelfSignedCertificate);
                            menu.AddItem(createCertificate);
                            break;

                        case "Signing Requests":
                            NSMenuItem createSigningRequest = new NSMenuItem("Create SigningRequest", HandleCreateSigningRequest);
                            menu.AddItem(createSigningRequest);
                            break;

                        default:
                            break;
                        }
                    }
                    else if (ds is CertificateDetailsListView || ds is PrivateCertsListView)
                    {
                        X509Certificate2 cert = null;
                        if (ds is CertificateDetailsListView)
                        {
                            CertificateDetailsListView lw = ds as CertificateDetailsListView;
                            cert = lw.Entries [(int)_selectedRow];
                            if (lw.CertificateState == (int)VMCA.CertificateState.Active)
                            {
                                NSMenuItem revokeCert = new NSMenuItem("Revoke Certificate", (object sender, EventArgs e) => {
                                    UIErrorHelper.CheckedExec(delegate() {
                                        VMCACertificateService.RevokeCertificate(cert, lw.ServerDto);
                                        NSNotificationCenter.DefaultCenter.PostNotificationName("ReloadAll", this);
                                    });
                                });
                                menu.AddItem(revokeCert);
                            }
                        }
                        if (ds is PrivateCertsListView)
                        {
                            cert = CertificateExtensions.GetX509Certificate2FromString((ds as PrivateCertsListView).Entries [(int)_selectedRow].Certificate);
                        }
                        NSMenuItem showCert = new NSMenuItem("Show Certificate", (object sender, EventArgs e) => {
                            CertificateService.DisplayX509Certificate2(this, cert);
                        });
                        menu.AddItem(showCert);

                        NSMenuItem showCertString = new NSMenuItem("Show Certificate String", (object sender, EventArgs e) => {
                            UIHelper.ShowGenericWindowAsSheet(VMCACertificate.GetCertificateAsString(cert), "Certificate String", VMCAAppEnvironment.Instance.MainWindow);
                        });
                        menu.AddItem(showCertString);

                        /* if (lw.CertificateState == -1) {
                         * NSMenuItem deleteString = new NSMenuItem ("Delete Certificate", (object sender, EventArgs e) => {
                         *  lw.ServerDto.PrivateCertificates.RemoveAll (x => x.Certificate.Equals (cert.ToString ()));
                         *  NSNotificationCenter.DefaultCenter.PostNotificationName ("ReloadTableView", this);
                         *
                         * });
                         * menu.AddItem (deleteString);
                         * }*/
                    }
                    else if (ds is CSRDetailListView)
                    {
                        CSRDetailListView lw = ds as CSRDetailListView;
                        var csr             = lw.Entries [(int)_selectedRow].CSR;
                        NSMenuItem showCert = new NSMenuItem("Show CSR", (object sender, EventArgs e) => {
                            UIHelper.ShowGenericWindowAsSheet(csr, "CSR", VMCAAppEnvironment.Instance.MainWindow);
                        });
                        menu.AddItem(showCert);
                    }
                    else if (ds is KeyPairDetailListView)
                    {
                        KeyPairDetailListView lw = ds as KeyPairDetailListView;
                        var privateKey           = lw.Entries [(int)_selectedRow].PrivateKey;
                        var publicKey            = lw.Entries [(int)_selectedRow].PublicKey;
                        KeyPairData keyPair      = new KeyPairData(privateKey, publicKey);
                        NSMenuItem showCert      = new NSMenuItem("Export KeyPair", (object sender, EventArgs e) => {
                            VMCAKeyPairNode.SaveKeyData(keyPair);
                        });
                        menu.AddItem(showCert);
                    }
                    NSMenu.PopUpContextMenu(menu, theEvent, theEvent.Window.ContentView);
                }
            });
            return(base.MenuForEvent(theEvent));
        }