Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove map[string]interface{} usage in x509/json #173

Merged
merged 1 commit into from
Aug 30, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 53 additions & 32 deletions x509/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,15 +212,61 @@ func (v *validity) UnmarshalJSON(b []byte) error {
return nil
}


type jsonECDSAPublicKey struct {
B []byte `json:"b"`
Curve string `json:"curve"`
Gx []byte `json:"gx"`
Gy []byte `json:"gy"`
Length int `json:"length"`
N []byte `json:"n"`
P []byte `json:"p"`
Pub []byte `json:"pub,omitempty"`
X []byte `json:"x"`
Y []byte `json:"y"`
}

func jsonECDSAConstructor(key *ecdsa.PublicKey) *jsonECDSAPublicKey {
params := key.Params()
return &jsonECDSAPublicKey{
P: params.P.Bytes(),
N: params.N.Bytes(),
B: params.B.Bytes(),
Gx: params.Gx.Bytes(),
Gy: params.Gy.Bytes(),
X: key.X.Bytes(),
Y: key.Y.Bytes(),
Curve: key.Curve.Params().Name,
Length: key.Curve.Params().BitSize,
}

}

type jsonDSAPublicKey struct {
G []byte`json:"g"`
P []byte`json:"p"`
Q []byte`json:"q"`
Y []byte`json:"y"`
}

func jsonDSAConstructor(key *dsa.PublicKey) *jsonDSAPublicKey {
return &jsonDSAPublicKey{
P: key.P.Bytes(),
Q: key.Q.Bytes(),
G: key.G.Bytes(),
Y: key.Y.Bytes(),
}
}

// JSONSubjectKeyInfo - used to condense several fields from x509.Certificate
// related to the subject public key into one field within JSONCertificate
// Unfortunately, this struct cannot have its own Marshal method since it
// needs information from multiple fields in x509.Certificate
type JSONSubjectKeyInfo struct {
KeyAlgorithm PublicKeyAlgorithm `json:"key_algorithm"`
RSAPublicKey *jsonKeys.RSAPublicKey `json:"rsa_public_key,omitempty"`
DSAPublicKey interface{} `json:"dsa_public_key,omitempty"`
ECDSAPublicKey interface{} `json:"ecdsa_public_key,omitempty"`
DSAPublicKey *jsonDSAPublicKey `json:"dsa_public_key,omitempty"`
ECDSAPublicKey *jsonECDSAPublicKey `json:"ecdsa_public_key,omitempty"`
SPKIFingerprint CertificateFingerprint `json:"fingerprint_sha256"`
}

Expand Down Expand Up @@ -273,26 +319,6 @@ type JSONCertificate struct {
Redacted bool `json:"redacted"`
}

func AddECDSAPublicKeyToKeyMap(keyMap map[string]interface{}, key *ecdsa.PublicKey) {
params := key.Params()
keyMap["p"] = params.P.Bytes()
keyMap["n"] = params.N.Bytes()
keyMap["b"] = params.B.Bytes()
keyMap["gx"] = params.Gx.Bytes()
keyMap["gy"] = params.Gy.Bytes()
keyMap["x"] = key.X.Bytes()
keyMap["y"] = key.Y.Bytes()
keyMap["curve"] = key.Curve.Params().Name
keyMap["length"] = key.Curve.Params().BitSize
}

func AddDSAPublicKeyToKeyMap(keyMap map[string]interface{}, key *dsa.PublicKey) {
keyMap["p"] = key.P.Bytes()
keyMap["q"] = key.Q.Bytes()
keyMap["g"] = key.G.Bytes()
keyMap["y"] = key.Y.Bytes()
}

func (c *Certificate) MarshalJSON() ([]byte, error) {
// Fill out the certificate
jc := new(JSONCertificate)
Expand Down Expand Up @@ -343,25 +369,20 @@ func (c *Certificate) MarshalJSON() ([]byte, error) {
}
}

// Pull out the key
keyMap := make(map[string]interface{})

jc.SubjectKeyInfo.SPKIFingerprint = c.SPKIFingerprint
switch key := c.PublicKey.(type) {
case *rsa.PublicKey:
rsaKey := new(jsonKeys.RSAPublicKey)
rsaKey.PublicKey = key
jc.SubjectKeyInfo.RSAPublicKey = rsaKey
case *dsa.PublicKey:
AddDSAPublicKeyToKeyMap(keyMap, key)
jc.SubjectKeyInfo.DSAPublicKey = keyMap
jc.SubjectKeyInfo.DSAPublicKey = jsonDSAConstructor(key)
case *ecdsa.PublicKey:
AddECDSAPublicKeyToKeyMap(keyMap, key)
jc.SubjectKeyInfo.ECDSAPublicKey = keyMap
jc.SubjectKeyInfo.ECDSAPublicKey = jsonECDSAConstructor(key)
case *AugmentedECDSA:
AddECDSAPublicKeyToKeyMap(keyMap, key.Pub)
keyMap["pub"] = key.Raw.Bytes
jc.SubjectKeyInfo.ECDSAPublicKey = keyMap
jsonKey := jsonECDSAConstructor(key.Pub)
jsonKey.Pub = key.Raw.Bytes
jc.SubjectKeyInfo.ECDSAPublicKey = jsonKey
}

jc.Extensions, jc.UnknownExtensions = c.jsonifyExtensions()
Expand Down