# Code signing with CodePush

CodePush code signing is done in three stages:

1. Generating an RSA keypair:

   - The private key is used to sign the CodePush bundles.
   - The public key is embedded into the mobile app to verify signatures.
1. When releasing a CodePush update, the Bitrise CodePush CLI signs the bundle using the private key. It creates a JWT (JSON Web Token) containing the bundle's hash, digitally signed with this private key.
1. The mobile app (with the embedded public key) verifies the JWT signature before applying the update. If verification fails, the update is rejected.

## Signing your app

**iOS**

1. Use version 5.1.0 or higher of the app SDK. Earlier versions don't support code signing.
1. Make sure the Bitrise CodePush CLI version is 1.0.0 or higher to take advantage of code signing features.
1. Generate an RSA keypair in PEM format with Open SSL:

   ```bash
   openssl genrsa -out private_key.pem 2048
   openssl rsa -in private_key.pem -pubout -out public_key.pem
   ```

   You must keep the private key secure and never share it! The public key will be embedded inside the app itself.
1. Add the public key string inside your app’s `Info.plist`:

   ```xml
   <key>CodePushPublicKey</key>
   <string>-----BEGIN PUBLIC KEY-----
   MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEArVJ2k...
   -----END PUBLIC KEY-----</string>
   ```
1. Rebuild your iOS app with the updated app SDK (>= 5.1.0) that supports code signing.
1. Bundle and sign your updates with the Bitrise CodePush CLI.

   :::warning

   The output directory must be named exactly `CodePush`. The SDK uses the directory name as a path prefix when verifying the package hash — any other name causes a hash mismatch and the update is silently rejected on-device.

   :::

   ```bash
   bitrise :codepush bundle --platform ios --private-key-path ./private_key.pem
   ```

   The signed JWT (`.codepushrelease` file) is generated inside the bundle directory and uploaded to the server. The uploaded `.zip` file contains both the `.codepushrelease` file and the bundled output file.
1. Push the already bundled update:

   ```bash
   bitrise :codepush push ./CodePush \
     --deployment Staging \
     --app-version 1.0.0 \
     --private-key-path ./private_key.pem
   ```

   :::tip

   Optionally, you can bundle, sign, and push in one command:

   ```bash
   bitrise :codepush push --bundle --platform ios \
     --deployment Staging \
     --app-version 1.0.0 \
     --private-key-path ./private_key.pem
   ```

   :::

**Android**

1. Use version 5.1.0 or higher of the app SDK. Earlier versions don't support code signing.
1. Make sure the Bitrise CodePush CLI version is 1.0.0 or higher to take advantage of code signing features.
1. Generate an RSA keypair in PEM format with Open SSL:

   ```bash
   openssl genrsa -out private_key.pem 2048
   openssl rsa -in private_key.pem -pubout -out public_key.pem
   ```

   You must keep the private key secure and never share it! The public key will be embedded inside the app itself.
1. Add the public key string in `/android/app/src/main/res/values/strings.xml`:

   ```xml
   <resources>
       <string name="CodePushPublicKey">-----BEGIN PUBLIC KEY-----
   MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEArVJ2k...
   -----END PUBLIC KEY-----</string>
   </resources>
   ```
1. Configure the CodePush client instance.

   You can use a constructor:

   ```java
   new CodePush("deployment-key", getApplicationContext(), BuildConfig.DEBUG, R.string.CodePushPublicKey);
   ```

   Or you can use a builder:

   ```java
   new CodePushBuilder("deployment-key", getApplicationContext())
       .setIsDebugMode(BuildConfig.DEBUG)
       .setPublicKeyResourceDescriptor(R.string.CodePushPublicKey)
       .build();
   ```

   For React Native >= 0.61, follow the [Android setup guide](https://github.com/bitrise-io/react-native-code-push/blob/master/docs/setup-android.md) in the CodePush SDK repository.

1. Rebuild your Android app with the updated app SDK (>= 5.1.0) that supports code signing.
1. Bundle and sign your updates with the Bitrise CodePush CLI.

   :::warning

   The output directory must be named exactly `CodePush`. The SDK uses the directory name as a path prefix when verifying the package hash — any other name causes a hash mismatch and the update is silently rejected on-device.

   :::

   ```bash
   bitrise :codepush bundle --platform android --private-key-path ./private_key.pem
   ```

   The signed JWT (`.codepushrelease` file) is generated inside the bundle directory and uploaded to the server. The uploaded `.zip` file contains both the `.codepushrelease` file and the bundled output file.
1. Push the already bundled update:

   ```bash
   bitrise :codepush push ./CodePush \
     --deployment Staging \
     --app-version 1.0.0 \
     --private-key-path ./private_key.pem
   ```

   :::tip

   Optionally, you can bundle, sign, and push in one command:

   ```bash
   bitrise :codepush push --bundle --platform android \
     --deployment Staging \
     --app-version 1.0.0 \
     --private-key-path ./private_key.pem
   ```

   :::

## Code signing and delta updates

Signed updates work with delta updates, including binary diffing. The signature covers the full update package, not the delta. The SDK first rebuilds the full package from the delta, then verifies the signature against the rebuilt package. If verification fails, the SDK rejects the update.

For more information, see [Delta updates](/release-management/codepush/delta-updates).

## Code signing support for Expo

The app SDK and the Bitrise CodePush CLI both support code signing for Expo projects, too. The overall flow is the same as for standard React Native apps: generate an RSA key pair, embed the public key in the app, and sign bundles at release time using the --private-key-path flag.

The main difference for managed Expo workflows is how the public key gets embedded: this typically requires [EAS Build](https://docs.expo.dev/build/introduction/) and a custom config plugin. When bundling and pushing updates, use the standard `bitrise :codepush` commands as usual.
