| Conditions | 3 |
| Paths | 2 |
| Total Lines | 26 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
| 1 | <?php |
||
| 44 | public static function pubkeyToPem(string $key) |
||
| 45 | { |
||
| 46 | if (strlen($key) !== PUBKEY_LEN || $key[0] !== "\x04") { |
||
| 47 | return null; |
||
| 48 | } |
||
| 49 | |||
| 50 | /* |
||
| 51 | * Convert the public key to binary DER format first |
||
| 52 | * Using the ECC SubjectPublicKeyInfo OIDs from RFC 5480 |
||
| 53 | * |
||
| 54 | * SEQUENCE(2 elem) 30 59 |
||
| 55 | * SEQUENCE(2 elem) 30 13 |
||
| 56 | * OID1.2.840.10045.2.1 (id-ecPublicKey) 06 07 2a 86 48 ce 3d 02 01 |
||
| 57 | * OID1.2.840.10045.3.1.7 (secp256r1) 06 08 2a 86 48 ce 3d 03 01 07 |
||
| 58 | * BIT STRING(520 bit) 03 42 ..key.. |
||
| 59 | */ |
||
| 60 | $der = "\x30\x59\x30\x13\x06\x07\x2a\x86\x48\xce\x3d\x02\x01"; |
||
| 61 | $der .= "\x06\x08\x2a\x86\x48\xce\x3d\x03\x01\x07\x03\x42"; |
||
| 62 | $der .= "\0" . $key; |
||
| 63 | |||
| 64 | $pem = "-----BEGIN PUBLIC KEY-----\r\n"; |
||
| 65 | $pem .= chunk_split(Encoding::base64Encode($der), 64); |
||
| 66 | $pem .= '-----END PUBLIC KEY-----'; |
||
| 67 | |||
| 68 | return $pem; |
||
| 69 | } |
||
| 70 | } |
||
| 71 |