Question RFC: .NET 4.5 licensing using RSA4096 SNK, SHA256 signed XML, assembly sig enforcing


Well-known member
Oct 18, 2011
Montreal, QC, CA
Programming Experience
Hello folks,

For the past week I have been looking at taking advantage of the .NET 4.5 improvements to code signing and XML signing to produce a licensing subsystem I can use to license my own products. I now have the thing working pretty well, and I am looking for input as to how it could be improved, or what security issues I might have missed. Please feel free to propose enhancements and point out flaws if you see them.


What I wanted was to be able to take a solution with assemblies signed with a strong name key, and use that same key pair to sign licenses on my server and validate them in the client application. Historically the strong name assembly signature in .NET was always pretty weak. Until .NET 4.5 it only supported RSA keylengths of 1024 bits and used the now obsolete SHA1 hashing algorithm to produce signatures. The assembly signature is also not enforced by default, and only used to identify assemblies in the GAC. NET 4.5 has now added the support for any key length, and for the SHA256 signatures. The same support was also added to the SignedXml class. I wanted to use the strong name key pair to A) avoid having to distribute a separate public key and B) avoid having to dish out for a suitable CA certificate. I had an idea to use reflection to extract the public key from the signed assembly and use it to first validate the assembly integrity by enforcing signature validation, and then validate the license when an assertion was made.

The code

First, I created a strong name key file with a 4096-bits RSA key:

    Public Shared Function SaveKeyPairToSnk(rsa As RSACryptoServiceProvider, filename As String) As Boolean
            Using fs As New FileStream(filename, FileMode.Create, FileAccess.Write)
                Dim bytes = rsa.ExportCspBlob(True)
                fs.Write(bytes, 0, bytes.Length)
            End Using
            Return True
        Catch ex As Exception
            Return False
        End Try
    End Function

And signed some test assemblies with it in a VS solution.

Then, I needed a way to enforce the assembly signatures before asserting the license, to ensure they were not tampered with. I designed a base class that P/Invokes to StrongNameSignatureVerificationEx (mscoree.dll) for this, and throws is the validation fails. My protected assemblies would inherit from this class and call the base constructor on activation, which would validate the calling assembly signature and throw if validation failed, or if the calling assembly had a different public key than the base assembly, to marry them together.

Public Class LicensedClassBase
    Shared Sub New()
    End Sub

    Public Sub New()
    End Sub

    Private Shared Sub VerifyAssemblySignature(assmbly As Assembly)
        Dim wasVerified As Boolean
        If Not (NativeMethods.StrongNameSignatureVerificationEx(assmbly.Location, True, wasVerified) _
                AndAlso wasVerified _
                AndAlso assmbly.GetName().GetPublicKey().SequenceEqual(Assembly.GetExecutingAssembly().GetName.GetPublicKey())) Then
            Throw New LicensingException("Signature verification failed: Assembly signature did not match.")
        End If
    End Sub

End Class

To enforce signature validation, I simply inherit from this class and call the base constructors from the derived constructors. If the base constructor throws, the derived class cannot be instantiated. This part works nicely and prevents any tampering in the signed assemblies.

Then I started working on the licenses. This is a sample of a signed license file. This is actually one of my debugging licenses:

<License xmlns:xsi="" xmlns:xsd="">
    <CustName>Joe Blow</CustName>
    <Signature xmlns="">
            <CanonicalizationMethod Algorithm="" />
            <SignatureMethod Algorithm="" />
            <Reference URI="">
                    <Transform Algorithm="" />
                    <Transform Algorithm="" />
                <DigestMethod Algorithm="" />

This was signed using this function:

    Public Shared Function SignXml(xmlDoc As XmlDocument, rsaKey As RSACryptoServiceProvider) As XmlDocument
            CryptoConfig.AddAlgorithm(GetType(RSAPKCS1SHA256SignatureDescription), "")

            Dim signedXml As New SignedXml(xmlDoc)
            signedXml.SigningKey = rsaKey
            signedXml.SignedInfo.SignatureMethod = ""

            Dim reference As New Reference()
            reference.Uri = ""
            reference.AddTransform(New XmlDsigEnvelopedSignatureTransform())
            reference.AddTransform(New XmlDsigExcC14NTransform())
            reference.DigestMethod = ""


            Dim xmlDigitalSignature As XmlElement = signedXml.GetXml()
            xmlDoc.DocumentElement.AppendChild(xmlDoc.ImportNode(xmlDigitalSignature, True))
            If xmlDoc.FirstChild.GetType() = GetType(XmlDeclaration) Then xmlDoc.RemoveChild(xmlDoc.FirstChild)

            Return xmlDoc
        Catch ex As Exception
            xmlDoc = Nothing
        End Try
        Return xmlDoc
    End Function

