github.com/zmap/zcrypto@v0.0.0-20240512203510-0fef58d9a9db/json/ecdhe.go (about)

     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  
    15  package json
    16  
    17  import (
    18  	"crypto/elliptic"
    19  	"encoding/json"
    20  	"math/big"
    21  )
    22  
    23  // TLSCurveID is the type of a TLS identifier for an elliptic curve. See
    24  // http://www.iana.org/assignments/tls-parameters/tls-parameters.xml#tls-parameters-8
    25  type TLSCurveID uint16
    26  
    27  // ECDHPrivateParams are the TLS key exchange parameters for ECDH keys.
    28  type ECDHPrivateParams struct {
    29  	Value  []byte `json:"value,omitempty"`
    30  	Length int    `json:"length,omitempty"`
    31  }
    32  
    33  // ECDHParams stores elliptic-curve Diffie-Hellman paramters.At any point in
    34  // time, it is unlikely that both ServerPrivate and ClientPrivate will be non-nil.
    35  type ECDHParams struct {
    36  	TLSCurveID    TLSCurveID         `json:"curve_id,omitempty"`
    37  	Curve         elliptic.Curve     `json:"-"`
    38  	ServerPublic  *ECPoint           `json:"server_public,omitempty"`
    39  	ServerPrivate *ECDHPrivateParams `json:"server_private,omitempty"`
    40  	ClientPublic  *ECPoint           `json:"client_public,omitempty"`
    41  	ClientPrivate *ECDHPrivateParams `json:"client_private,omitempty"`
    42  }
    43  
    44  // ECPoint represents an elliptic curve point and serializes nicely to JSON
    45  type ECPoint struct {
    46  	X *big.Int
    47  	Y *big.Int
    48  }
    49  
    50  // MarshalJSON implements the json.Marshler interface
    51  func (p *ECPoint) MarshalJSON() ([]byte, error) {
    52  	aux := struct {
    53  		X *cryptoParameter `json:"x"`
    54  		Y *cryptoParameter `json:"y"`
    55  	}{
    56  		X: &cryptoParameter{Int: p.X},
    57  		Y: &cryptoParameter{Int: p.Y},
    58  	}
    59  	return json.Marshal(&aux)
    60  }
    61  
    62  // UnmarshalJSON implements the json.Unmarshler interface
    63  func (p *ECPoint) UnmarshalJSON(b []byte) error {
    64  	aux := struct {
    65  		X *cryptoParameter `json:"x"`
    66  		Y *cryptoParameter `json:"y"`
    67  	}{}
    68  	if err := json.Unmarshal(b, &aux); err != nil {
    69  		return err
    70  	}
    71  	p.X = aux.X.Int
    72  	p.Y = aux.Y.Int
    73  	return nil
    74  }
    75  
    76  // Description returns the description field for the given ID. See
    77  // http://www.iana.org/assignments/tls-parameters/tls-parameters.xml#tls-parameters-8
    78  func (c *TLSCurveID) Description() string {
    79  	if desc, ok := ecIDToName[*c]; ok {
    80  		return desc
    81  	}
    82  	return "unknown"
    83  }
    84  
    85  // MarshalJSON implements the json.Marshaler interface
    86  func (c *TLSCurveID) MarshalJSON() ([]byte, error) {
    87  	aux := struct {
    88  		Name string `json:"name"`
    89  		ID   uint16 `json:"id"`
    90  	}{
    91  		Name: c.Description(),
    92  		ID:   uint16(*c),
    93  	}
    94  	return json.Marshal(&aux)
    95  }
    96  
    97  // UnmarshalJSON implements the json.Unmarshaler interface
    98  func (c *TLSCurveID) UnmarshalJSON(b []byte) error {
    99  	aux := struct {
   100  		ID uint16 `json:"id"`
   101  	}{}
   102  	if err := json.Unmarshal(b, &aux); err != nil {
   103  		return err
   104  	}
   105  	*c = TLSCurveID(aux.ID)
   106  	return nil
   107  }