github.com/zmap/zcrypto@v0.0.0-20240512203510-0fef58d9a9db/json/dhe.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 "encoding/json" 19 "math/big" 20 ) 21 22 // DHParams can be used to store finite-field Diffie-Hellman parameters. At any 23 // point in time, it is unlikely that both OurPrivate and TheirPrivate will be 24 // non-nil. 25 type DHParams struct { 26 Prime *big.Int 27 Generator *big.Int 28 ServerPublic *big.Int 29 ServerPrivate *big.Int 30 ClientPublic *big.Int 31 ClientPrivate *big.Int 32 SessionKey *big.Int 33 } 34 35 type auxDHParams struct { 36 Prime *cryptoParameter `json:"prime"` 37 Generator *cryptoParameter `json:"generator"` 38 ServerPublic *cryptoParameter `json:"server_public,omitempty"` 39 ServerPrivate *cryptoParameter `json:"server_private,omitempty"` 40 ClientPublic *cryptoParameter `json:"client_public,omitempty"` 41 ClientPrivate *cryptoParameter `json:"client_private,omitempty"` 42 SessionKey *cryptoParameter `json:"session_key,omitempty"` 43 } 44 45 // MarshalJSON implements the json.Marshal interface 46 func (p *DHParams) MarshalJSON() ([]byte, error) { 47 aux := auxDHParams{ 48 Prime: &cryptoParameter{Int: p.Prime}, 49 Generator: &cryptoParameter{Int: p.Generator}, 50 } 51 if p.ServerPublic != nil { 52 aux.ServerPublic = &cryptoParameter{Int: p.ServerPublic} 53 } 54 if p.ServerPrivate != nil { 55 aux.ServerPrivate = &cryptoParameter{Int: p.ServerPrivate} 56 } 57 if p.ClientPublic != nil { 58 aux.ClientPublic = &cryptoParameter{Int: p.ClientPublic} 59 } 60 if p.ClientPrivate != nil { 61 aux.ClientPrivate = &cryptoParameter{Int: p.ClientPrivate} 62 } 63 if p.SessionKey != nil { 64 aux.SessionKey = &cryptoParameter{Int: p.SessionKey} 65 } 66 return json.Marshal(aux) 67 } 68 69 // UnmarshalJSON implement the json.Unmarshaler interface 70 func (p *DHParams) UnmarshalJSON(b []byte) error { 71 var aux auxDHParams 72 if err := json.Unmarshal(b, &aux); err != nil { 73 return err 74 } 75 if aux.Prime != nil { 76 p.Prime = aux.Prime.Int 77 } 78 if aux.Generator != nil { 79 p.Generator = aux.Generator.Int 80 } 81 if aux.ServerPublic != nil { 82 p.ServerPublic = aux.ServerPublic.Int 83 } 84 if aux.ServerPrivate != nil { 85 p.ServerPrivate = aux.ServerPrivate.Int 86 } 87 if aux.ClientPublic != nil { 88 p.ClientPublic = aux.ClientPublic.Int 89 } 90 if aux.ClientPrivate != nil { 91 p.ClientPrivate = aux.ClientPrivate.Int 92 } 93 if aux.SessionKey != nil { 94 p.SessionKey = aux.SessionKey.Int 95 } 96 return nil 97 } 98 99 // CryptoParameter represents a big.Int used a parameter in some cryptography. 100 // It serializes to json as a tupe of a base64-encoded number and a length in 101 // bits. 102 type cryptoParameter struct { 103 *big.Int 104 } 105 106 type auxCryptoParameter struct { 107 Raw []byte `json:"value"` 108 Length int `json:"length"` 109 } 110 111 // MarshalJSON implements the json.Marshaler interface 112 func (p *cryptoParameter) MarshalJSON() ([]byte, error) { 113 var aux auxCryptoParameter 114 if p.Int != nil { 115 aux.Raw = p.Bytes() 116 aux.Length = 8 * len(aux.Raw) 117 } 118 return json.Marshal(&aux) 119 } 120 121 // UnmarshalJSON implements the json.Unmarshal interface 122 func (p *cryptoParameter) UnmarshalJSON(b []byte) error { 123 var aux auxCryptoParameter 124 if err := json.Unmarshal(b, &aux); err != nil { 125 return err 126 } 127 p.Int = new(big.Int) 128 p.SetBytes(aux.Raw) 129 return nil 130 }