160 lines
4.3 KiB
JavaScript
160 lines
4.3 KiB
JavaScript
// 测试证书模块 - 使用 CommonJS 模块系统
|
||
const forge = require('node-forge');
|
||
|
||
// 直接测试证书生成功能
|
||
console.log('=== 测试证书模块功能 ===');
|
||
|
||
// 生成随机的序列号
|
||
const generateSerialNumber = () => {
|
||
return Math.floor(Math.random() * Number.MAX_SAFE_INTEGER).toString();
|
||
};
|
||
|
||
// 测试生成根证书
|
||
console.log('\n1. 测试生成根证书');
|
||
try {
|
||
// 生成密钥对
|
||
const keys = forge.pki.rsa.generateKeyPair(2048);
|
||
|
||
// 创建证书
|
||
const cert = forge.pki.createCertificate();
|
||
|
||
// 设置版本
|
||
cert.version = 3;
|
||
|
||
// 设置序列号
|
||
cert.serialNumber = generateSerialNumber();
|
||
|
||
// 设置主题和颁发者(根证书自己颁发自己)
|
||
const attrs = [
|
||
{ name: 'commonName', value: 'Root CA' },
|
||
{ name: 'countryName', value: 'CN' },
|
||
{ shortName: 'ST', value: 'Beijing' },
|
||
{ name: 'localityName', value: 'Beijing' },
|
||
{ name: 'organizationName', value: 'Security Kit' },
|
||
{ shortName: 'OU', value: 'Root Certificate Authority' }
|
||
];
|
||
|
||
cert.setSubject(attrs);
|
||
cert.setIssuer(attrs);
|
||
|
||
// 设置公钥
|
||
cert.publicKey = keys.publicKey;
|
||
|
||
// 设置有效期
|
||
const notBefore = new Date();
|
||
const notAfter = new Date();
|
||
notAfter.setFullYear(notBefore.getFullYear() + 10); // 10年有效期
|
||
cert.validity.notBefore = notBefore;
|
||
cert.validity.notAfter = notAfter;
|
||
|
||
// 添加扩展
|
||
cert.setExtensions([
|
||
{
|
||
name: 'basicConstraints',
|
||
cA: true
|
||
},
|
||
{
|
||
name: 'keyUsage',
|
||
digitalSignature: true,
|
||
keyCertSign: true,
|
||
cRLSign: true
|
||
}
|
||
]);
|
||
|
||
// 使用私钥自签名
|
||
cert.sign(keys.privateKey, forge.md.sha256.create());
|
||
|
||
// 转换为PEM格式
|
||
const certPem = forge.pki.certificateToPem(cert);
|
||
|
||
console.log('✓ 根证书生成成功!');
|
||
console.log('根证书内容(前200字符):', certPem.substring(0, 200) + '...');
|
||
|
||
// 测试生成应用证书
|
||
console.log('\n2. 测试生成应用证书');
|
||
|
||
// 生成应用证书的密钥对
|
||
const appKeys = forge.pki.rsa.generateKeyPair(2048);
|
||
|
||
// 创建应用证书
|
||
const appCert = forge.pki.createCertificate();
|
||
|
||
// 设置版本
|
||
appCert.version = 3;
|
||
|
||
// 设置序列号
|
||
appCert.serialNumber = generateSerialNumber();
|
||
|
||
// 设置主题
|
||
const appAttrs = [
|
||
{ name: 'commonName', value: 'test.example.com' },
|
||
{ name: 'countryName', value: 'CN' },
|
||
{ shortName: 'ST', value: 'Beijing' },
|
||
{ name: 'localityName', value: 'Beijing' },
|
||
{ name: 'organizationName', value: 'Security Kit' },
|
||
{ shortName: 'OU', value: 'Application' }
|
||
];
|
||
|
||
appCert.setSubject(appAttrs);
|
||
|
||
// 设置颁发者(根证书)
|
||
appCert.setIssuer(cert.subject.attributes);
|
||
|
||
// 设置公钥
|
||
appCert.publicKey = appKeys.publicKey;
|
||
|
||
// 设置有效期
|
||
const appNotBefore = new Date();
|
||
const appNotAfter = new Date();
|
||
appNotAfter.setFullYear(appNotBefore.getFullYear() + 5); // 5年有效期
|
||
appCert.validity.notBefore = appNotBefore;
|
||
appCert.validity.notAfter = appNotAfter;
|
||
|
||
// 添加扩展
|
||
appCert.setExtensions([
|
||
{
|
||
name: 'basicConstraints',
|
||
cA: false
|
||
},
|
||
{
|
||
name: 'keyUsage',
|
||
digitalSignature: true,
|
||
keyEncipherment: true
|
||
},
|
||
{
|
||
name: 'extendedKeyUsage',
|
||
serverAuth: true,
|
||
clientAuth: true
|
||
}
|
||
]);
|
||
|
||
// 使用根证书的私钥签名
|
||
appCert.sign(keys.privateKey, forge.md.sha256.create());
|
||
|
||
// 转换为PEM格式
|
||
const appCertPem = forge.pki.certificateToPem(appCert);
|
||
|
||
console.log('✓ 应用证书生成成功!');
|
||
console.log('应用证书内容(前200字符):', appCertPem.substring(0, 200) + '...');
|
||
|
||
// 测试证书解析
|
||
console.log('\n3. 测试证书解析');
|
||
|
||
// 解析根证书
|
||
const parsedRootCert = forge.pki.certificateFromPem(certPem);
|
||
console.log('✓ 根证书解析成功!');
|
||
console.log('根证书主题:', parsedRootCert.subject.getField('CN').value);
|
||
|
||
// 解析应用证书
|
||
const parsedAppCert = forge.pki.certificateFromPem(appCertPem);
|
||
console.log('✓ 应用证书解析成功!');
|
||
console.log('应用证书主题:', parsedAppCert.subject.getField('CN').value);
|
||
console.log('应用证书颁发者:', parsedAppCert.issuer.getField('CN').value);
|
||
|
||
console.log('\n=== 所有测试通过!===');
|
||
|
||
} catch (error) {
|
||
console.error('测试失败:', error.message);
|
||
console.error(error.stack);
|
||
}
|