github.com/zmap/zcrypto@v0.0.0-20240512203510-0fef58d9a9db/x509/certificate_type.go (about) 1 // Copyright 2017 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package x509 6 7 import "encoding/json" 8 9 // TODO: Automatically generate this file from a CSV 10 11 // CertificateType represents whether a certificate is a root, intermediate, or 12 // leaf. 13 type CertificateType int 14 15 // CertificateType constants. Values should not be considered significant aside 16 // from CertificateTypeUnknown is the zero value. 17 const ( 18 CertificateTypeUnknown CertificateType = 0 19 CertificateTypeLeaf CertificateType = 1 20 CertificateTypeIntermediate CertificateType = 2 21 CertificateTypeRoot CertificateType = 3 22 ) 23 24 const ( 25 certificateTypeStringLeaf = "leaf" 26 certificateTypeStringIntermediate = "intermediate" 27 certificateTypeStringRoot = "root" 28 certificateTypeStringUnknown = "unknown" 29 ) 30 31 // MarshalJSON implements the json.Marshaler interface. Any unknown integer 32 // value is considered the same as CertificateTypeUnknown. 33 func (t CertificateType) MarshalJSON() ([]byte, error) { 34 switch t { 35 case CertificateTypeLeaf: 36 return json.Marshal(certificateTypeStringLeaf) 37 case CertificateTypeIntermediate: 38 return json.Marshal(certificateTypeStringIntermediate) 39 case CertificateTypeRoot: 40 return json.Marshal(certificateTypeStringRoot) 41 default: 42 return json.Marshal(certificateTypeStringUnknown) 43 } 44 } 45 46 // UnmarshalJSON implements the json.Unmarshaler interface. Any unknown string 47 // is considered the same CertificateTypeUnknown. 48 func (t *CertificateType) UnmarshalJSON(b []byte) error { 49 var certificateTypeString string 50 if err := json.Unmarshal(b, &certificateTypeString); err != nil { 51 return err 52 } 53 switch certificateTypeString { 54 case certificateTypeStringLeaf: 55 *t = CertificateTypeLeaf 56 case certificateTypeStringIntermediate: 57 *t = CertificateTypeIntermediate 58 case certificateTypeStringRoot: 59 *t = CertificateTypeRoot 60 default: 61 *t = CertificateTypeUnknown 62 } 63 return nil 64 }