Skip to content

Commit c88a9da

Browse files
committed
headers: validate CWT claims
Signed-off-by: Pranjal Kole <[email protected]>
1 parent eb9cdec commit c88a9da

File tree

2 files changed

+68
-9
lines changed

2 files changed

+68
-9
lines changed

cwt_test.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,11 @@ func ExampleCWTClaims() {
2121
cose.CWTClaimIssuer: "issuer.example",
2222
cose.CWTClaimSubject: "subject.example",
2323
}
24-
msgToSign.Headers.Protected.SetCWTClaims(claims)
24+
25+
claims, err := msgToSign.Headers.Protected.SetCWTClaims(claims)
26+
if err != nil {
27+
panic(err)
28+
}
2529

2630
msgToSign.Headers.Unprotected[cose.HeaderLabelKeyID] = []byte("1")
2731

headers.go

Lines changed: 63 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -115,15 +115,55 @@ func (h ProtectedHeader) SetType(typ any) (any, error) {
115115

116116
// SetCWTClaims sets the CWT Claims value of the protected header.
117117
func (h ProtectedHeader) SetCWTClaims(claims CWTClaims) (CWTClaims, error) {
118-
iss, hasIss := claims[1]
119-
if hasIss && !canTstr(iss) {
120-
return claims, errors.New("cwt claim: iss: require tstr")
121-
}
122-
sub, hasSub := claims[2]
123-
if hasSub && !canTstr(sub) {
124-
return claims, errors.New("cwt claim: sub: require tstr")
118+
for name, _ := range claims {
119+
switch name {
120+
case 1:
121+
iss, hasIss := claims[name]
122+
if hasIss && !canTstr(iss) {
123+
return claims, errors.New("cwt claim: iss: require tstr")
124+
}
125+
case 2:
126+
sub, hasSub := claims[name]
127+
if hasSub && !canTstr(sub) {
128+
return claims, errors.New("cwt claim: sub: require tstr")
129+
}
130+
case 3:
131+
aud, hasAud := claims[name]
132+
if hasAud && !canTstr(aud) {
133+
return claims, errors.New("cwt claim: aud: require tstr")
134+
}
135+
case 4:
136+
exp, hasExp := claims[name]
137+
if hasExp && !canInt(exp) && !canFloat(exp) {
138+
return claims, errors.New("cwt claim: exp: require int or float")
139+
}
140+
case 5:
141+
nbf, hasNbf := claims[name]
142+
if hasNbf && !canInt(nbf) && !canFloat(nbf) {
143+
return claims, errors.New("cwt claim: nbf: require int or float")
144+
}
145+
case 6:
146+
iat, hasIat := claims[name]
147+
if hasIat && !canInt(iat) && !canFloat(iat) {
148+
return claims, errors.New("cwt claim: iat: require int or float")
149+
}
150+
case 7:
151+
cti, hasCti := claims[name]
152+
if hasCti && !canBstr(cti) {
153+
return claims, errors.New("cwt claim: cti: require tstr")
154+
}
155+
case 8:
156+
cnf, hasCnf := claims[name]
157+
if hasCnf && !canMap(cnf) {
158+
return claims, errors.New("cwt claim: cnf: require map")
159+
}
160+
case 9:
161+
scope, hasScope := claims[name]
162+
if hasScope && !canBstr(scope) && !canTstr(scope) {
163+
return claims, errors.New("cwt claim: scope: require bstr or tstr")
164+
}
165+
}
125166
}
126-
// TODO: validate claims, other claims
127167
h[HeaderLabelCWTClaims] = claims
128168
return claims, nil
129169
}
@@ -620,6 +660,15 @@ func canInt(v any) bool {
620660
return false
621661
}
622662

663+
// canFloat reports whether v can be used as a CBOR float type
664+
func canFloat(v any) bool {
665+
switch v.(type) {
666+
case float32, float64:
667+
return true
668+
}
669+
return false
670+
}
671+
623672
// canTstr reports whether v can be used as a CBOR tstr type.
624673
func canTstr(v any) bool {
625674
_, ok := v.(string)
@@ -632,6 +681,12 @@ func canBstr(v any) bool {
632681
return ok
633682
}
634683

684+
// canMap reports whether v can be used as a CBOR map type.
685+
func canMap(v any) bool {
686+
_, ok := v.(map[any]any)
687+
return ok
688+
}
689+
635690
// normalizeLabel tries to cast label into a int64 or a string.
636691
// Returns (nil, false) if the label type is not valid.
637692
func normalizeLabel(label any) (any, bool) {

0 commit comments

Comments
 (0)