blob: e6e2745cd76e92f54bc803730e248c9786e8ee1f [file] [log] [blame]
Avi Drissman64595482022-09-14 20:52:291// Copyright 2013 The Chromium Authors
[email protected]03a07b2e2013-02-11 20:13:452// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
bnc3698b0a02016-12-09 23:36:505#ifndef NET_ANDROID_KEYSTORE_H_
6#define NET_ANDROID_KEYSTORE_H_
[email protected]03a07b2e2013-02-11 20:13:457
8#include <jni.h>
wtc69f8ea82015-06-04 00:08:139#include <stdint.h>
[email protected]03a07b2e2013-02-11 20:13:4510
11#include <string>
12#include <vector>
13
[email protected]eeff8532014-07-11 22:07:5914#include "base/android/scoped_java_ref.h"
David Benjamin9ba36b02017-11-10 19:01:5315#include "base/containers/span.h"
David Benjamin5b4410e2017-11-10 21:50:2316#include "base/strings/string_piece.h"
[email protected]03a07b2e2013-02-11 20:13:4517
[email protected]03a07b2e2013-02-11 20:13:4518// Misc functions to access the Android platform KeyStore.
19
Tsuyoshi Horo4f516be2022-06-14 11:53:1320namespace net::android {
[email protected]03a07b2e2013-02-11 20:13:4521
22// Define a list of constants describing private key types. The
23// values are shared with Java through org.chromium.net.PrivateKeyType.
24// Example: PRIVATE_KEY_TYPE_RSA.
mkosibaf6ebbf6b2014-09-30 14:42:3925//
26// A Java counterpart will be generated for this enum.
27// GENERATED_JAVA_ENUM_PACKAGE: org.chromium.net
[email protected]03a07b2e2013-02-11 20:13:4528enum PrivateKeyType {
mkosibaf6ebbf6b2014-09-30 14:42:3929 PRIVATE_KEY_TYPE_RSA = 0,
davidben370b6fa2015-06-09 18:34:2730 // Obsolete: PRIVATE_KEY_TYPE_DSA = 1,
mkosibaf6ebbf6b2014-09-30 14:42:3931 PRIVATE_KEY_TYPE_ECDSA = 2,
32 PRIVATE_KEY_TYPE_INVALID = 255,
[email protected]03a07b2e2013-02-11 20:13:4533};
34
David Benjaminb65b0732018-11-09 20:33:5335// Returns the name of the class which implements the private key.
36std::string GetPrivateKeyClassName(const base::android::JavaRef<jobject>& key);
37
David Benjamin08d50eb2019-04-03 21:05:3438// Returns whether |key| supports the signature algorithm |algorithm|.
39bool PrivateKeySupportsSignature(const base::android::JavaRef<jobject>& key,
40 base::StringPiece algorithm);
41
42// Returns whether |key| supports the encryption algorithm |algorithm|.
43bool PrivateKeySupportsCipher(const base::android::JavaRef<jobject>& key,
44 base::StringPiece algorithm);
45
David Benjamin5b4410e2017-11-10 21:50:2346// Compute the signature of a given input using a private key. For more
47// details, please read the comments for the signWithPrivateKey method in
48// AndroidKeyStore.java.
[email protected]03a07b2e2013-02-11 20:13:4549//
50// |private_key| is a JNI reference for the private key.
David Benjamin5b4410e2017-11-10 21:50:2351// |algorithm| is the name of the algorithm to sign.
52// |input| is the input to sign.
[email protected]03a07b2e2013-02-11 20:13:4553// |signature| will receive the signature on success.
54// Returns true on success, false on failure.
David Benjamin5b4410e2017-11-10 21:50:2355bool SignWithPrivateKey(const base::android::JavaRef<jobject>& private_key,
56 base::StringPiece algorithm,
57 base::span<const uint8_t> input,
58 std::vector<uint8_t>* signature);
[email protected]03a07b2e2013-02-11 20:13:4559
David Benjamin08d50eb2019-04-03 21:05:3460// Encrypts a given input using a private key. For more details, please read the
61// comments for the encryptWithPrivateKey method in AndroidKeyStore.java.
62//
63// |private_key| is a JNI reference for the private key.
64// |algorithm| is the name of the algorithm to use.
65// |input| is the input to encrypt.
66// |ciphertext| will receive the ciphertext on success.
67// Returns true on success, false on failure.
68bool EncryptWithPrivateKey(const base::android::JavaRef<jobject>& private_key,
69 base::StringPiece algorithm,
70 base::span<const uint8_t> input,
71 std::vector<uint8_t>* ciphertext);
72
Tsuyoshi Horo4f516be2022-06-14 11:53:1373} // namespace net::android
[email protected]03a07b2e2013-02-11 20:13:4574
bnc3698b0a02016-12-09 23:36:5075#endif // NET_ANDROID_KEYSTORE_H_