X.509 certificate can't find with "FindBySubjectName" - wcf

After a brutal struggle with WCF Security, I think I'm at the final stage now and can see the light.
I've got a Client certificate installed on my server, and is now, as advised, in the Trusted People folder of the certificate store.
However, when I try and read the certificate application -> service, I get this error:
Cannot find the X.509 certificate using the following search criteria: StoreName 'My', StoreLocation 'CurrentUser', FindType
'FindBySubjectName', FindValue 'Forename Surname'.
With the "Forename Surname" being the "Issued to" part of my certificate. In all tutorials I have seen, this is just one word; is this the problem? I received my certificate from my CA with these two words, with a space.
Anyone ever come across this, is there something I'm blatantly doing wrong?
Update, cert can be seen here:
Update:
It gets even more strange:
I installed Visual Studio on my web server, and used the following code to pick up the cert by Thumbprint:
var store = new X509Store(StoreName.TrustedPeople, StoreLocation.CurrentUser);
store.Open(OpenFlags.ReadOnly);
var certs = store.Certificates.Find(X509FindType.FindByThumbprint, "71995159BFF803D25BFB691DEF7AF625D4EE6DFB", false);
This actually RETURNS a valid result. When I put this information into the web.config of my service/client though, I still get the error.

I think..You installed certificate at location Trusted People and searching at store name my
var store = new X509Store(StoreName.TrustedPeople, StoreLocation.CurrentUser);
store.Open(OpenFlags.ReadOnly);
var certs = store.Certificates.Find(X509FindType.FindBySubjectDistinguishedName, certificateSubject, false);
Also there are two search terms FindBySubjectName or FindBySubjectDistinguishedName, the later is more relevant with keywords and first one will find anything with search keywords.
So basically you need to look for Subject and if you use above code then your search string would be .."CN=urs.microsoft.com, O=DO_NOT_TRUST, OU=Created by http://fiddler2.com"

https://i.stack.imgur.com/QtYvV.png
private X509Certificate2 GetCertificateFromStore()
{
var store = new X509Store(StoreName.My, StoreLocation.LocalMachine);
store.Open(OpenFlags.ReadOnly);
var certCollection = store.Certificates;
var currentCerts = certCollection.Find(X509FindType.FindBySubjectDistinguishedName, "CN=sf.sandbox.mapshc.com", false);
return currentCerts.Count == 0 ? null : currentCerts[0];
}

Related

How to setup Gatling to use client certificates per virtual user?

How to setup Gatling to use client certificates per virtual user?
I am preparing Gatling simulations for API that is protected by client certificates and currently I only managed to set using one client certificate for all simulations in Gatling configuration
ssl {
keyStore {
#type = ""
file = "C:/client_certificates/00001.pfx"
password = "password"
#algorithm = ""
}
}
However, I would like to have different client certificates for different virtual users (client certificates can be selected randomly from the list of available client certificates or circularly). Galing documentation mentions keymanagerfactory and perUserKeyManagerFactory function, but without explicit examples of how to use it.
The keys of the config provided in the question are referenced in io.gatling.core.ConfigKeys.ssl.keyStore. The values are read and passed to Ssl.newKeyManagerFactory to create a KeyManagerFactory.
perUserKeyManagerFactory takes in a function Long => KeyManagerFactory that constructs the KeyManagerFactory from the virtual user's ID.
Knowing that we can write the following:
import io.gatling.commons.util.Ssl
...
.perUserKeyManagerFactory { userId =>
val fileName = f"C:/client_certificates/$userId%05d.pfx"
Ssl.newKeyManagerFactory(None, fileName, "password", None)
}
With the f interpolator, we can easily pad the userId with zeros to have 5 digits.
To select the files circularly, we can write ${userId % totalNumber} instead of $userId.

what is the use of "https://expired.badssl.com" ? Can someone explain it to me

