1239281Sgonzo/* 2239281Sgonzo * Crypto wrapper for internal crypto implementation - RSA parts 3239281Sgonzo * Copyright (c) 2006-2009, Jouni Malinen <j@w1.fi> 4239281Sgonzo * 5239281Sgonzo * This software may be distributed under the terms of the BSD license. 6239281Sgonzo * See README for more details. 7239281Sgonzo */ 8239281Sgonzo 9239281Sgonzo#include "includes.h" 10239281Sgonzo 11239281Sgonzo#include "common.h" 12239281Sgonzo#include "crypto.h" 13239281Sgonzo#include "tls/rsa.h" 14239281Sgonzo#include "tls/pkcs1.h" 15239281Sgonzo#include "tls/pkcs8.h" 16239281Sgonzo 17239281Sgonzo/* Dummy structures; these are just typecast to struct crypto_rsa_key */ 18239281Sgonzostruct crypto_public_key; 19239281Sgonzostruct crypto_private_key; 20239281Sgonzo 21239281Sgonzo 22239281Sgonzostruct crypto_public_key * crypto_public_key_import(const u8 *key, size_t len) 23239281Sgonzo{ 24239281Sgonzo return (struct crypto_public_key *) 25239281Sgonzo crypto_rsa_import_public_key(key, len); 26239281Sgonzo} 27239281Sgonzo 28239281Sgonzo 29239281Sgonzostruct crypto_private_key * crypto_private_key_import(const u8 *key, 30239281Sgonzo size_t len, 31239281Sgonzo const char *passwd) 32239281Sgonzo{ 33239281Sgonzo struct crypto_private_key *res; 34239281Sgonzo 35239281Sgonzo /* First, check for possible PKCS #8 encoding */ 36239281Sgonzo res = pkcs8_key_import(key, len); 37239281Sgonzo if (res) 38239281Sgonzo return res; 39239281Sgonzo 40239281Sgonzo if (passwd) { 41239281Sgonzo /* Try to parse as encrypted PKCS #8 */ 42239281Sgonzo res = pkcs8_enc_key_import(key, len, passwd); 43239281Sgonzo if (res) 44239281Sgonzo return res; 45239281Sgonzo } 46239281Sgonzo 47239281Sgonzo /* Not PKCS#8, so try to import PKCS #1 encoded RSA private key */ 48239281Sgonzo wpa_printf(MSG_DEBUG, "Trying to parse PKCS #1 encoded RSA private " 49239281Sgonzo "key"); 50239281Sgonzo return (struct crypto_private_key *) 51239281Sgonzo crypto_rsa_import_private_key(key, len); 52239281Sgonzo} 53239281Sgonzo 54239281Sgonzo 55239281Sgonzostruct crypto_public_key * crypto_public_key_from_cert(const u8 *buf, 56239281Sgonzo size_t len) 57239281Sgonzo{ 58239281Sgonzo /* No X.509 support in crypto_internal.c */ 59239281Sgonzo return NULL; 60239281Sgonzo} 61239281Sgonzo 62239281Sgonzo 63239281Sgonzoint crypto_public_key_encrypt_pkcs1_v15(struct crypto_public_key *key, 64239281Sgonzo const u8 *in, size_t inlen, 65239281Sgonzo u8 *out, size_t *outlen) 66239281Sgonzo{ 67239281Sgonzo return pkcs1_encrypt(2, (struct crypto_rsa_key *) key, 68239281Sgonzo 0, in, inlen, out, outlen); 69239281Sgonzo} 70239281Sgonzo 71239281Sgonzo 72239281Sgonzoint crypto_private_key_decrypt_pkcs1_v15(struct crypto_private_key *key, 73239281Sgonzo const u8 *in, size_t inlen, 74239281Sgonzo u8 *out, size_t *outlen) 75239281Sgonzo{ 76239281Sgonzo return pkcs1_v15_private_key_decrypt((struct crypto_rsa_key *) key, 77239281Sgonzo in, inlen, out, outlen); 78239281Sgonzo} 79239281Sgonzo 80239281Sgonzo 81239281Sgonzoint crypto_private_key_sign_pkcs1(struct crypto_private_key *key, 82239281Sgonzo const u8 *in, size_t inlen, 83239281Sgonzo u8 *out, size_t *outlen) 84239281Sgonzo{ 85239281Sgonzo return pkcs1_encrypt(1, (struct crypto_rsa_key *) key, 86239281Sgonzo 1, in, inlen, out, outlen); 87239281Sgonzo} 88239281Sgonzo 89239281Sgonzo 90239281Sgonzovoid crypto_public_key_free(struct crypto_public_key *key) 91239281Sgonzo{ 92239281Sgonzo crypto_rsa_free((struct crypto_rsa_key *) key); 93239281Sgonzo} 94239281Sgonzo 95239281Sgonzo 96239281Sgonzovoid crypto_private_key_free(struct crypto_private_key *key) 97239281Sgonzo{ 98239281Sgonzo crypto_rsa_free((struct crypto_rsa_key *) key); 99239281Sgonzo} 100239281Sgonzo 101239281Sgonzo 102239281Sgonzoint crypto_public_key_decrypt_pkcs1(struct crypto_public_key *key, 103239281Sgonzo const u8 *crypt, size_t crypt_len, 104239281Sgonzo u8 *plain, size_t *plain_len) 105239281Sgonzo{ 106239281Sgonzo return pkcs1_decrypt_public_key((struct crypto_rsa_key *) key, 107239281Sgonzo crypt, crypt_len, plain, plain_len); 108239281Sgonzo} 109239281Sgonzo