-
Notifications
You must be signed in to change notification settings - Fork 147
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: adding private key argument to patch command (#2133)
Co-authored-by: Bryan Oltman <[email protected]>
- Loading branch information
1 parent
83e0908
commit b93f5fd
Showing
19 changed files
with
358 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -578,4 +578,4 @@ packages: | |
source: hosted | ||
version: "3.1.2" | ||
sdks: | ||
dart: ">=3.4.0-256.0.dev <4.0.0" | ||
dart: ">=3.4.0 <4.0.0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -570,4 +570,4 @@ packages: | |
source: hosted | ||
version: "3.1.2" | ||
sdks: | ||
dart: ">=3.4.0-256.0.dev <4.0.0" | ||
dart: ">=3.4.0 <4.0.0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import 'dart:convert'; | ||
import 'dart:io'; | ||
import 'dart:typed_data'; | ||
|
||
import 'package:pem/pem.dart'; | ||
import 'package:pointycastle/pointycastle.dart'; | ||
import 'package:scoped_deps/scoped_deps.dart'; | ||
|
||
/// A reference to a [CodeSigner] instance. | ||
final codeSignerRef = create(CodeSigner.new); | ||
|
||
/// The [CodeSigner] instance available in the current zone. | ||
CodeSigner get codeSigner => read(codeSignerRef); | ||
|
||
/// {@template code_signer} | ||
/// Manages code signing operations. | ||
/// {@endtemplate} | ||
class CodeSigner { | ||
/// Signs a [message] using the provided [privateKeyPemFile] using | ||
/// SHA-256/RSA. | ||
/// | ||
/// This is the equivalent of: | ||
/// $ openssl dgst -sha256 -sign privateKey.pem -out signature message | ||
String sign({required String message, required File privateKeyPemFile}) { | ||
final privateKeyData = _privateKeyBytes(pemFile: privateKeyPemFile); | ||
final privateKey = RSAPrivateKeyFromInt.from(privateKeyData); | ||
|
||
final signer = Signer('SHA-256/RSA') | ||
..init(true, PrivateKeyParameter<RSAPrivateKey>(privateKey)); | ||
|
||
final signature = | ||
signer.generateSignature(utf8.encode(message)) as RSASignature; | ||
return base64.encode(signature.bytes); | ||
} | ||
|
||
/// Decodes a PEM file containing a private key and returns its contents as | ||
/// bytes. | ||
List<int> _privateKeyBytes({required File pemFile}) { | ||
final privateKeyString = pemFile.readAsStringSync(); | ||
final pemCodec = PemCodec(PemLabel.privateKey); | ||
return pemCodec.decode(privateKeyString); | ||
} | ||
} | ||
|
||
extension RSAPrivateKeyFromInt on RSAPrivateKey { | ||
/// Converts an RSA private key bytes to a pointycastle [RSAPrivateKey]. | ||
/// | ||
/// Based on https://github.com/konstantinullrich/crypton/blob/trunk/lib/src/rsa/private_key.dart | ||
static RSAPrivateKey from(List<int> privateKeyBytes) { | ||
var asn1Parser = ASN1Parser(Uint8List.fromList(privateKeyBytes)); | ||
final topLevelSeq = asn1Parser.nextObject() as ASN1Sequence; | ||
final privateKey = topLevelSeq.elements![2]; | ||
|
||
asn1Parser = ASN1Parser(privateKey.valueBytes); | ||
final pkSeq = asn1Parser.nextObject() as ASN1Sequence; | ||
|
||
final modulus = pkSeq.elements![1] as ASN1Integer; | ||
final privateExponent = pkSeq.elements![3] as ASN1Integer; | ||
final p = pkSeq.elements![4] as ASN1Integer; | ||
final q = pkSeq.elements![5] as ASN1Integer; | ||
|
||
return RSAPrivateKey( | ||
modulus.integer!, | ||
privateExponent.integer!, | ||
p.integer, | ||
q.integer, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
-----BEGIN PRIVATE KEY----- | ||
MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDbB2kQZu6+U+xv | ||
2LSpit8x58mcTDUEdOLxJhlMqtc68laYSk8TWFZ9uS9jNe7lr3qBXXKhwXcMzCfT | ||
hWZGUqELgCGwPQ0vRQ2FiGi1sob3UrCLW8BekeEJ3PmBAQHDQrW4HgnP7MrpYr7f | ||
U+vJinAvBvJc2pehjwgABRDhJOwdhXnD4ExKLyl6lYxF3sNH1Edxs05mUm90FDk3 | ||
G8Hgk3h1EyrxwLvd7PU/13sN/C/dNZj6F70Sa5ctPZSK9lKUcisYFrswV+rJR7au | ||
jQXtN78HSyLXaK0FtYirJy+pxeN4482fpYSmo3shaNv0tSHXrYnJhrPktv9V54lf | ||
wsq0SVxzAgMBAAECggEAI+rwre8Qn/yM4tXji9lepZqa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OSfxBmjuVHFAoGBAMF57jTOZwJ/66MNqYJi | ||
wZdX9VxTLbX4YGBSeWi5X+qT5AVsKUlR6yxCzLBxJMDXn472RkNurscdakvI3nnt | ||
h8PU5ur9uFKEcYsO9HPtvkuSZsQ8VEN3fMr8XdaTtH30eF6xKqNQdCMzqnWmWjM0 | ||
UT/B/sUKZUuxZ3Y2NH59Ba+J | ||
-----END PRIVATE KEY----- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
-----BEGIN PUBLIC KEY----- | ||
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA2wdpEGbuvlPsb9i0qYrf | ||
MefJnEw1BHTi8SYZTKrXOvJWmEpPE1hWfbkvYzXu5a96gV1yocF3DMwn04VmRlKh | ||
C4AhsD0NL0UNhYhotbKG91Kwi1vAXpHhCdz5gQEBw0K1uB4Jz+zK6WK+31PryYpw | ||
LwbyXNqXoY8IAAUQ4STsHYV5w+BMSi8pepWMRd7DR9RHcbNOZlJvdBQ5NxvB4JN4 | ||
dRMq8cC73ez1P9d7Dfwv3TWY+he9EmuXLT2UivZSlHIrGBa7MFfqyUe2ro0F7Te/ | ||
B0si12itBbWIqycvqcXjeOPNn6WEpqN7IWjb9LUh162JyYaz5Lb/VeeJX8LKtElc | ||
cwIDAQAB | ||
-----END PUBLIC KEY----- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import 'dart:convert'; | ||
|
||
import 'package:path/path.dart' as p; | ||
import 'package:shorebird_cli/src/code_signer.dart'; | ||
import 'package:shorebird_cli/src/third_party/flutter_tools/lib/flutter_tools.dart'; | ||
import 'package:test/test.dart'; | ||
|
||
void main() { | ||
group( | ||
CodeSigner, | ||
() { | ||
final cryptoFixturesBasePath = p.join('test', 'fixtures', 'crypto'); | ||
final privateKeyFile = File( | ||
p.join(cryptoFixturesBasePath, 'private.pem'), | ||
); | ||
|
||
late CodeSigner codeSigner; | ||
|
||
setUp(() { | ||
codeSigner = CodeSigner(); | ||
}); | ||
|
||
group('sign', () { | ||
const message = | ||
'6b86b273ff34fce19d6b804eff5a3f5747ada4eaa22f1d49c01e52ddb7875b4b'; | ||
|
||
test('signature matches openssl output', () async { | ||
final outputDir = Directory.systemTemp.createTempSync(); | ||
final messageFile = File(p.join(outputDir.path, 'message')) | ||
..writeAsStringSync(message); | ||
final signatureFile = File(p.join(outputDir.path, 'signature')); | ||
await Process.run('openssl', [ | ||
'dgst', | ||
'-sha256', | ||
'-sign', | ||
privateKeyFile.path, | ||
'-out', | ||
signatureFile.path, | ||
messageFile.path, | ||
]); | ||
|
||
final expectedSignature = | ||
base64Encode(signatureFile.readAsBytesSync()); | ||
final actualSignature = codeSigner.sign( | ||
message: message, | ||
privateKeyPemFile: privateKeyFile, | ||
); | ||
expect(actualSignature, equals(expectedSignature)); | ||
}); | ||
}); | ||
}, | ||
onPlatform: { | ||
'windows': const Skip('Does not have openssl installed by default'), | ||
}, | ||
); | ||
} |
Oops, something went wrong.