I am using it in my code as follow. but can't understand what is the use of it.
URL url = new URL("https://expired.badssl.com/");//https://revoked.grc.com/
HttpsURLConnection urlConnection = (HttpsURLConnection)url.openConnection();
urlConnection.setHostnameVerifier(connection.getHostnameVerifier());
urlConnection.setSSLSocketFactory(connection.getSSLContext().getSocketFactory();
LineNumberReader lnr =
new LineNumberReader(new InputStreamReader(urlConnection.getInputStream()));
String line;
while (null != (line = lnr.readLine()))
System.out.println(line);
https://expired.badssl.com/
has expired SSL certificate, used to test edge cases where you may encounter expired certificate while connecting to some website.
You can find more # https://github.com/chromium/badssl.com
It provides other sub-domains as
self-signed.badssl.com to test self signed certificate related edge cases
mixed.badssl.com etc.

Verify user identity on my site, using SSL Certificates

I need to register companies on my site for an electronic procurement system. Up to now these were local companies I could meet physically and give credentials to, but now they can be companies based anywhere in the world.
The solution is to have an online registration process whereby they submit a third party certificate. So say Verisign says they are 'Company X' so I register them as Company X and issue them credentials.
How can I implement this on my site? Do I simply give them a field in the registration form where they upload their certificate file? Do I then manually check these certificates in my back office? How does one check this manually? Is there a way to automate this process?
Once they have an account, should I simply request the credentials I issue them with to log in, or can all future logins request the same certificate file? In these a particular format for certificates I can request or should I allow a number of common formats that different certificate vendors provide?
Thanks in advance.
Being able to provide a certificate does unfortunately not prove anything. A certificate is completely public, and anyone can get a hold of the SSL certificate for any website. The certificate contains a public key. Proving ownership of the corresponding private key is what's required.
This is possible to do, but it requires that your users are technical enough to know how to run scripts and/or OpenSSL terminal commands so that they can sign something with their private key. Having the users upload their private key is of course a big no-no, as it means you can now act as the user, and that would require an enormous amount of trust in you to discard the private key after you've verified it.
From a technical perspective, you can do the verification by creating some kind of challenge, for example a random string, and have the user encrypt this string with their private key. If you decrypt this string with the public key in the certificate, and get the original string back, then you know that they have possession of the corresponding private key.
Here's a self-contained Ruby script that demonstrates this, with comments indicating which part of it is run on your side, and which part is run on their side.
require "openssl"
## This happens on the client side. They generate a private key and a certificate.
## This particular certificate is not signed by a CA - it is assumed that a CA
## signature check is already done elsewhere on the user cert.
user_keypair = OpenSSL::PKey::RSA.new(2048)
user_cert = OpenSSL::X509::Certificate.new
user_cert.not_before = Time.now
user_cert.subject = OpenSSL::X509::Name.new([
["C", "NO"],
["ST", "Oslo"],
["L", "Oslo"],
["CN", "August Lilleaas"]
])
user_cert.issuer = user_cert.subject
user_cert.not_after = Time.now + 1000000000 # 40 or so years
user_cert.public_key = user_keypair.public_key
user_cert.sign(user_keypair, OpenSSL::Digest::SHA256.new)
File.open("/tmp/user-cert.crt", "w+") do |f|
f.write user_cert.to_pem
end
## This happens on your side - generate a random phrase, and agree on a digest algorithm
random_phrase = "A small brown fox"
digest = OpenSSL::Digest::SHA256.new
## The client signs (encrypts a cheksum) the random phrase
signature = user_keypair.sign(digest, random_phrase)
## On your side, verify the signature using the user's certificate.
your_user_cert = OpenSSL::X509::Certificate.new(File.new("/tmp/user-cert.crt"))
puts your_user_cert.public_key.verify(digest, signature, random_phrase + "altered")
# => falase
puts your_user_cert.public_key.verify(digest, signature, random_phrase)
# => true
## On your side - attempting to verify with another public key/keypair fails
malicious_keypair = OpenSSL::PKey::RSA.new(2048)
puts malicious_keypair.public_key.verify(digest, signature, random_phrase)
Note that this script does not take into account the CA verification step - you also obviously want to verify that the user's certificate is verified by a CA, such as Verisign that you mentioned, because anyone can issue a certificate and hold a private key for foo.com - it's the CA signature of the certificate that provides authenticity guarantees.

What subject to use for SetClientCertificate?

I'm trying to send a request with:
ActiveXObject("WinHttp.WinHttpRequest.5.1")
however this requires a client certificate to do so (which we have been provided).
Having tested in PHP cURL I can do so with:
curl_setopt($SOAP, CURLOPT_SSLCERT,$filepathtocertificate);
Which works fine. However I must use IIS running asp(javascript) and point to the certificate store of the machine the script is running on with:
SetClientCertificate("LOCAL_MACHINE\\Personal\\Certificate subject");
for our actual deployment. The MS documentation (http://msdn.microsoft.com/en-us/library/windows/desktop/aa384055(v=vs.85).aspx) suggests that the path above has to state the 'subject' of the certificate, however the certificate seems to have several subjects and no combination of several or all of them seems to yeild any results and I am stuck with the following error before the request is even sent:
WinHttp.WinHttpRequest error '80072f0c'
A certificate is required to complete client authentication
Looking in the certificate store and using other scripts in the same folder show they are definitely there but have subjects like:
C=US, O=Organisation NAme, OU="Another Organisation Name, Inc.", CN=Organisation Name Root
Or similar.
Any advice on what parameters SetClientCertificate needs to be given to select and send certificates in the certificate store would be much appreciated.
I had a lot of trouble with this same issue - using winhttp 5.1 from a scripting language to set a client certificate before a send.
I had used mmc with the certificates snap-in to import the certificate in CURRENT_USER \ Personal - but the Winhttp SetClientCertificate didn't seem to be doing anything, nor was I able to pick up any error code or message so it was a case of repeated trial and error - the SetClientCertificate string should be something like "Location\store\subject" eg "CURRENT_USER\Personal\My Certificate" (or \ \ if your language requires \ to be escaped) -the final part being 'subject' which is not as clear as it should be. Under MMC the subject is broken into many elements.
I eventually got it working by dropping the location and store - they were the defaults so I may have been fortunate - and providing just the subject field - the value I used for the subject field was the value in the line "CN = " under subject (when the cert is opened under mmc) - but this (perhaps coincidentally) was also the value in the 'Issued To' column on the main mmc certificate list. In my case it worked - clearly if there is a cert with these two values different then you'd need to try each.
Hope this helps if somebody is similarly stuck.
This is a very old question yet I had to find an answer today. The answer provided above by #JSL helped me. If you only provide the certificate subject name then it works! So it is clear that there is a mistake in the way full path is specified.
I got the right info for Windows 7 from this link https://msdn.microsoft.com/en-us/library/windows/desktop/aa384076(v=vs.85).aspx
here is VBA script that works.
Dim objHttp As New WinHttp.WinHttpRequest
objHttp.Open "GET", url, False
objHttp.SetClientCertificate "CURRENT_USER\My\TestCert"
objHttp.send
Valid locations are LOCAL_MACHINE and CURRENT_USER and
Valid store names are "MY", "Root", and "TrustedPeople".
Remember to escape the backslashes in C++, C# etc.

How to use personal certificate for WCF communication?

When inserting a smart card to a reader the certificates will be read in to the personal store, my question is simple how I then explain for WCF that it should use a specific (of these) certificate in runtime?
My WCF service is selfhosted and are communicating with the client over TCP.
I do already have a communication that uses certificate but these certificates is stated in the config files(one for service and one for client).
Now I need to switch the certificate on the client side before communicating.
You should try to find a specific set of attributes that is unique to the certificate you would like to use.
For example we use filtering like this:
/// <summary>
/// Get the certificate we need for authentication
/// </summary>
/// <returns>the certificate</returns>
/// <exception cref="InvalidOperationException">when no certificate is available</exception>
public static X509Certificate2 GetCertificate()
{
// open private certificate store of the user
X509Store store = new X509Store(StoreName.My);
store.Open(OpenFlags.ReadOnly);
// get the collection of certificates which we need
X509Certificate2Collection certColl = store.Certificates
.Find(X509FindType.FindByExtension, new X509KeyUsageExtension().Oid.Value, false)
.Find(X509FindType.FindByKeyUsage, X509KeyUsageFlags.NonRepudiation, false);
store.Close();
// if more certificates match the criteria, let the user choose one
if (certColl.Count > 1)
{
certColl = X509Certificate2UI.SelectFromCollection(
certColl, "Choose certificate", "We were unable to find the correct certificate. Please choose one from the list.",
X509SelectionFlag.SingleSelection);
}
// if no certificate is available, fail
if (certColl.Count < 1)
{
throw new InvalidOperationException("No certificates selected");
}
else
{
return certColl[0];
}
}
Check the attributes of the certificates you need and try to create your own Find criteria. Of course the use could have additional certificates in his store which match the criteria, this case a dialog window pops up and asks the user to do this himself.
Of course once the user chooses the correct certificate you can store the CN of the certificate in the application configuration file so next time he won't have to do these steps again.