155714Skris/* crypto/pem/pem_seal.c */ 255714Skris/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 355714Skris * All rights reserved. 455714Skris * 555714Skris * This package is an SSL implementation written 655714Skris * by Eric Young (eay@cryptsoft.com). 755714Skris * The implementation was written so as to conform with Netscapes SSL. 8280304Sjkim * 955714Skris * This library is free for commercial and non-commercial use as long as 1055714Skris * the following conditions are aheared to. The following conditions 1155714Skris * apply to all code found in this distribution, be it the RC4, RSA, 1255714Skris * lhash, DES, etc., code; not just the SSL code. The SSL documentation 1355714Skris * included with this distribution is covered by the same copyright terms 1455714Skris * except that the holder is Tim Hudson (tjh@cryptsoft.com). 15280304Sjkim * 1655714Skris * Copyright remains Eric Young's, and as such any Copyright notices in 1755714Skris * the code are not to be removed. 1855714Skris * If this package is used in a product, Eric Young should be given attribution 1955714Skris * as the author of the parts of the library used. 2055714Skris * This can be in the form of a textual message at program startup or 2155714Skris * in documentation (online or textual) provided with the package. 22280304Sjkim * 2355714Skris * Redistribution and use in source and binary forms, with or without 2455714Skris * modification, are permitted provided that the following conditions 2555714Skris * are met: 2655714Skris * 1. Redistributions of source code must retain the copyright 2755714Skris * notice, this list of conditions and the following disclaimer. 2855714Skris * 2. Redistributions in binary form must reproduce the above copyright 2955714Skris * notice, this list of conditions and the following disclaimer in the 3055714Skris * documentation and/or other materials provided with the distribution. 3155714Skris * 3. All advertising materials mentioning features or use of this software 3255714Skris * must display the following acknowledgement: 3355714Skris * "This product includes cryptographic software written by 3455714Skris * Eric Young (eay@cryptsoft.com)" 3555714Skris * The word 'cryptographic' can be left out if the rouines from the library 3655714Skris * being used are not cryptographic related :-). 37280304Sjkim * 4. If you include any Windows specific code (or a derivative thereof) from 3855714Skris * the apps directory (application code) you must include an acknowledgement: 3955714Skris * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 40280304Sjkim * 4155714Skris * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 4255714Skris * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 4355714Skris * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 4455714Skris * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 4555714Skris * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 4655714Skris * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 4755714Skris * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 4855714Skris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 4955714Skris * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 5055714Skris * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 5155714Skris * SUCH DAMAGE. 52280304Sjkim * 5355714Skris * The licence and distribution terms for any publically available version or 5455714Skris * derivative of this code cannot be changed. i.e. this code cannot simply be 5555714Skris * copied and put under another distribution licence 5655714Skris * [including the GNU Public Licence.] 5755714Skris */ 5855714Skris 59280304Sjkim#include <openssl/opensslconf.h> /* for OPENSSL_NO_RSA */ 60109998Smarkm#ifndef OPENSSL_NO_RSA 61280304Sjkim# include <stdio.h> 62280304Sjkim# include "cryptlib.h" 63280304Sjkim# include <openssl/evp.h> 64280304Sjkim# include <openssl/rand.h> 65280304Sjkim# include <openssl/objects.h> 66280304Sjkim# include <openssl/x509.h> 67280304Sjkim# include <openssl/pem.h> 68280304Sjkim# include <openssl/rsa.h> 6955714Skris 7055714Skrisint PEM_SealInit(PEM_ENCODE_SEAL_CTX *ctx, EVP_CIPHER *type, EVP_MD *md_type, 71280304Sjkim unsigned char **ek, int *ekl, unsigned char *iv, 72280304Sjkim EVP_PKEY **pubk, int npubk) 73280304Sjkim{ 74280304Sjkim unsigned char key[EVP_MAX_KEY_LENGTH]; 75280304Sjkim int ret = -1; 76280304Sjkim int i, j, max = 0; 77280304Sjkim char *s = NULL; 7855714Skris 79280304Sjkim for (i = 0; i < npubk; i++) { 80280304Sjkim if (pubk[i]->type != EVP_PKEY_RSA) { 81280304Sjkim PEMerr(PEM_F_PEM_SEALINIT, PEM_R_PUBLIC_KEY_NO_RSA); 82280304Sjkim goto err; 83280304Sjkim } 84280304Sjkim j = RSA_size(pubk[i]->pkey.rsa); 85280304Sjkim if (j > max) 86280304Sjkim max = j; 87280304Sjkim } 88280304Sjkim s = (char *)OPENSSL_malloc(max * 2); 89280304Sjkim if (s == NULL) { 90280304Sjkim PEMerr(PEM_F_PEM_SEALINIT, ERR_R_MALLOC_FAILURE); 91280304Sjkim goto err; 92280304Sjkim } 9355714Skris 94280304Sjkim EVP_EncodeInit(&ctx->encode); 9555714Skris 96280304Sjkim EVP_MD_CTX_init(&ctx->md); 97280304Sjkim if (!EVP_SignInit(&ctx->md, md_type)) 98280304Sjkim goto err; 99109998Smarkm 100280304Sjkim EVP_CIPHER_CTX_init(&ctx->cipher); 101280304Sjkim ret = EVP_SealInit(&ctx->cipher, type, ek, ekl, iv, pubk, npubk); 102280304Sjkim if (ret <= 0) 103280304Sjkim goto err; 10455714Skris 105280304Sjkim /* base64 encode the keys */ 106280304Sjkim for (i = 0; i < npubk; i++) { 107280304Sjkim j = EVP_EncodeBlock((unsigned char *)s, ek[i], 108280304Sjkim RSA_size(pubk[i]->pkey.rsa)); 109280304Sjkim ekl[i] = j; 110280304Sjkim memcpy(ek[i], s, j + 1); 111280304Sjkim } 11255714Skris 113280304Sjkim ret = npubk; 114280304Sjkim err: 115280304Sjkim if (s != NULL) 116280304Sjkim OPENSSL_free(s); 117280304Sjkim OPENSSL_cleanse(key, EVP_MAX_KEY_LENGTH); 118280304Sjkim return (ret); 119280304Sjkim} 12055714Skris 12155714Skrisvoid PEM_SealUpdate(PEM_ENCODE_SEAL_CTX *ctx, unsigned char *out, int *outl, 122280304Sjkim unsigned char *in, int inl) 123280304Sjkim{ 124280304Sjkim unsigned char buffer[1600]; 125280304Sjkim int i, j; 12655714Skris 127280304Sjkim *outl = 0; 128280304Sjkim EVP_SignUpdate(&ctx->md, in, inl); 129280304Sjkim for (;;) { 130280304Sjkim if (inl <= 0) 131280304Sjkim break; 132280304Sjkim if (inl > 1200) 133280304Sjkim i = 1200; 134280304Sjkim else 135280304Sjkim i = inl; 136280304Sjkim EVP_EncryptUpdate(&ctx->cipher, buffer, &j, in, i); 137280304Sjkim EVP_EncodeUpdate(&ctx->encode, out, &j, buffer, j); 138280304Sjkim *outl += j; 139280304Sjkim out += j; 140280304Sjkim in += i; 141280304Sjkim inl -= i; 142280304Sjkim } 143280304Sjkim} 14455714Skris 14555714Skrisint PEM_SealFinal(PEM_ENCODE_SEAL_CTX *ctx, unsigned char *sig, int *sigl, 146280304Sjkim unsigned char *out, int *outl, EVP_PKEY *priv) 147280304Sjkim{ 148280304Sjkim unsigned char *s = NULL; 149280304Sjkim int ret = 0, j; 150280304Sjkim unsigned int i; 15155714Skris 152280304Sjkim if (priv->type != EVP_PKEY_RSA) { 153280304Sjkim PEMerr(PEM_F_PEM_SEALFINAL, PEM_R_PUBLIC_KEY_NO_RSA); 154280304Sjkim goto err; 155280304Sjkim } 156280304Sjkim i = RSA_size(priv->pkey.rsa); 157280304Sjkim if (i < 100) 158280304Sjkim i = 100; 159280304Sjkim s = (unsigned char *)OPENSSL_malloc(i * 2); 160280304Sjkim if (s == NULL) { 161280304Sjkim PEMerr(PEM_F_PEM_SEALFINAL, ERR_R_MALLOC_FAILURE); 162280304Sjkim goto err; 163280304Sjkim } 16455714Skris 165280304Sjkim if (!EVP_EncryptFinal_ex(&ctx->cipher, s, (int *)&i)) 166280304Sjkim goto err; 167280304Sjkim EVP_EncodeUpdate(&ctx->encode, out, &j, s, i); 168280304Sjkim *outl = j; 169280304Sjkim out += j; 170280304Sjkim EVP_EncodeFinal(&ctx->encode, out, &j); 171280304Sjkim *outl += j; 17255714Skris 173280304Sjkim if (!EVP_SignFinal(&ctx->md, s, &i, priv)) 174280304Sjkim goto err; 175280304Sjkim *sigl = EVP_EncodeBlock(sig, s, i); 17655714Skris 177280304Sjkim ret = 1; 178280304Sjkim err: 179280304Sjkim EVP_MD_CTX_cleanup(&ctx->md); 180280304Sjkim EVP_CIPHER_CTX_cleanup(&ctx->cipher); 181280304Sjkim if (s != NULL) 182280304Sjkim OPENSSL_free(s); 183280304Sjkim return (ret); 184280304Sjkim} 185280304Sjkim#else /* !OPENSSL_NO_RSA */ 18659191Skris 18759191Skris# if PEDANTIC 188280304Sjkimstatic void *dummy = &dummy; 18959191Skris# endif 19059191Skris 19155714Skris#endif 192