diff options
author | Craig Tiller <ctiller@google.com> | 2017-11-10 09:53:21 -0800 |
---|---|---|
committer | Craig Tiller <ctiller@google.com> | 2017-11-10 09:53:21 -0800 |
commit | 4782d92b2d4fab261b5520a29d79ba97fea9ce7b (patch) | |
tree | d060fc1c4093583abc9348cc69a64b52d065f6e3 /src/core/lib/security/credentials/jwt | |
parent | 1ef989cd50cdfb172e99c552a67e3ed5f350e5cc (diff) |
s/NULL/nullptr
Diffstat (limited to 'src/core/lib/security/credentials/jwt')
-rw-r--r-- | src/core/lib/security/credentials/jwt/json_token.cc | 84 | ||||
-rw-r--r-- | src/core/lib/security/credentials/jwt/jwt_credentials.cc | 16 | ||||
-rw-r--r-- | src/core/lib/security/credentials/jwt/jwt_verifier.cc | 256 |
3 files changed, 178 insertions, 178 deletions
diff --git a/src/core/lib/security/credentials/jwt/json_token.cc b/src/core/lib/security/credentials/jwt/json_token.cc index e195ec7509..07807201ce 100644 --- a/src/core/lib/security/credentials/jwt/json_token.cc +++ b/src/core/lib/security/credentials/jwt/json_token.cc @@ -52,30 +52,30 @@ gpr_timespec grpc_max_auth_token_lifetime() { /* --- Override for testing. --- */ -static grpc_jwt_encode_and_sign_override g_jwt_encode_and_sign_override = NULL; +static grpc_jwt_encode_and_sign_override g_jwt_encode_and_sign_override = nullptr; /* --- grpc_auth_json_key. --- */ int grpc_auth_json_key_is_valid(const grpc_auth_json_key* json_key) { - return (json_key != NULL) && + return (json_key != nullptr) && strcmp(json_key->type, GRPC_AUTH_JSON_TYPE_INVALID); } grpc_auth_json_key grpc_auth_json_key_create_from_json(const grpc_json* json) { grpc_auth_json_key result; - BIO* bio = NULL; + BIO* bio = nullptr; const char* prop_value; int success = 0; memset(&result, 0, sizeof(grpc_auth_json_key)); result.type = GRPC_AUTH_JSON_TYPE_INVALID; - if (json == NULL) { + if (json == nullptr) { gpr_log(GPR_ERROR, "Invalid json."); goto end; } prop_value = grpc_json_get_string_property(json, "type"); - if (prop_value == NULL || + if (prop_value == nullptr || strcmp(prop_value, GRPC_AUTH_JSON_TYPE_SERVICE_ACCOUNT)) { goto end; } @@ -90,7 +90,7 @@ grpc_auth_json_key grpc_auth_json_key_create_from_json(const grpc_json* json) { } prop_value = grpc_json_get_string_property(json, "private_key"); - if (prop_value == NULL) { + if (prop_value == nullptr) { goto end; } bio = BIO_new(BIO_s_mem()); @@ -99,15 +99,15 @@ grpc_auth_json_key grpc_auth_json_key_create_from_json(const grpc_json* json) { gpr_log(GPR_ERROR, "Could not write into openssl BIO."); goto end; } - result.private_key = PEM_read_bio_RSAPrivateKey(bio, NULL, NULL, (void*)""); - if (result.private_key == NULL) { + result.private_key = PEM_read_bio_RSAPrivateKey(bio, nullptr, nullptr, (void*)""); + if (result.private_key == nullptr) { gpr_log(GPR_ERROR, "Could not deserialize private key."); goto end; } success = 1; end: - if (bio != NULL) BIO_free(bio); + if (bio != nullptr) BIO_free(bio); if (!success) grpc_auth_json_key_destruct(&result); return result; } @@ -117,29 +117,29 @@ grpc_auth_json_key grpc_auth_json_key_create_from_string( char* scratchpad = gpr_strdup(json_string); grpc_json* json = grpc_json_parse_string(scratchpad); grpc_auth_json_key result = grpc_auth_json_key_create_from_json(json); - if (json != NULL) grpc_json_destroy(json); + if (json != nullptr) grpc_json_destroy(json); gpr_free(scratchpad); return result; } void grpc_auth_json_key_destruct(grpc_auth_json_key* json_key) { - if (json_key == NULL) return; + if (json_key == nullptr) return; json_key->type = GRPC_AUTH_JSON_TYPE_INVALID; - if (json_key->client_id != NULL) { + if (json_key->client_id != nullptr) { gpr_free(json_key->client_id); - json_key->client_id = NULL; + json_key->client_id = nullptr; } - if (json_key->private_key_id != NULL) { + if (json_key->private_key_id != nullptr) { gpr_free(json_key->private_key_id); - json_key->private_key_id = NULL; + json_key->private_key_id = nullptr; } - if (json_key->client_email != NULL) { + if (json_key->client_email != nullptr) { gpr_free(json_key->client_email); - json_key->client_email = NULL; + json_key->client_email = nullptr; } - if (json_key->private_key != NULL) { + if (json_key->private_key != nullptr) { RSA_free(json_key->private_key); - json_key->private_key = NULL; + json_key->private_key = nullptr; } } @@ -159,11 +159,11 @@ static grpc_json* create_child(grpc_json* brother, grpc_json* parent, static char* encoded_jwt_header(const char* key_id, const char* algorithm) { grpc_json* json = grpc_json_create(GRPC_JSON_OBJECT); - grpc_json* child = NULL; - char* json_str = NULL; - char* result = NULL; + grpc_json* child = nullptr; + char* json_str = nullptr; + char* result = nullptr; - child = create_child(NULL, json, "alg", algorithm, GRPC_JSON_STRING); + child = create_child(nullptr, json, "alg", algorithm, GRPC_JSON_STRING); child = create_child(child, json, "typ", GRPC_JWT_TYPE, GRPC_JSON_STRING); create_child(child, json, "kid", key_id, GRPC_JSON_STRING); @@ -178,9 +178,9 @@ static char* encoded_jwt_claim(const grpc_auth_json_key* json_key, const char* audience, gpr_timespec token_lifetime, const char* scope) { grpc_json* json = grpc_json_create(GRPC_JSON_OBJECT); - grpc_json* child = NULL; - char* json_str = NULL; - char* result = NULL; + grpc_json* child = nullptr; + char* json_str = nullptr; + char* result = nullptr; gpr_timespec now = gpr_now(GPR_CLOCK_REALTIME); gpr_timespec expiration = gpr_time_add(now, token_lifetime); char now_str[GPR_LTOA_MIN_BUFSIZE]; @@ -193,8 +193,8 @@ static char* encoded_jwt_claim(const grpc_auth_json_key* json_key, int64_ttoa(expiration.tv_sec, expiration_str); child = - create_child(NULL, json, "iss", json_key->client_email, GRPC_JSON_STRING); - if (scope != NULL) { + create_child(nullptr, json, "iss", json_key->client_email, GRPC_JSON_STRING); + if (scope != nullptr) { child = create_child(child, json, "scope", scope, GRPC_JSON_STRING); } else { /* Unscoped JWTs need a sub field. */ @@ -237,7 +237,7 @@ const EVP_MD* openssl_digest_from_algorithm(const char* algorithm) { return EVP_sha256(); } else { gpr_log(GPR_ERROR, "Unknown algorithm %s.", algorithm); - return NULL; + return nullptr; } } @@ -245,19 +245,19 @@ char* compute_and_encode_signature(const grpc_auth_json_key* json_key, const char* signature_algorithm, const char* to_sign) { const EVP_MD* md = openssl_digest_from_algorithm(signature_algorithm); - EVP_MD_CTX* md_ctx = NULL; + EVP_MD_CTX* md_ctx = nullptr; EVP_PKEY* key = EVP_PKEY_new(); size_t sig_len = 0; - unsigned char* sig = NULL; - char* result = NULL; - if (md == NULL) return NULL; + unsigned char* sig = nullptr; + char* result = nullptr; + if (md == nullptr) return nullptr; md_ctx = EVP_MD_CTX_create(); - if (md_ctx == NULL) { + if (md_ctx == nullptr) { gpr_log(GPR_ERROR, "Could not create MD_CTX"); goto end; } EVP_PKEY_set1_RSA(key, json_key->private_key); - if (EVP_DigestSignInit(md_ctx, NULL, md, NULL, key) != 1) { + if (EVP_DigestSignInit(md_ctx, nullptr, md, nullptr, key) != 1) { gpr_log(GPR_ERROR, "DigestInit failed."); goto end; } @@ -265,7 +265,7 @@ char* compute_and_encode_signature(const grpc_auth_json_key* json_key, gpr_log(GPR_ERROR, "DigestUpdate failed."); goto end; } - if (EVP_DigestSignFinal(md_ctx, NULL, &sig_len) != 1) { + if (EVP_DigestSignFinal(md_ctx, nullptr, &sig_len) != 1) { gpr_log(GPR_ERROR, "DigestFinal (get signature length) failed."); goto end; } @@ -277,16 +277,16 @@ char* compute_and_encode_signature(const grpc_auth_json_key* json_key, result = grpc_base64_encode(sig, sig_len, 1, 0); end: - if (key != NULL) EVP_PKEY_free(key); - if (md_ctx != NULL) EVP_MD_CTX_destroy(md_ctx); - if (sig != NULL) gpr_free(sig); + if (key != nullptr) EVP_PKEY_free(key); + if (md_ctx != nullptr) EVP_MD_CTX_destroy(md_ctx); + if (sig != nullptr) gpr_free(sig); return result; } char* grpc_jwt_encode_and_sign(const grpc_auth_json_key* json_key, const char* audience, gpr_timespec token_lifetime, const char* scope) { - if (g_jwt_encode_and_sign_override != NULL) { + if (g_jwt_encode_and_sign_override != nullptr) { return g_jwt_encode_and_sign_override(json_key, audience, token_lifetime, scope); } else { @@ -295,9 +295,9 @@ char* grpc_jwt_encode_and_sign(const grpc_auth_json_key* json_key, encoded_jwt_header(json_key->private_key_id, sig_algo), encoded_jwt_claim(json_key, audience, token_lifetime, scope)); char* sig = compute_and_encode_signature(json_key, sig_algo, to_sign); - if (sig == NULL) { + if (sig == nullptr) { gpr_free(to_sign); - return NULL; + return nullptr; } return dot_concat_and_free_strings(to_sign, sig); } diff --git a/src/core/lib/security/credentials/jwt/jwt_credentials.cc b/src/core/lib/security/credentials/jwt/jwt_credentials.cc index e8baa7e053..d666e6b658 100644 --- a/src/core/lib/security/credentials/jwt/jwt_credentials.cc +++ b/src/core/lib/security/credentials/jwt/jwt_credentials.cc @@ -34,9 +34,9 @@ static void jwt_reset_cache(grpc_exec_ctx* exec_ctx, grpc_service_account_jwt_access_credentials* c) { GRPC_MDELEM_UNREF(exec_ctx, c->cached.jwt_md); c->cached.jwt_md = GRPC_MDNULL; - if (c->cached.service_url != NULL) { + if (c->cached.service_url != nullptr) { gpr_free(c->cached.service_url); - c->cached.service_url = NULL; + c->cached.service_url = nullptr; } c->cached.jwt_expiration = gpr_inf_past(GPR_CLOCK_REALTIME); } @@ -66,7 +66,7 @@ static bool jwt_get_request_metadata(grpc_exec_ctx* exec_ctx, grpc_mdelem jwt_md = GRPC_MDNULL; { gpr_mu_lock(&c->cache_mu); - if (c->cached.service_url != NULL && + if (c->cached.service_url != nullptr && strcmp(c->cached.service_url, context.service_url) == 0 && !GRPC_MDISNULL(c->cached.jwt_md) && (gpr_time_cmp(gpr_time_sub(c->cached.jwt_expiration, @@ -78,13 +78,13 @@ static bool jwt_get_request_metadata(grpc_exec_ctx* exec_ctx, } if (GRPC_MDISNULL(jwt_md)) { - char* jwt = NULL; + char* jwt = nullptr; /* Generate a new jwt. */ gpr_mu_lock(&c->cache_mu); jwt_reset_cache(exec_ctx, c); jwt = grpc_jwt_encode_and_sign(&c->key, context.service_url, - c->jwt_lifetime, NULL); - if (jwt != NULL) { + c->jwt_lifetime, nullptr); + if (jwt != nullptr) { char* md_value; gpr_asprintf(&md_value, "Bearer %s", jwt); gpr_free(jwt); @@ -126,7 +126,7 @@ grpc_service_account_jwt_access_credentials_create_from_auth_json_key( grpc_service_account_jwt_access_credentials* c; if (!grpc_auth_json_key_is_valid(&key)) { gpr_log(GPR_ERROR, "Invalid input for jwt credentials creation"); - return NULL; + return nullptr; } c = (grpc_service_account_jwt_access_credentials*)gpr_zalloc( sizeof(grpc_service_account_jwt_access_credentials)); @@ -185,7 +185,7 @@ grpc_call_credentials* grpc_service_account_jwt_access_credentials_create( (int)token_lifetime.clock_type, reserved); gpr_free(clean_json); } - GPR_ASSERT(reserved == NULL); + GPR_ASSERT(reserved == nullptr); grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; grpc_call_credentials* creds = grpc_service_account_jwt_access_credentials_create_from_auth_json_key( diff --git a/src/core/lib/security/credentials/jwt/jwt_verifier.cc b/src/core/lib/security/credentials/jwt/jwt_verifier.cc index 0fce5f5555..105178ff4c 100644 --- a/src/core/lib/security/credentials/jwt/jwt_verifier.cc +++ b/src/core/lib/security/credentials/jwt/jwt_verifier.cc @@ -70,7 +70,7 @@ static const EVP_MD* evp_md_from_alg(const char* alg) { } else if (strcmp(alg, "RS512") == 0) { return EVP_sha512(); } else { - return NULL; + return nullptr; } } @@ -82,11 +82,11 @@ static grpc_json* parse_json_part_from_jwt(grpc_exec_ctx* exec_ctx, *buffer = grpc_base64_decode_with_len(exec_ctx, str, len, 1); if (GRPC_SLICE_IS_EMPTY(*buffer)) { gpr_log(GPR_ERROR, "Invalid base64."); - return NULL; + return nullptr; } json = grpc_json_parse_string_with_len((char*)GRPC_SLICE_START_PTR(*buffer), GRPC_SLICE_LENGTH(*buffer)); - if (json == NULL) { + if (json == nullptr) { grpc_slice_unref_internal(exec_ctx, *buffer); gpr_log(GPR_ERROR, "JSON parsing error."); } @@ -97,7 +97,7 @@ static const char* validate_string_field(const grpc_json* json, const char* key) { if (json->type != GRPC_JSON_STRING) { gpr_log(GPR_ERROR, "Invalid %s field [%s]", key, json->value); - return NULL; + return nullptr; } return json->value; } @@ -109,7 +109,7 @@ static gpr_timespec validate_time_field(const grpc_json* json, gpr_log(GPR_ERROR, "Invalid %s field [%s]", key, json->value); return result; } - result.tv_sec = strtol(json->value, NULL, 10); + result.tv_sec = strtol(json->value, nullptr, 10); return result; } @@ -134,27 +134,27 @@ static jose_header* jose_header_from_json(grpc_exec_ctx* exec_ctx, grpc_json* cur; jose_header* h = (jose_header*)gpr_zalloc(sizeof(jose_header)); h->buffer = buffer; - for (cur = json->child; cur != NULL; cur = cur->next) { + for (cur = json->child; cur != nullptr; cur = cur->next) { if (strcmp(cur->key, "alg") == 0) { /* We only support RSA-1.5 signatures for now. Beware of this if we add HMAC support: https://auth0.com/blog/2015/03/31/critical-vulnerabilities-in-json-web-token-libraries/ */ if (cur->type != GRPC_JSON_STRING || strncmp(cur->value, "RS", 2) || - evp_md_from_alg(cur->value) == NULL) { + evp_md_from_alg(cur->value) == nullptr) { gpr_log(GPR_ERROR, "Invalid alg field [%s]", cur->value); goto error; } h->alg = cur->value; } else if (strcmp(cur->key, "typ") == 0) { h->typ = validate_string_field(cur, "typ"); - if (h->typ == NULL) goto error; + if (h->typ == nullptr) goto error; } else if (strcmp(cur->key, "kid") == 0) { h->kid = validate_string_field(cur, "kid"); - if (h->kid == NULL) goto error; + if (h->kid == nullptr) goto error; } } - if (h->alg == NULL) { + if (h->alg == nullptr) { gpr_log(GPR_ERROR, "Missing alg field."); goto error; } @@ -165,7 +165,7 @@ static jose_header* jose_header_from_json(grpc_exec_ctx* exec_ctx, error: grpc_json_destroy(json); jose_header_destroy(exec_ctx, h); - return NULL; + return nullptr; } /* --- JWT claims. see http://tools.ietf.org/html/rfc7519#section-4.1 */ @@ -191,42 +191,42 @@ void grpc_jwt_claims_destroy(grpc_exec_ctx* exec_ctx, grpc_jwt_claims* claims) { } const grpc_json* grpc_jwt_claims_json(const grpc_jwt_claims* claims) { - if (claims == NULL) return NULL; + if (claims == nullptr) return nullptr; return claims->json; } const char* grpc_jwt_claims_subject(const grpc_jwt_claims* claims) { - if (claims == NULL) return NULL; + if (claims == nullptr) return nullptr; return claims->sub; } const char* grpc_jwt_claims_issuer(const grpc_jwt_claims* claims) { - if (claims == NULL) return NULL; + if (claims == nullptr) return nullptr; return claims->iss; } const char* grpc_jwt_claims_id(const grpc_jwt_claims* claims) { - if (claims == NULL) return NULL; + if (claims == nullptr) return nullptr; return claims->jti; } const char* grpc_jwt_claims_audience(const grpc_jwt_claims* claims) { - if (claims == NULL) return NULL; + if (claims == nullptr) return nullptr; return claims->aud; } gpr_timespec grpc_jwt_claims_issued_at(const grpc_jwt_claims* claims) { - if (claims == NULL) return gpr_inf_past(GPR_CLOCK_REALTIME); + if (claims == nullptr) return gpr_inf_past(GPR_CLOCK_REALTIME); return claims->iat; } gpr_timespec grpc_jwt_claims_expires_at(const grpc_jwt_claims* claims) { - if (claims == NULL) return gpr_inf_future(GPR_CLOCK_REALTIME); + if (claims == nullptr) return gpr_inf_future(GPR_CLOCK_REALTIME); return claims->exp; } gpr_timespec grpc_jwt_claims_not_before(const grpc_jwt_claims* claims) { - if (claims == NULL) return gpr_inf_past(GPR_CLOCK_REALTIME); + if (claims == nullptr) return gpr_inf_past(GPR_CLOCK_REALTIME); return claims->nbf; } @@ -244,19 +244,19 @@ grpc_jwt_claims* grpc_jwt_claims_from_json(grpc_exec_ctx* exec_ctx, claims->exp = gpr_inf_future(GPR_CLOCK_REALTIME); /* Per the spec, all fields are optional. */ - for (cur = json->child; cur != NULL; cur = cur->next) { + for (cur = json->child; cur != nullptr; cur = cur->next) { if (strcmp(cur->key, "sub") == 0) { claims->sub = validate_string_field(cur, "sub"); - if (claims->sub == NULL) goto error; + if (claims->sub == nullptr) goto error; } else if (strcmp(cur->key, "iss") == 0) { claims->iss = validate_string_field(cur, "iss"); - if (claims->iss == NULL) goto error; + if (claims->iss == nullptr) goto error; } else if (strcmp(cur->key, "aud") == 0) { claims->aud = validate_string_field(cur, "aud"); - if (claims->aud == NULL) goto error; + if (claims->aud == nullptr) goto error; } else if (strcmp(cur->key, "jti") == 0) { claims->jti = validate_string_field(cur, "jti"); - if (claims->jti == NULL) goto error; + if (claims->jti == nullptr) goto error; } else if (strcmp(cur->key, "iat") == 0) { claims->iat = validate_time_field(cur, "iat"); if (gpr_time_cmp(claims->iat, gpr_time_0(GPR_CLOCK_REALTIME)) == 0) @@ -275,7 +275,7 @@ grpc_jwt_claims* grpc_jwt_claims_from_json(grpc_exec_ctx* exec_ctx, error: grpc_jwt_claims_destroy(exec_ctx, claims); - return NULL; + return nullptr; } grpc_jwt_verifier_status grpc_jwt_claims_check(const grpc_jwt_claims* claims, @@ -283,7 +283,7 @@ grpc_jwt_verifier_status grpc_jwt_claims_check(const grpc_jwt_claims* claims, gpr_timespec skewed_now; int audience_ok; - GPR_ASSERT(claims != NULL); + GPR_ASSERT(claims != nullptr); skewed_now = gpr_time_add(gpr_now(GPR_CLOCK_REALTIME), grpc_jwt_verifier_clock_skew); @@ -301,23 +301,23 @@ grpc_jwt_verifier_status grpc_jwt_claims_check(const grpc_jwt_claims* claims, /* This should be probably up to the upper layer to decide but let's harcode the 99% use case here for email issuers, where the JWT must be self issued. */ - if (grpc_jwt_issuer_email_domain(claims->iss) != NULL && - claims->sub != NULL && strcmp(claims->iss, claims->sub) != 0) { + if (grpc_jwt_issuer_email_domain(claims->iss) != nullptr && + claims->sub != nullptr && strcmp(claims->iss, claims->sub) != 0) { gpr_log(GPR_ERROR, "Email issuer (%s) cannot assert another subject (%s) than itself.", claims->iss, claims->sub); return GRPC_JWT_VERIFIER_BAD_SUBJECT; } - if (audience == NULL) { - audience_ok = claims->aud == NULL; + if (audience == nullptr) { + audience_ok = claims->aud == nullptr; } else { - audience_ok = claims->aud != NULL && strcmp(audience, claims->aud) == 0; + audience_ok = claims->aud != nullptr && strcmp(audience, claims->aud) == 0; } if (!audience_ok) { gpr_log(GPR_ERROR, "Audience mismatch: expected %s and found %s.", - audience == NULL ? "NULL" : audience, - claims->aud == NULL ? "NULL" : claims->aud); + audience == nullptr ? "NULL" : audience, + claims->aud == nullptr ? "NULL" : claims->aud); return GRPC_JWT_VERIFIER_BAD_AUDIENCE; } return GRPC_JWT_VERIFIER_OK; @@ -366,8 +366,8 @@ static verifier_cb_ctx* verifier_cb_ctx_create( } void verifier_cb_ctx_destroy(grpc_exec_ctx* exec_ctx, verifier_cb_ctx* ctx) { - if (ctx->audience != NULL) gpr_free(ctx->audience); - if (ctx->claims != NULL) grpc_jwt_claims_destroy(exec_ctx, ctx->claims); + if (ctx->audience != nullptr) gpr_free(ctx->audience); + if (ctx->claims != nullptr) grpc_jwt_claims_destroy(exec_ctx, ctx->claims); grpc_slice_unref_internal(exec_ctx, ctx->signature); grpc_slice_unref_internal(exec_ctx, ctx->signed_data); jose_header_destroy(exec_ctx, ctx->header); @@ -399,20 +399,20 @@ struct grpc_jwt_verifier { }; static grpc_json* json_from_http(const grpc_httpcli_response* response) { - grpc_json* json = NULL; + grpc_json* json = nullptr; - if (response == NULL) { + if (response == nullptr) { gpr_log(GPR_ERROR, "HTTP response is NULL."); - return NULL; + return nullptr; } if (response->status != 200) { gpr_log(GPR_ERROR, "Call to http server failed with error %d.", response->status); - return NULL; + return nullptr; } json = grpc_json_parse_string_with_len(response->body, response->body_length); - if (json == NULL) { + if (json == nullptr) { gpr_log(GPR_ERROR, "Invalid JSON found in response."); } return json; @@ -421,26 +421,26 @@ static grpc_json* json_from_http(const grpc_httpcli_response* response) { static const grpc_json* find_property_by_name(const grpc_json* json, const char* name) { const grpc_json* cur; - for (cur = json->child; cur != NULL; cur = cur->next) { + for (cur = json->child; cur != nullptr; cur = cur->next) { if (strcmp(cur->key, name) == 0) return cur; } - return NULL; + return nullptr; } static EVP_PKEY* extract_pkey_from_x509(const char* x509_str) { - X509* x509 = NULL; - EVP_PKEY* result = NULL; + X509* x509 = nullptr; + EVP_PKEY* result = nullptr; BIO* bio = BIO_new(BIO_s_mem()); size_t len = strlen(x509_str); GPR_ASSERT(len < INT_MAX); BIO_write(bio, x509_str, (int)len); - x509 = PEM_read_bio_X509(bio, NULL, NULL, NULL); - if (x509 == NULL) { + x509 = PEM_read_bio_X509(bio, nullptr, nullptr, nullptr); + if (x509 == nullptr) { gpr_log(GPR_ERROR, "Unable to parse x509 cert."); goto end; } result = X509_get_pubkey(x509); - if (result == NULL) { + if (result == nullptr) { gpr_log(GPR_ERROR, "Cannot find public key in X509 cert."); } @@ -451,17 +451,17 @@ end: } static BIGNUM* bignum_from_base64(grpc_exec_ctx* exec_ctx, const char* b64) { - BIGNUM* result = NULL; + BIGNUM* result = nullptr; grpc_slice bin; - if (b64 == NULL) return NULL; + if (b64 == nullptr) return nullptr; bin = grpc_base64_decode(exec_ctx, b64, 1); if (GRPC_SLICE_IS_EMPTY(bin)) { gpr_log(GPR_ERROR, "Invalid base64 for big num."); - return NULL; + return nullptr; } result = BN_bin2bn(GRPC_SLICE_START_PTR(bin), - TSI_SIZE_AS_SIZE(GRPC_SLICE_LENGTH(bin)), NULL); + TSI_SIZE_AS_SIZE(GRPC_SLICE_LENGTH(bin)), nullptr); grpc_slice_unref_internal(exec_ctx, bin); return result; } @@ -474,19 +474,19 @@ static int RSA_set0_key(RSA* r, BIGNUM* n, BIGNUM* e, BIGNUM* d) { * parameters MUST be non-NULL for n and e. d may be * left NULL (in case only the public key is used). */ - if ((r->n == NULL && n == NULL) || (r->e == NULL && e == NULL)) { + if ((r->n == nullptr && n == nullptr) || (r->e == nullptr && e == nullptr)) { return 0; } - if (n != NULL) { + if (n != nullptr) { BN_free(r->n); r->n = n; } - if (e != NULL) { + if (e != nullptr) { BN_free(r->e); r->e = e; } - if (d != NULL) { + if (d != nullptr) { BN_free(r->d); r->d = d; } @@ -498,43 +498,43 @@ static int RSA_set0_key(RSA* r, BIGNUM* n, BIGNUM* e, BIGNUM* d) { static EVP_PKEY* pkey_from_jwk(grpc_exec_ctx* exec_ctx, const grpc_json* json, const char* kty) { const grpc_json* key_prop; - RSA* rsa = NULL; - EVP_PKEY* result = NULL; - BIGNUM* tmp_n = NULL; - BIGNUM* tmp_e = NULL; + RSA* rsa = nullptr; + EVP_PKEY* result = nullptr; + BIGNUM* tmp_n = nullptr; + BIGNUM* tmp_e = nullptr; - GPR_ASSERT(kty != NULL && json != NULL); + GPR_ASSERT(kty != nullptr && json != nullptr); if (strcmp(kty, "RSA") != 0) { gpr_log(GPR_ERROR, "Unsupported key type %s.", kty); goto end; } rsa = RSA_new(); - if (rsa == NULL) { + if (rsa == nullptr) { gpr_log(GPR_ERROR, "Could not create rsa key."); goto end; } - for (key_prop = json->child; key_prop != NULL; key_prop = key_prop->next) { + for (key_prop = json->child; key_prop != nullptr; key_prop = key_prop->next) { if (strcmp(key_prop->key, "n") == 0) { tmp_n = bignum_from_base64(exec_ctx, validate_string_field(key_prop, "n")); - if (tmp_n == NULL) goto end; + if (tmp_n == nullptr) goto end; } else if (strcmp(key_prop->key, "e") == 0) { tmp_e = bignum_from_base64(exec_ctx, validate_string_field(key_prop, "e")); - if (tmp_e == NULL) goto end; + if (tmp_e == nullptr) goto end; } } - if (tmp_e == NULL || tmp_n == NULL) { + if (tmp_e == nullptr || tmp_n == nullptr) { gpr_log(GPR_ERROR, "Missing RSA public key field."); goto end; } - if (!RSA_set0_key(rsa, tmp_n, tmp_e, NULL)) { + if (!RSA_set0_key(rsa, tmp_n, tmp_e, nullptr)) { gpr_log(GPR_ERROR, "Cannot set RSA key from inputs."); goto end; } /* RSA_set0_key takes ownership on success. */ - tmp_n = NULL; - tmp_e = NULL; + tmp_n = nullptr; + tmp_e = nullptr; result = EVP_PKEY_new(); EVP_PKEY_set1_RSA(result, rsa); /* uprefs rsa. */ @@ -554,29 +554,29 @@ static EVP_PKEY* find_verification_key(grpc_exec_ctx* exec_ctx, /* Try to parse the json as a JWK set: https://tools.ietf.org/html/rfc7517#section-5. */ jwk_keys = find_property_by_name(json, "keys"); - if (jwk_keys == NULL) { + if (jwk_keys == nullptr) { /* Use the google proprietary format which is: { <kid1>: <x5091>, <kid2>: <x5092>, ... } */ const grpc_json* cur = find_property_by_name(json, header_kid); - if (cur == NULL) return NULL; + if (cur == nullptr) return nullptr; return extract_pkey_from_x509(cur->value); } if (jwk_keys->type != GRPC_JSON_ARRAY) { gpr_log(GPR_ERROR, "Unexpected value type of keys property in jwks key set."); - return NULL; + return nullptr; } /* Key format is specified in: https://tools.ietf.org/html/rfc7518#section-6. */ - for (jkey = jwk_keys->child; jkey != NULL; jkey = jkey->next) { + for (jkey = jwk_keys->child; jkey != nullptr; jkey = jkey->next) { grpc_json* key_prop; - const char* alg = NULL; - const char* kid = NULL; - const char* kty = NULL; + const char* alg = nullptr; + const char* kid = nullptr; + const char* kty = nullptr; if (jkey->type != GRPC_JSON_OBJECT) continue; - for (key_prop = jkey->child; key_prop != NULL; key_prop = key_prop->next) { + for (key_prop = jkey->child; key_prop != nullptr; key_prop = key_prop->next) { if (strcmp(key_prop->key, "alg") == 0 && key_prop->type == GRPC_JSON_STRING) { alg = key_prop->value; @@ -588,7 +588,7 @@ static EVP_PKEY* find_verification_key(grpc_exec_ctx* exec_ctx, kty = key_prop->value; } } - if (alg != NULL && kid != NULL && kty != NULL && + if (alg != nullptr && kid != nullptr && kty != nullptr && strcmp(kid, header_kid) == 0 && strcmp(alg, header_alg) == 0) { return pkey_from_jwk(exec_ctx, jkey, kty); } @@ -596,7 +596,7 @@ static EVP_PKEY* find_verification_key(grpc_exec_ctx* exec_ctx, gpr_log(GPR_ERROR, "Could not find matching key in key set for kid=%s and alg=%s", header_kid, header_alg); - return NULL; + return nullptr; } static int verify_jwt_signature(EVP_PKEY* key, const char* alg, @@ -605,12 +605,12 @@ static int verify_jwt_signature(EVP_PKEY* key, const char* alg, const EVP_MD* md = evp_md_from_alg(alg); int result = 0; - GPR_ASSERT(md != NULL); /* Checked before. */ - if (md_ctx == NULL) { + GPR_ASSERT(md != nullptr); /* Checked before. */ + if (md_ctx == nullptr) { gpr_log(GPR_ERROR, "Could not create EVP_MD_CTX."); goto end; } - if (EVP_DigestVerifyInit(md_ctx, NULL, md, NULL, key) != 1) { + if (EVP_DigestVerifyInit(md_ctx, nullptr, md, nullptr, key) != 1) { gpr_log(GPR_ERROR, "EVP_DigestVerifyInit failed."); goto end; } @@ -635,17 +635,17 @@ static void on_keys_retrieved(grpc_exec_ctx* exec_ctx, void* user_data, grpc_error* error) { verifier_cb_ctx* ctx = (verifier_cb_ctx*)user_data; grpc_json* json = json_from_http(&ctx->responses[HTTP_RESPONSE_KEYS]); - EVP_PKEY* verification_key = NULL; + EVP_PKEY* verification_key = nullptr; grpc_jwt_verifier_status status = GRPC_JWT_VERIFIER_GENERIC_ERROR; - grpc_jwt_claims* claims = NULL; + grpc_jwt_claims* claims = nullptr; - if (json == NULL) { + if (json == nullptr) { status = GRPC_JWT_VERIFIER_KEY_RETRIEVAL_ERROR; goto end; } verification_key = find_verification_key(exec_ctx, json, ctx->header->alg, ctx->header->kid); - if (verification_key == NULL) { + if (verification_key == nullptr) { gpr_log(GPR_ERROR, "Could not find verification key with kid %s.", ctx->header->kid); status = GRPC_JWT_VERIFIER_KEY_RETRIEVAL_ERROR; @@ -662,11 +662,11 @@ static void on_keys_retrieved(grpc_exec_ctx* exec_ctx, void* user_data, if (status == GRPC_JWT_VERIFIER_OK) { /* Pass ownership. */ claims = ctx->claims; - ctx->claims = NULL; + ctx->claims = nullptr; } end: - if (json != NULL) grpc_json_destroy(json); + if (json != nullptr) grpc_json_destroy(json); EVP_PKEY_free(verification_key); ctx->user_cb(exec_ctx, ctx->user_data, status, claims); verifier_cb_ctx_destroy(exec_ctx, ctx); @@ -680,17 +680,17 @@ static void on_openid_config_retrieved(grpc_exec_ctx* exec_ctx, void* user_data, grpc_json* json = json_from_http(response); grpc_httpcli_request req; const char* jwks_uri; - grpc_resource_quota* resource_quota = NULL; + grpc_resource_quota* resource_quota = nullptr; /* TODO(jboeuf): Cache the jwks_uri in order to avoid this hop next time. */ - if (json == NULL) goto error; + if (json == nullptr) goto error; cur = find_property_by_name(json, "jwks_uri"); - if (cur == NULL) { + if (cur == nullptr) { gpr_log(GPR_ERROR, "Could not find jwks_uri in openid config."); goto error; } jwks_uri = validate_string_field(cur, "jwks_uri"); - if (jwks_uri == NULL) goto error; + if (jwks_uri == nullptr) goto error; if (strstr(jwks_uri, "https://") != jwks_uri) { gpr_log(GPR_ERROR, "Invalid non https jwks_uri: %s.", jwks_uri); goto error; @@ -699,7 +699,7 @@ static void on_openid_config_retrieved(grpc_exec_ctx* exec_ctx, void* user_data, req.handshaker = &grpc_httpcli_ssl; req.host = gpr_strdup(jwks_uri); req.http.path = (char*)strchr(jwks_uri, '/'); - if (req.http.path == NULL) { + if (req.http.path == nullptr) { req.http.path = (char*)""; } else { *(req.host + (req.http.path - jwks_uri)) = '\0'; @@ -720,29 +720,29 @@ static void on_openid_config_retrieved(grpc_exec_ctx* exec_ctx, void* user_data, return; error: - if (json != NULL) grpc_json_destroy(json); + if (json != nullptr) grpc_json_destroy(json); ctx->user_cb(exec_ctx, ctx->user_data, GRPC_JWT_VERIFIER_KEY_RETRIEVAL_ERROR, - NULL); + nullptr); verifier_cb_ctx_destroy(exec_ctx, ctx); } static email_key_mapping* verifier_get_mapping(grpc_jwt_verifier* v, const char* email_domain) { size_t i; - if (v->mappings == NULL) return NULL; + if (v->mappings == nullptr) return nullptr; for (i = 0; i < v->num_mappings; i++) { if (strcmp(email_domain, v->mappings[i].email_domain) == 0) { return &v->mappings[i]; } } - return NULL; + return nullptr; } static void verifier_put_mapping(grpc_jwt_verifier* v, const char* email_domain, const char* key_url_prefix) { email_key_mapping* mapping = verifier_get_mapping(v, email_domain); GPR_ASSERT(v->num_mappings < v->allocated_mappings); - if (mapping != NULL) { + if (mapping != nullptr) { gpr_free(mapping->key_url_prefix); mapping->key_url_prefix = gpr_strdup(key_url_prefix); return; @@ -757,16 +757,16 @@ static void verifier_put_mapping(grpc_jwt_verifier* v, const char* email_domain, enough for now... */ const char* grpc_jwt_issuer_email_domain(const char* issuer) { const char* at_sign = strchr(issuer, '@'); - if (at_sign == NULL) return NULL; + if (at_sign == nullptr) return nullptr; const char* email_domain = at_sign + 1; - if (*email_domain == '\0') return NULL; + if (*email_domain == '\0') return nullptr; const char* dot = strrchr(email_domain, '.'); - if (dot == NULL || dot == email_domain) return email_domain; + if (dot == nullptr || dot == email_domain) return email_domain; GPR_ASSERT(dot > email_domain); /* There may be a subdomain, we just want the domain. */ dot = (const char*)gpr_memrchr((void*)email_domain, '.', (size_t)(dot - email_domain)); - if (dot == NULL) return email_domain; + if (dot == nullptr) return email_domain; return dot + 1; } @@ -775,21 +775,21 @@ static void retrieve_key_and_verify(grpc_exec_ctx* exec_ctx, verifier_cb_ctx* ctx) { const char* email_domain; grpc_closure* http_cb; - char* path_prefix = NULL; + char* path_prefix = nullptr; const char* iss; grpc_httpcli_request req; - grpc_resource_quota* resource_quota = NULL; + grpc_resource_quota* resource_quota = nullptr; memset(&req, 0, sizeof(grpc_httpcli_request)); req.handshaker = &grpc_httpcli_ssl; http_response_index rsp_idx; - GPR_ASSERT(ctx != NULL && ctx->header != NULL && ctx->claims != NULL); + GPR_ASSERT(ctx != nullptr && ctx->header != nullptr && ctx->claims != nullptr); iss = ctx->claims->iss; - if (ctx->header->kid == NULL) { + if (ctx->header->kid == nullptr) { gpr_log(GPR_ERROR, "Missing kid in jose header."); goto error; } - if (iss == NULL) { + if (iss == nullptr) { gpr_log(GPR_ERROR, "Missing iss in claims."); goto error; } @@ -800,17 +800,17 @@ static void retrieve_key_and_verify(grpc_exec_ctx* exec_ctx, so we will rely instead on email/url mappings if we detect such an issuer. Part 4, on the other hand is implemented by both google and salesforce. */ email_domain = grpc_jwt_issuer_email_domain(iss); - if (email_domain != NULL) { + if (email_domain != nullptr) { email_key_mapping* mapping; - GPR_ASSERT(ctx->verifier != NULL); + GPR_ASSERT(ctx->verifier != nullptr); mapping = verifier_get_mapping(ctx->verifier, email_domain); - if (mapping == NULL) { + if (mapping == nullptr) { gpr_log(GPR_ERROR, "Missing mapping for issuer email."); goto error; } req.host = gpr_strdup(mapping->key_url_prefix); path_prefix = strchr(req.host, '/'); - if (path_prefix == NULL) { + if (path_prefix == nullptr) { gpr_asprintf(&req.http.path, "/%s", iss); } else { *(path_prefix++) = '\0'; @@ -822,7 +822,7 @@ static void retrieve_key_and_verify(grpc_exec_ctx* exec_ctx, } else { req.host = gpr_strdup(strstr(iss, "https://") == iss ? iss + 8 : iss); path_prefix = strchr(req.host, '/'); - if (path_prefix == NULL) { + if (path_prefix == nullptr) { req.http.path = gpr_strdup(GRPC_OPENID_CONFIG_URL_SUFFIX); } else { *(path_prefix++) = 0; @@ -849,7 +849,7 @@ static void retrieve_key_and_verify(grpc_exec_ctx* exec_ctx, error: ctx->user_cb(exec_ctx, ctx->user_data, GRPC_JWT_VERIFIER_KEY_RETRIEVAL_ERROR, - NULL); + nullptr); verifier_cb_ctx_destroy(exec_ctx, ctx); } @@ -859,33 +859,33 @@ void grpc_jwt_verifier_verify(grpc_exec_ctx* exec_ctx, const char* audience, grpc_jwt_verification_done_cb cb, void* user_data) { - const char* dot = NULL; + const char* dot = nullptr; grpc_json* json; - jose_header* header = NULL; - grpc_jwt_claims* claims = NULL; + jose_header* header = nullptr; + grpc_jwt_claims* claims = nullptr; grpc_slice header_buffer; grpc_slice claims_buffer; grpc_slice signature; size_t signed_jwt_len; const char* cur = jwt; - GPR_ASSERT(verifier != NULL && jwt != NULL && audience != NULL && cb != NULL); + GPR_ASSERT(verifier != nullptr && jwt != nullptr && audience != nullptr && cb != nullptr); dot = strchr(cur, '.'); - if (dot == NULL) goto error; + if (dot == nullptr) goto error; json = parse_json_part_from_jwt(exec_ctx, cur, (size_t)(dot - cur), &header_buffer); - if (json == NULL) goto error; + if (json == nullptr) goto error; header = jose_header_from_json(exec_ctx, json, header_buffer); - if (header == NULL) goto error; + if (header == nullptr) goto error; cur = dot + 1; dot = strchr(cur, '.'); - if (dot == NULL) goto error; + if (dot == nullptr) goto error; json = parse_json_part_from_jwt(exec_ctx, cur, (size_t)(dot - cur), &claims_buffer); - if (json == NULL) goto error; + if (json == nullptr) goto error; claims = grpc_jwt_claims_from_json(exec_ctx, json, claims_buffer); - if (claims == NULL) goto error; + if (claims == nullptr) goto error; signed_jwt_len = (size_t)(dot - jwt); cur = dot + 1; @@ -898,9 +898,9 @@ void grpc_jwt_verifier_verify(grpc_exec_ctx* exec_ctx, return; error: - if (header != NULL) jose_header_destroy(exec_ctx, header); - if (claims != NULL) grpc_jwt_claims_destroy(exec_ctx, claims); - cb(exec_ctx, user_data, GRPC_JWT_VERIFIER_BAD_FORMAT, NULL); + if (header != nullptr) jose_header_destroy(exec_ctx, header); + if (claims != nullptr) grpc_jwt_claims_destroy(exec_ctx, claims); + cb(exec_ctx, user_data, GRPC_JWT_VERIFIER_BAD_FORMAT, nullptr); } grpc_jwt_verifier* grpc_jwt_verifier_create( @@ -917,7 +917,7 @@ grpc_jwt_verifier* grpc_jwt_verifier_create( verifier_put_mapping(v, GRPC_GOOGLE_SERVICE_ACCOUNTS_EMAIL_DOMAIN, GRPC_GOOGLE_SERVICE_ACCOUNTS_KEY_URL_PREFIX); /* User-Provided mappings. */ - if (mappings != NULL) { + if (mappings != nullptr) { size_t i; for (i = 0; i < num_mappings; i++) { verifier_put_mapping(v, mappings[i].email_domain, @@ -929,9 +929,9 @@ grpc_jwt_verifier* grpc_jwt_verifier_create( void grpc_jwt_verifier_destroy(grpc_exec_ctx* exec_ctx, grpc_jwt_verifier* v) { size_t i; - if (v == NULL) return; + if (v == nullptr) return; grpc_httpcli_context_destroy(exec_ctx, &v->http_ctx); - if (v->mappings != NULL) { + if (v->mappings != nullptr) { for (i = 0; i < v->num_mappings; i++) { gpr_free(v->mappings[i].email_domain); gpr_free(v->mappings[i].key_url_prefix); |