Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: chunk signing #47

Merged
merged 2 commits into from
Mar 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/common/src/aws/sdk/kotlin/crt/auth/signing/AwsSigner.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@ public expect object AwsSigner {
public suspend fun signRequest(request: HttpRequest, config: AwsSigningConfig): HttpRequest

public suspend fun sign(request: HttpRequest, config: AwsSigningConfig): AwsSigningResult

public suspend fun signChunk(chunkBody: ByteArray, prevSignature: ByteArray, config: AwsSigningConfig): AwsSigningResult
}
20 changes: 19 additions & 1 deletion src/jvm/src/aws/sdk/kotlin/crt/auth/signing/AwsSignerJVM.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ package aws.sdk.kotlin.crt.auth.signing

import aws.sdk.kotlin.crt.asyncCrtJniCall
import aws.sdk.kotlin.crt.auth.credentials.JniCredentialsProvider
import aws.sdk.kotlin.crt.http.HttpRequest
import aws.sdk.kotlin.crt.http.*
import aws.sdk.kotlin.crt.http.from
import aws.sdk.kotlin.crt.http.into
import kotlinx.coroutines.future.await
Expand Down Expand Up @@ -50,6 +50,24 @@ public actual object AwsSigner {
AwsSigningResult(signedRequest, jniResult.signature)
}
}

/**
* Signs a body chunk according to the supplied signing configuration
* @param chunkBody the chunk payload to sign
* @param prevSignature the signature of the previous component of the request (either the initial request itself
* for the first chunk or the previous chunk otherwise)
* @param config signing configuration
* @return signing result, which provides access to all signing-related result properties
*/
public actual suspend fun signChunk(chunkBody: ByteArray, prevSignature: ByteArray, config: AwsSigningConfig): AwsSigningResult {
val ktStream = HttpRequestBodyStream.fromByteArray(chunkBody)
val bodyStream = JniRequestBodyStream(ktStream)
return asyncCrtJniCall {
val signFuture = AwsSignerJni.sign(bodyStream, prevSignature, config.into())
val jniResult = signFuture.await()
AwsSigningResult(null, jniResult.signature)
}
}
}

private fun AwsSigningConfig.into(): AwsSigningConfigJni {
Expand Down