aisop/tools/New-License.ps1

32 lines
1.9 KiB
PowerShell
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<#
.SYNOPSIS
发行方离线签发绑定磁盘硬件的短期或永久授权;要求 PowerShell 7,不能把私钥发给客户。
.EXAMPLE
pwsh -File tools/New-License.ps1 -Machine 64位硬件机器码 -Kind Month
#>
[CmdletBinding()]
param(
[Parameter(Mandatory)][ValidatePattern('^[A-F0-9]{64}$')][string]$Machine,
[Parameter(Mandatory)][ValidateSet('Month', 'Permanent')][string]$Kind,
[string]$PrivateKeyPath = (Join-Path $env:USERPROFILE '.codex/secrets/aisop-license/issuer-private.pem'),
# 同一订单重发时必须沿用原 Id,避免重复发码导致额外续期。
[ValidatePattern('^[a-f0-9]{32}$')][string]$Id = ([guid]::NewGuid().ToString('N'))
)
$ErrorActionPreference = 'Stop'
if ($PSVersionTable.PSVersion.Major -lt 7) { throw '请使用 PowerShell 7(pwsh)运行发码工具' }
$rsa = [System.Security.Cryptography.RSA]::Create()
try {
$rsa.ImportFromPem([IO.File]::ReadAllText((Resolve-Path -LiteralPath $PrivateKeyPath)))
# 防止误用另一个产品的私钥,签发前先与本仓库公钥比对。
$publicRsa = [System.Security.Cryptography.RSA]::Create()
try {
$publicRsa.ImportFromPem([IO.File]::ReadAllText((Join-Path $PSScriptRoot '../src/SopHelper.CrossPlatform.Engine/Licensing/issuer-public.pem')))
if ($rsa.ExportSubjectPublicKeyInfoPem() -cne $publicRsa.ExportSubjectPublicKeyInfoPem()) { throw '私钥与当前软件的验签公钥不匹配' }
} finally { $publicRsa.Dispose() }
$payload = [Text.Encoding]::UTF8.GetBytes(([ordered]@{
Version = 1; Id = $Id; Machine = $Machine; Kind = $Kind; IssuedUtc = [DateTimeOffset]::UtcNow.ToString('O')
} | ConvertTo-Json -Compress))
$signature = $rsa.SignData($payload, [Security.Cryptography.HashAlgorithmName]::SHA256, [Security.Cryptography.RSASignaturePadding]::Pss)
Write-Output ('AISOP1.' + [Convert]::ToBase64String($payload) + '.' + [Convert]::ToBase64String($signature))
} finally { $rsa.Dispose() }