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