July 2004 - Posts

kennyandkarin.com is moving...

I'm in the process of moving our website from Yahoo's hosting service to http://www.easerve.com. Unfortunately www.kennyandkarin.com won't be available for a day or two. Yahoo cut the service earlier than expected.

So far the experience with EAServe has been great. As a result of the far superior features offered by them, you can expect some interesting new content and features on kennyandkarin.com. Stay tuned...

[Update: if you need to get at the site in a hurry, you can add 67.19.7.146 as a DNS server to your IP configuration. This will allow you to resolve kennyandkarin.com while we wait for the new configuration to propagate around the web.]


© 2004 Kenny Kerr

Posted by KennyKerr with 2 comment(s)

Stelnyk Life

I just noticed that my colleague and friend, John Stetic, just started blogging and they have some exciting news.

Congrats John and Mel!


© 2004 Kenny Kerr

Posted by KennyKerr with no comments

C++/CLI: The Most Powerful Language for .NET Framework Programming

My latest article is now live on MSDN. It should be headlined on the Visual C++ Developer Center soon.

C++/CLI: The Most Powerful Language for .NET Framework Programming
Summary: Explore the design and rationale for the new C++/CLI language introduced with Visual C++ 2005. Use this knowledge to write powerful .NET applications with the most powerful programming language for .NET programming.

This article tells a story about the new C++/CLI language design that I have wanted to share for some time now. I actually wrote this about six months ago, before any compiler was available to implement any of the new language features (don’t ask me why it took so long to publish). The language specification was very helpful in getting to know the new language design.

Many thanks to the Program Managers on the Visual C++ team for their helpful feedback on the draft of this article. Special thanks to Herb Sutter for taking the time to review the article and providing some very helpful feedback.

Visual C++ 2005 is a great product. Kudos to the entire team for delivering such an awesome compiler.

After reading my article I’m sure you will want to start playing with the compiler immediately. Keep in mind that some of the capabilities I describe in the article are not yet implemented in Beta 1.

Have fun!


© 2004 Kenny Kerr

 

Posted by KennyKerr with 9 comment(s)

Using Namespaces

There are some subtle differences in how C++ and C# handle namespaces and scope. I have had a few people ask me about it so I thought I would talk about it here. First the basics: a namespace allows you to group names in your program together to provide some context for them and to avoid conflicts with names declared in other namespaces. How you actually make use of names and namespaces in C++ and C# differ quite a bit.

Explicit Qualification

Consider the name Console declared in the namespace System.

In C++ the namespace member can be accessed using explicit qualification using the scope resolution operator:

System::Console

In C# the same can be accomplished with the period token:

System.Console

The difference here is merely syntactic sugar. The main difference is that C# uses the period for explicit qualification of names as well as for member access. C++ uses the scope resolution operator for declaring the scope of a name and uses the member access operators (. and ->) for referring to members of classes and structures.

That is where the behavioral similarities of namespaces in C++ and C# end.

Using Directive

A using directive allows the names in a namespace to be used without explicit qualification. In C++ you write a using directive as follows:

using namespace System;

This allows me to use Console without the qualifier. All the names declared in the namespace are now available. This includes nested namespace. For example since System has a nested namespace called IO that declares a name, FileStream, you can access FileStream as follows:

IO::FileStream

This is in contrast to C#’s using directive which only permits the types in a namespace to be accessed and does not bring forward any nested namespaces. In C# you write a using directive as follows:

using System;

If you wanted to access FileStream you would need to either use explicit qualification or add yet another using directive for the nested namespace:

using System.IO;

In this area I much prefer the C++ rules as I can write a using directive for the ubiquitous System namespace and use all manner of names and nested names without polluting the global namespace too much:

using namespace System;

int main()
{
    String^ string;
    IO::FileStream stream;
    Net::NetworkCredential^ credentials;
}

In C# it is more common (and popularized by Visual C#) to simply flood the global namespace with all the necessary types:

using System;
using System.IO;
using System.Net;

class Program
{
    static void Main()
    {
        String string1;
        FileStream stream;
        NetworkCredential credentials;
    }
}

The problem here of course is that we now face the problem namespaces were originally designed to solve: name collisions.

Alias

There are of course a few alternatives to using directives. In C++ you can use the typedef specifier to declare a new name for an existing type. The new name and the original name are interchangeable at compile-time (think templates) and runtime:

using namespace System;
typedef Runtime::Serialization::Formatters::Binary::BinaryFormatter Formatter;

int main()
{
    Formatter^ formatter;
}

C# allows you to create an alias with the using directive. An alias can be created for both namespaces and types:

using Formatter = System.Runtime.Serialization.Formatters.Binary.BinaryFormatter;

class Program
{
    static void Main()
    {
        Formatter formatter;
    }
}

C++ also offers the ability to introduce a name from a different name space without creating an alias per se:

using namespace System;
using Runtime::Serialization::Formatters::Binary::BinaryFormatter;

int main()
{
    BinaryFormatter^ formatter;
}

Hopefully this summary will help to clear up some confusion among developers moving between C++ and C#. This is certainly not a complete description of namespaces, but it covers the main usage patterns and differences that developers are likely to come across.


