chungen0126 commented on code in PR #11061: URL: https://github.com/apache/ozone/pull/11061#discussion_r3891696053
########## hadoop-ozone/s3gateway/src/main/java/org/apache/hadoop/ozone/s3/signature/ChunksValidator.java: ########## @@ -0,0 +1,124 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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 org.apache.hadoop.ozone.s3.signature; + +import static org.apache.hadoop.ozone.s3.exception.S3ErrorTable.SIGNATURE_DOES_NOT_MATCH; +import static org.apache.hadoop.ozone.s3.exception.S3ErrorTable.newError; + +import java.nio.charset.StandardCharsets; +import java.security.InvalidKeyException; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; +import java.util.Locale; +import javax.crypto.Mac; +import javax.crypto.spec.SecretKeySpec; +import org.apache.hadoop.ozone.s3.exception.OS3Exception; +import org.apache.kerby.util.Hex; + +/** + * Verifies the per-chunk signatures of a SigV4 chunked upload + * ({@code STREAMING-AWS4-HMAC-SHA256-PAYLOAD}). + * <p> + * Each chunk signature is {@code hex(HMAC-SHA256(signingKey, stringToSign))}, + * where the string-to-sign is: + * <pre> + * AWS4-HMAC-SHA256-PAYLOAD\n + * <date-time>\n + * <credential-scope>\n + * <previous-signature>\n + * <SHA-256("")>\n + * <SHA-256(chunk-payload)> + * </pre> + * The signatures are chained: the first chunk uses the request (seed) signature + * as the previous signature, and each subsequent chunk uses the previous + * chunk's computed signature. The signing key is the SigV4 signing key derived + * from the caller's secret; it is provided by the caller so that the S3 Gateway + * does not have to handle the secret directly (see HDDS-15140). + * + * @see <a href="https://docs.aws.amazon.com/AmazonS3/latest/API/sigv4-streaming.html"> + * Signature Calculation: Transfer Payload in Multiple Chunks</a> + */ +public class ChunksValidator { + + private static final String CHUNK_STRING_TO_SIGN_ALGORITHM = + "AWS4-HMAC-SHA256-PAYLOAD"; + private static final String HMAC_SHA256 = "HmacSHA256"; + private static final String NEWLINE = "\n"; + + /** SHA-256 hex of the empty string (the hashed empty headers slot). */ + private static final String EMPTY_STRING_SHA256 = + "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"; + + private static final ThreadLocal<Mac> HMAC = ThreadLocal.withInitial(() -> { + try { + return Mac.getInstance(HMAC_SHA256); + } catch (NoSuchAlgorithmException e) { + throw new IllegalStateException(HMAC_SHA256 + " not available", e); + } + }); + + private final byte[] signingKey; + private final String dateTime; + private final String credentialScope; + private String previousSignature; + + public ChunksValidator(byte[] signingKey, String dateTime, + String credentialScope, String seedSignature) { + this.signingKey = signingKey.clone(); + this.dateTime = dateTime; + this.credentialScope = credentialScope; + this.previousSignature = seedSignature; + } + + /** + * Verify one chunk and advance the signature chain. + * + * @param chunkSignature the signature parsed from the chunk header line + * @param payloadSha256Hex hex SHA-256 of the chunk payload + * @throws OS3Exception if the computed signature does not match + */ + public void validateChunk(String chunkSignature, String payloadSha256Hex) + throws OS3Exception { + String stringToSign = String.join(NEWLINE, + CHUNK_STRING_TO_SIGN_ALGORITHM, dateTime, credentialScope, + previousSignature, EMPTY_STRING_SHA256, payloadSha256Hex); + String expected = hex(hmacSha256(signingKey, stringToSign)); + String normalizedSignature = chunkSignature == null ? null : chunkSignature.toLowerCase(Locale.ROOT); + // Constant-time comparison to avoid leaking the signature via timing. + if (normalizedSignature == null || !MessageDigest.isEqual( + expected.getBytes(StandardCharsets.UTF_8), + normalizedSignature.getBytes(StandardCharsets.UTF_8))) { + throw newError(SIGNATURE_DOES_NOT_MATCH, "chunk-signature"); Review Comment: The current implementation converts the payload to a String, forms the stringToSign, converts it back to a byte[] for the HMAC operation, converts the resulting HMAC output back to a String (hex), and then converts it to a byte[] again for comparison. This approach introduces unnecessary conversions (two encodes and one decode). We should ideally handle everything directly using byte[] to improve efficiency. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
