Display subjectAltName from a CSR in php

112 Views Asked by At

I got an issue on my end while displaying the CSR Subject Alternative Name using PHP. I run some research and found some interesting, but the problem is that, it was 10 years ago and no code for reference. Here is the link: How do you get the subjectAltName from a CSR in PHP?

The library is phpseclib. It has documentation related to CSR but don't know how to use such library. Do you have idea on how to use the library and display the Subject Alternative Name?. I'm not good at coding using libraries. Really appreciate any help. Been searching for 2 days.

1

There are 1 best solutions below

0
lothux1987 On

Thanks to @neubert for the answer that I needed, I also want to share this to anyone has the same issue as mine.

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