Skip to content

solid-matrix/solid-matrix-device-licenses

Repository files navigation

SolidMatrix.DeviceLicenses

Generate license for device authorization.

Useful when developing device-bound applications.

Step 1

The Licensor generates the RSA private key and stores it very carefully (do not send it to the Licensee)

var privatePem = DeviceLicense.GeneratePrivatePem();

File.WriteAllText(PRIVATE_PEM_FILENAME, privatePem);

Step 2

Run the following code on the Licensee device to generate a unique GUID and copy it.

This GUID is based on CPU ID, Windows OS generated ID, and Baseboard SerialNumber.

var guid = DeviceLicense.GenerateDeviceGuid();
Console.WriteLine(guid);

Step 3

The Licensor generate a license based on the GUID from the Licensee device.

var privatePem = File.ReadAllText(PRIVATE_PEM_FILENAME);
var license = new DeviceLicense
{
    Issuer = ISSUER,
    Audience = AUDIENCE,
    Subject = SUBJECT,
    DeviceGuid = Guid.Parse(GUID_GET_FROM_AUDIENCE_SIDE),
    IssueAt = DateTimeOffset.UtcNow,
    ExpireAt = DateTimeOffset.UtcNow + TimeSpan.FromDays(365),
};

var licenseString = DeviceLicense.Encode(license, privatePem);
File.WriteAllText(LICENSE_FILENAME, licenseString);

Step 4

copy the license file to the Licensee device and run the following code

var licenseString = File.ReadAllText(LICENSE_FILENAME);

var guid = DeviceLicense.GenerateDeviceGuid();

// only verify the formatting and signature
// have to verify DeviceGuid and Expiration manually
var license = DeviceLicense.DecodeVerify(licenseString);

if (license == null)
    throw new Exception("failed to decode and verify");

if (license.Issuer != ISSUER)
    throw new Exception("invalid issuer");

if (license.Audience != AUDIENCE)
    throw new Exception("invalid audience");

if (license.Subject != SUBJECT)
    throw new Exception("invalid subject");

if (license.DeviceGuid != guid)
    throw new Exception("invalid device guid");

if (license.ExpireAt <= DateTimeOffset.UtcNow)
    throw new Exception("license expired");

Console.WriteLine("license certifaced");

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages