forked from globalsign/hvclient
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpolicy.go
839 lines (702 loc) · 24 KB
/
policy.go
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
/*
Copyright (c) 2019-2021 GMO GlobalSign Pte. Ltd.
Licensed under the MIT License (the "License"); you may not use this file except
in compliance with the License. You may obtain a copy of the License at
https://opensource.org/licenses/MIT
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 hvclient
import (
"encoding/asn1"
"encoding/json"
"fmt"
"sort"
"strings"
"github.com/globalsign/hvclient/internal/oids"
)
// Policy is a certificate request validation policy.
type Policy struct {
Validity *ValidityPolicy
SubjectDN *SubjectDNPolicy
SAN *SANPolicy
EKUs *EKUPolicy
SubjectDA *SubjectDAPolicy
QualifiedStatements *QualifiedStatementsPolicy
MSExtensionTemplate *MSExtensionTemplatePolicy
CustomExtensions []CustomExtensionsPolicy
PublicKey *PublicKeyPolicy
PublicKeySignature Presence
}
// jsonPolicy is used internally for JSON marshalling/unmarshalling.
type jsonPolicy struct {
Validity *ValidityPolicy `json:"validity,omitempty"`
SubjectDN *SubjectDNPolicy `json:"subject_dn,omitempty"`
SAN *SANPolicy `json:"san,omitempty"`
EKUs *EKUPolicy `json:"extended_key_usages,omitempty"`
SubjectDA *SubjectDAPolicy `json:"subject_da,omitempty"`
QualifiedStatements *QualifiedStatementsPolicy `json:"qualified_statements,omitempty"`
MSExtensionTemplate *MSExtensionTemplatePolicy `json:"ms_extension_template,omitempty"`
CustomExtensions customExtensionsPolicies `json:"custom_extensions,omitempty"`
PublicKey *PublicKeyPolicy `json:"public_key,omitempty"`
PublicKeySignature Presence `json:"public_key_signature"`
}
// ValidityPolicy is a validity field in a validation policy.
type ValidityPolicy struct {
SecondsMin int64 `json:"secondsmin"`
SecondsMax int64 `json:"secondsmax"`
NotBeforeNegativeSkew int64 `json:"not_before_negative_skew"`
NotBeforePositiveSkew int64 `json:"not_before_positive_skew"`
}
// SubjectDNPolicy is a subject distinguished name field in a validation policy.
type SubjectDNPolicy struct {
CommonName *StringPolicy
Organization *StringPolicy
OrganizationalUnit *ListPolicy
Country *StringPolicy
State *StringPolicy
Locality *StringPolicy
StreetAddress *StringPolicy
Email *StringPolicy
JOILocality *StringPolicy
JOIState *StringPolicy
JOICountry *StringPolicy
BusinessCategory *StringPolicy
ExtraAttributes []TypeAndValuePolicy
}
// jsonSubjectDNPolicy is used internally for JSON marshalling/unmarshalling.
type jsonSubjectDNPolicy struct {
CommonName *StringPolicy `json:"common_name,omitempty"`
Organization *StringPolicy `json:"organization,omitempty"`
OrganizationalUnit *ListPolicy `json:"organizational_unit,omitempty"`
Country *StringPolicy `json:"country,omitempty"`
State *StringPolicy `json:"state,omitempty"`
Locality *StringPolicy `json:"locality,omitempty"`
StreetAddress *StringPolicy `json:"street_address,omitempty"`
Email *StringPolicy `json:"email,omitempty"`
JOILocality *StringPolicy `json:"jurisdiction_of_incorporation_locality_name,omitempty"`
JOIState *StringPolicy `json:"jurisdiction_of_incorporation_state_or_province_name,omitempty"`
JOICountry *StringPolicy `json:"jurisdiction_of_incorporation_country_name,omitempty"`
BusinessCategory *StringPolicy `json:"business_category,omitempty"`
ExtraAttributes typeAndValuePolicies `json:"extra_attributes,omitempty"`
}
// SANPolicy is the subject alternative names field in a validation policy.
type SANPolicy struct {
DNSNames *ListPolicy
Emails *ListPolicy
IPAddresses *ListPolicy
URIs *ListPolicy
OtherNames []TypeAndValuePolicy
}
// jsonSANPolicy is used internally for JSON marshalling/unmarshalling.
type jsonSANPolicy struct {
DNSNames *ListPolicy `json:"dns_names"`
Emails *ListPolicy `json:"emails"`
IPAddresses *ListPolicy `json:"ip_addresses"`
URIs *ListPolicy `json:"uris"`
OtherNames typeAndValuePolicies `json:"other_names"`
}
// EKUPolicy is the extended key usages field in a validation
// policy.
type EKUPolicy struct {
EKUs ListPolicy `json:"ekus"`
Critical bool `json:"critical"`
}
// SubjectDAPolicy is the subject directory attributes field in a validation
// policy.
type SubjectDAPolicy struct {
Gender *StringPolicy `json:"gender,omitempty"`
DateOfBirth Presence `json:"date_of_birth,omitempty"`
PlaceOfBirth *StringPolicy `json:"place_of_birth,omitempty"`
CountryOfCitizenship *ListPolicy `json:"country_of_citizenship,omitempty"`
CountryOfResidence *ListPolicy `json:"country_of_residence,omitempty"`
ExtraAttributes []TypeAndValuePolicy `json:"extra_attributes,omitempty"`
}
// jsonSubjectDAPolicy is used internally for JSON marshalling/unmarshalling.
type jsonSubjectDAPolicy struct {
Gender *StringPolicy `json:"gender,omitempty"`
DateOfBirth Presence `json:"date_of_birth,omitempty"`
PlaceOfBirth *StringPolicy `json:"place_of_birth,omitempty"`
CountryOfCitizenship *ListPolicy `json:"country_of_citizenship,omitempty"`
CountryOfResidence *ListPolicy `json:"country_of_residence,omitempty"`
ExtraAttributes typeAndValuePolicies `json:"extra_attributes,omitempty"`
}
// QualifiedStatementsPolicy is the qualified statements field in a validation
// policy.
type QualifiedStatementsPolicy struct {
Semantics *SemanticsPolicy `json:"semantics"`
ETSIQCCompliance OptionalStaticPresence `json:"etsi_qc_compliance"`
ETSIQCSSCDCompliance OptionalStaticPresence `json:"etsi_qc_sscd_compliance"`
ETSIQCType *StringPolicy `json:"etsi_qc_type"`
ETSIQCRetentionPeriod *IntegerPolicy `json:"etsi_qc_retention_period"`
ETSIQCPDs *ETSIPDsPolicy `json:"etsi_qc_pds"`
}
// SemanticsPolicy is the semantics field in the qualified statements field
// in a validation policy.
type SemanticsPolicy struct {
Identifier *StringPolicy `json:"identifier"`
NameAuthorities *ListPolicy `json:"name_authorities"`
}
// ETSIPDsPolicy is the PKI disclosure statements field in the qualified
// statements field in a validation policy.
type ETSIPDsPolicy struct {
Presence Presence `json:"presence"`
Policies map[string]string `json:"policies"`
}
// MSExtensionTemplatePolicy is the Microsoft template extension field in a
// validation policy.
type MSExtensionTemplatePolicy struct {
Critical bool `json:"critical"`
TemplateID *StringPolicy `json:"template_id,omitempty"`
MajorVersion *IntegerPolicy `json:"major_version,omitempty"`
MinorVersion *IntegerPolicy `json:"minor_version,omitempty"`
}
// CustomExtensionsPolicy is the custom extensions field in a validation policy.
type CustomExtensionsPolicy struct {
OID asn1.ObjectIdentifier `json:"-"`
Presence Presence `json:"presence"`
Critical bool `json:"critical"`
ValueType ValueType `json:"value_type"`
ValueFormat string `json:"value_format,omitempty"`
}
// PublicKeyPolicy is the public key field in a validation policy.
type PublicKeyPolicy struct {
KeyType KeyType `json:"key_type"`
AllowedLengths []int `json:"allowed_lengths"`
KeyFormat KeyFormat `json:"key_format"`
}
// StringPolicy is a string value entry in a validation policy.
type StringPolicy struct {
Presence Presence `json:"presence"`
Format string `json:"format"`
}
// IntegerPolicy is an integer value entry in a validation policy.
type IntegerPolicy struct {
Presence Presence `json:"presence"`
Min int `json:"min"`
Max int `json:"max"`
}
// ListPolicy is a list value entry in a validation policy.
type ListPolicy struct {
Static bool `json:"static"`
List []string `json:"list"`
MinCount int `json:"mincount"`
MaxCount int `json:"maxcount"`
}
// TypeAndValuePolicy is a type and value entry in a validation policy.
type TypeAndValuePolicy struct {
OID asn1.ObjectIdentifier `json:"-"`
Static bool `json:"static"`
ValueType ValueType `json:"value_type"`
ValueFormat string `json:"value_format"`
MinCount int `json:"mincount"`
MaxCount int `json:"maxcount"`
}
// typeAndValuePolicies is used internally for JSON marshalling/unmarshalling.
type typeAndValuePolicies []TypeAndValuePolicy
// customExtensionsPolicies is used internally for JSON marshalling/unmarshalling.
type customExtensionsPolicies []CustomExtensionsPolicy
// ValueType is a value_type field in a validation policy.
type ValueType int
// Presence is the presence field in a validation policy.
type Presence int
// KeyType is the type of a public key.
type KeyType int
// KeyFormat is the allowed format of a public key.
type KeyFormat int
// OptionalStaticPresence denotes whether a static boolean is optional, or
// true, or false.
type OptionalStaticPresence int
// ValueType value constants.
const (
IA5String ValueType = iota + 1
PrintableString
UTF8String
Integer
DER
Nil
)
// Presence value constants.
const (
Optional Presence = iota + 1
Required
Forbidden
Static
)
// Key format value constants.
const (
PKCS8 KeyFormat = iota + 1
PKCS10
)
// Key type value constants.
const (
RSA KeyType = iota + 1
ECDSA
)
// Optional static presence values.
const (
StaticOptional OptionalStaticPresence = iota + 1
StaticTrue
StaticFalse
)
// valueTypeDescriptions maps value type values to their string descriptions.
var valueTypeDescriptions = []string{
IA5String: "IA5STRING",
PrintableString: "PRINTABLESTRING",
UTF8String: "UTF8STRING",
Integer: "INTEGER",
DER: "DER",
Nil: "NIL",
}
// valueTypeValues maps value type string descriptions to their values.
var valueTypeValues = map[string]ValueType{
"IA5STRING": IA5String,
"PRINTABLESTRING": PrintableString,
"UTF8STRING": UTF8String,
"INTEGER": Integer,
"DER": DER,
"NIL": Nil,
}
// presenceDescriptions maps presence values to their string descriptions.
var presenceDescriptions = []string{
Optional: "OPTIONAL",
Required: "REQUIRED",
Forbidden: "FORBIDDEN",
Static: "STATIC",
}
// presenceValues maps presence string descriptions to their values.
var presenceValues = map[string]Presence{
"OPTIONAL": Optional,
"REQUIRED": Required,
"FORBIDDEN": Forbidden,
"STATIC": Static,
}
// keyTypeDescriptions maps key type values to their string descriptions.
var keyTypeDescriptions = []string{
RSA: "RSA",
ECDSA: "ECDSA",
}
// keyTypeValues maps key type string descriptions to their values.
var keyTypeValues = map[string]KeyType{
"RSA": RSA,
"ECDSA": ECDSA,
}
// keyFormatDescriptions maps key format values to their string descriptions.
var keyFormatDescriptions = []string{
PKCS8: "PKCS8",
PKCS10: "PKCS10",
}
// keyFormatValues maps key format string descriptions to their values.
var keyFormatValues = map[string]KeyFormat{
"PKCS8": PKCS8,
"PKCS10": PKCS10,
}
// optionalStaticPresenceDescriptions maps QC compliance values to their string
// descriptions.
var optionalStaticPresenceDescriptions = []string{
StaticOptional: "OPTIONAL",
StaticTrue: "STATIC_TRUE",
StaticFalse: "STATIC_FALSE",
}
// optionalStaticPresenceValuess maps optional static presence value string
// descriptions to their values.
var optionalStaticPresenceValues = map[string]OptionalStaticPresence{
"OPTIONAL": StaticOptional,
"STATIC_TRUE": StaticTrue,
"STATIC_FALSE": StaticFalse,
}
// MarshalJSON returns the JSON encoding of a validation policy.
func (p Policy) MarshalJSON() ([]byte, error) {
return json.Marshal(jsonPolicy{
Validity: p.Validity,
SubjectDN: p.SubjectDN,
SAN: p.SAN,
EKUs: p.EKUs,
SubjectDA: p.SubjectDA,
QualifiedStatements: p.QualifiedStatements,
MSExtensionTemplate: p.MSExtensionTemplate,
CustomExtensions: customExtensionsPolicies(p.CustomExtensions),
PublicKey: p.PublicKey,
PublicKeySignature: p.PublicKeySignature,
})
}
// UnmarshalJSON parses a JSON-encoded validation policy and stores the result
// in the object.
func (p *Policy) UnmarshalJSON(b []byte) error {
var data jsonPolicy
if err := json.Unmarshal(b, &data); err != nil {
return err
}
*p = Policy{
Validity: data.Validity,
SubjectDN: data.SubjectDN,
SAN: data.SAN,
EKUs: data.EKUs,
SubjectDA: data.SubjectDA,
QualifiedStatements: data.QualifiedStatements,
MSExtensionTemplate: data.MSExtensionTemplate,
CustomExtensions: []CustomExtensionsPolicy(data.CustomExtensions),
PublicKey: data.PublicKey,
PublicKeySignature: data.PublicKeySignature,
}
return nil
}
// MarshalJSON returns the JSON encoding of a subject distinguished name
// policy.
func (p SubjectDNPolicy) MarshalJSON() ([]byte, error) {
return json.Marshal(jsonSubjectDNPolicy{
CommonName: p.CommonName,
Organization: p.Organization,
OrganizationalUnit: p.OrganizationalUnit,
Country: p.Country,
State: p.State,
Locality: p.Locality,
StreetAddress: p.StreetAddress,
Email: p.Email,
JOILocality: p.JOILocality,
JOIState: p.JOIState,
JOICountry: p.JOICountry,
BusinessCategory: p.BusinessCategory,
ExtraAttributes: typeAndValuePolicies(p.ExtraAttributes),
})
}
// UnmarshalJSON parses a JSON-encoded subject distinguished name policy and
// stores the result in the object.
func (p *SubjectDNPolicy) UnmarshalJSON(b []byte) error {
var data jsonSubjectDNPolicy
if err := json.Unmarshal(b, &data); err != nil {
return err
}
*p = SubjectDNPolicy{
CommonName: data.CommonName,
Organization: data.Organization,
OrganizationalUnit: data.OrganizationalUnit,
Country: data.Country,
State: data.State,
Locality: data.Locality,
StreetAddress: data.StreetAddress,
Email: data.Email,
JOILocality: data.JOILocality,
JOIState: data.JOIState,
JOICountry: data.JOICountry,
BusinessCategory: data.BusinessCategory,
ExtraAttributes: []TypeAndValuePolicy(data.ExtraAttributes),
}
return nil
}
// MarshalJSON returns the JSON encoding of a subject alternative names
// policy.
func (p SANPolicy) MarshalJSON() ([]byte, error) {
return json.Marshal(jsonSANPolicy{
DNSNames: p.DNSNames,
Emails: p.Emails,
IPAddresses: p.IPAddresses,
URIs: p.URIs,
OtherNames: typeAndValuePolicies(p.OtherNames),
})
}
// UnmarshalJSON parses a JSON-encoded subject alternative names policy and
// stores the result in the object.
func (p *SANPolicy) UnmarshalJSON(b []byte) error {
var data jsonSANPolicy
if err := json.Unmarshal(b, &data); err != nil {
return err
}
*p = SANPolicy{
DNSNames: data.DNSNames,
Emails: data.Emails,
IPAddresses: data.IPAddresses,
URIs: data.URIs,
OtherNames: []TypeAndValuePolicy(data.OtherNames),
}
return nil
}
// MarshalJSON returns the JSON encoding of a subject directory attributes
// policy.
func (p SubjectDAPolicy) MarshalJSON() ([]byte, error) {
return json.Marshal(jsonSubjectDAPolicy{
Gender: p.Gender,
DateOfBirth: p.DateOfBirth,
PlaceOfBirth: p.PlaceOfBirth,
CountryOfCitizenship: p.CountryOfCitizenship,
CountryOfResidence: p.CountryOfResidence,
ExtraAttributes: typeAndValuePolicies(p.ExtraAttributes),
})
}
// UnmarshalJSON parses a JSON-encoded subject directory attributes names
// policy and stores the result in the object.
func (p *SubjectDAPolicy) UnmarshalJSON(b []byte) error {
var data jsonSubjectDAPolicy
if err := json.Unmarshal(b, &data); err != nil {
return err
}
*p = SubjectDAPolicy{
Gender: data.Gender,
DateOfBirth: data.DateOfBirth,
PlaceOfBirth: data.PlaceOfBirth,
CountryOfCitizenship: data.CountryOfCitizenship,
CountryOfResidence: data.CountryOfResidence,
ExtraAttributes: []TypeAndValuePolicy(data.ExtraAttributes),
}
return nil
}
// MarshalJSON returns the JSON encoding of a list of type and value policies.
func (p typeAndValuePolicies) MarshalJSON() ([]byte, error) {
var result = []byte("{")
for i, val := range p {
if i != 0 {
result = append(result, byte(','))
}
result = append(result, []byte(fmt.Sprintf(`"%s":`, val.OID.String()))...)
var this, err = json.Marshal(val)
if err != nil {
return nil, err
}
result = append(result, this...)
}
result = append(result, byte('}'))
return result, nil
}
// UnmarshalJSON parses a JSON-encoded list of type and value policies and
// and stores the result in the object.
func (p *typeAndValuePolicies) UnmarshalJSON(b []byte) error {
var data map[string]TypeAndValuePolicy
var err = json.Unmarshal(b, &data)
if err != nil {
return err
}
var result typeAndValuePolicies
for key, value := range data {
var oid, err = oids.StringToOID(key)
if err != nil {
return err
}
result = append(result, TypeAndValuePolicy{
OID: oid,
Static: value.Static,
ValueType: value.ValueType,
ValueFormat: value.ValueFormat,
MinCount: value.MinCount,
MaxCount: value.MaxCount,
})
}
sort.Slice(result, func(i, j int) bool {
return result[i].OID.String() < result[j].OID.String()
})
*p = result
return nil
}
// MarshalJSON returns the JSON encoding of a list of custom extensions
// policies.
func (p customExtensionsPolicies) MarshalJSON() ([]byte, error) {
var result = []byte("{")
for i, ext := range p {
if i != 0 {
result = append(result, byte(','))
}
result = append(result, []byte(fmt.Sprintf(`"%s":`, ext.OID.String()))...)
var this, err = json.Marshal(ext)
if err != nil {
return nil, err
}
result = append(result, this...)
}
result = append(result, byte('}'))
return result, nil
}
// UnmarshalJSON parses a JSON-encoded list of custom extensions policies
// and stores the result in the object.
func (p *customExtensionsPolicies) UnmarshalJSON(b []byte) error {
var data map[string]CustomExtensionsPolicy
var err = json.Unmarshal(b, &data)
if err != nil {
return err
}
var result customExtensionsPolicies
for key, value := range data {
var oid, err = oids.StringToOID(key)
if err != nil {
return err
}
result = append(result, CustomExtensionsPolicy{
OID: oid,
Presence: value.Presence,
Critical: value.Critical,
ValueType: value.ValueType,
ValueFormat: value.ValueFormat,
})
}
sort.Slice(result, func(i, j int) bool {
return result[i].OID.String() < result[j].OID.String()
})
*p = result
return nil
}
// isValid checks if a value is within a valid range.
func (v ValueType) isValid() bool {
return v >= IA5String && v <= Nil
}
// String returns a description of the value type value.
func (v ValueType) String() string {
if !v.isValid() {
return "UNKNOWN VALUE_TYPE VALUE"
}
return valueTypeDescriptions[v]
}
// MarshalJSON returns the JSON encoding of a value type value.
func (v ValueType) MarshalJSON() ([]byte, error) {
if !v.isValid() {
return nil, fmt.Errorf("invalid value_type value: %d", v)
}
return json.Marshal(v.String())
}
// UnmarshalJSON parses a JSON-encoded value type value and stores the result
// in the object.
func (v *ValueType) UnmarshalJSON(b []byte) error {
var data string
var err = json.Unmarshal(b, &data)
if err != nil {
return err
}
var value, ok = valueTypeValues[strings.ToUpper(data)]
if !ok {
return fmt.Errorf("unknown value_type value %q", data)
}
*v = value
return nil
}
// isValid checks if a value is within a valid range.
func (p Presence) isValid() bool {
return p >= Optional && p <= Static
}
// String returns a description of the presence value.
func (p Presence) String() string {
if !p.isValid() {
return "UNKNOWN PRESENCE VALUE"
}
return presenceDescriptions[p]
}
// MarshalJSON returns the JSON encoding of a presence value.
func (p Presence) MarshalJSON() ([]byte, error) {
if !p.isValid() {
return nil, fmt.Errorf("invalid presence value: %d", p)
}
return json.Marshal(p.String())
}
// UnmarshalJSON parses a JSON-encoded presence value and stores the result in
// the object.
func (p *Presence) UnmarshalJSON(b []byte) error {
var data string
var err = json.Unmarshal(b, &data)
if err != nil {
return err
}
var value, ok = presenceValues[strings.ToUpper(data)]
if !ok {
return fmt.Errorf("unknown presence value %q", data)
}
*p = value
return nil
}
// isValid checks if a value is within a valid range.
func (t KeyType) isValid() bool {
return t >= RSA && t <= ECDSA
}
// String returns a description of the key type value.
func (t KeyType) String() string {
if !t.isValid() {
return "UNKNOWN KEY TYPE VALUE"
}
return keyTypeDescriptions[t]
}
// MarshalJSON returns the JSON encoding of a key type value.
func (t KeyType) MarshalJSON() ([]byte, error) {
if !t.isValid() {
return nil, fmt.Errorf("invalid key type value: %d", t)
}
return json.Marshal(t.String())
}
// UnmarshalJSON parses a JSON-encoded key type value and stores the result in
// the object.
func (t *KeyType) UnmarshalJSON(b []byte) error {
var data string
var err = json.Unmarshal(b, &data)
if err != nil {
return err
}
var value, ok = keyTypeValues[strings.ToUpper(data)]
if !ok {
return fmt.Errorf("unknown key type value %q", data)
}
*t = value
return nil
}
// isValid checks if a value is within a valid range.
func (f KeyFormat) isValid() bool {
return f >= PKCS8 && f <= PKCS10
}
// String returns a description of the key format value.
func (f KeyFormat) String() string {
if !f.isValid() {
return "UNKNOWN KEY FORMAT VALUE"
}
return keyFormatDescriptions[f]
}
// MarshalJSON returns the JSON encoding of a key format value.
func (f KeyFormat) MarshalJSON() ([]byte, error) {
if !f.isValid() {
return nil, fmt.Errorf("invalid key format value: %d", f)
}
return json.Marshal(f.String())
}
// UnmarshalJSON parses a JSON-encoded key format value and stores the result in
// the object.
func (f *KeyFormat) UnmarshalJSON(b []byte) error {
var data string
var err = json.Unmarshal(b, &data)
if err != nil {
return err
}
var value, ok = keyFormatValues[strings.ToUpper(data)]
if !ok {
return fmt.Errorf("unknown key format value %q", data)
}
*f = value
return nil
}
// isValid checks if a value is within a valid range.
func (v OptionalStaticPresence) isValid() bool {
return v >= StaticOptional && v <= StaticFalse
}
// String returns a description of the optional static presence value.
func (v OptionalStaticPresence) String() string {
if !v.isValid() {
return "UNKNOWN OPTIONAL STATIC PRESENCE VALUE"
}
return optionalStaticPresenceDescriptions[v]
}
// MarshalJSON returns the JSON encoding of an optional static presence value.
func (v OptionalStaticPresence) MarshalJSON() ([]byte, error) {
if !v.isValid() {
return nil, fmt.Errorf("invalid optional static presence value: %d", v)
}
return json.Marshal(v.String())
}
// UnmarshalJSON parses a JSON-encoded optional static presence value and
// stores the result in the object.
func (v *OptionalStaticPresence) UnmarshalJSON(b []byte) error {
var data string
var err = json.Unmarshal(b, &data)
if err != nil {
return err
}
var value, ok = optionalStaticPresenceValues[strings.ToUpper(data)]
if !ok {
return fmt.Errorf("unknown optional static presence value %q", data)
}
*v = value
return nil
}