ecdhe.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * ZGrab Copyright 2015 Regents of the University of Michigan
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy
  6. * of the License at http://www.apache.org/licenses/LICENSE-2.0
  7. *
  8. * Unless required by applicable law or agreed to in writing, software
  9. * distributed under the License is distributed on an "AS IS" BASIS,
  10. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
  11. * implied. See the License for the specific language governing
  12. * permissions and limitations under the License.
  13. */
  14. package json
  15. import (
  16. "crypto/elliptic"
  17. "encoding/json"
  18. "math/big"
  19. )
  20. // TLSCurveID is the type of a TLS identifier for an elliptic curve. See
  21. // http://www.iana.org/assignments/tls-parameters/tls-parameters.xml#tls-parameters-8
  22. type TLSCurveID uint16
  23. // ECDHPrivateParams are the TLS key exchange parameters for ECDH keys.
  24. type ECDHPrivateParams struct {
  25. Value []byte `json:"value,omitempty"`
  26. Length int `json:"length,omitempty"`
  27. }
  28. // ECDHParams stores elliptic-curve Diffie-Hellman paramters.At any point in
  29. // time, it is unlikely that both ServerPrivate and ClientPrivate will be non-nil.
  30. type ECDHParams struct {
  31. TLSCurveID TLSCurveID `json:"curve_id,omitempty"`
  32. Curve elliptic.Curve `json:"-"`
  33. ServerPublic *ECPoint `json:"server_public,omitempty"`
  34. ServerPrivate *ECDHPrivateParams `json:"server_private,omitempty"`
  35. ClientPublic *ECPoint `json:"client_public,omitempty"`
  36. ClientPrivate *ECDHPrivateParams `json:"client_private,omitempty"`
  37. }
  38. // ECPoint represents an elliptic curve point and serializes nicely to JSON
  39. type ECPoint struct {
  40. X *big.Int
  41. Y *big.Int
  42. }
  43. // MarshalJSON implements the json.Marshler interface
  44. func (p *ECPoint) MarshalJSON() ([]byte, error) {
  45. aux := struct {
  46. X *cryptoParameter `json:"x"`
  47. Y *cryptoParameter `json:"y"`
  48. }{
  49. X: &cryptoParameter{Int: p.X},
  50. Y: &cryptoParameter{Int: p.Y},
  51. }
  52. return json.Marshal(&aux)
  53. }
  54. // UnmarshalJSON implements the json.Unmarshler interface
  55. func (p *ECPoint) UnmarshalJSON(b []byte) error {
  56. aux := struct {
  57. X *cryptoParameter `json:"x"`
  58. Y *cryptoParameter `json:"y"`
  59. }{}
  60. if err := json.Unmarshal(b, &aux); err != nil {
  61. return err
  62. }
  63. p.X = aux.X.Int
  64. p.Y = aux.Y.Int
  65. return nil
  66. }
  67. // Description returns the description field for the given ID. See
  68. // http://www.iana.org/assignments/tls-parameters/tls-parameters.xml#tls-parameters-8
  69. func (c *TLSCurveID) Description() string {
  70. if desc, ok := ecIDToName[*c]; ok {
  71. return desc
  72. }
  73. return "unknown"
  74. }
  75. // MarshalJSON implements the json.Marshaler interface
  76. func (c *TLSCurveID) MarshalJSON() ([]byte, error) {
  77. aux := struct {
  78. Name string `json:"name"`
  79. ID uint16 `json:"id"`
  80. }{
  81. Name: c.Description(),
  82. ID: uint16(*c),
  83. }
  84. return json.Marshal(&aux)
  85. }
  86. //UnmarshalJSON implements the json.Unmarshaler interface
  87. func (c *TLSCurveID) UnmarshalJSON(b []byte) error {
  88. aux := struct {
  89. ID uint16 `json:"id"`
  90. }{}
  91. if err := json.Unmarshal(b, &aux); err != nil {
  92. return err
  93. }
  94. *c = TLSCurveID(aux.ID)
  95. return nil
  96. }