Skip to content

Lightpanda: the headless browser designed for AI and automation

canonicalHash is case insensitive

Karl Seguin87329663cff4
diff --git a/src/browser/tests/crypto.html b/src/browser/tests/crypto.html
index 3880793e7..ae1903466 100644
--- a/src/browser/tests/crypto.html
+++ b/src/browser/tests/crypto.html
@@ -147,6 +147,7 @@
testing.expectEqual(true, privateKey instanceof CryptoKey);
testing.expectEqual(true, publicKey instanceof CryptoKey);
testing.expectEqual(2048, publicKey.algorithm.modulusLength);
+ testing.expectEqual("1,0,1", publicKey.algorithm.publicExponent.join(","));
testing.expectEqual(256, ciphertext.byteLength);
testing.expectEqual("lightpanda rsa-oaep", new TextDecoder().decode(decrypted));
});
@@ -161,6 +162,8 @@
const pkcs8 = fromB64("MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDYnr4VSfpIXajj7q+PqNuwZ+6YGuVkhLs5NGbDewk7O+KesNFDeF/5OYKAFIdwZuiEdMi2AHyZ8zUwxuwiCBiNs1Kz5tGFWZu0wVLcbEGys235i9oQwXQLex1iLr6BZmolit8QX9rwdPb4hlAnkHs+wg6hU9aUORotAE+VAOArQklAn7WY4jx1cfP09o9cHW/3/DedSlFXpt3L0i6LjndeehjxqMgrLRvOTXbXrQ/Fkzi8C+u92ZNjXYHCQayKb7uKo2UuNTp8kpvgPTvrph+ugYM+HF3ewOrEu15B4Drighyp5DMOrz3zjcNLVV4bC/6evA0cLLdwCYtVrK2Ti8QJAgMBAAECggEAHW4z1SCf6IURFSawo4wFjQoR2IiO8gE2jqcX7ycgMfQI7haH1+ra3zl5CX3AiPzySkyzmEaopyHr8ByWqGkBLP9YFSke/kcWaJB7eYcbxfOq1WSy0+P0qDqp21UOfPY1JbSgadEcJd8+TrWw3i87D2O+KR0C4lB/xe1s4omj6zxuSMOL3igRtu+pSem3e5Wb/lid2OIAskxgt+RiyRSPsajBunhah4WxhY0NQIt5WK0kiNfpd8vz3qVTnF8t5oPPWLvdHIApDHLOkbeX+0XA5XeJojdPgN/vdInuXYoFp/VF6Jc+nAgCOEDI/+JPQ+Jpoy1FJoB5fZimivPvGbXrIQKBgQDup6Y/+zRYIRCN8VpcUBuvQHLbE17UDTrVrSh8fqPJpAWvNPrYZb+tG23cuPec4FIGoadjwiuFgsXs9lXv0qPxcgTyR6l0RzrDWNmt8HvPRrgEIJEwv+BT+heRUecaXHSvG1vCam06MkjgFLvnCJ7hEVCNf4+irhLCNV+J9TnZ4wKBgQDoXR6WdVCDEHz+PBsfzcINbRxyWLPIJmULcngqpeQ3UyrxWXxXR2BKuVrOZq5Dn9XcmP/zZ9u4eCI2Nst4eLlzQIxXDxsj62Y/5yAc9tw4nzTvxPaKf7orBMrRjYSmj9vOrpYIlVjV+cPRBfjxfRonSDf3kWlUS62GFweeDQo+IwKBgQDiMJ8PEOVhiaUJAvEjgQFcLnctT8SWshtLHZy7scNGf2Cr2EjPBFCQnuiXQ+/bDXhwtJHTqNpa5cD1f9ufO0MKuYgaoPToMgJt1y4o/z45C8uojb1OOO1Cakwf/UJN58bnl8R2FSyE9dfAGaalp7P9mM2B7p6SDu3Uh4WPWY6FBQKBgHINgv4HB0XNygxgJFAwBpfgfp4Bu/z6G47K2QDV/HbLc8CTU9zjQ/hpkpDNBaxbv/VPhBJNYCPjk6dYrfasCxrvC/OPjprK6J1lLPYKlX2VZ75oxzqkBxUmfQbul7XJ1vJT74C3Dau5tMvcSpz98uBlKlNyJGkj3EB8/VguvmBRAoGBAJFSOHauuxGtvw64JnMEkUL9vnjuryyH6CcXgSP6SMY58O2KL/nMEGeAYguwrkl5CdoN2zrWr8/I8cr1UrkKHtzsQHrCYlA6LaZaHDUdxCEIyarO7z18ZXzi32HRol1UFjpQTVg2N9t9rGqC2Hg+hg2D0kGJCwf/t8Uydc2tiTh/");
const publicKey = await crypto.subtle.importKey("spki", spki, { name: "RSA-OAEP", hash: "SHA-256" }, false, ["encrypt"]);
const privateKey = await crypto.subtle.importKey("pkcs8", pkcs8, { name: "RSA-OAEP", hash: "SHA-256" }, false, ["decrypt"]);
+ // Hash names match case-insensitively and are reported in canonical form.
+ const lowerKey = await crypto.subtle.importKey("spki", spki, { name: "RSA-OAEP", hash: "sha-256" }, false, ["encrypt"]);
const message = new TextEncoder().encode("imported key path");
const ciphertext = await crypto.subtle.encrypt({ name: "RSA-OAEP" }, publicKey, message);
@@ -170,6 +173,64 @@
await state.done(() => {
testing.expectEqual(2048, publicKey.algorithm.modulusLength);
testing.expectEqual("imported key path", new TextDecoder().decode(decrypted));
+ testing.expectEqual("1,0,1", publicKey.algorithm.publicExponent.join(","));
+ testing.expectEqual("SHA-256", lowerKey.algorithm.hash.name);
+ });
+</script>
+
+<script id=rsa-oaep-errors type=module>
+ const state = await testing.async();
+ const errorName = async (fn) => {
+ try {
+ await fn();
+ return null;
+ } catch (err) {
+ return err.name;
+ }
+ };
+ const gen = (hash, usages) => crypto.subtle.generateKey(
+ { name: "RSA-OAEP", modulusLength: 1024, publicExponent: new Uint8Array([1, 0, 1]), hash },
+ true,
+ usages,
+ );
+ const msg = new TextEncoder().encode("hi");
+
+ // A lowercase hash still produces a usable key.
+ const k = await gen("sha-256", ["encrypt", "decrypt"]);
+ const ciphertext = await crypto.subtle.encrypt({ name: "RSA-OAEP" }, k.publicKey, msg);
+
+ // The label is part of the ciphertext: it must match on decrypt.
+ const label = new Uint8Array([1, 2, 3]);
+ const labeled = await crypto.subtle.encrypt({ name: "RSA-OAEP", label }, k.publicKey, msg);
+ const unlabeled = await crypto.subtle.decrypt({ name: "RSA-OAEP", label }, k.privateKey, labeled);
+ const labelMismatch = await errorName(() => crypto.subtle.decrypt({ name: "RSA-OAEP" }, k.privateKey, labeled));
+
+ // The operation's algorithm must match the key's.
+ const aesNameOnRsa = await errorName(() => crypto.subtle.encrypt({ name: "AES-GCM", iv: new Uint8Array(12) }, k.publicKey, msg));
+ const aes = await crypto.subtle.generateKey({ name: "AES-GCM", length: 128 }, true, ["encrypt"]);
+ const rsaNameOnAes = await errorName(() => crypto.subtle.encrypt({ name: "RSA-OAEP" }, aes, msg));
+
+ // A pair whose private key would have no usages is rejected.
+ const emptyPrivate = await errorName(() => gen("SHA-256", ["encrypt"]));
+ // Generation blocks the main thread, so the modulus is capped at 4096 bits.
+ const tooLarge = await errorName(() => crypto.subtle.generateKey(
+ { name: "RSA-OAEP", modulusLength: 8192, publicExponent: new Uint8Array([1, 0, 1]), hash: "SHA-256" },
+ true,
+ ["encrypt", "decrypt"],
+ ));
+ const rawExport = await errorName(() => crypto.subtle.exportKey("raw", k.publicKey));
+
+ state.resolve();
+ await state.done(() => {
+ testing.expectEqual("SHA-256", k.publicKey.algorithm.hash.name);
+ testing.expectEqual(128, ciphertext.byteLength);
+ testing.expectEqual("hi", new TextDecoder().decode(unlabeled));
+ testing.expectEqual("OperationError", labelMismatch);
+ testing.expectEqual("InvalidAccessError", aesNameOnRsa);
+ testing.expectEqual("InvalidAccessError", rsaNameOnAes);
+ testing.expectEqual("SyntaxError", emptyPrivate);
+ testing.expectEqual("OperationError", tooLarge);
+ testing.expectEqual("NotSupportedError", rawExport);
});
</script>
diff --git a/src/browser/webapi/CryptoKey.zig b/src/browser/webapi/CryptoKey.zig
index 7a60b689e..b4a8cb286 100644
--- a/src/browser/webapi/CryptoKey.zig
+++ b/src/browser/webapi/CryptoKey.zig
@@ -62,6 +62,8 @@ pub const Algorithm = struct {
hash: ?[]const u8 = null,
named_curve: ?[]const u8 = null,
modulus_length: ?u32 = null,
+ /// RSA: big-endian, duped into the key's arena by init.
+ public_exponent: ?[]const u8 = null,
};
/// https://developer.mozilla.org/en-US/docs/Web/API/CryptoKeyPair
@@ -100,7 +102,7 @@ pub const Usages = struct {
};
/// Copies `key` into an owned pooled arena, duping the caller-provided
-/// slices (`_key`, `_algorithm.hash`). `_algorithm.name` and `_named_curve`
+/// slices (`_key`, `_algorithm.hash`, `_algorithm.public_exponent`). `_algorithm.name` and `_named_curve`
/// are expected to be static strings. Takes ownership of `_vary.pkey`.
pub fn init(exec: *const Execution, key: CryptoKey) !*CryptoKey {
const arena = try exec.getArena(.tiny, "CryptoKey");
@@ -113,6 +115,9 @@ pub fn init(exec: *const Execution, key: CryptoKey) !*CryptoKey {
if (key._algorithm.hash) |hash| {
self._algorithm.hash = try arena.dupe(u8, hash);
}
+ if (key._algorithm.public_exponent) |exponent| {
+ self._algorithm.public_exponent = try arena.dupe(u8, exponent);
+ }
return self;
}
@@ -185,7 +190,12 @@ const AlgorithmReport = union(enum) {
keyed: struct { name: []const u8, length: u32 },
hmac: struct { name: []const u8, length: u32, hash: struct { name: []const u8 } },
ec: struct { name: []const u8, namedCurve: []const u8 },
- rsa: struct { name: []const u8, modulusLength: u32, hash: struct { name: []const u8 } },
+ rsa: struct {
+ name: []const u8,
+ hash: struct { name: []const u8 },
+ modulusLength: u32,
+ publicExponent: js.TypedArray(u8),
+ },
named: struct { name: []const u8 },
};
@@ -204,8 +214,9 @@ pub fn getAlgorithm(self: *const CryptoKey) AlgorithmReport {
} },
.rsa => .{ .rsa = .{
.name = self._algorithm.name,
- .modulusLength = self._algorithm.modulus_length orelse 0,
.hash = .{ .name = self._algorithm.hash orelse "" },
+ .modulusLength = self._algorithm.modulus_length orelse 0,
+ .publicExponent = .{ .values = self._algorithm.public_exponent orelse "" },
} },
else => .{ .named = .{ .name = self._algorithm.name } },
};
diff --git a/src/browser/webapi/SubtleCrypto.zig b/src/browser/webapi/SubtleCrypto.zig
index cee3e9886..8bfda0830 100644
--- a/src/browser/webapi/SubtleCrypto.zig
+++ b/src/browser/webapi/SubtleCrypto.zig
@@ -270,6 +270,10 @@ pub fn exportKey(
}
if (std.mem.eql(u8, format, "raw")) {
+ // RSA keys live in `_vary.pkey`; there's no raw form.
+ if (key._type == .rsa) {
+ return error.NotSupported;
+ }
return local.resolvePromise(js.ArrayBuffer{ .values = key._key });
}
@@ -439,19 +443,17 @@ fn cryptOp(
exec: *const Execution,
) !js.Promise {
const local = exec.js.local.?;
- // RSA-OAEP is handled before the switch: the algorithm may arrive as a bare
- // string ("RSA-OAEP"), which would hit `.name => NotSupported` below. The key
- // type, not the algorithm's shape, decides. No iv/counter is involved.
- if (key._type == .rsa) {
- const rsa_out = blk: {
- if (encrypting) {
- if (!key.canEncrypt()) return error.InvalidAccessError;
- break :blk try RSA.encrypt(key, data, exec);
- }
- if (!key.canDecrypt()) return error.InvalidAccessError;
- break :blk try RSA.decrypt(key, data, exec);
+ // RSA-OAEP is handled before the switch: it may arrive as a bare string
+ // ("RSA-OAEP"), which would hit `.name => NotSupported` below. An RSA key
+ // used with another algorithm's name falls through to AES.crypt, which
+ // rejects the key type.
+ if (RSA.canonicalName(algo.algoName()) != null) {
+ const label: []const u8 = switch (algo) {
+ .params => |p| if (p.label) |l| l.values else "",
+ .name => "",
};
- return local.resolvePromise(js.ArrayBuffer{ .values = rsa_out });
+ const out = try RSA.crypt(key, data, label, encrypting, exec);
+ return local.resolvePromise(js.ArrayBuffer{ .values = out });
}
const params = switch (algo) {
diff --git a/src/browser/webapi/crypto/RSA.zig b/src/browser/webapi/crypto/RSA.zig
index 29613cb70..0a40fea52 100644
--- a/src/browser/webapi/crypto/RSA.zig
+++ b/src/browser/webapi/crypto/RSA.zig
@@ -44,11 +44,7 @@ pub fn validate(params: algorithm.Init.RsaHashedKeyGen, key_usages: []const []co
.string => |s| s,
.object => |o| o.name,
};
- if (!eql(hash_name, "SHA-1") and
- !eql(hash_name, "SHA-256") and
- !eql(hash_name, "SHA-384") and
- !eql(hash_name, "SHA-512"))
- {
+ if (canonicalHash(hash_name) == null) {
return error.NotSupported;
}
@@ -93,11 +89,21 @@ pub fn canonicalName(name: []const u8) ?[]const u8 {
return null;
}
-/// The (private, public) usages legal for an RSA-OAEP key pair.
-fn usageSets() struct { private: []const []const u8, public: []const []const u8 } {
- return .{ .private = &.{ "decrypt", "unwrapKey" }, .public = &.{ "encrypt", "wrapKey" } };
+/// The registered hash name (case-insensitive), or null if not a supported
+/// hash. Stored on the key, so `.algorithm.hash.name` reports it and
+/// `findDigest` (which is case-sensitive) can look it up.
+pub fn canonicalHash(name: []const u8) ?[]const u8 {
+ inline for ([_][]const u8{ "SHA-1", "SHA-256", "SHA-384", "SHA-512" }) |canonical| {
+ if (eql(name, canonical)) return canonical;
+ }
+ return null;
}
+const max_modulus_length = 4096;
+
+const private_usages: []const []const u8 = &.{ "decrypt", "unwrapKey" };
+const public_usages: []const []const u8 = &.{ "encrypt", "wrapKey" };
+
/// Generates an RSA-OAEP key pair of `modulusLength` bits with the given public
/// exponent. Mirrors EC.generate: each key keeps the EVP_PKEY of its half, and
/// the requested usages are split across the pair (validate() rejected illegal
@@ -115,17 +121,20 @@ pub fn generate(
// errors are reported correctly), but only RSA-OAEP generation is implemented.
if (!eql(params.name, "RSA-OAEP")) return error.NotSupported;
- // modulusLength must fit the c_int BoringSSL's generator takes; reject an
- // absurd size here instead of trapping on the @intCast below.
- if (params.modulusLength > std.math.maxInt(c_int)) return error.OperationError;
+ // Generation runs on the main thread and can't be interrupted, and its cost
+ // grows steeply with the size: 8192 bits takes seconds, 16384 (BoringSSL's
+ // ceiling) tens of seconds, several times that on a vCPU. Pages generate
+ // 2048 or 4096 in practice. Chrome allows 16384 but generates off-thread.
+ if (params.modulusLength > max_modulus_length) return error.OperationError;
- const hash_name = switch (params.hash) {
+ const hash_name = canonicalHash(switch (params.hash) {
.string => |s| s,
.object => |o| o.name,
- };
- const sets = usageSets();
- const private_mask = maskOf(sets.private, key_usages);
- const public_mask = maskOf(sets.public, key_usages);
+ }).?;
+ const private_mask = maskOf(private_usages, key_usages);
+ const public_mask = maskOf(public_usages, key_usages);
+ // The private key must end up with at least one usage.
+ if (private_mask == 0) return error.SyntaxError;
// Public exponent as a big-endian BIGNUM (e.g. 65537 == {0x01, 0x00, 0x01}).
const e = crypto.BN_new() orelse return error.OutOfMemory;
@@ -152,6 +161,7 @@ pub fn generate(
if (crypto.EVP_PKEY_set1_RSA(public_pkey, rsa_pub) != 1) return error.OutOfMemory;
const modulus_length: u32 = @intCast(crypto.EVP_PKEY_bits(private_pkey));
+ const public_exponent = try exponentBytes(private_pkey, exec);
const private = try CryptoKey.init(exec, .{
._type = .rsa,
@@ -159,7 +169,7 @@ pub fn generate(
._extractable = extractable,
._usages = private_mask,
._key = &.{},
- ._algorithm = .{ .name = "RSA-OAEP", .hash = hash_name, .modulus_length = modulus_length },
+ ._algorithm = .{ .name = "RSA-OAEP", .hash = hash_name, .modulus_length = modulus_length, .public_exponent = public_exponent },
._vary = .{ .pkey = private_pkey },
});
@@ -170,7 +180,7 @@ pub fn generate(
._extractable = true,
._usages = public_mask,
._key = &.{},
- ._algorithm = .{ .name = "RSA-OAEP", .hash = hash_name, .modulus_length = modulus_length },
+ ._algorithm = .{ .name = "RSA-OAEP", .hash = hash_name, .modulus_length = modulus_length, .public_exponent = public_exponent },
._vary = .{ .pkey = public_pkey },
});
@@ -191,9 +201,9 @@ pub fn import(
) !js.Promise {
const local = exec.js.local.?;
- // Reject an unsupported OAEP digest at import time (findDigest knows the set),
- // rather than letting it surface only on the first encrypt/decrypt.
- _ = crypto.findDigest(hash_name) catch return error.NotSupported;
+ // Reject an unsupported OAEP digest at import time, rather than letting it
+ // surface only on the first encrypt/decrypt.
+ const hash = canonicalHash(hash_name) orelse return error.NotSupported;
var ptr: [*c]const u8 = der.ptr;
const pkey: *crypto.EVP_PKEY = blk: {
@@ -216,26 +226,41 @@ pub fn import(
._extractable = extractable,
._usages = usages_mask,
._key = &.{},
- ._algorithm = .{ .name = "RSA-OAEP", .hash = hash_name, .modulus_length = @intCast(crypto.EVP_PKEY_bits(pkey)) },
+ ._algorithm = .{
+ .name = "RSA-OAEP",
+ .hash = hash,
+ .modulus_length = @intCast(crypto.EVP_PKEY_bits(pkey)),
+ .public_exponent = try exponentBytes(pkey, exec),
+ },
._vary = .{ .pkey = pkey },
});
return local.resolvePromise(crypto_key);
}
-/// Encrypts `data` with the public key (RSA-OAEP).
-pub fn encrypt(key: *const CryptoKey, data: []const u8, exec: *const Execution) ![]const u8 {
- return crypt(key, data, true, exec);
+/// The public exponent of an RSA EVP_PKEY, big-endian, in the local arena.
+fn exponentBytes(pkey: *const crypto.EVP_PKEY, exec: *const Execution) ![]const u8 {
+ const rsa = crypto.EVP_PKEY_get0_RSA(pkey) orelse return error.OperationError;
+ const e = crypto.RSA_get0_e(rsa) orelse return error.OperationError;
+ const out = try exec.local_arena.alloc(u8, crypto.BN_num_bytes(e));
+ return out[0..crypto.BN_bn2bin(e, out.ptr)];
}
-/// Decrypts `data` with the private key (RSA-OAEP).
-pub fn decrypt(key: *const CryptoKey, data: []const u8, exec: *const Execution) ![]const u8 {
- return crypt(key, data, false, exec);
-}
+/// RSA-OAEP encrypt/decrypt. `encrypting` selects the init/op pair; the
+/// padding, OAEP digest (the hash stored on the key), MGF1 digest, label and
+/// the two-call output sizing are identical for both directions.
+pub fn crypt(
+ key: *const CryptoKey,
+ data: []const u8,
+ label: []const u8,
+ encrypting: bool,
+ exec: *const Execution,
+) ![]const u8 {
+ // The key must be an RSA-OAEP key with the matching usage.
+ if (key._type != .rsa) return error.InvalidAccessError;
+ if ((encrypting and !key.canEncrypt()) or (!encrypting and !key.canDecrypt())) {
+ return error.InvalidAccessError;
+ }
-/// Shared RSA-OAEP transform. `encrypting` selects the init/op pair; the
-/// padding, OAEP digest (the hash stored on the key), MGF1 digest and the
-/// two-call output sizing are identical for both directions.
-fn crypt(key: *const CryptoKey, data: []const u8, encrypting: bool, exec: *const Execution) ![]const u8 {
const hash_name = key._algorithm.hash orelse return error.OperationError;
const md = crypto.findDigest(hash_name) catch return error.OperationError;
@@ -247,6 +272,15 @@ fn crypt(key: *const CryptoKey, data: []const u8, encrypting: bool, exec: *const
if (crypto.EVP_PKEY_CTX_set_rsa_padding(ctx, crypto.RSA_PKCS1_OAEP_PADDING) != 1) return error.OperationError;
if (crypto.EVP_PKEY_CTX_set_rsa_oaep_md(ctx, md) != 1) return error.OperationError;
if (crypto.EVP_PKEY_CTX_set_rsa_mgf1_md(ctx, md) != 1) return error.OperationError;
+ if (label.len > 0) {
+ // The context takes ownership of the copy, but only on success.
+ const copy: [*]u8 = @ptrCast(crypto.OPENSSL_malloc(label.len) orelse return error.OutOfMemory);
+ @memcpy(copy[0..label.len], label);
+ if (crypto.EVP_PKEY_CTX_set0_rsa_oaep_label(ctx, copy, label.len) != 1) {
+ crypto.OPENSSL_free(copy);
+ return error.OperationError;
+ }
+ }
// First call (null output) reports the size; the second writes the buffer.
var out_len: usize = 0;
diff --git a/src/browser/webapi/crypto/algorithm.zig b/src/browser/webapi/crypto/algorithm.zig
index 0a3c13e11..983003860 100644
--- a/src/browser/webapi/crypto/algorithm.zig
+++ b/src/browser/webapi/crypto/algorithm.zig
@@ -187,7 +187,7 @@ pub const KeyData = union(enum) {
};
};
-/// Algorithm for `encrypt()` / `decrypt()`. AES-CBC/CTR/GCM share one struct
+/// Algorithm for `encrypt()` / `decrypt()`. AES-CBC/CTR/GCM and RSA-OAEP share one struct
/// (fields are mode-specific and optional) and dispatch on `name`; a bare string
/// is the fallback form.
pub const Encrypt = union(enum) {
@@ -195,6 +195,8 @@ pub const Encrypt = union(enum) {
additionalData: ?js.TypedArray(u8) = null,
counter: ?js.TypedArray(u8) = null,
iv: ?js.TypedArray(u8) = null,
+ /// RSA-OAEP.
+ label: ?js.TypedArray(u8) = null,
length: ?u32 = null,
name: []const u8,
tagLength: ?u32 = null,
diff --git a/src/sys/libcrypto.zig b/src/sys/libcrypto.zig
index b255091ba..9cb5dd1c9 100644
--- a/src/sys/libcrypto.zig
+++ b/src/sys/libcrypto.zig
@@ -152,10 +152,17 @@ pub extern fn RSA_free(rsa: ?*RSA) void;
pub extern fn RSA_generate_key_ex(rsa: *RSA, bits: c_int, e: *const BIGNUM, cb: ?*anyopaque) c_int;
pub extern fn RSAPublicKey_dup(rsa: *const RSA) ?*RSA;
pub extern fn EVP_PKEY_set1_RSA(pkey: *EVP_PKEY, key: *RSA) c_int;
+pub extern fn EVP_PKEY_get0_RSA(pkey: *const EVP_PKEY) ?*RSA;
+pub extern fn RSA_get0_e(rsa: *const RSA) ?*const BIGNUM;
pub extern fn BN_new() ?*BIGNUM;
pub extern fn BN_free(bn: ?*BIGNUM) void;
pub extern fn BN_bin2bn(in: [*c]const u8, len: usize, ret: ?*BIGNUM) ?*BIGNUM;
+pub extern fn BN_num_bytes(bn: *const BIGNUM) c_uint;
+pub extern fn BN_bn2bin(in: *const BIGNUM, out: [*]u8) usize;
+
+pub extern fn OPENSSL_malloc(size: usize) ?*anyopaque;
+pub extern fn OPENSSL_free(ptr: ?*anyopaque) void;
pub extern fn EVP_PKEY_encrypt_init(ctx: ?*EVP_PKEY_CTX) c_int;
pub extern fn EVP_PKEY_encrypt(ctx: ?*EVP_PKEY_CTX, out: [*c]u8, out_len: *usize, in: [*c]const u8, in_len: usize) c_int;
@@ -164,6 +171,8 @@ pub extern fn EVP_PKEY_decrypt(ctx: ?*EVP_PKEY_CTX, out: [*c]u8, out_len: *usize
pub extern fn EVP_PKEY_CTX_set_rsa_padding(ctx: ?*EVP_PKEY_CTX, padding: c_int) c_int;
pub extern fn EVP_PKEY_CTX_set_rsa_oaep_md(ctx: ?*EVP_PKEY_CTX, md: *const EVP_MD) c_int;
pub extern fn EVP_PKEY_CTX_set_rsa_mgf1_md(ctx: ?*EVP_PKEY_CTX, md: *const EVP_MD) c_int;
+/// Takes ownership of `label` (an OPENSSL_malloc buffer) on success only.
+pub extern fn EVP_PKEY_CTX_set0_rsa_oaep_label(ctx: ?*EVP_PKEY_CTX, label: [*]u8, label_len: usize) c_int;
const NID_X25519 = @as(c_int, 948);
pub const EVP_PKEY_X25519 = NID_X25519;