提供されている PowerShell スクリプトを使用して、OpenSSL を使用して無料のルート CA と秘密キーを作成できます。 このスクリプトはテストにのみ使用し、運用環境では使用しないでください。
前提条件
Device Registry 名前空間。 設定手順については、「Azure IoT Hubの Azure Device Registry から始めましょう。」をご覧ください。
外部ルートCAによって署名されるよう設定されたデバイスレジストリの中間CA。 中間CAからローカルマシンにCSRファイルをダウンロードしてください。
OpenSSL をインストールします。 OpenSSL がインストールされていない場合は、コマンド
winget install --id ShiningLight.OpenSSL.Dev -eを使用します。 環境変数に PATH に OpenSSL を追加します。
スクリプト
次の PowerShell スクリプトを selfsign.ps1という名前のファイルに保存します。 このスクリプトは、CSR ファイルをダウンロードしたのと同じディレクトリに配置します。
スクリプトの <path to the downloaded CSR> を CSR ファイルの名前に置き換えます。
# What this script does:
# 1. Creates a self-signed Root CA (a top-level certificate authority) and its private key.
# 2. Takes a CSR (Certificate Signing Request) that was generated elsewhere, and signs it with that Root CA to produce an intermediate CA (ICA) certificate
# 3. Builds a certificate chain file (ICA certificate + Root CA certificate) that you can upload/use as proof that the ICA is trusted.
$SCRIPT_DIR = if ($PSScriptRoot) { $PSScriptRoot } else { Split-Path -Parent $MyInvocation.MyCommand.Path }
$ROOT_CA_KEY = Join-Path $SCRIPT_DIR "byor-root-ca-key.pem"
$ROOT_CA_CERT = Join-Path $SCRIPT_DIR "byor-root-ca.pem"
$ICA_CERT = Join-Path $SCRIPT_DIR "byor-ica-cert.pem"
$ICA_EXTENSIONS = Join-Path $SCRIPT_DIR "byor-ica-extensions.cnf"
$CERT_CHAIN = Join-Path $SCRIPT_DIR "byor-certificate-chain.pem"
# IMPORTANT: Add the file path for your CSR
$CSR_FILE = "<path to the downloaded CSR>"
# Create Self-Signed Root CA
Write-Host "Creating Self-Signed Root CA (ECC P-384)..." -ForegroundColor Green
$timestamp = Get-Date -Format "yyyyMMddHHmmss"
$ROOT_CA_SUBJECT = "/CN=Test External Root CA - - $timestamp"
# Create ECC P-384 Root CA private key
Write-Host " Creating ECC P-384 private key..." -ForegroundColor Gray
& openssl ecparam -name secp384r1 -genkey -noout -out $ROOT_CA_KEY 2>&1
if (-not (Test-Path $ROOT_CA_KEY)) {
Write-Host " ERROR: Failed to create Root CA private key" -ForegroundColor Red
exit 1
}
Write-Host " SUCCESS: Private key created: $ROOT_CA_KEY" -ForegroundColor Gray
# Create self-signed Root CA certificate with a validity of 10 years, path length of 1 (Root -> ICA -> Leaf), and restricted key usage
Write-Host " Creating self-signed Root CA certificate..." -ForegroundColor Gray
& openssl req -new -x509 -key $ROOT_CA_KEY -out $ROOT_CA_CERT -days 3650 -subj $ROOT_CA_SUBJECT `
-addext "basicConstraints=critical,CA:TRUE,pathlen:1" `
-addext "keyUsage=critical,keyCertSign,cRLSign" `
-sha384 2>&1
if (-not (Test-Path $ROOT_CA_CERT)) {
Write-Host " ERROR: Failed to create Root CA certificate" -ForegroundColor Red
exit 1
}
Write-Host " SUCCESS: Root CA certificate created: $ROOT_CA_CERT" -ForegroundColor Gray
# Sign CSR with Root CA
Write-Host "Signing CSR with Root CA (fixing EC named curve encoding)..." -ForegroundColor Green
# Create temporary files for public key extraction and fix
$CSR_PUBKEY = Join-Path $SCRIPT_DIR "byor-csr-pubkey.pem"
$CSR_PUBKEY_FIXED = Join-Path $SCRIPT_DIR "byor-csr-pubkey-fixed.pem"
$CSR_FIXED = Join-Path $SCRIPT_DIR "byor-csr-fixed.pem"
# Extract public key from original CSR
Write-Host " Extracting public key from CSR..." -ForegroundColor Gray
& openssl req -in $CSR_FILE -pubkey -noout -out $CSR_PUBKEY 2>&1
if (-not (Test-Path $CSR_PUBKEY)) {
Write-Host " ERROR: Failed to extract public key from CSR" -ForegroundColor Red
exit 1
}
# Check if the public key has explicit parameters and fix it
# We use openssl ec to read and re-write the key with named curve format
Write-Host "Converting EC key to named curve format..." -ForegroundColor Green
& openssl ec -pubin -in $CSR_PUBKEY -out $CSR_PUBKEY_FIXED -param_enc named_curve 2>&1
# If conversion succeeded, use the fixed key; otherwise fall back to original
if (Test-Path $CSR_PUBKEY_FIXED) {
Write-Host " SUCCESS: EC key converted to named curve format" -ForegroundColor Gray
$useFixedKey = $true
} else {
Write-Host " Using original public key (may already be named curve)" -ForegroundColor Yellow
$CSR_PUBKEY_FIXED = $CSR_PUBKEY
$useFixedKey = $false
}
# Extract subject from original CSR
$csrSubject = & openssl req -in $CSR_FILE -noout -subject 2>&1
# Parse subject - it comes as "subject=CN = xxx" or "subject= /CN=xxx"
$csrSubjectCN = $csrSubject -replace "subject\s*=\s*", "" -replace "^\s*", ""
Write-Host " Original CSR subject: $csrSubjectCN" -ForegroundColor Gray
# Create OpenSSL extensions configuration file for ICA certificate
# IMPORTANT: This section tells OpenSSL what properties the ICA Certificate must have: extendedKeyUsage = clientAuth is REQUIRED for BYOR activation
Write-Host " Creating extensions configuration file..." -ForegroundColor Gray
$extensionsConfig = @"
[ ica_ext ]
basicConstraints = critical, CA:TRUE, pathlen:0
keyUsage = critical, keyCertSign, cRLSign, digitalSignature
extendedKeyUsage = clientAuth
subjectKeyIdentifier = hash
authorityKeyIdentifier = keyid:always, issuer:always
"@
# Use Set-Content which works more reliably in PowerShell 7
$extensionsConfig | Set-Content -Path $ICA_EXTENSIONS -Encoding UTF8 -NoNewline
if (-not (Test-Path $ICA_EXTENSIONS)) {
Write-Host " ERROR: Failed to create extensions config file" -ForegroundColor Red
Write-Host " Attempting alternative method..." -ForegroundColor Yellow
# Fallback: try writing line by line
"[ ica_ext ]" | Out-File -FilePath $ICA_EXTENSIONS -Encoding utf8
"basicConstraints = critical, CA:TRUE, pathlen:0" | Out-File -FilePath $ICA_EXTENSIONS -Encoding utf8 -Append
"keyUsage = critical, keyCertSign, cRLSign, digitalSignature" | Out-File -FilePath $ICA_EXTENSIONS -Encoding utf8 -Append
"extendedKeyUsage = clientAuth" | Out-File -FilePath $ICA_EXTENSIONS -Encoding utf8 -Append
"subjectKeyIdentifier = hash" | Out-File -FilePath $ICA_EXTENSIONS -Encoding utf8 -Append
"authorityKeyIdentifier = keyid:always, issuer:always" | Out-File -FilePath $ICA_EXTENSIONS -Encoding utf8 -Append
}
if (Test-Path $ICA_EXTENSIONS) {
Write-Host " SUCCESS: Extensions config created: $ICA_EXTENSIONS" -ForegroundColor Gray
} else {
Write-Host " ERROR: Failed to create extensions config file!" -ForegroundColor Red
exit 1
}
# Sign the CSR using -force_pubkey to use the fixed public key
# This replaces the public key in the CSR with our named-curve-encoded version
Write-Host "Signing CSR with fixed named curve encoding..." -ForegroundColor Green
& openssl x509 -req -in $CSR_FILE -CA $ROOT_CA_CERT -CAkey $ROOT_CA_KEY `
-CAcreateserial -out $ICA_CERT -days 1825 -sha384 `
-extfile $ICA_EXTENSIONS -extensions ica_ext `
-force_pubkey $CSR_PUBKEY_FIXED 2>$null
if (-not (Test-Path $ICA_CERT)) {
Write-Host " ERROR: Failed to create ICA certificate" -ForegroundColor Red
exit 1
}
# Verify the ICA certificate has named curve encoding
Write-Host " Verifying ICA certificate EC encoding..." -ForegroundColor Gray
$icaKeyInfo = & openssl x509 -in $ICA_CERT -noout -text 2>&1 | Select-String -Pattern "ASN1 OID:|NIST CURVE:|P-384|P-256|secp384r1|secp256r1"
if ($icaKeyInfo) {
Write-Host " SUCCESS: ICA certificate EC key: $($icaKeyInfo -join ', ')" -ForegroundColor Gray
}
Write-Host " SUCCESS: ICA certificate created with named curve encoding: $ICA_CERT" -ForegroundColor Gray
# Cleanup temporary files
if (Test-Path $CSR_PUBKEY) { Remove-Item $CSR_PUBKEY -Force -ErrorAction SilentlyContinue }
if ($useFixedKey -and (Test-Path $CSR_PUBKEY_FIXED)) { Remove-Item $CSR_PUBKEY_FIXED -Force -ErrorAction SilentlyContinue }
# Build certificate chain (ICA + Root CA)
Write-Host "Building certificate chain..." -ForegroundColor Green
$icaCert = Get-Content -Path $ICA_CERT -Raw
$rootCaCert = Get-Content -Path $ROOT_CA_CERT -Raw
# Ensure proper formatting with newlines
$certificateChain = $icaCert.Trim() + "`n" + $rootCaCert.Trim() + "`n"
[System.IO.File]::WriteAllText($CERT_CHAIN, $certificateChain)
Write-Host " SUCCESS: Certificate chain saved to: $CERT_CHAIN" -ForegroundColor Gray
スクリプトを実行する
ファイルが配置されているディレクトリで、次のコマンドを実行します。
.\selfsign.ps1
このスクリプトでは、 byor-certificate-chain.pem という名前の署名付き証明書チェーン ファイルが生成されます。 「 自分のCAを持ち込む 」の手順に従ってアクティベーションと設定を完了してください。