1/*
2 * Crypto wrapper for internal crypto implementation - RSA parts
3 * Copyright (c) 2006-2009, Jouni Malinen <j@w1.fi>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 *
9 * Alternatively, this software may be distributed under the terms of BSD
10 * license.
11 *
12 * See README and COPYING for more details.
13 */
14
15#include "includes.h"
16
17#include "common.h"
18#include "crypto.h"
19#include "tls/rsa.h"
20#include "tls/bignum.h"
21#include "tls/pkcs1.h"
22#include "tls/pkcs8.h"
23
24/* Dummy structures; these are just typecast to struct crypto_rsa_key */
25struct crypto_public_key;
26struct crypto_private_key;
27
28
29struct crypto_public_key * crypto_public_key_import(const u8 *key, size_t len)
30{
31	return (struct crypto_public_key *)
32		crypto_rsa_import_public_key(key, len);
33}
34
35
36struct crypto_private_key * crypto_private_key_import(const u8 *key,
37						      size_t len,
38						      const char *passwd)
39{
40	struct crypto_private_key *res;
41
42	/* First, check for possible PKCS #8 encoding */
43	res = pkcs8_key_import(key, len);
44	if (res)
45		return res;
46
47	if (passwd) {
48		/* Try to parse as encrypted PKCS #8 */
49		res = pkcs8_enc_key_import(key, len, passwd);
50		if (res)
51			return res;
52	}
53
54	/* Not PKCS#8, so try to import PKCS #1 encoded RSA private key */
55	wpa_printf(MSG_DEBUG, "Trying to parse PKCS #1 encoded RSA private "
56		   "key");
57	return (struct crypto_private_key *)
58		crypto_rsa_import_private_key(key, len);
59}
60
61
62struct crypto_public_key * crypto_public_key_from_cert(const u8 *buf,
63						       size_t len)
64{
65	/* No X.509 support in crypto_internal.c */
66	return NULL;
67}
68
69
70int crypto_public_key_encrypt_pkcs1_v15(struct crypto_public_key *key,
71					const u8 *in, size_t inlen,
72					u8 *out, size_t *outlen)
73{
74	return pkcs1_encrypt(2, (struct crypto_rsa_key *) key,
75			     0, in, inlen, out, outlen);
76}
77
78
79int crypto_private_key_decrypt_pkcs1_v15(struct crypto_private_key *key,
80					 const u8 *in, size_t inlen,
81					 u8 *out, size_t *outlen)
82{
83	return pkcs1_v15_private_key_decrypt((struct crypto_rsa_key *) key,
84					     in, inlen, out, outlen);
85}
86
87
88int crypto_private_key_sign_pkcs1(struct crypto_private_key *key,
89				  const u8 *in, size_t inlen,
90				  u8 *out, size_t *outlen)
91{
92	return pkcs1_encrypt(1, (struct crypto_rsa_key *) key,
93			     1, in, inlen, out, outlen);
94}
95
96
97void crypto_public_key_free(struct crypto_public_key *key)
98{
99	crypto_rsa_free((struct crypto_rsa_key *) key);
100}
101
102
103void crypto_private_key_free(struct crypto_private_key *key)
104{
105	crypto_rsa_free((struct crypto_rsa_key *) key);
106}
107
108
109int crypto_public_key_decrypt_pkcs1(struct crypto_public_key *key,
110				    const u8 *crypt, size_t crypt_len,
111				    u8 *plain, size_t *plain_len)
112{
113	return pkcs1_decrypt_public_key((struct crypto_rsa_key *) key,
114					crypt, crypt_len, plain, plain_len);
115}
116