Debian Codegraph @

protocol/attestation_safetynet.go

raw symbols SCIP
1 package protocolscip-go gomod github.com/go-webauthn/webauthn . `github.com/go-webauthn/webauthn/protocol`/
2
3 import (
4 "bytesscip-go gomod github.com/golang/go/src go1.25.0 bytes/"
5 "contextscip-go gomod github.com/golang/go/src go1.25.0 context/"
6 "crypto/sha256scip-go gomod github.com/golang/go/src go1.25.0 `crypto/sha256`/"
7 "crypto/x509scip-go gomod github.com/golang/go/src go1.25.0 `crypto/x509`/"
8 "encoding/base64scip-go gomod github.com/golang/go/src go1.25.0 `encoding/base64`/"
9 "fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/"
10 "timescip-go gomod github.com/golang/go/src go1.25.0 time/"
11
12 "github.com/go-viper/mapstructure/v2scip-go gomod github.com/go-viper/mapstructure/v2 v2.5.0 `github.com/go-viper/mapstructure/v2`/"
13 "github.com/golang-jwt/jwt/v5scip-go gomod github.com/golang-jwt/jwt/v5 v5.3.1 `github.com/golang-jwt/jwt/v5`/"
14
15 "github.com/go-webauthn/webauthn/metadatascip-go gomod github.com/go-webauthn/webauthn . `github.com/go-webauthn/webauthn/metadata`/"
16 )
17
18 // attestationFormatValidationHandlerAndroidSafetyNet is the handler for the Android SafetyNet Attestation Statement
19 // Format.
20 //
21 // When the authenticator is a platform authenticator on certain Android platforms, the attestation statement may be
22 // based on the SafetyNet API. In this case the authenticator data is completely controlled by the caller of the
23 // SafetyNet API (typically an application running on the Android platform) and the attestation statement provides some
24 // statements about the health of the platform and the identity of the calling application (see SafetyNet Documentation
25 // for more details).
26 //
27 // The syntax of an Android Attestation statement is defined as follows:
28 //
29 // $$attStmtType //= (
30 // fmt: "android-safetynet",
31 // attStmt: safetynetStmtFormat
32 // )
33 //
34 // safetynetStmtFormat = {
35 // ver: text,
36 // response: bytes
37 // }
38 //
39 // Specification: §8.5. Android SafetyNet Attestation Statement Format
40 //
41 // See: https://www.w3.org/TR/webauthn/#sctn-android-safetynet-attestation
42 func attestationFormatValidationHandlerAndroidSafetyNetscip-go gomod github.com/go-webauthn/webauthn . `github.com/go-webauthn/webauthn/protocol`/attestationFormatValidationHandlerAndroidSafetyNet().(att AttestationObjectscip-go gomod github.com/go-webauthn/webauthn . `github.com/go-webauthn/webauthn/protocol`/AttestationObject#, clientDataHash []byte, mds metadatascip-go gomod github.com/go-webauthn/webauthn . `github.com/go-webauthn/webauthn/metadata`/.Providerscip-go gomod github.com/go-webauthn/webauthn . `github.com/go-webauthn/webauthn/metadata`/Provider#) (attestationType string, x5cs []any, err error) {
43 // The syntax of an Android Attestation statement is defined as follows:
44 // $$attStmtType //= (
45 // fmt: "android-safetynet",
46 // attStmt: safetynetStmtFormat
47 // )
48
49 // safetynetStmtFormat = {
50 // ver: text,
51 // response: bytes
52 // }
53
54 // §8.5.1 Verify that attStmt is valid CBOR conforming to the syntax defined above and perform CBOR decoding on it to extract
55 // the contained fields.
56
57 // We have done this
58 // §8.5.2 Verify that response is a valid SafetyNet response of version ver.
59 version, present := att.AttStatementscip-go gomod github.com/go-webauthn/webauthn . `github.com/go-webauthn/webauthn/protocol`/AttestationObject#AttStatement.[stmtVersionscip-go gomod github.com/go-webauthn/webauthn . `github.com/go-webauthn/webauthn/protocol`/stmtVersion.].(string)
60 if !present {
61 return "", nil, ErrAttestationFormatscip-go gomod github.com/go-webauthn/webauthn . `github.com/go-webauthn/webauthn/protocol`/ErrAttestationFormat..WithDetailsscip-go gomod github.com/go-webauthn/webauthn . `github.com/go-webauthn/webauthn/protocol`/Error#WithDetails().("Unable to find the version of SafetyNet")
62 }
63
64 if version == "" {
65 return "", nil, ErrAttestationFormatscip-go gomod github.com/go-webauthn/webauthn . `github.com/go-webauthn/webauthn/protocol`/ErrAttestationFormat..WithDetailsscip-go gomod github.com/go-webauthn/webauthn . `github.com/go-webauthn/webauthn/protocol`/Error#WithDetails().("Not a proper version for SafetyNet")
66 }
67
68 // TODO: provide user the ability to designate their supported versions.
69
70 response, present := att.AttStatementscip-go gomod github.com/go-webauthn/webauthn . `github.com/go-webauthn/webauthn/protocol`/AttestationObject#AttStatement.["response"].([]byte)
71 if !present {
72 return "", nil, ErrAttestationFormatscip-go gomod github.com/go-webauthn/webauthn . `github.com/go-webauthn/webauthn/protocol`/ErrAttestationFormat..WithDetailsscip-go gomod github.com/go-webauthn/webauthn . `github.com/go-webauthn/webauthn/protocol`/Error#WithDetails().("Unable to find the SafetyNet response")
73 }
74
75 var token *jwtscip-go gomod github.com/golang-jwt/jwt/v5 v5.3.1 `github.com/golang-jwt/jwt/v5`/.Tokenscip-go gomod github.com/golang-jwt/jwt/v5 v5.3.1 `github.com/golang-jwt/jwt/v5`/Token#
76
77 if token, err = jwtscip-go gomod github.com/golang-jwt/jwt/v5 v5.3.1 `github.com/golang-jwt/jwt/v5`/.Parsescip-go gomod github.com/golang-jwt/jwt/v5 v5.3.1 `github.com/golang-jwt/jwt/v5`/Parse().(string(response), keyFuncSafetyNetJWTscip-go gomod github.com/go-webauthn/webauthn . `github.com/go-webauthn/webauthn/protocol`/keyFuncSafetyNetJWT()., jwtscip-go gomod github.com/golang-jwt/jwt/v5 v5.3.1 `github.com/golang-jwt/jwt/v5`/.WithValidMethodsscip-go gomod github.com/golang-jwt/jwt/v5 v5.3.1 `github.com/golang-jwt/jwt/v5`/WithValidMethods().([]string{jwtscip-go gomod github.com/golang-jwt/jwt/v5 v5.3.1 `github.com/golang-jwt/jwt/v5`/.SigningMethodRS256scip-go gomod github.com/golang-jwt/jwt/v5 v5.3.1 `github.com/golang-jwt/jwt/v5`/SigningMethodRS256..Algscip-go gomod github.com/golang-jwt/jwt/v5 v5.3.1 `github.com/golang-jwt/jwt/v5`/SigningMethodRSA#Alg().()})); err != nil {
78 return "", nil, ErrInvalidAttestationscip-go gomod github.com/go-webauthn/webauthn . `github.com/go-webauthn/webauthn/protocol`/ErrInvalidAttestation..WithDetailsscip-go gomod github.com/go-webauthn/webauthn . `github.com/go-webauthn/webauthn/protocol`/Error#WithDetails().(fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Sprintf().("Error finding cert issued to correct hostname: %+v", err)).WithErrorscip-go gomod github.com/go-webauthn/webauthn . `github.com/go-webauthn/webauthn/protocol`/Error#WithError().(err)
79 }
80
81 // marshall the JWT payload into the safetynet response json.
82 var safetyNetResponse SafetyNetResponsescip-go gomod github.com/go-webauthn/webauthn . `github.com/go-webauthn/webauthn/protocol`/SafetyNetResponse#
83
84 if err = mapstructurescip-go gomod github.com/go-viper/mapstructure/v2 v2.5.0 `github.com/go-viper/mapstructure/v2`/.Decodescip-go gomod github.com/go-viper/mapstructure/v2 v2.5.0 `github.com/go-viper/mapstructure/v2`/Decode().(token.Claimsscip-go gomod github.com/golang-jwt/jwt/v5 v5.3.1 `github.com/golang-jwt/jwt/v5`/Token#Claims., &safetyNetResponse); err != nil {
85 return "", nil, ErrAttestationFormatscip-go gomod github.com/go-webauthn/webauthn . `github.com/go-webauthn/webauthn/protocol`/ErrAttestationFormat..WithDetailsscip-go gomod github.com/go-webauthn/webauthn . `github.com/go-webauthn/webauthn/protocol`/Error#WithDetails().(fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Sprintf().("Error parsing the SafetyNet response: %+v", err)).WithErrorscip-go gomod github.com/go-webauthn/webauthn . `github.com/go-webauthn/webauthn/protocol`/Error#WithError().(err)
86 }
87
88 // §8.5.3 Verify that the nonce in the response is identical to the Base64 encoding of the SHA-256 hash of the concatenation
89 // of authenticatorData and clientDataHash.
90 nonceBuffer := sha256scip-go gomod github.com/golang/go/src go1.25.0 `crypto/sha256`/.Sum256scip-go gomod github.com/golang/go/src go1.25.0 `crypto/sha256`/Sum256().(append(att.RawAuthDatascip-go gomod github.com/go-webauthn/webauthn . `github.com/go-webauthn/webauthn/protocol`/AttestationObject#RawAuthData., clientDataHash...))
91
92 nonceBytes, err := base64scip-go gomod github.com/golang/go/src go1.25.0 `encoding/base64`/.StdEncodingscip-go gomod github.com/golang/go/src go1.25.0 `encoding/base64`/StdEncoding..DecodeStringscip-go gomod github.com/golang/go/src go1.25.0 `encoding/base64`/Encoding#DecodeString().(safetyNetResponse.Noncescip-go gomod github.com/go-webauthn/webauthn . `github.com/go-webauthn/webauthn/protocol`/SafetyNetResponse#Nonce.)
93 if !bytesscip-go gomod github.com/golang/go/src go1.25.0 bytes/.Equalscip-go gomod github.com/golang/go/src go1.25.0 bytes/Equal().(nonceBuffer[:], nonceBytes) || err != nil {
94 return "", nil, ErrInvalidAttestationscip-go gomod github.com/go-webauthn/webauthn . `github.com/go-webauthn/webauthn/protocol`/ErrInvalidAttestation..WithDetailsscip-go gomod github.com/go-webauthn/webauthn . `github.com/go-webauthn/webauthn/protocol`/Error#WithDetails().("Invalid nonce for in SafetyNet response").WithErrorscip-go gomod github.com/go-webauthn/webauthn . `github.com/go-webauthn/webauthn/protocol`/Error#WithError().(err)
95 }
96
97 // §8.5.4 Let attestationCert be the attestation certificate (https://www.w3.org/TR/webauthn/#attestation-certificate)
98 certChain := token.Headerscip-go gomod github.com/golang-jwt/jwt/v5 v5.3.1 `github.com/golang-jwt/jwt/v5`/Token#Header.[stmtX5Cscip-go gomod github.com/go-webauthn/webauthn . `github.com/go-webauthn/webauthn/protocol`/stmtX5C.].([]any)
99 l := make([]byte, base64scip-go gomod github.com/golang/go/src go1.25.0 `encoding/base64`/.StdEncodingscip-go gomod github.com/golang/go/src go1.25.0 `encoding/base64`/StdEncoding..DecodedLenscip-go gomod github.com/golang/go/src go1.25.0 `encoding/base64`/Encoding#DecodedLen().(len(certChain[0].(string))))
100
101 n, err := base64scip-go gomod github.com/golang/go/src go1.25.0 `encoding/base64`/.StdEncodingscip-go gomod github.com/golang/go/src go1.25.0 `encoding/base64`/StdEncoding..Decodescip-go gomod github.com/golang/go/src go1.25.0 `encoding/base64`/Encoding#Decode().(l, []byte(certChain[0].(string)))
102 if err != nil {
103 return "", nil, ErrInvalidAttestationscip-go gomod github.com/go-webauthn/webauthn . `github.com/go-webauthn/webauthn/protocol`/ErrInvalidAttestation..WithDetailsscip-go gomod github.com/go-webauthn/webauthn . `github.com/go-webauthn/webauthn/protocol`/Error#WithDetails().(fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Sprintf().("Error finding cert issued to correct hostname: %+v", err)).WithErrorscip-go gomod github.com/go-webauthn/webauthn . `github.com/go-webauthn/webauthn/protocol`/Error#WithError().(err)
104 }
105
106 attestationCert, err := x509scip-go gomod github.com/golang/go/src go1.25.0 `crypto/x509`/.ParseCertificatescip-go gomod github.com/golang/go/src go1.25.0 `crypto/x509`/ParseCertificate().(l[:n])
107 if err != nil {
108 return "", nil, ErrInvalidAttestationscip-go gomod github.com/go-webauthn/webauthn . `github.com/go-webauthn/webauthn/protocol`/ErrInvalidAttestation..WithDetailsscip-go gomod github.com/go-webauthn/webauthn . `github.com/go-webauthn/webauthn/protocol`/Error#WithDetails().(fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Sprintf().("Error finding cert issued to correct hostname: %+v", err)).WithErrorscip-go gomod github.com/go-webauthn/webauthn . `github.com/go-webauthn/webauthn/protocol`/Error#WithError().(err)
109 }
110
111 // §8.5.5 Verify that attestationCert is issued to the hostname "attest.android.com".
112 if err = attestationCert.VerifyHostnamescip-go gomod github.com/golang/go/src go1.25.0 `crypto/x509`/Certificate#VerifyHostname().(attStatementAndroidSafetyNetHostnamescip-go gomod github.com/go-webauthn/webauthn . `github.com/go-webauthn/webauthn/protocol`/attStatementAndroidSafetyNetHostname.); err != nil {
113 return "", nil, ErrInvalidAttestationscip-go gomod github.com/go-webauthn/webauthn . `github.com/go-webauthn/webauthn/protocol`/ErrInvalidAttestation..WithDetailsscip-go gomod github.com/go-webauthn/webauthn . `github.com/go-webauthn/webauthn/protocol`/Error#WithDetails().(fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Sprintf().("Error finding cert issued to correct hostname: %+v", err)).WithErrorscip-go gomod github.com/go-webauthn/webauthn . `github.com/go-webauthn/webauthn/protocol`/Error#WithError().(err)
114 }
115
116 // §8.5.6 Verify that the ctsProfileMatch attribute in the payload of response is true.
117 if !safetyNetResponse.CtsProfileMatchscip-go gomod github.com/go-webauthn/webauthn . `github.com/go-webauthn/webauthn/protocol`/SafetyNetResponse#CtsProfileMatch. {
118 return "", nil, ErrInvalidAttestationscip-go gomod github.com/go-webauthn/webauthn . `github.com/go-webauthn/webauthn/protocol`/ErrInvalidAttestation..WithDetailsscip-go gomod github.com/go-webauthn/webauthn . `github.com/go-webauthn/webauthn/protocol`/Error#WithDetails().("ctsProfileMatch attribute of the JWT payload is false")
119 }
120
121 if t := timescip-go gomod github.com/golang/go/src go1.25.0 time/.Unixscip-go gomod github.com/golang/go/src go1.25.0 time/Unix().(safetyNetResponse.TimestampMsscip-go gomod github.com/go-webauthn/webauthn . `github.com/go-webauthn/webauthn/protocol`/SafetyNetResponse#TimestampMs./1000, 0); t.Afterscip-go gomod github.com/golang/go/src go1.25.0 time/Time#After().(timescip-go gomod github.com/golang/go/src go1.25.0 time/.Nowscip-go gomod github.com/golang/go/src go1.25.0 time/Now().()) {
122 // Zero tolerance for post-dated timestamps.
123 return "", nil, ErrInvalidAttestationscip-go gomod github.com/go-webauthn/webauthn . `github.com/go-webauthn/webauthn/protocol`/ErrInvalidAttestation..WithDetailsscip-go gomod github.com/go-webauthn/webauthn . `github.com/go-webauthn/webauthn/protocol`/Error#WithDetails().("SafetyNet response with timestamp after current time")
124 } else if t.Beforescip-go gomod github.com/golang/go/src go1.25.0 time/Time#Before().(timescip-go gomod github.com/golang/go/src go1.25.0 time/.Nowscip-go gomod github.com/golang/go/src go1.25.0 time/Now().().Addscip-go gomod github.com/golang/go/src go1.25.0 time/Time#Add().(-timescip-go gomod github.com/golang/go/src go1.25.0 time/.Minutescip-go gomod github.com/golang/go/src go1.25.0 time/Minute.)) {
125 // Small tolerance for pre-dated timestamps.
126 if mds != nil && mds.GetValidateEntryscip-go gomod github.com/go-webauthn/webauthn . `github.com/go-webauthn/webauthn/metadata`/Provider#GetValidateEntry.(contextscip-go gomod github.com/golang/go/src go1.25.0 context/.Backgroundscip-go gomod github.com/golang/go/src go1.25.0 context/Background().()) {
127 return "", nil, ErrInvalidAttestationscip-go gomod github.com/go-webauthn/webauthn . `github.com/go-webauthn/webauthn/protocol`/ErrInvalidAttestation..WithDetailsscip-go gomod github.com/go-webauthn/webauthn . `github.com/go-webauthn/webauthn/protocol`/Error#WithDetails().("SafetyNet response with timestamp before one minute ago")
128 }
129 }
130
131 // §8.5.7 If successful, return implementation-specific values representing attestation type Basic and attestation
132 // trust path attestationCert.
133 return string(metadatascip-go gomod github.com/go-webauthn/webauthn . `github.com/go-webauthn/webauthn/metadata`/.BasicFullscip-go gomod github.com/go-webauthn/webauthn . `github.com/go-webauthn/webauthn/metadata`/BasicFull.), nil, nil
134 }
135
136 func keyFuncSafetyNetJWTscip-go gomod github.com/go-webauthn/webauthn . `github.com/go-webauthn/webauthn/protocol`/keyFuncSafetyNetJWT().(token *jwtscip-go gomod github.com/golang-jwt/jwt/v5 v5.3.1 `github.com/golang-jwt/jwt/v5`/.Tokenscip-go gomod github.com/golang-jwt/jwt/v5 v5.3.1 `github.com/golang-jwt/jwt/v5`/Token#) (key any, err error) {
137 var (
138 ok bool
139 raw any
140 chain []any
141 first string
142 der []byte
143 cert *x509scip-go gomod github.com/golang/go/src go1.25.0 `crypto/x509`/.Certificatescip-go gomod github.com/golang/go/src go1.25.0 `crypto/x509`/Certificate#
144 )
145
146 if raw, ok = token.Headerscip-go gomod github.com/golang-jwt/jwt/v5 v5.3.1 `github.com/golang-jwt/jwt/v5`/Token#Header.[stmtX5Cscip-go gomod github.com/go-webauthn/webauthn . `github.com/go-webauthn/webauthn/protocol`/stmtX5C.]; !ok {
147 return nil, fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("jwt header missing x5c")
148 }
149
150 if chain, ok = raw.([]any); !ok || len(chain) == 0 {
151 return nil, fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("jwt header x5c is not a non-empty array")
152 }
153
154 if first, ok = chain[0].(string); !ok || first == "" {
155 return nil, fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("jwt header x5c[0] not a base64 string")
156 }
157
158 if der, err = base64scip-go gomod github.com/golang/go/src go1.25.0 `encoding/base64`/.StdEncodingscip-go gomod github.com/golang/go/src go1.25.0 `encoding/base64`/StdEncoding..DecodeStringscip-go gomod github.com/golang/go/src go1.25.0 `encoding/base64`/Encoding#DecodeString().(first); err != nil {
159 return nil, fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("decode x5c leaf: %w", err)
160 }
161
162 if cert, err = x509scip-go gomod github.com/golang/go/src go1.25.0 `crypto/x509`/.ParseCertificatescip-go gomod github.com/golang/go/src go1.25.0 `crypto/x509`/ParseCertificate().(der); err != nil {
163 if cert != nil {
164 return cert.PublicKeyscip-go gomod github.com/golang/go/src go1.25.0 `crypto/x509`/Certificate#PublicKey., fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("parse x5c leaf: %w", err)
165 }
166
167 return nil, fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("parse x5c leaf: %w", err)
168 }
169
170 return cert.PublicKeyscip-go gomod github.com/golang/go/src go1.25.0 `crypto/x509`/Certificate#PublicKey., nil
171 }
172
173 type SafetyNetResponsescip-go gomod github.com/go-webauthn/webauthn . `github.com/go-webauthn/webauthn/protocol`/SafetyNetResponse# struct {
174 Noncescip-go gomod github.com/go-webauthn/webauthn . `github.com/go-webauthn/webauthn/protocol`/SafetyNetResponse#Nonce. string `json:"nonce"`
175 TimestampMsscip-go gomod github.com/go-webauthn/webauthn . `github.com/go-webauthn/webauthn/protocol`/SafetyNetResponse#TimestampMs. int64 `json:"timestampMs"`
176 ApkPackageNamescip-go gomod github.com/go-webauthn/webauthn . `github.com/go-webauthn/webauthn/protocol`/SafetyNetResponse#ApkPackageName. string `json:"apkPackageName"`
177 ApkDigestSha256scip-go gomod github.com/go-webauthn/webauthn . `github.com/go-webauthn/webauthn/protocol`/SafetyNetResponse#ApkDigestSha256. string `json:"apkDigestSha256"`
178 CtsProfileMatchscip-go gomod github.com/go-webauthn/webauthn . `github.com/go-webauthn/webauthn/protocol`/SafetyNetResponse#CtsProfileMatch. bool `json:"ctsProfileMatch"`
179 ApkCertificateDigestSha256scip-go gomod github.com/go-webauthn/webauthn . `github.com/go-webauthn/webauthn/protocol`/SafetyNetResponse#ApkCertificateDigestSha256. []any `json:"apkCertificateDigestSha256"`
180 BasicIntegrityscip-go gomod github.com/go-webauthn/webauthn . `github.com/go-webauthn/webauthn/protocol`/SafetyNetResponse#BasicIntegrity. bool `json:"basicIntegrity"`
181 }
182
183 func initscip-go gomod github.com/go-webauthn/webauthn . `github.com/go-webauthn/webauthn/protocol`/init().() {
184 RegisterAttestationFormatscip-go gomod github.com/go-webauthn/webauthn . `github.com/go-webauthn/webauthn/protocol`/RegisterAttestationFormat().(AttestationFormatAndroidSafetyNetscip-go gomod github.com/go-webauthn/webauthn . `github.com/go-webauthn/webauthn/protocol`/AttestationFormatAndroidSafetyNet., attestationFormatValidationHandlerAndroidSafetyNetscip-go gomod github.com/go-webauthn/webauthn . `github.com/go-webauthn/webauthn/protocol`/attestationFormatValidationHandlerAndroidSafetyNet().)
185 }