|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Tsugi\Util; |
| 4 | + |
| 5 | +use Mmccook\JsonCanonicalizator\JsonCanonicalizatorFactory; |
| 6 | + |
| 7 | +/** |
| 8 | + * Data Integrity Proof for Open Badges 3.0 (eddsa-jcs-2022 cryptosuite) |
| 9 | + * |
| 10 | + * Implements W3C VC Data Integrity EdDSA Cryptosuites - eddsa-jcs-2022. |
| 11 | + * Note: The 1EdTech OB3 verifier at vc.1ed.tech only accepts eddsa-rdfc-2022 for |
| 12 | + * certification. This implementation uses eddsa-jcs-2022 (also in the W3C spec). |
| 13 | + * For 1EdTech certification, consider filing a request to support eddsa-jcs-2022. |
| 14 | + * @see https://www.w3.org/TR/vc-di-eddsa/ |
| 15 | + */ |
| 16 | +class Ob3DataIntegrity { |
| 17 | + |
| 18 | + /** @var string eddsa-jcs-2022 cryptosuite */ |
| 19 | + const CRYPTOSUITE = 'eddsa-jcs-2022'; |
| 20 | + |
| 21 | + /** |
| 22 | + * Create a DataIntegrityProof for an OB3 credential (eddsa-jcs-2022) |
| 23 | + * |
| 24 | + * @param array $credential The credential document (without proof) |
| 25 | + * @param string $verificationMethodId URL of the verification method (e.g. issuer#key-0) |
| 26 | + * @param string $secretKey Ed25519 secret key bytes (64 bytes: 32 seed + 32 public) |
| 27 | + * @return array|null Proof object or null on failure |
| 28 | + */ |
| 29 | + public static function createProof(array $credential, $verificationMethodId, $secretKey) { |
| 30 | + if (strlen($secretKey) !== SODIUM_CRYPTO_SIGN_SECRETKEYBYTES) { |
| 31 | + error_log('Ob3DataIntegrity: Invalid secret key length, expected ' . SODIUM_CRYPTO_SIGN_SECRETKEYBYTES); |
| 32 | + return null; |
| 33 | + } |
| 34 | + |
| 35 | + $created = gmdate('Y-m-d\TH:i:s\Z'); |
| 36 | + $proofOptions = array( |
| 37 | + 'type' => 'DataIntegrityProof', |
| 38 | + 'cryptosuite' => self::CRYPTOSUITE, |
| 39 | + 'created' => $created, |
| 40 | + 'verificationMethod' => $verificationMethodId, |
| 41 | + 'proofPurpose' => 'assertionMethod' |
| 42 | + ); |
| 43 | + if (isset($credential['@context'])) { |
| 44 | + $proofOptions['@context'] = $credential['@context']; |
| 45 | + } |
| 46 | + |
| 47 | + // Proof config for hashing (clone without proofValue) |
| 48 | + $proofConfig = $proofOptions; |
| 49 | + |
| 50 | + $canonicalizator = JsonCanonicalizatorFactory::getInstance(); |
| 51 | + |
| 52 | + // 1. Transformed data = JCS(canonicalize credential) |
| 53 | + $transformedData = $canonicalizator->canonicalize($credential); |
| 54 | + |
| 55 | + // 2. Canonical proof config = JCS(proofConfig) |
| 56 | + $canonicalProofConfig = $canonicalizator->canonicalize($proofConfig); |
| 57 | + |
| 58 | + // 3. hashData = SHA256(proofConfig) + SHA256(transformedData) |
| 59 | + $proofConfigHash = hash('sha256', $canonicalProofConfig, true); |
| 60 | + $transformedDocHash = hash('sha256', $transformedData, true); |
| 61 | + $hashData = $proofConfigHash . $transformedDocHash; |
| 62 | + |
| 63 | + // 4. Sign hashData with Ed25519 |
| 64 | + $proofBytes = sodium_crypto_sign_detached($hashData, $secretKey); |
| 65 | + |
| 66 | + // 5. proofValue = base58btc(proofBytes) |
| 67 | + $proofValue = self::base58Encode($proofBytes); |
| 68 | + |
| 69 | + $proofOptions['proofValue'] = 'z' . $proofValue; |
| 70 | + return $proofOptions; |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Extract public key from libsodium secret key (last 32 bytes) |
| 75 | + * |
| 76 | + * @param string $secretKey 64-byte Ed25519 secret key from sodium |
| 77 | + * @return string 32-byte public key |
| 78 | + */ |
| 79 | + public static function secretKeyToPublicKey($secretKey) { |
| 80 | + if (strlen($secretKey) !== SODIUM_CRYPTO_SIGN_SECRETKEYBYTES) { |
| 81 | + return ''; |
| 82 | + } |
| 83 | + return substr($secretKey, 32, 32); |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Encode Ed25519 public key as Multikey (base58btc with 0xed01 prefix) |
| 88 | + * Multikey format: 0xed01 (2 bytes) + public key (32 bytes) = 34 bytes |
| 89 | + * |
| 90 | + * @param string $publicKey 32-byte Ed25519 public key |
| 91 | + * @return string Multibase multikey string (z prefix + base58) |
| 92 | + */ |
| 93 | + public static function publicKeyToMultibase($publicKey) { |
| 94 | + if (strlen($publicKey) !== SODIUM_CRYPTO_SIGN_PUBLICKEYBYTES) { |
| 95 | + return ''; |
| 96 | + } |
| 97 | + $multikeyBytes = "\xed\x01" . $publicKey; |
| 98 | + return 'z' . self::base58Encode($multikeyBytes); |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * Base58 (Bitcoin alphabet) encode - for multibase "z" prefix |
| 103 | + * Alphabet: 123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz |
| 104 | + * Uses GMP or BCMath extension (at least one required for large byte strings). |
| 105 | + */ |
| 106 | + private static function base58Encode($input) { |
| 107 | + $alphabet = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'; |
| 108 | + $hex = bin2hex($input); |
| 109 | + $result = ''; |
| 110 | + |
| 111 | + if (function_exists('gmp_init')) { |
| 112 | + $num = gmp_init($hex, 16); |
| 113 | + $zero = gmp_init(0); |
| 114 | + while (gmp_cmp($num, $zero) > 0) { |
| 115 | + list($num, $remainder) = gmp_div_qr($num, 58); |
| 116 | + $result = $alphabet[(int) gmp_strval($remainder)] . $result; |
| 117 | + } |
| 118 | + } elseif (function_exists('bcadd')) { |
| 119 | + $num = '0'; |
| 120 | + for ($i = 0; $i < strlen($hex); $i++) { |
| 121 | + $num = bcadd(bcmul($num, '16', 0), (string) hexdec($hex[$i]), 0); |
| 122 | + } |
| 123 | + while (bccomp($num, '0', 0) > 0) { |
| 124 | + $remainder = (int) bcmod($num, '58'); |
| 125 | + $num = bcdiv(bcsub($num, (string) $remainder, 0), '58', 0); |
| 126 | + $result = $alphabet[$remainder] . $result; |
| 127 | + } |
| 128 | + } else { |
| 129 | + throw new \RuntimeException( |
| 130 | + 'OB3 base58 encoding requires PHP gmp or bcmath extension. ' . |
| 131 | + 'Install one: apt-get install php-gmp or apt-get install php-bcmath' |
| 132 | + ); |
| 133 | + } |
| 134 | + |
| 135 | + // Leading zeros become '1's in base58 |
| 136 | + for ($i = 0; $i < strlen($input) && $input[$i] === "\x00"; $i++) { |
| 137 | + $result = $alphabet[0] . $result; |
| 138 | + } |
| 139 | + return $result; |
| 140 | + } |
| 141 | + |
| 142 | + /** |
| 143 | + * Generate a new Ed25519 key pair for OB3 signing |
| 144 | + * Returns [ 'secretKey' => string (64 bytes), 'publicKey' => string (32 bytes) ] |
| 145 | + * |
| 146 | + * @return array Key pair |
| 147 | + */ |
| 148 | + public static function generateKeyPair() { |
| 149 | + $keypair = sodium_crypto_sign_keypair(); |
| 150 | + return array( |
| 151 | + 'secretKey' => sodium_crypto_sign_secretkey($keypair), |
| 152 | + 'publicKey' => sodium_crypto_sign_publickey($keypair) |
| 153 | + ); |
| 154 | + } |
| 155 | +} |
0 commit comments