13ff40c12SJohn Marino /*
23ff40c12SJohn Marino * Crypto wrapper for internal crypto implementation - RSA parts
33ff40c12SJohn Marino * Copyright (c) 2006-2009, Jouni Malinen <j@w1.fi>
43ff40c12SJohn Marino *
53ff40c12SJohn Marino * This software may be distributed under the terms of the BSD license.
63ff40c12SJohn Marino * See README for more details.
73ff40c12SJohn Marino */
83ff40c12SJohn Marino
93ff40c12SJohn Marino #include "includes.h"
103ff40c12SJohn Marino
113ff40c12SJohn Marino #include "common.h"
123ff40c12SJohn Marino #include "crypto.h"
133ff40c12SJohn Marino #include "tls/rsa.h"
143ff40c12SJohn Marino #include "tls/pkcs1.h"
153ff40c12SJohn Marino #include "tls/pkcs8.h"
163ff40c12SJohn Marino
173ff40c12SJohn Marino /* Dummy structures; these are just typecast to struct crypto_rsa_key */
183ff40c12SJohn Marino struct crypto_public_key;
193ff40c12SJohn Marino struct crypto_private_key;
203ff40c12SJohn Marino
213ff40c12SJohn Marino
crypto_public_key_import(const u8 * key,size_t len)223ff40c12SJohn Marino struct crypto_public_key * crypto_public_key_import(const u8 *key, size_t len)
233ff40c12SJohn Marino {
243ff40c12SJohn Marino return (struct crypto_public_key *)
253ff40c12SJohn Marino crypto_rsa_import_public_key(key, len);
263ff40c12SJohn Marino }
273ff40c12SJohn Marino
283ff40c12SJohn Marino
29*a1157835SDaniel Fojt struct crypto_public_key *
crypto_public_key_import_parts(const u8 * n,size_t n_len,const u8 * e,size_t e_len)30*a1157835SDaniel Fojt crypto_public_key_import_parts(const u8 *n, size_t n_len,
31*a1157835SDaniel Fojt const u8 *e, size_t e_len)
32*a1157835SDaniel Fojt {
33*a1157835SDaniel Fojt return (struct crypto_public_key *)
34*a1157835SDaniel Fojt crypto_rsa_import_public_key_parts(n, n_len, e, e_len);
35*a1157835SDaniel Fojt }
36*a1157835SDaniel Fojt
37*a1157835SDaniel Fojt
crypto_private_key_import(const u8 * key,size_t len,const char * passwd)383ff40c12SJohn Marino struct crypto_private_key * crypto_private_key_import(const u8 *key,
393ff40c12SJohn Marino size_t len,
403ff40c12SJohn Marino const char *passwd)
413ff40c12SJohn Marino {
423ff40c12SJohn Marino struct crypto_private_key *res;
433ff40c12SJohn Marino
443ff40c12SJohn Marino /* First, check for possible PKCS #8 encoding */
453ff40c12SJohn Marino res = pkcs8_key_import(key, len);
463ff40c12SJohn Marino if (res)
473ff40c12SJohn Marino return res;
483ff40c12SJohn Marino
493ff40c12SJohn Marino if (passwd) {
503ff40c12SJohn Marino /* Try to parse as encrypted PKCS #8 */
513ff40c12SJohn Marino res = pkcs8_enc_key_import(key, len, passwd);
523ff40c12SJohn Marino if (res)
533ff40c12SJohn Marino return res;
543ff40c12SJohn Marino }
553ff40c12SJohn Marino
563ff40c12SJohn Marino /* Not PKCS#8, so try to import PKCS #1 encoded RSA private key */
573ff40c12SJohn Marino wpa_printf(MSG_DEBUG, "Trying to parse PKCS #1 encoded RSA private "
583ff40c12SJohn Marino "key");
593ff40c12SJohn Marino return (struct crypto_private_key *)
603ff40c12SJohn Marino crypto_rsa_import_private_key(key, len);
613ff40c12SJohn Marino }
623ff40c12SJohn Marino
633ff40c12SJohn Marino
crypto_public_key_from_cert(const u8 * buf,size_t len)643ff40c12SJohn Marino struct crypto_public_key * crypto_public_key_from_cert(const u8 *buf,
653ff40c12SJohn Marino size_t len)
663ff40c12SJohn Marino {
673ff40c12SJohn Marino /* No X.509 support in crypto_internal.c */
683ff40c12SJohn Marino return NULL;
693ff40c12SJohn Marino }
703ff40c12SJohn Marino
713ff40c12SJohn Marino
crypto_public_key_encrypt_pkcs1_v15(struct crypto_public_key * key,const u8 * in,size_t inlen,u8 * out,size_t * outlen)723ff40c12SJohn Marino int crypto_public_key_encrypt_pkcs1_v15(struct crypto_public_key *key,
733ff40c12SJohn Marino const u8 *in, size_t inlen,
743ff40c12SJohn Marino u8 *out, size_t *outlen)
753ff40c12SJohn Marino {
763ff40c12SJohn Marino return pkcs1_encrypt(2, (struct crypto_rsa_key *) key,
773ff40c12SJohn Marino 0, in, inlen, out, outlen);
783ff40c12SJohn Marino }
793ff40c12SJohn Marino
803ff40c12SJohn Marino
crypto_private_key_decrypt_pkcs1_v15(struct crypto_private_key * key,const u8 * in,size_t inlen,u8 * out,size_t * outlen)813ff40c12SJohn Marino int crypto_private_key_decrypt_pkcs1_v15(struct crypto_private_key *key,
823ff40c12SJohn Marino const u8 *in, size_t inlen,
833ff40c12SJohn Marino u8 *out, size_t *outlen)
843ff40c12SJohn Marino {
853ff40c12SJohn Marino return pkcs1_v15_private_key_decrypt((struct crypto_rsa_key *) key,
863ff40c12SJohn Marino in, inlen, out, outlen);
873ff40c12SJohn Marino }
883ff40c12SJohn Marino
893ff40c12SJohn Marino
crypto_private_key_sign_pkcs1(struct crypto_private_key * key,const u8 * in,size_t inlen,u8 * out,size_t * outlen)903ff40c12SJohn Marino int crypto_private_key_sign_pkcs1(struct crypto_private_key *key,
913ff40c12SJohn Marino const u8 *in, size_t inlen,
923ff40c12SJohn Marino u8 *out, size_t *outlen)
933ff40c12SJohn Marino {
943ff40c12SJohn Marino return pkcs1_encrypt(1, (struct crypto_rsa_key *) key,
953ff40c12SJohn Marino 1, in, inlen, out, outlen);
963ff40c12SJohn Marino }
973ff40c12SJohn Marino
983ff40c12SJohn Marino
crypto_public_key_free(struct crypto_public_key * key)993ff40c12SJohn Marino void crypto_public_key_free(struct crypto_public_key *key)
1003ff40c12SJohn Marino {
1013ff40c12SJohn Marino crypto_rsa_free((struct crypto_rsa_key *) key);
1023ff40c12SJohn Marino }
1033ff40c12SJohn Marino
1043ff40c12SJohn Marino
crypto_private_key_free(struct crypto_private_key * key)1053ff40c12SJohn Marino void crypto_private_key_free(struct crypto_private_key *key)
1063ff40c12SJohn Marino {
1073ff40c12SJohn Marino crypto_rsa_free((struct crypto_rsa_key *) key);
1083ff40c12SJohn Marino }
1093ff40c12SJohn Marino
1103ff40c12SJohn Marino
crypto_public_key_decrypt_pkcs1(struct crypto_public_key * key,const u8 * crypt,size_t crypt_len,u8 * plain,size_t * plain_len)1113ff40c12SJohn Marino int crypto_public_key_decrypt_pkcs1(struct crypto_public_key *key,
1123ff40c12SJohn Marino const u8 *crypt, size_t crypt_len,
1133ff40c12SJohn Marino u8 *plain, size_t *plain_len)
1143ff40c12SJohn Marino {
1153ff40c12SJohn Marino return pkcs1_decrypt_public_key((struct crypto_rsa_key *) key,
1163ff40c12SJohn Marino crypt, crypt_len, plain, plain_len);
1173ff40c12SJohn Marino }
118