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