Home  >  Q&A  >  body text

Display subjectAltName from CSR in php

<p>I'm having trouble displaying the CSR subject alternative name using PHP. I did some research and found some interesting stuff, but the problem is, that was 10 years ago and there was no code to reference. The link is as follows: How do you get the subjectAltName from a CSR in PHP?</p> <p>The library is phpseclib. It has documentation related to CSR, but don't know how to use such a library. Do you know how to use this library and display theme alternative names? I'm not good at coding with libraries. Any help is greatly appreciated. Been looking for it for two days. </p>
P粉115840076P粉115840076433 days ago457

reply all(1)I'll reply

  • P粉351138462

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

    Thanks to @neubert for providing the answer I needed, and I wanted to share this with anyone who has the same question as me.

    <?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>

    reply
    0
  • Cancelreply