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; |
Manuel Pégourié-Gonnard | 244569f | 2013-07-10 09:46:30 +0200 | [diff] [blame^] | 49 | ctx->dont_free = 0; |
Manuel Pégourié-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | /* |
| 53 | * Free (the components of) a pk_context |
| 54 | */ |
| 55 | void pk_free( pk_context *ctx ) |
| 56 | { |
| 57 | if( ctx == NULL ) |
| 58 | return; |
| 59 | |
| 60 | switch( ctx->type ) |
| 61 | { |
| 62 | case POLARSSL_PK_NONE: |
| 63 | break; |
| 64 | |
Manuel Pégourié-Gonnard | 81c313c | 2013-07-09 10:35:54 +0200 | [diff] [blame] | 65 | #if defined(POLARSSL_RSA_C) |
Manuel Pégourié-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 66 | case POLARSSL_PK_RSA: |
| 67 | rsa_free( ctx->data ); |
| 68 | break; |
Manuel Pégourié-Gonnard | 81c313c | 2013-07-09 10:35:54 +0200 | [diff] [blame] | 69 | #endif |
Manuel Pégourié-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 70 | |
Manuel Pégourié-Gonnard | 81c313c | 2013-07-09 10:35:54 +0200 | [diff] [blame] | 71 | #if defined(POLARSSL_ECP_C) |
Manuel Pégourié-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 72 | case POLARSSL_PK_ECKEY: |
| 73 | case POLARSSL_PK_ECKEY_DH: |
| 74 | ecp_keypair_free( ctx->data ); |
| 75 | break; |
Manuel Pégourié-Gonnard | 81c313c | 2013-07-09 10:35:54 +0200 | [diff] [blame] | 76 | #endif |
Manuel Pégourié-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 77 | } |
| 78 | |
Manuel Pégourié-Gonnard | 244569f | 2013-07-10 09:46:30 +0200 | [diff] [blame^] | 79 | if( ! ctx->dont_free ) |
| 80 | free( ctx->data ); |
Manuel Pégourié-Gonnard | 1f73a65 | 2013-07-09 10:26:41 +0200 | [diff] [blame] | 81 | |
Manuel Pégourié-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 82 | ctx->type = POLARSSL_PK_NONE; |
| 83 | ctx->data = NULL; |
| 84 | } |
| 85 | |
| 86 | /* |
| 87 | * Set a pk_context to a given type |
| 88 | */ |
| 89 | int pk_set_type( pk_context *ctx, pk_type_t type ) |
| 90 | { |
Manuel Pégourié-Gonnard | 81c313c | 2013-07-09 10:35:54 +0200 | [diff] [blame] | 91 | size_t size = 0; |
Manuel Pégourié-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 92 | |
Manuel Pégourié-Gonnard | 374e4b8 | 2013-07-09 10:21:34 +0200 | [diff] [blame] | 93 | if( ctx->type == type ) |
| 94 | return( 0 ); |
| 95 | |
| 96 | if( ctx->type != POLARSSL_PK_NONE ) |
| 97 | return( POLARSSL_ERR_PK_TYPE_MISMATCH ); |
| 98 | |
Manuel Pégourié-Gonnard | 81c313c | 2013-07-09 10:35:54 +0200 | [diff] [blame] | 99 | switch( type ) |
| 100 | { |
| 101 | #if defined(POLARSSL_RSA_C) |
| 102 | case POLARSSL_PK_RSA: |
| 103 | size = sizeof( rsa_context ); |
| 104 | break; |
| 105 | #endif |
| 106 | |
| 107 | #if defined(POLARSSL_ECP_C) |
| 108 | case POLARSSL_PK_ECKEY: |
| 109 | case POLARSSL_PK_ECKEY_DH: |
| 110 | size = sizeof( ecp_keypair ); |
| 111 | break; |
| 112 | #endif |
| 113 | |
| 114 | case POLARSSL_PK_NONE: |
Manuel Pégourié-Gonnard | 374e4b8 | 2013-07-09 10:21:34 +0200 | [diff] [blame] | 115 | ; /* Cannot happen, but the cmpiler doesn't know */ |
Manuel Pégourié-Gonnard | 81c313c | 2013-07-09 10:35:54 +0200 | [diff] [blame] | 116 | } |
Manuel Pégourié-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 117 | |
| 118 | if( ( ctx->data = malloc( size ) ) == NULL ) |
Manuel Pégourié-Gonnard | 7a6c946 | 2013-07-09 10:04:07 +0200 | [diff] [blame] | 119 | return( POLARSSL_ERR_PK_MALLOC_FAILED ); |
Manuel Pégourié-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 120 | |
| 121 | memset( ctx->data, 0, size ); |
| 122 | ctx->type = type; |
| 123 | |
| 124 | return( 0 ); |
| 125 | } |
Manuel Pégourié-Gonnard | 244569f | 2013-07-10 09:46:30 +0200 | [diff] [blame^] | 126 | |
| 127 | #if defined(POLARSSL_RSA_C) |
| 128 | /* |
| 129 | * Wrap an RSA context in a PK context |
| 130 | */ |
| 131 | int pk_wrap_rsa( pk_context *ctx, const rsa_context *rsa) |
| 132 | { |
| 133 | if( ctx->type != POLARSSL_PK_NONE ) |
| 134 | return( POLARSSL_ERR_PK_TYPE_MISMATCH ); |
| 135 | |
| 136 | ctx->type = POLARSSL_PK_RSA; |
| 137 | ctx->data = (rsa_context *) rsa; |
| 138 | ctx->dont_free = 1; |
| 139 | |
| 140 | return( 0 ); |
| 141 | } |
| 142 | #endif |