© 2004 Kenny Kerr

 

Posted by KennyKerr with no comments
Filed under: ,

I'm in German!

I just noticed that MSDN has translated my security article into German...

Windows-Sicherheitsfunktionen für eigene Anwendungen nutzen

And French...

Techniques de sécurisation des objets privés dans vos applications

And Italian…

Tecniche per la protezione di oggetti privati nelle applicazioni

And Spanish…

Técnicas para asegurar objetos privados en aplicaciones

That explains all those foreign language emails I’ve been getting!

:)

MSDN rocks!


© 2004 Kenny Kerr

Posted by KennyKerr with no comments

Programming with Credentials

Writing code to deal with password-based credentials can introduce many subtle bugs into your code. Having a good understanding of credentials is important in writing robust and correct code, not to mention secure code. In this piece I will focus on what makes up a set of credentials and how you should interpret them as a provider and consumer of credentials.

Firstly, password-based credentials involve three pieces of information:

Authority:

The entity that manages the account and provides authentication services. In Windows parlance this typically refers to a domain, but can also refer to the Security Account Manager (SAM) that manages local user accounts on a computer.

Principal:

A security principal is an entity that can be identified via authentication. A principal's account is managed by an authority. Principals typically represent users but can also represent computers when the computers are part of a domain.

Password:

The secret associated with a principal's account used for authentication.

Often people speak in terms of a "user name". This is not a strong term but generally refers to a string containing both the authority and principal names. A user name is most often expressed in one of three forms:

Authority\Principal:

This is commonly referred to as the SAM, or Windows NT, format since it is the format traditionally used by Windows NT. On Windows 2000 and later the Authority name is specified using the domain's down-level name.

For example: KENNYANDKARIN\kenny

Principal@Authority:

This is referred to as the User Principal Name (UPN) and was introduced with Windows 2000.

For example: kenny@kennyandkarin.com

Principal:

When the user provides only a principal name and no authority, the program needs to make some decisions on how to interpret it. Typically the authority is assumed to be the computer that is being accessed unless there is some other authority indicator in a user interface such as a combo box with a list of authorities.

For example: kenny

When dealing with credentials explicitly, it is common to use the System.Net.NetworkCredential class. The nice thing about this class is that internally it encrypts the credentials so a bag guy trying to steal the credentials by scanning your address space somehow, or possibly reading the swap file, cannot gain access to the clear text credentials. On the downside, the NetworkCredential class makes it easy to manage credentials incorrectly. Here are the properties of the NetworkCredential class and their descriptions from the MSDN Library:

Domain:

Gets or sets the domain or computer name that verifies the credentials.

Password:

Gets or sets the password for the user name associated with the credentials.

UserName:

Gets or sets the user name associated with the credentials.

As I said before, most people think of a user name as referring to both the principal and authority names. To make it worse, the NetworkCredential class offers a constructor that populates only the UserName and Password properties:

public NetworkCredential(string userName, string password);

I have noticed that many developers simply populate the UserName property with a SAM or UPN user name. This would be fine if there were not also a Domain property. A better approach is to simply consider the UserName property the Principal name and the Domain property the Authority name. That way there is no ambiguity from the consuming end whether the authority name should be taken from the UserName property or from the Domain property.

Of course, given a populated NetworkCredential object, library authors cannot make any assumptions about how the object was populated. If Domain is not a blank string you should assume this is the name of the authority. In this case the UserName property cannot contain an @ or \ character as this would introduce ambiguity. If Domain is blank then you need to determine whether the authority name is embedded in the UserName property.

Once you have deciphered the credentials, there may still be some complications depending on what you are going to do with them. If you are going to authenticate the principal yourself then it should be simple. If however you need to pass the credentials to another security subsystem, then you need to understand its requirements for expressing credentials. This is where you just need to be very careful. Read the documentation carefully for the functions you are calling and make sure you test your code with appropriate test cases.


© 2004 Kenny Kerr

 

Posted by KennyKerr with 2 comment(s)
Filed under:

Character Disassembler

Every so often I need to convert one or more characters to their numeric equivalents. This can be especially painful when dealing with Unicode characters. So I wrote a little tool called the Character Disassembler to help out.

Given a string as input it will list each character and its respective numeric value. Then you can select any of the resulting characters in the list and copy them to the clipboard. This is useful for pasting into a source file that is stored in ASCII encoding for example. The check box instructs the tool to insert the necessary casts to be able to initialize a character array in C#.

char[] chars = { (char) 0x48, (char) 0x65, (char) 0x6C, (char) 0x6C, (char) 0x6F };

Of course, this is not needed for C++:

wchar_t chars[] = { 0x48, 0x65, 0x6C, 0x6C, 0x6F };

Download it here. It's a pretty simple little program, but if you're interested in how it works, simply use the mighty .NET Reflector.

P.S. Don't take this to mean that it's OK to store disguised strings in your source code. Its not! But it may be useful for something that is culture neutral like the character mask for password text boxes.


© 2004 Kenny Kerr

Posted by KennyKerr with 1 comment(s)
Filed under:
More Posts