Manuel Pégourié-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Public Key abstraction layer |
| 3 | * |
| 4 | * Copyright (C) 2006-2013, Brainspark B.V. |
| 5 | * |
| 6 | * This file is part of PolarSSL (http://www.polarssl.org) |
| 7 | * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org> |
| 8 | * |
| 9 | * All rights reserved. |
| 10 | * |
| 11 | * This program is free software; you can redistribute it and/or modify |
| 12 | * it under the terms of the GNU General Public License as published by |
| 13 | * the Free Software Foundation; either version 2 of the License, or |
| 14 | * (at your option) any later version. |
| 15 | * |
| 16 | * This program is distributed in the hope that it will be useful, |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | * GNU General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU General Public License along |
| 22 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 23 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 24 | */ |
| 25 | |
| 26 | #include "polarssl/config.h" |
| 27 | |
Manuel Pégourié-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 28 | #include "polarssl/pk.h" |
| 29 | |
Manuel Pégourié-Gonnard | 81c313c | 2013-07-09 10:35:54 +0200 | [diff] [blame] | 30 | #if defined(POLARSSL_RSA_C) |
| 31 | #include "polarssl/rsa.h" |
| 32 | #endif |
| 33 | #if defined(POLARSSL_ECP_C) |
| 34 | #include "polarssl/ecp.h" |
| 35 | #endif |
| 36 | |
Manuel Pégourié-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 37 | #include <stdlib.h> |
| 38 | |
| 39 | /* |
| 40 | * Initialise a pk_context |
| 41 | */ |
| 42 | void pk_init( pk_context *ctx ) |
| 43 | { |
| 44 | if( ctx == NULL ) |
| 45 | return; |
| 46 | |
| 47 | ctx->type = POLARSSL_PK_NONE; |
| 48 | ctx->data = NULL; |
| 49 | } |
| 50 | |
| 51 | /* |
| 52 | * Free (the components of) a pk_context |
| 53 | */ |
| 54 | void pk_free( pk_context *ctx ) |
| 55 | { |
| 56 | if( ctx == NULL ) |
| 57 | return; |
| 58 | |
| 59 | switch( ctx->type ) |
| 60 | { |
| 61 | case POLARSSL_PK_NONE: |
| 62 | break; |
| 63 | |
Manuel Pégourié-Gonnard | 81c313c | 2013-07-09 10:35:54 +0200 | [diff] [blame] | 64 | #if defined(POLARSSL_RSA_C) |
Manuel Pégourié-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 65 | case POLARSSL_PK_RSA: |
| 66 | rsa_free( ctx->data ); |
| 67 | break; |
Manuel Pégourié-Gonnard | 81c313c | 2013-07-09 10:35:54 +0200 | [diff] [blame] | 68 | #endif |
Manuel Pégourié-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 69 | |
Manuel Pégourié-Gonnard | 81c313c | 2013-07-09 10:35:54 +0200 | [diff] [blame] | 70 | #if defined(POLARSSL_ECP_C) |
Manuel Pégourié-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 71 | case POLARSSL_PK_ECKEY: |
| 72 | case POLARSSL_PK_ECKEY_DH: |
| 73 | ecp_keypair_free( ctx->data ); |
| 74 | break; |
Manuel Pégourié-Gonnard | 81c313c | 2013-07-09 10:35:54 +0200 | [diff] [blame] | 75 | #endif |
Manuel Pégourié-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 76 | } |
| 77 | |
Manuel Pégourié-Gonnard | 1f73a65 | 2013-07-09 10:26:41 +0200 | [diff] [blame] | 78 | free( ctx-> data ); |
| 79 | |
Manuel Pégourié-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 80 | ctx->type = POLARSSL_PK_NONE; |
| 81 | ctx->data = NULL; |
| 82 | } |
| 83 | |
| 84 | /* |
| 85 | * Set a pk_context to a given type |
| 86 | */ |
| 87 | int pk_set_type( pk_context *ctx, pk_type_t type ) |
| 88 | { |
Manuel Pégourié-Gonnard | 81c313c | 2013-07-09 10:35:54 +0200 | [diff] [blame] | 89 | size_t size = 0; |
Manuel Pégourié-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 90 | |
Manuel Pégourié-Gonnard | 374e4b8 | 2013-07-09 10:21:34 +0200 | [diff] [blame] | 91 | if( ctx->type == type ) |
| 92 | return( 0 ); |
| 93 | |
| 94 | if( ctx->type != POLARSSL_PK_NONE ) |
| 95 | return( POLARSSL_ERR_PK_TYPE_MISMATCH ); |
| 96 | |
Manuel Pégourié-Gonnard | 81c313c | 2013-07-09 10:35:54 +0200 | [diff] [blame] | 97 | switch( type ) |
| 98 | { |
| 99 | #if defined(POLARSSL_RSA_C) |
| 100 | case POLARSSL_PK_RSA: |
| 101 | size = sizeof( rsa_context ); |
| 102 | break; |
| 103 | #endif |
| 104 | |
| 105 | #if defined(POLARSSL_ECP_C) |
| 106 | case POLARSSL_PK_ECKEY: |
| 107 | case POLARSSL_PK_ECKEY_DH: |
| 108 | size = sizeof( ecp_keypair ); |
| 109 | break; |
| 110 | #endif |
| 111 | |
| 112 | case POLARSSL_PK_NONE: |
Manuel Pégourié-Gonnard | 374e4b8 | 2013-07-09 10:21:34 +0200 | [diff] [blame] | 113 | ; /* Cannot happen, but the cmpiler doesn't know */ |
Manuel Pégourié-Gonnard | 81c313c | 2013-07-09 10:35:54 +0200 | [diff] [blame] | 114 | } |
Manuel Pégourié-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 115 | |
| 116 | if( ( ctx->data = malloc( size ) ) == NULL ) |
Manuel Pégourié-Gonnard | 7a6c946 | 2013-07-09 10:04:07 +0200 | [diff] [blame] | 117 | return( POLARSSL_ERR_PK_MALLOC_FAILED ); |
Manuel Pégourié-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 118 | |
| 119 | memset( ctx->data, 0, size ); |
| 120 | ctx->type = type; |
| 121 | |
| 122 | return( 0 ); |
| 123 | } |