Socks 5: Credentials on Windows

In this installment, we will continue our implementation of GSSAPI/SSPI, this time on Windows, where we’ll try to get some credentials.

We will look at two topics this time: first, we’ll look at data encapsulation, after which we’ll look at when RAII is a bit too much, and how to handle that.

Data encapsulation

One of the main reasons we’re going to the trouble of creating an abstract factory is because we want the users of our code to be pretty close to impervious to the way security is implemented. Most notably, we don’t want the user to be obliged to know anything about SSPI or GSSAPI and we don’t want them to have to include any of the associated headers – or even necessarily have those headers around. This comes at a cost, though: we need to hide our implementation so far that we need to allow the user to not even know about the types we are using. In our first patch today, we’ll see how to do that.

Let’s first take a look at the changes we make to the Mechanism.h file, in the sspi library:

Like I said in a previous installment, there’s not much point in trying to hide the standard library implementation, as whoever uses our code will be using it anyway. There’s not much point, either, in trying to hide that an instance of Mechanism needs a package name to be created under SSPI: the MechanismFactory will do the creating for us, so if all goes well, the client code won’t even include this file. There is a chance, however, that they will, in which case we don’t want the user to be dependent on the Windows headers. That’s why I’ve created a forward-declaration to a private member structure called Data_. The structure itself is not here, but is defined in the .cpp file. It contains the package information we will need for later use, and it is conveniently hidden from view.

You might be wondering by now why the Data_ structure doesn’t have to be visible here – after all, I do have a pointer to an instance of it in my class. The thing is, though, that there are only a few reasons why the compiler needs to know the definition of a structure: it needs to know how to create an instance of it when one is defined or explicitly allocated with new; it needs to know how to destroy it when one goes out of scope or is explicitly destroyed with delete, and it needs to know how big it is when one is used as a member of something else. You might think that our use in this header would fall in that last category, but it doesn’t: our member is a pointer to an instance of Data_. It is not an instance of Data_ by itself. Pointers usually have the same size regardless of what they point to and, in any case, the compiler can figure out what that size is without knowing what it points to, so there’s no need, here, to know the definition of Data_ in order to use it.

As you can see in this chunk, Data_ doesn’t do much of anything: it will free the PSecPkgInfo instance when it’s done, but that’s about all. All of the logic is still in the Mechanism class, where it belongs.

As you can see, we assume that on Windows, the default package is whichever is listed first in the available packages – or “mechanisms” as we call them. This holds true for Windows and, as long as we don’t start sorting those packages, will hold true when we care for it to hold true.

in which we get the default mechanism in our test. Of course, we should really think of creating an actual unit test, but that’s beyond the scope of this installment.

So, data encapsulation works because we don’t need to know the definition of the data in order to be able to work with it, and it’s useful because it hides the nitty-gritty from those who don’t need to know it.

When RAII is too much

I’ve been going on about RAII in practically every installment of this podcast now – at least since I introduced the concept a while ago. You must either think that I am stark raving mad, or that I’m addicted to RAII. You might actually be right – whichever option you opt for – but you should also know that I can go without RAII for a few (albeit very few) lines of code. Sometimes, RAII is more trouble than it’s worth.

This is where I obtain a handle on the credentials of a given principal and, if that works, pass it on to a new instance of Credentials, which will manage its lifetime and whose lifetime is managed by an auto_ptr. Now, the important bit here is that there are two things that can go wrong between the try and the catch: the allocation might fail, in which case we’d be in big trouble but, more importantly, the constructor of the Credentials class would never be called, and construction of the Credentials instance might fail. If I were to define Credentials such that anything that is passed to its constructor will be taken over by the Credentials class, even if its construction fails – a popular and often worthy option – I’d have a problem in that I’d need to know which step went wrong. It is the option auto_ptr implements, though, and that is a Good Thing, though it would also complicate matters if construction of auto_ptr actually could fail.

Now, what would have happened had I used RAII for this task? There are two options: either I’d have created a scoped handle on the credentials handle, which I would have dismissed after the construction of the Credentials instance, but that would have left the same problem as I have now: determining which construction failed in order to know whether or not to dismiss the guard. An option that doesn’t actually make the code any cleaner is not really an option.

The alternative would have been to create a class that would explicitly hand ownership of the credentials handle to the new instance of the Credentials class, much like auto_ptr does with pointers. If it goes out of scope while still holding the reference, it will call the proper clean-up function.

Implementing that option can be fun – I would invite you to do it and contribute your result. It can also be far more work than writing a try–catch block with as its only benefit to make the code marginally cleaner. I say marginally because, although the clarity of the semantics would be a lot better, this piece of code isn’t repeated anywhere else. As such, weighing the benefits of implementing the “right” RAII solution (automatic ownership transfer) vs. the use of a try–catch block, the try–catch block wins because it is that much less work. Implementing the wrong RAII solution simply doesn’t make any sense.

So, I may be off my rocker and/or addicted to RAII, but apparently I still have a wee bit of common sense. I’ll try applying that again in four weeks – I’m taking the next two weeks off from the podcast, so there will be no mid-August installment.

About rlc

Software Analyst in embedded systems and C++, C and VHDL developer, I specialize in security, communications protocols and time synchronization, and am interested in concurrency, generic meta-programming and functional programming and their practical applications.
I take a pragmatic approach to project management, focusing on the management of risk and scope.
I have over two decades of experience as a software professional and a background in science.