【问题标题】:Generating crx file via PHP fails with: "Package is invalid: CRX_SIGNATURE_VERIFICATION_INITIALIZATION_FAILED"通过 PHP 生成 crx 文件失败:“包无效:CRX_SIGNATURE_VERIFICATION_INITIALIZATION_FAILED”
【发布时间】:2013-05-23 20:55:32
【问题描述】:

我正在使用phpseclib 在 PHP 中生成一个 crx 文件。当我尝试将 crx 安装到 Chrome 中时,出现错误:

Package is invalid: 'CRX_SIGNATURE_VERIFICATION_INITIALIZATION_FAILED'

这是我的代码:

<?php
//Include phpseclib files
include('File/X509.php');
include('Crypt/RSA.php');

//RSA Handler
$rsa = new Crypt_RSA();

//Create key pair
$keyPair = $rsa->createKey();

//Get the keys
$privKey = $keyPair[ "privatekey" ];
$pubKey = $keyPair[ "publickey" ];

//The Zip file contents
$zipContents = file_get_contents( "helloworld.zip" );

//Load the private key into the handler
$rsa->loadKey( $privKey );

//Sign the content (default is SHA1)
$signature = $rsa->sign( $zipContents ) ;

/* Tried this, but it also did not work */
//Convert to openSSH and remove the leading/trailing "comments": "ssh-rsa ", " phpseclib-generated-key"
//$rsa->loadKey( $pubKey );
//$rsa->setPublicKey(); 
//$pubKey = $rsa->getPublicKey( CRYPT_RSA_PUBLIC_FORMAT_OPENSSH );
//$pubKey = substr( $pubKey, 8, strlen( $pubKey ) - 32 );

//Encode public key in Base64 and remove the "-----BEGIN PUBLIC KEY-----\r\n" and "\r\n-----END PUBLIC KEY-----" (to put in .crx)
$base64Key = base64_decode( substr( $pubKey, 28, strlen( $pubKey ) - 54 ) );

//Create the crx (wb = write in binary mode)
$crxFile = fopen( "helloworld.crx", "wb" );

//Add crx "magic" marker, format version
fwrite( $crxFile, "Cr24" );
fwrite( $crxFile, pack( "V", 2 ) );

//Write public key and signature length
fwrite( $crxFile, pack( "V", strlen( $base64Key ) ) );
fwrite( $crxFile, pack( "V", strlen( $signature ) ) );

//Write public key (base64 encoded) and signature
fwrite( $crxFile, $base64Key );
fwrite( $crxFile, $signature );

//Write the zip file contents
fwrite( $crxFile, $zipContents );

fclose( $crxFile );
?>

我做错了什么?我猜这与密钥的格式和签名有关?

【问题讨论】:

    标签: php google-chrome google-chrome-extension openssl public-key-encryption


    【解决方案1】:

    我找到了答案!签名使用CRYPT_RSA_SIGNATURE_PSS,显然只适用于PKCS1。所以你需要添加这一行:

    $rsa->setSignatureMode( CRYPT_RSA_SIGNATURE_PKCS1 );
    

    在签名创建代码之前。所以签名代码现在看起来像这样:

    //Load the private key into the handler
    $rsa->loadKey( $privKey );
    
    //Sign the content (default is SHA1)
    $rsa->setSignatureMode( CRYPT_RSA_SIGNATURE_PKCS1 ); /* <-- This is required */
    $signature = $rsa->sign( $zipContents ) ;
    

    【讨论】:

      猜你喜欢
      • 2012-12-03
      • 2017-01-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-26
      • 1970-01-01
      • 2018-04-03
      • 2022-07-22
      相关资源
      最近更新 更多