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