blob: 1186ef6dd4e6abf17a94def01fa0a79f1592ae06 [file] [log] [blame]
[email protected]03a07b2e2013-02-11 20:13:451// Copyright (c) 2013 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef NET_ANDROID_KEYSTORE_H
6#define NET_ANDROID_KEYSTORE_H
7
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"
[email protected]d069c11a2013-04-13 00:01:5515#include "base/strings/string_piece.h"
[email protected]03a07b2e2013-02-11 20:13:4516#include "net/base/net_export.h"
[email protected]536fd0b2013-03-14 17:41:5717#include "net/ssl/ssl_client_cert_type.h"
[email protected]03a07b2e2013-02-11 20:13:4518
[email protected]03a07b2e2013-02-11 20:13:4519// Misc functions to access the Android platform KeyStore.
20
21namespace net {
22namespace android {
23
[email protected]eeff8532014-07-11 22:07:5924struct AndroidEVP_PKEY;
25
[email protected]03a07b2e2013-02-11 20:13:4526// Define a list of constants describing private key types. The
27// values are shared with Java through org.chromium.net.PrivateKeyType.
28// Example: PRIVATE_KEY_TYPE_RSA.
mkosibaf6ebbf6b2014-09-30 14:42:3929//
30// A Java counterpart will be generated for this enum.
31// GENERATED_JAVA_ENUM_PACKAGE: org.chromium.net
[email protected]03a07b2e2013-02-11 20:13:4532enum PrivateKeyType {
mkosibaf6ebbf6b2014-09-30 14:42:3933 PRIVATE_KEY_TYPE_RSA = 0,
34 PRIVATE_KEY_TYPE_DSA = 1,
35 PRIVATE_KEY_TYPE_ECDSA = 2,
36 PRIVATE_KEY_TYPE_INVALID = 255,
[email protected]03a07b2e2013-02-11 20:13:4537};
38
39// Returns the modulus of a given RSAPrivateKey platform object,
40// as a series of bytes, in big-endian representation. This can be
41// used with BN_bin2bn() to convert to an OpenSSL BIGNUM.
42//
43// |private_key| is a JNI reference for the private key.
44// |modulus| will receive the modulus bytes on success.
45// Returns true on success, or false on failure (e.g. if the key
46// is not RSA).
[email protected]4f2b94f2013-02-22 21:06:3747NET_EXPORT bool GetRSAKeyModulus(jobject private_key,
wtc69f8ea82015-06-04 00:08:1348 std::vector<uint8_t>* modulus);
[email protected]03a07b2e2013-02-11 20:13:4549
50// Returns the Q parameter of a given DSAPrivateKey platform object,
51// as a series of bytes, in big-endian representation. This can be used
52// with BN_bin2bn() to convert to an OpenSSL BIGNUM.
53// |private_key| is a JNI reference for the private key.
54// |q| will receive the result bytes on success.
55// Returns true on success, or false on failure (e.g. if the key is
56// not DSA).
wtc69f8ea82015-06-04 00:08:1357NET_EXPORT bool GetDSAKeyParamQ(jobject private_key, std::vector<uint8_t>* q);
[email protected]03a07b2e2013-02-11 20:13:4558
59// Returns the order parameter of a given ECPrivateKey platform object,
60// as a series of bytes, in big-endian representation. This can be used
61// with BN_bin2bn() to convert to an OpenSSL BIGNUM.
62// |private_key| is a JNI reference for the private key.
63// |order| will receive the result bytes on success.
64// Returns true on success, or false on failure (e.g. if the key is
65// not EC).
wtc69f8ea82015-06-04 00:08:1366bool GetECKeyOrder(jobject private_key, std::vector<uint8_t>* order);
[email protected]03a07b2e2013-02-11 20:13:4567
68// Returns the encoded PKCS#8 representation of a private key.
69// This only works on Android 4.0.3 and older releases for platform keys
70// (i.e. all keys except those explicitely generated by the application).
71// |private_key| is a JNI reference for the private key.
72// |encoded| will receive the encoded data on success.
73// Returns true on success, or false on failure (e.g. on 4.0.4 or higher).
74bool GetPrivateKeyEncodedBytes(jobject private_key,
wtc69f8ea82015-06-04 00:08:1375 std::vector<uint8_t>* encoded);
[email protected]03a07b2e2013-02-11 20:13:4576
77// Compute the signature of a given message, which is actually a hash,
78// using a private key. For more details, please read the comments for the
79// rawSignDigestWithPrivateKey method in AndroidKeyStore.java.
80//
81// |private_key| is a JNI reference for the private key.
82// |digest| is the input digest.
83// |signature| will receive the signature on success.
84// Returns true on success, false on failure.
85//
wtc69f8ea82015-06-04 00:08:1386NET_EXPORT bool RawSignDigestWithPrivateKey(jobject private_key,
87 const base::StringPiece& digest,
88 std::vector<uint8_t>* signature);
[email protected]03a07b2e2013-02-11 20:13:4589
90// Return the PrivateKeyType of a given private key.
91// |private_key| is a JNI reference for the private key.
92// Returns a PrivateKeyType, while will be CLIENT_CERT_INVALID_TYPE
93// on error.
[email protected]4f2b94f2013-02-22 21:06:3794NET_EXPORT PrivateKeyType GetPrivateKeyType(jobject private_key);
[email protected]03a07b2e2013-02-11 20:13:4595
[email protected]eeff8532014-07-11 22:07:5996// Returns a handle to the system AndroidEVP_PKEY object used to back a given
97// private_key object. This must *only* be used for RSA private keys on Android
98// < 4.2. Technically, this is only guaranteed to work if the system image
99// contains a vanilla implementation of the Java API frameworks based on Harmony
100// + OpenSSL.
[email protected]03a07b2e2013-02-11 20:13:45101//
102// |private_key| is a JNI reference for the private key.
[email protected]eeff8532014-07-11 22:07:59103// Returns an AndroidEVP_PKEY* handle, or NULL in case of error.
[email protected]03a07b2e2013-02-11 20:13:45104//
105// Note: Despite its name and return type, this function doesn't know
106// anything about OpenSSL, it just type-casts a system pointer that
107// is passed as an int through JNI. As such, it never increments
108// the returned key's reference count.
[email protected]eeff8532014-07-11 22:07:59109AndroidEVP_PKEY* GetOpenSSLSystemHandleForPrivateKey(jobject private_key);
110
111// Returns a JNI reference to the OpenSSLEngine object which is used to back a
112// given private_key object. This must *only* be used for RSA private keys on
113// Android < 4.2. Technically, this is only guaranteed to work if the system
114// image contains a vanilla implementation of the Java API frameworks based on
115// Harmony + OpenSSL.
116base::android::ScopedJavaLocalRef<jobject> GetOpenSSLEngineForPrivateKey(
117 jobject private_key);
[email protected]03a07b2e2013-02-11 20:13:45118
[email protected]2816e1f2014-02-15 00:54:27119NET_EXPORT void ReleaseKey(jobject private_key);
120
[email protected]03a07b2e2013-02-11 20:13:45121// Register JNI methods
122NET_EXPORT bool RegisterKeyStore(JNIEnv* env);
123
124} // namespace android
125} // namespace net
126
127#endif // NET_ANDROID_KEYSTORE_H