Skip to content

Trusted entitlements: Add Signature type to process new signature response format#1109

Merged
tonidero merged 3 commits into
mainfrom
toniricodiez/sdk-3200-verify-intermediate-signature-android
Jun 30, 2023
Merged

Trusted entitlements: Add Signature type to process new signature response format#1109
tonidero merged 3 commits into
mainfrom
toniricodiez/sdk-3200-verify-intermediate-signature-android

Conversation

@tonidero

@tonidero tonidero commented Jun 29, 2023

Copy link
Copy Markdown
Contributor

Description

First PR for SDK-3200

This adds the new Signature type which will encode the different fields that are part of the new signature format. This is currently unused and will be used in future PRs.

@codecov

codecov Bot commented Jun 29, 2023

Copy link
Copy Markdown

Codecov Report

Merging #1109 (c7d4619) into main (5a5c66d) will increase coverage by 0.01%.
The diff coverage is 86.66%.

@@            Coverage Diff             @@
##             main    #1109      +/-   ##
==========================================
+ Coverage   85.12%   85.13%   +0.01%     
==========================================
  Files         183      184       +1     
  Lines        6493     6538      +45     
  Branches      917      927      +10     
==========================================
+ Hits         5527     5566      +39     
  Misses        601      601              
- Partials      365      371       +6     
Impacted Files Coverage Δ
...enuecat/purchases/common/verification/Signature.kt 86.66% <86.66%> (ø)

}
}

override fun equals(other: Any?): Boolean {

@tonidero tonidero Jun 29, 2023

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When using ByteArray, data classes default equals don't seem to work well, so we need to override the equals and hashCode methods

@tonidero tonidero marked this pull request as ready for review June 29, 2023 11:19
@tonidero tonidero requested a review from a team June 29, 2023 11:19

@NachoSoto NachoSoto left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Love it.

Comment on lines +20 to +24
val startByte: Int
get() = values().copyOfRange(0, ordinal).sumOf { it.size }

val endByte: Int
get() = startByte + size

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice 😛

import androidx.annotation.VisibleForTesting

@VisibleForTesting(otherwise = VisibleForTesting.PRIVATE)
internal enum class SignatureComponent(val size: Int) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible in Kotlin to make this a nested type of Signature? So this becomes Signature.Component?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah we can do that 👍

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in c7d4619

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome.

Comment on lines +31 to +37
data class Signature(
val intermediateKey: ByteArray,
val intermediateKeyExpiration: ByteArray,
val intermediateKeySignature: ByteArray,
val salt: ByteArray,
val payload: ByteArray,
) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I love this abstraction.

internal fun fromString(signature: String): Signature {
val signatureBytes = Base64.decode(signature, Base64.DEFAULT)
val expectedSize = SignatureComponent.totalSize
if (signatureBytes.size != expectedSize) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍🏻

val signatureBytes = Base64.decode(signature, Base64.DEFAULT)
val expectedSize = SignatureComponent.totalSize
if (signatureBytes.size != expectedSize) {
throw InvalidSignatureSizeException(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess it will be important than the SigningManager catches this, right? If only Kotlin had checked exceptions...

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, it would be great to have that 😞

@tonidero tonidero merged commit 3558a24 into main Jun 30, 2023
@tonidero tonidero deleted the toniricodiez/sdk-3200-verify-intermediate-signature-android branch June 30, 2023 07:11
tonidero added a commit that referenced this pull request Jul 3, 2023
### Description
Third PR for SDK-3200

- Adds support to the new signature format (salt + nonce + TS + etag +
content)
- Adds support for intermediate signatures verification
- Makes nonce optional in preparation of static endpoint signing.

Based on #1109 and #1110.
tonidero added a commit that referenced this pull request Jul 7, 2023
### Description
Third PR for SDK-3200

- Adds support to the new signature format (salt + nonce + TS + etag +
content)
- Adds support for intermediate signatures verification
- Makes nonce optional in preparation of static endpoint signing.

Based on #1109 and #1110.
tonidero added a commit that referenced this pull request Jul 12, 2023
**This is an automatic release.**

### New Features
* `Trusted Entitlements`: made API stable (#1105) via NachoSoto
(@NachoSoto)

This new feature prevents MitM attacks between the SDK and the
RevenueCat server.
With verification enabled, the SDK ensures that the response created by
the server was not modified by a third-party, and the entitlements
received are exactly what was sent.
This is 100% opt-in. `EntitlementInfos` have a new `VerificationResult`
property, which will indicate the validity of the responses when this
feature is enabled.

```kotlin
fun configureRevenueCat() {
    val configuration = PurchasesConfiguration.Builder(context, apiKey)
        .entitlementVerificationMode(EntitlementVerificationMode.INFORMATIONAL)
        .build()
    Purchases.configure(configuration)
}
```
### Experimental features
* Add await offerings (#1096) via Cesar de la Vega (@vegaro)
### Bugfixes
* Fix issue updating customer info on app open (#1128) via Toni Rico
(@tonidero)
### Dependency Updates
* Bump fastlane-plugin-revenuecat_internal from `13773d2` to `b2108fb`
(#1095) via dependabot[bot] (@dependabot[bot])
### Other Changes
* [PurchaseTester] Add option to purchase an arbitrary product id
(#1099) via Mark Villacampa (@MarkVillacampa)
* Fix release path after module refactor (#1129) via Toni Rico
(@tonidero)
* Fix load shedder integration tests (#1125) via Toni Rico (@tonidero)
* Trusted entitlements: New trusted entitlements signature format
(#1117) via Toni Rico (@tonidero)
* Fix integration tests and change to a different project (#1123) via
Toni Rico (@tonidero)
* Move files into src/main/kotlin (#1122) via Cesar de la Vega (@vegaro)
* Remove public module (#1113) via Cesar de la Vega (@vegaro)
* Remove common module (#1106) via Cesar de la Vega (@vegaro)
* Fix flaky integration tests: Wait for coroutines to finish before
continuing (#1120) via Toni Rico (@tonidero)
* Move amazon module into purchases (#1112) via Cesar de la Vega
(@vegaro)
* Trusted entitlements: Add IntermediateSignatureHelper to handle
intermediate signature verification process (#1110) via Toni Rico
(@tonidero)
* Trusted entitlements: Add Signature type to process new signature
response format (#1109) via Toni Rico (@tonidero)
* [EXTERNAL] Add `awaitCustomerInfo` / coroutines tests to
`TrustedEntitlementsInformationalModeIntegrationTest` (#1077) via
@pablo-guardiola (#1107) via Toni Rico (@tonidero)
* Remove feature:google module (#1104) via Cesar de la Vega (@vegaro)
* Remove identity module (#1103) via Cesar de la Vega (@vegaro)
* Remove subscriber attributes module (#1102) via Cesar de la Vega
(@vegaro)
* Delete utils module (#1098) via Cesar de la Vega (@vegaro)
* Remove strings module (#1097) via Cesar de la Vega (@vegaro)
* Update CHANGELOG.md to include external contribution (#1100) via Cesar
de la Vega (@vegaro)
* [EXTERNAL] Add missing `fetchPolicy` parameter to `awaitCustomerInfo`
API (#1086) via @pablo-guardiola (#1090) via Toni Rico (@tonidero)

---------

Co-authored-by: revenuecat-ops <ops@revenuecat.com>
Co-authored-by: Toni Rico <antonio.rico.diez@revenuecat.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants