File size: 12,913 Bytes
a4ff84b | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 | // Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'dart:async';
import 'package:process/process.dart';
import '../base/common.dart';
import '../base/config.dart';
import '../base/io.dart';
import '../base/logger.dart';
import '../base/platform.dart';
import '../base/process.dart';
import '../base/terminal.dart';
import '../convert.dart' show utf8;
const String _developmentTeamBuildSettingName = 'DEVELOPMENT_TEAM';
/// User message when no development certificates are found in the keychain.
///
/// The user likely never did any iOS development.
const String noCertificatesInstruction = '''
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
No valid code signing certificates were found
You can connect to your Apple Developer account by signing in with your Apple ID
in Xcode and create an iOS Development Certificate as well as a Provisioning\u0020
Profile for your project by:
$fixWithDevelopmentTeamInstruction
5- Trust your newly created Development Certificate on your iOS device
via Settings > General > Device Management > [your new certificate] > Trust
For more information, please visit:
https://developer.apple.com/library/content/documentation/IDEs/Conceptual/
AppDistributionGuide/MaintainingCertificates/MaintainingCertificates.html
Or run on an iOS simulator without code signing
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ''';
/// User message when there are no provisioning profile for the current app bundle identifier.
///
/// The user did iOS development but never on this project and/or device.
const String noProvisioningProfileInstruction = '''
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
No Provisioning Profile was found for your project's Bundle Identifier or your\u0020
device. You can create a new Provisioning Profile for your project in Xcode for\u0020
your team by:
$fixWithDevelopmentTeamInstruction
It's also possible that a previously installed app with the same Bundle\u0020
Identifier was signed with a different certificate.
For more information, please visit:
https://flutter.dev/docs/get-started/install/macos#deploy-to-ios-devices
Or run on an iOS simulator without code signing
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ''';
/// Fallback error message for signing issues.
///
/// Couldn't auto sign the app but can likely solved by retracing the signing flow in Xcode.
const String noDevelopmentTeamInstruction = '''
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
Building a deployable iOS app requires a selected Development Team with a\u0020
Provisioning Profile. Please ensure that a Development Team is selected by:
$fixWithDevelopmentTeamInstruction
For more information, please visit:
https://flutter.dev/docs/get-started/install/macos#deploy-to-ios-devices
Or run on an iOS simulator without code signing
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ''';
const String fixWithDevelopmentTeamInstruction = '''
1- Open the Flutter project's Xcode target with
open ios/Runner.xcworkspace
2- Select the 'Runner' project in the navigator then the 'Runner' target
in the project settings
3- Make sure a 'Development Team' is selected under Signing & Capabilities > Team.\u0020
You may need to:
- Log in with your Apple ID in Xcode first
- Ensure you have a valid unique Bundle ID
- Register your device with your Apple Developer Account
- Let Xcode automatically provision a profile for your app
4- Build or run your project again''';
final RegExp _securityFindIdentityDeveloperIdentityExtractionPattern =
RegExp(r'^\s*\d+\).+"(.+Develop(ment|er).+)"$');
final RegExp _securityFindIdentityCertificateCnExtractionPattern = RegExp(r'.*\(([a-zA-Z0-9]+)\)');
final RegExp _certificateOrganizationalUnitExtractionPattern = RegExp(r'OU=([a-zA-Z0-9]+)');
/// Given a [BuildableIOSApp], this will try to find valid development code
/// signing identities in the user's keychain prompting a choice if multiple
/// are found.
///
/// Returns a set of build configuration settings that uses the selected
/// signing identities.
///
/// Will return null if none are found, if the user cancels or if the Xcode
/// project has a development team set in the project's build settings.
Future<Map<String, String>?> getCodeSigningIdentityDevelopmentTeamBuildSetting({
required Map<String, String> buildSettings,
required ProcessManager processManager,
required Platform platform,
required Logger logger,
required Config config,
required Terminal terminal,
}) async {
// If the user already has it set in the project build settings itself,
// continue with that.
if (_isNotEmpty(buildSettings[_developmentTeamBuildSettingName])) {
logger.printStatus(
'Automatically signing iOS for device deployment using specified development '
'team in Xcode project: ${buildSettings[_developmentTeamBuildSettingName]}'
);
return null;
}
if (_isNotEmpty(buildSettings['PROVISIONING_PROFILE'])) {
return null;
}
final String? developmentTeam = await _getCodeSigningIdentityDevelopmentTeam(
processManager: processManager,
platform: platform,
logger: logger,
config: config,
terminal: terminal,
shouldExitOnNoCerts: true,
);
if (developmentTeam == null) {
return null;
}
return <String, String>{
_developmentTeamBuildSettingName: developmentTeam,
};
}
Future<String?> getCodeSigningIdentityDevelopmentTeam({
required ProcessManager processManager,
required Platform platform,
required Logger logger,
required Config config,
required Terminal terminal,
}) async =>
_getCodeSigningIdentityDevelopmentTeam(
processManager: processManager,
platform: platform,
logger: logger,
config: config,
terminal: terminal,
);
/// Set [shouldExitOnNoCerts] to show instructions for how to add a cert when none are found, then [toolExit].
Future<String?> _getCodeSigningIdentityDevelopmentTeam({
required ProcessManager processManager,
required Platform platform,
required Logger logger,
required Config config,
required Terminal terminal,
bool shouldExitOnNoCerts = false,
}) async {
if (!platform.isMacOS) {
return null;
}
// If the user's environment is missing the tools needed to find and read
// certificates, abandon. Tools should be pre-equipped on macOS.
final ProcessUtils processUtils = ProcessUtils(processManager: processManager, logger: logger);
if (!await processUtils.exitsHappy(const <String>['which', 'security']) ||
!await processUtils.exitsHappy(const <String>['which', 'openssl'])) {
return null;
}
const List<String> findIdentityCommand =
<String>['security', 'find-identity', '-p', 'codesigning', '-v'];
String findIdentityStdout;
try {
findIdentityStdout = (await processUtils.run(
findIdentityCommand,
throwOnError: true,
)).stdout.trim();
} on ProcessException catch (error) {
logger.printTrace('Unexpected failure from find-identity: $error.');
return null;
}
final List<String> validCodeSigningIdentities = findIdentityStdout
.split('\n')
.map<String?>((String outputLine) {
return _securityFindIdentityDeveloperIdentityExtractionPattern
.firstMatch(outputLine)
?.group(1);
})
.where(_isNotEmpty)
.whereType<String>()
.toSet() // Unique.
.toList();
final String? signingIdentity =
await _chooseSigningIdentity(validCodeSigningIdentities, logger, config, terminal, shouldExitOnNoCerts);
// If none are chosen, return null.
if (signingIdentity == null) {
return null;
}
logger.printStatus('Developer identity "$signingIdentity" selected for iOS code signing');
final String? signingCertificateId =
_securityFindIdentityCertificateCnExtractionPattern
.firstMatch(signingIdentity)
?.group(1);
// If `security`'s output format changes, we'd have to update the above regex.
if (signingCertificateId == null) {
return null;
}
String signingCertificateStdout;
try {
signingCertificateStdout = (await processUtils.run(
<String>['security', 'find-certificate', '-c', signingCertificateId, '-p'],
throwOnError: true,
)).stdout.trim();
} on ProcessException catch (error) {
logger.printTrace("Couldn't find the certificate: $error.");
return null;
}
final Process opensslProcess = await processUtils.start(
const <String>['openssl', 'x509', '-subject']);
await (opensslProcess.stdin..write(signingCertificateStdout)).close();
final String opensslOutput = await utf8.decodeStream(opensslProcess.stdout);
// Fire and forget discard of the stderr stream so we don't hold onto resources.
// Don't care about the result.
unawaited(opensslProcess.stderr.drain<String?>());
if (await opensslProcess.exitCode != 0) {
return null;
}
return _certificateOrganizationalUnitExtractionPattern.firstMatch(opensslOutput)?.group(1);
}
/// Set [shouldExitOnNoCerts] to show instructions for how to add a cert when none are found, then [toolExit].
Future<String?> _chooseSigningIdentity(
List<String> validCodeSigningIdentities,
Logger logger,
Config config,
Terminal terminal,
bool shouldExitOnNoCerts,
) async {
// The user has no valid code signing identities.
if (validCodeSigningIdentities.isEmpty) {
if (shouldExitOnNoCerts) {
logger.printError(noCertificatesInstruction, emphasis: true);
throwToolExit('No development certificates available to code sign app for device deployment');
} else {
return null;
}
}
if (validCodeSigningIdentities.length == 1) {
return validCodeSigningIdentities.first;
}
if (validCodeSigningIdentities.length > 1) {
final String? savedCertChoice = config.getValue('ios-signing-cert') as String?;
if (savedCertChoice != null) {
if (validCodeSigningIdentities.contains(savedCertChoice)) {
logger.printStatus('Found saved certificate choice "$savedCertChoice". To clear, use "flutter config".');
return savedCertChoice;
} else {
logger.printError('Saved signing certificate "$savedCertChoice" is not a valid development certificate');
}
}
// If terminal UI can't be used, just attempt with the first valid certificate
// since we can't ask the user.
if (!terminal.usesTerminalUi) {
return validCodeSigningIdentities.first;
}
final int count = validCodeSigningIdentities.length;
logger.printStatus(
'Multiple valid development certificates available (your choice will be saved):',
emphasis: true,
);
for (int i=0; i<count; i++) {
logger.printStatus(' ${i+1}) ${validCodeSigningIdentities[i]}', emphasis: true);
}
logger.printStatus(' a) Abort', emphasis: true);
final String choice = await terminal.promptForCharInput(
List<String>.generate(count, (int number) => '${number + 1}')
..add('a'),
prompt: 'Please select a certificate for code signing',
defaultChoiceIndex: 0, // Just pressing enter chooses the first one.
logger: logger,
);
if (choice == 'a') {
throwToolExit('Aborted. Code signing is required to build a deployable iOS app.');
} else {
final String selectedCert = validCodeSigningIdentities[int.parse(choice) - 1];
logger.printStatus('Certificate choice "$selectedCert" saved');
config.setValue('ios-signing-cert', selectedCert);
return selectedCert;
}
}
return null;
}
/// Returns true if s is a not empty string.
bool _isNotEmpty(String? s) => s != null && s.isNotEmpty;
|