XML.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. /**
  3. * XML Formatted DSA Key Handler
  4. *
  5. * While XKMS defines a private key format for RSA it does not do so for DSA. Quoting that standard:
  6. *
  7. * "[XKMS] does not specify private key parameters for the DSA signature algorithm since the algorithm only
  8. * supports signature modes and so the application of server generated keys and key recovery is of limited
  9. * value"
  10. *
  11. * PHP version 5
  12. *
  13. * @category Crypt
  14. * @package DSA
  15. * @author Jim Wigginton <terrafrost@php.net>
  16. * @copyright 2015 Jim Wigginton
  17. * @license http://www.opensource.org/licenses/mit-license.html MIT License
  18. * @link http://phpseclib.sourceforge.net
  19. */
  20. namespace phpseclib\Crypt\DSA\Keys;
  21. use ParagonIE\ConstantTime\Base64;
  22. use phpseclib\Math\BigInteger;
  23. /**
  24. * XML Formatted DSA Key Handler
  25. *
  26. * @package DSA
  27. * @author Jim Wigginton <terrafrost@php.net>
  28. * @access public
  29. */
  30. abstract class XML
  31. {
  32. /**
  33. * Break a public or private key down into its constituent components
  34. *
  35. * @access public
  36. * @param string $key
  37. * @param string $password optional
  38. * @return array
  39. */
  40. public static function load($key, $password = '')
  41. {
  42. if (!is_string($key)) {
  43. return false;
  44. }
  45. $use_errors = libxml_use_internal_errors(true);
  46. $dom = new \DOMDocument();
  47. if (!$dom->loadXML('<xml>' . $key . '</xml>')) {
  48. return false;
  49. }
  50. $xpath = new \DOMXPath($dom);
  51. $keys = ['p', 'q', 'g', 'y', 'j', 'seed', 'pgencounter'];
  52. foreach ($keys as $key) {
  53. // $dom->getElementsByTagName($key) is case-sensitive
  54. $temp = $xpath->query("//*[translate(local-name(), 'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklmnopqrstuvwxyz')='$key']");
  55. if (!$temp->length) {
  56. continue;
  57. }
  58. $value = new BigInteger(Base64::decode($temp->item(0)->nodeValue), 256);
  59. switch ($key) {
  60. case 'p': // a prime modulus meeting the [DSS] requirements
  61. // Parameters P, Q, and G can be public and common to a group of users. They might be known
  62. // from application context. As such, they are optional but P and Q must either both appear
  63. // or both be absent
  64. $components['p'] = $value;
  65. break;
  66. case 'q': // an integer in the range 2**159 < Q < 2**160 which is a prime divisor of P-1
  67. $components['q'] = $value;
  68. break;
  69. case 'g': // an integer with certain properties with respect to P and Q
  70. $components['g'] = $value;
  71. break;
  72. case 'y': // G**X mod P (where X is part of the private key and not made public)
  73. $components['y'] = $value;
  74. // the remaining options do not do anything
  75. case 'j': // (P - 1) / Q
  76. // Parameter J is available for inclusion solely for efficiency as it is calculatable from
  77. // P and Q
  78. case 'seed': // a DSA prime generation seed
  79. // Parameters seed and pgenCounter are used in the DSA prime number generation algorithm
  80. // specified in [DSS]. As such, they are optional but must either both be present or both
  81. // be absent
  82. case 'pgencounter': // a DSA prime generation counter
  83. }
  84. }
  85. libxml_use_internal_errors($use_errors);
  86. if (!isset($components['y'])) {
  87. return false;
  88. }
  89. switch (true) {
  90. case !isset($components['p']):
  91. case !isset($components['q']):
  92. case !isset($components['g']):
  93. return ['y' => $components['y']];
  94. }
  95. return $components;
  96. }
  97. /**
  98. * Convert a public key to the appropriate format
  99. *
  100. * See https://www.w3.org/TR/xmldsig-core/#sec-DSAKeyValue
  101. *
  102. * @access public
  103. * @param \phpseclib\Math\BigInteger $p
  104. * @param \phpseclib\Math\BigInteger $q
  105. * @param \phpseclib\Math\BigInteger $g
  106. * @param \phpseclib\Math\BigInteger $y
  107. * @return string
  108. */
  109. public static function savePublicKey($p, $q, $g, $y)
  110. {
  111. return "<DSAKeyValue>\r\n" .
  112. ' <P>' . Base64::encode($p->toBytes()) . "</P>\r\n" .
  113. ' <Q>' . Base64::encode($q->toBytes()) . "</Q>\r\n" .
  114. ' <G>' . Base64::encode($g->toBytes()) . "</G>\r\n" .
  115. ' <Y>' . Base64::encode($y->toBytes()) . "</Y>\r\n" .
  116. '</DSAKeyValue>';
  117. }
  118. }