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