首頁  >  問答  >  主體

在 php 中顯示來自 CSR 的 subjectAltName

<p>我在使用 PHP 顯示 CSR 主題備用名稱時遇到了問題。我做了一些研究,發現了一些有趣的東西,但問題是,那是 10 年前的事了,沒有可供參考的程式碼。連結如下:How do you get the subjectAltName from a CSR in PHP?</p> <p>該函式庫是 phpseclib。它有與 CSR 相關的文檔,但不知道如何使用這樣的庫。您知道如何使用該庫並顯示主題備用名稱嗎?我不擅長使用庫進行編碼。非常感謝任何幫助。找了兩天了。 </p>
P粉115840076P粉115840076383 天前395

全部回覆(1)我來回復

  • P粉351138462

    P粉3511384622023-09-06 10:11:11

    感謝 @neubert 提供我需要的答案,我也想將此分享給任何與我有相同問題的人。

    <?php
    require __DIR__ . '/vendor/autoload.php';
    
    use phpseclib3\File\X509;
    ?>
    
    <!DOCTYPE html>
    <html>
    <head>
        <title>CSR Decode</title>
        <style type="text/css">
            body{
                background-color: #f1f1f1;
                font-family: Arial, sans-serif;
                color: #333333;
            }
            table.section{width:100%;}
            table.ftable td{padding:0px;text-align:center;font-size:13px;}
            h3{
                color: #2e6da4;
                padding: 10px;
                font-size: 20px;
                text-align: center;
                margin-top: 0px;
                margin-bottom: 15px;
                border-radius: 10px;
            }
            table{
                width: 50%;
                margin: auto;
                border-collapse: collapse;
                border: 1px solid #d4d4d4;
                text-align: left;
            }
            th, td{
                padding: 8px;
                border: 1px solid #d4d4d4;
                font-size:15px;
            }
            tr:nth-child(even){
                background-color: #f2f2f2;
            }
            img{
                 width: 50%;
                margin: auto;
                margin-bottom: 10px;
            }
    
            table.table td{border:none;}
                    table.table{border:none;}
        input[type="text"] {width: 30%;padding: 5px 20px;border:1px solid white;}
            input[type="submit"]{
                width: 16%;
                padding: 5px 20px;
                background-color:#67BC5A;
                color:white;
                font-weight:bold;
                border:1px solid #67BC5A;
                border-radius:5px;}
        form.form {padding-bottom: 20px;}
    
    
        p {
            display: flex;
        justify-content: center;
            font-weight: bold;
            font-size: 24px;
        }
    
        .csr_info {
            display: flex;
        justify-content: center;
        }
    
        .csr_info > ul{
            list-style: none;
        }
    
        .csr_info > ul > li{
            background: url(./img/check.png) no-repeat left;
            font-size: 14px;
        height: 24px;
        padding: 20px 0 0px 58px;
        margin: auto;
        font-weight: normal;
        line-height: 4px;
        }
    
        </style>
    </head>
    <body>
    
    <table class="ftable" style="border:none;">
        <tr>
        <td  style="border:none;">
        <h3>Decode CSR</h3>
        <form class="form"action="" method="POST">
            <div>
                <textarea name="csr" rows="22" cols="99"></textarea>
            </div>
            <div>
                <input type="submit" name="submit"  value="Decode CSR">
            </div>
        </form>
        </tr>
        </td>
        </table>
    <?php
    
    if(isset($_POST['submit'])){
    
      $csr = $_POST['csr'];
      $x509 = new X509();
      $csr2 = $x509->loadCSR($csr);
    ?>
    <p>CSR Information</p>
    <div class="csr_info">
        <ul>
            <li>
                <strong>Common Name</strong>:
          <?php
            $result = array_values($x509->getDNProp('CN'));
            echo $result[0];
          ?>
            </li>
        <li>
                <strong>Subject Alternative Name:</strong>:
          <?php
            $ext = array_values($x509->getExtension('id-ce-subjectAltName'));
            foreach ($ext as $value) {
              $san = array_values($value);
              echo " , " .$san[0];
            }
          ?>
            </li>
        <li>
                <strong>Organization</strong>:
          <?php
            $result = array_values($x509->getDNProp('O'));
            echo $result[0];
          ?>
            </li>
            <li>
                <strong>Organization Unit</strong>:
          <?php
            $result = array_values($x509->getDNProp('OU'));
            echo $result[0];
          ?>
            </li>
            <li>
                <strong>Locality</strong>:
          <?php
            $result = array_values($x509->getDNProp('L'));
            echo $result[0];
          ?>
            </li>
            <li>
                <strong>State</strong>:
          <?php
            $result = array_values($x509->getDNProp('ST'));
            echo $result[0];
          ?>
            </li>
            <li>
                <strong>Country</strong>:
          <?php
            $result = array_values($x509->getDNProp('C'));
            echo $result[0];
          ?>
            </li>
    
        </ul>
        <?php
        }
    
        ?>
    </div>
    
    </body>
    </html>

    回覆
    0
  • 取消回覆