File: ecdhe.go

package info (click to toggle)
golang-github-zmap-zcrypto 0.0~git20240512.0fef58d-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 6,856 kB
  • sloc: python: 567; sh: 124; makefile: 9
file content (107 lines) | stat: -rw-r--r-- 3,144 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*
 * ZGrab Copyright 2015 Regents of the University of Michigan
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy
 * of the License at http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied. See the License for the specific language governing
 * permissions and limitations under the License.
 */

package json

import (
	"crypto/elliptic"
	"encoding/json"
	"math/big"
)

// TLSCurveID is the type of a TLS identifier for an elliptic curve. See
// http://www.iana.org/assignments/tls-parameters/tls-parameters.xml#tls-parameters-8
type TLSCurveID uint16

// ECDHPrivateParams are the TLS key exchange parameters for ECDH keys.
type ECDHPrivateParams struct {
	Value  []byte `json:"value,omitempty"`
	Length int    `json:"length,omitempty"`
}

// ECDHParams stores elliptic-curve Diffie-Hellman paramters.At any point in
// time, it is unlikely that both ServerPrivate and ClientPrivate will be non-nil.
type ECDHParams struct {
	TLSCurveID    TLSCurveID         `json:"curve_id,omitempty"`
	Curve         elliptic.Curve     `json:"-"`
	ServerPublic  *ECPoint           `json:"server_public,omitempty"`
	ServerPrivate *ECDHPrivateParams `json:"server_private,omitempty"`
	ClientPublic  *ECPoint           `json:"client_public,omitempty"`
	ClientPrivate *ECDHPrivateParams `json:"client_private,omitempty"`
}

// ECPoint represents an elliptic curve point and serializes nicely to JSON
type ECPoint struct {
	X *big.Int
	Y *big.Int
}

// MarshalJSON implements the json.Marshler interface
func (p *ECPoint) MarshalJSON() ([]byte, error) {
	aux := struct {
		X *cryptoParameter `json:"x"`
		Y *cryptoParameter `json:"y"`
	}{
		X: &cryptoParameter{Int: p.X},
		Y: &cryptoParameter{Int: p.Y},
	}
	return json.Marshal(&aux)
}

// UnmarshalJSON implements the json.Unmarshler interface
func (p *ECPoint) UnmarshalJSON(b []byte) error {
	aux := struct {
		X *cryptoParameter `json:"x"`
		Y *cryptoParameter `json:"y"`
	}{}
	if err := json.Unmarshal(b, &aux); err != nil {
		return err
	}
	p.X = aux.X.Int
	p.Y = aux.Y.Int
	return nil
}

// Description returns the description field for the given ID. See
// http://www.iana.org/assignments/tls-parameters/tls-parameters.xml#tls-parameters-8
func (c *TLSCurveID) Description() string {
	if desc, ok := ecIDToName[*c]; ok {
		return desc
	}
	return "unknown"
}

// MarshalJSON implements the json.Marshaler interface
func (c *TLSCurveID) MarshalJSON() ([]byte, error) {
	aux := struct {
		Name string `json:"name"`
		ID   uint16 `json:"id"`
	}{
		Name: c.Description(),
		ID:   uint16(*c),
	}
	return json.Marshal(&aux)
}

// UnmarshalJSON implements the json.Unmarshaler interface
func (c *TLSCurveID) UnmarshalJSON(b []byte) error {
	aux := struct {
		ID uint16 `json:"id"`
	}{}
	if err := json.Unmarshal(b, &aux); err != nil {
		return err
	}
	*c = TLSCurveID(aux.ID)
	return nil
}