Sample code issue

  • Thread starter Thread starter EMonaco
  • Start date Start date
E

EMonaco

All,

Got this sample from a discussion on ICertificatePolicy.

public enum CertificateProblem : long
{
CertEXPIRED = 0x800B0101,
CertVALIDITYPERIODNESTING = 0x800B0102,
CertROLE = 0x800B0103,
CertPATHLENCONST = 0x800B0104,
CertCRITICAL = 0x800B0105,
CertPURPOSE = 0x800B0106,
CertISSUERCHAINING = 0x800B0107,
CertMALFORMED = 0x800B0108,
CertUNTRUSTEDROOT = 0x800B0109,
CertCHAINING = 0x800B010A,
CertREVOKED = 0x800B010C,
CertUNTRUSTEDTESTROOT = 0x800B010D,
CertREVOCATION_FAILURE = 0x800B010E,
CertCN_NO_MATCH = 0x800B010F,
CertWRONG_USAGE = 0x800B0110,
CertUNTRUSTEDCA = 0x800B0112
}

private String GetProblemMessage(CertificateProblem Problem)
{
String ProblemMessage = "";
CertificateProblem problemList = new CertificateProblem();
String ProblemCodeName = Enum.GetName(problemList.GetType(),Problem);

if(ProblemCodeName != null)
ProblemMessage = ProblemMessage + "-Certificate Problem:" +
ProblemCodeName;
else
ProblemMessage = "Unknown Certificate Problem";

return ProblemMessage;
}

The problem is GetProblemMessage() always returns "Unknown Certificate
Problem" even for Problem codes that are in the CertificateProblem enum.
What gives?


Erin.
 
The problem is GetProblemMessage() always returns "Unknown Certificate
Problem" even for Problem codes that are in the CertificateProblem enum.
What gives?

I don't know - the code snippet you gave works fine for me, when put
into a complete program. Could you come up with a complete program
which exhibits the problem? See
http://www.pobox.com/~skeet/csharp/complete.html for exactly what I
mean.

One thing to note: there's no need for problemList in the above. You
could just use typeof(CertificateProblem) to get the appropriate type
for Enum.GetName().
 
Hi,
Enum.GetName return null if the value is not found, try this:

String ProblemCodeName = Enum.GetName( typeof( CertificateProblem )
,Problem);

Hope this help,
 
EMonaco said:
Below is a simple c# console app that attempts to GET
https://www.microsoft.com, however I've used the IP address so it will
report a CN_NO_MATCH problem to the CheckValidationResults(), this function
calls GetProblemMessage (which I've cleaned up from the MS sample it came
from). Run this and you should see:

Server Certificate Info:
.Issuer=DC=com, DC=microsoft, DC=corp, DC=redmond, CN=Microsoft
Secure Server Authority
.EffectiveDate=3/27/2003 12:08:35 PM
.ExpirationDate=3/26/2004 12:08:35 PM
.Subject=C=US, S=washington, L=Redmond, O=Microsoft, OU=mscom,
CN=www.microsoft.com
Certificate Problem with accessing https://207.46.134.190/. Problem code
0x800B010F. Unknown Certificate Problem.

Press Enter To Exit

Clearly 0x800B010F is in the CertificateProblem enum- so what gives?

You're passing in an int as the problem number. However, an int can't
possibly hold a value of 0x800b010f - it's only because you're printing
it out as hex that you're seeing it as a positive number at all.

If you change each value to either the actual negative number or use

unchecked((int)0x800b010f) etc

then you'll find it starts working. You can also change the
CertificateProblem enum to have a base type of int at that stage, too.
 
Jon,

Good catch. By changing the CertificateProblem enum to : uint it performs
as expected with no other changes needed.
Is there a link to report .NET documentation corrections? The
ICertificatePolicy Interface (overview) incorrectly uses long for the
CertificateProblem enum, as well as the extra bloat already mentioned with
declaring an instance of the enum to do the Enum.GetName() lookup in
GetProblemMessage().

Erin.
 
EMonaco said:
Good catch. By changing the CertificateProblem enum to : uint it performs
as expected with no other changes needed.

Right - it then won't be CLS compliant though, as far as I know. (You
may have problems using it from VB.NET, for instance.) I'm not *sure*
on this, but it's something to consider. (It isn't an issue if it's
only internal and will only be used by your C# code, of course.)
Is there a link to report .NET documentation corrections? The
ICertificatePolicy Interface (overview) incorrectly uses long for the
CertificateProblem enum, as well as the extra bloat already mentioned with
declaring an instance of the enum to do the Enum.GetName() lookup in
GetProblemMessage().

I don't know, to be honest.
 
Back
Top