And is validated using this one:

    Public Shared Sub VerifySignedXml(xmlDoc As XmlDocument, rsaKey As RSACryptoServiceProvider)
        Dim signedXml As New SignedXml(xmlDoc)
        Dim nodeList As XmlNodeList = xmlDoc.GetElementsByTagName("Signature")

        If nodeList.Count > 0 Then
            signedXml.LoadXml(CType(nodeList(0), XmlElement))
            Throw New LicensingException("Signed XML verification failed: No Signature was found in the document.")
        End If

        If Not signedXml.CheckSignature(rsaKey) Then
            Throw New LicensingException("Signed XML verification failed: Document signature did not match.")
        End If
    End Sub

Both are called from the base class constructor, to which I added this code to assert the license:

    Private Shared Sub AssertLicense(assmbly As Assembly)

        If assmbly IsNot Assembly.GetExecutingAssembly() Then
            Dim _config = New Configuration.ConfigManager()

            Dim serializer As New XmlSerializer(GetType(License))

            Dim featureId = ""
            Dim attrib = assmbly.GetCustomAttributes(True).OfType(Of FeatureIdAttribute)().FirstOrDefault
            If attrib IsNot Nothing Then
                featureId = attrib.FeatureId
            End If

            Utils.VerifySignedXml(_config.License, Utils.GetAssemblyPublicKey(assmbly))

            Using reader As XmlReader = New XmlNodeReader(_config.License)

                If serializer.CanDeserialize(reader) Then
                    Dim lic As License = serializer.Deserialize(reader)
                     Dim now = Utils.GetCurrentDateTime() ' This retrieves date and time  from HTTP response headers, or fallsback to DateTime.Now

                    If lic Is Nothing Then
                        Throw New LicensingException("Your license is corrupted.")
                    End If

                    If lic.IssueDate > now Then
                        Throw New LicensingException("Your license has not been activated yet.")
                    End If

                    If lic.ExpiryDate < now Then
                        Throw New LicensingException("Your license is expired.")
                    End If

                    If Not lic.HardwareIds.Contains(Utils.GetHardwareId()) Then
                        Throw New LicensingException("Your license is not valid for this hardware platform.")
                    End If

                    If Not My.Application.Info.ProductName.StartsWith(lic.ProductName, True, CultureInfo.InvariantCulture) Then
                        Throw New LicensingException("Your license is not valid for this product.")
                    End If

                     If Not  My.Application.Info.Version.ToString().StartsWith(lic.ProductVersion,  True, CultureInfo.InvariantCulture) Then
                        Throw New LicensingException("Your license is not valid for this version of the product.")
                    End If
                    If Not (attrib IsNot Nothing _
                         AndAlso lic.Features.FirstOrDefault(Function(f) f.ToUpperInvariant() =  featureId.ToUpperInvariant) IsNot Nothing) Then
                        Throw New LicensingException("You current license does not include access to the feature invoked.")
                    End If
                End If
            End Using
        End If
    End Sub

For the features support, I created a custom assembly attribute with a GUID as the feature ID, and I stamp the feature's assembly with it.

I realize this was a long post, and I welcome your input and suggestions/improvements, as well as possible vulnerabilities (other than the private key becoming known, that is a given).
Some planned additions are a licensing server using a WCF service backed with a database that can be linked to a CRM for automated license generation, activation and management, and support for floating licenses, as well as client component for online license activation and updates.

Also, I forgot to add above, this is how the hardware ID is generated:

    Public Shared Function GetHardwareId() As String
            Dim rawId = ""

            Using mbs As New ManagementObjectSearcher("Select * From Win32_processor")
                rawId += mbs.Get().Cast(Of ManagementObject)().First()("ProcessorID").ToString
            End Using

            Using dsk As New ManagementObject("win32_logicaldisk.deviceid=""c:""")
                rawId += dsk("VolumeSerialNumber").ToString()
            End Using

            Using mos As New ManagementObjectSearcher("Select * From Win32_ComputerSystemProduct")
                rawId += DirectCast(mos.Get().Cast(Of ManagementObject)().First()("UUID"), String)
            End Using

            Using md5 As New MD5CryptoServiceProvider
                Return Convert.ToBase64String(md5.ComputeHash(Encoding.UTF8.GetBytes(rawId)))
            End Using
        Catch ex As Exception
            Return Nothing
        End Try
    End Function