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