<commit_before>﻿// Copyright (c) 2015 - Basho Technologies, Inc.
//
// This file is provided to you under the Apache License,
// Version 2.0 (the "License"); you may not use this file
// except in compliance with the License.  You may obtain
// a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.  See the License for the
// specific language governing permissions and limitations
// under the License.

using System;
using System.IO;
using System.Linq;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using RiakClient.Extensions;
using RiakClient.Config;
using RiakClient.Messages;

namespace RiakClient.Auth
{
    internal class RiakSecurityManager
    {
        private static readonly StoreLocation[] storeLocations =
            new StoreLocation[] { StoreLocation.CurrentUser, StoreLocation.LocalMachine };
        private static readonly string[] subjectSplit = new[] { ", " };
        private readonly string targetHostCommonName;
        private readonly IRiakAuthenticationConfiguration authConfig;
        private readonly X509CertificateCollection clientCertificates;
        private readonly X509Certificate2 certificateAuthorityCert;

        // Interesting discussion:
        // http://stackoverflow.com/questions/3780801/whats-the-difference-between-a-public-constructor-in-an-internal-class-and-an-i
        // http://stackoverflow.com/questions/9302236/why-use-a-public-method-in-an-internal-class
        internal RiakSecurityManager(string targetHost, IRiakAuthenticationConfiguration authConfig)
        {
            if (targetHost.IsNullOrEmpty())
            {
                throw new ArgumentNullException("targetHost");
            }
            targetHostCommonName = String.Format("CN={0}", targetHost);
            this.authConfig = authConfig;

            if (IsSecurityEnabled)
            {
                clientCertificates = GetClientCertificates();
                certificateAuthorityCert = GetCertificateAuthorityCert();
            }
        }

        public bool IsSecurityEnabled
        {
            get
            {
                return (authConfig != null && (!authConfig.Username.IsNullOrEmpty()));
            }
        }

        public bool ClientCertificatesConfigured
        {
            get { return clientCertificates.Count > 0; }
        }

        public X509CertificateCollection ClientCertificates
        {
            get { return clientCertificates; }
        }

        public bool ServerCertificateValidationCallback(object sender, X509Certificate certificate,
            X509Chain chain, SslPolicyErrors sslPolicyErrors)<commit_msg>Ensure security is not configured on Mono at all<commit_after>﻿// Copyright (c) 2015 - Basho Technologies, Inc.
//
// This file is provided to you under the Apache License,
// Version 2.0 (the "License"); you may not use this file
// except in compliance with the License.  You may obtain
// a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.  See the License for the
// specific language governing permissions and limitations
// under the License.

using System;
using System.IO;
using System.Linq;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using RiakClient.Extensions;
using RiakClient.Config;
using RiakClient.Messages;
using RiakClient.Util;

namespace RiakClient.Auth
{
    internal class RiakSecurityManager
    {
        private static readonly StoreLocation[] storeLocations =
            new StoreLocation[] { StoreLocation.CurrentUser, StoreLocation.LocalMachine };
        private static readonly string[] subjectSplit = new[] { ", " };
        private readonly string targetHostCommonName;
        private readonly IRiakAuthenticationConfiguration authConfig;
        private readonly X509CertificateCollection clientCertificates;
        private readonly X509Certificate2 certificateAuthorityCert;

        // Interesting discussion:
        // http://stackoverflow.com/questions/3780801/whats-the-difference-between-a-public-constructor-in-an-internal-class-and-an-i
        // http://stackoverflow.com/questions/9302236/why-use-a-public-method-in-an-internal-class
        internal RiakSecurityManager(string targetHost, IRiakAuthenticationConfiguration authConfig)
        {
            if (targetHost.IsNullOrEmpty())
            {
                throw new ArgumentNullException("targetHost");
            }
            targetHostCommonName = String.Format("CN={0}", targetHost);
            this.authConfig = authConfig;

            if (IsSecurityEnabled)
            {
                clientCertificates = GetClientCertificates();
                certificateAuthorityCert = GetCertificateAuthorityCert();
            }
        }

        public bool IsSecurityEnabled
        {
            get
            {
                return (false == MonoUtil.IsRunningOnMono) &&
                       (authConfig != null && (!authConfig.Username.IsNullOrEmpty()));
            }
        }

        public bool ClientCertificatesConfigured
        {
            get { return clientCertificates.Count > 0; }
        }

        public X509CertificateCollection ClientCertificates
        {
            get { return clientCertificates; }
        }

        public bool ServerCertificateValidationCallback(object sender, X509Certificate certificate,
            X509Chain chain, SslPolicyErrors sslPolicyErrors)
{'fraction_non_alphanumeric': 0.07965353260869565, 'fraction_numerical': 0.014605978260869566, 'mean_word_length': 2.626231527093596, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1549580', 'n_tokens_mistral': 1481, 'n_tokens_neox': 1390, 'n_words': 407}