1300548Sadrian/* crypto/evp/p_sign.c */
2300548Sadrian/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3300548Sadrian * All rights reserved.
4300548Sadrian *
5300548Sadrian * This package is an SSL implementation written
6300548Sadrian * by Eric Young (eay@cryptsoft.com).
7300548Sadrian * The implementation was written so as to conform with Netscapes SSL.
8300548Sadrian *
9300548Sadrian * This library is free for commercial and non-commercial use as long as
10300548Sadrian * the following conditions are aheared to.  The following conditions
11300548Sadrian * apply to all code found in this distribution, be it the RC4, RSA,
12300548Sadrian * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13300548Sadrian * included with this distribution is covered by the same copyright terms
14300548Sadrian * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15300548Sadrian *
16300548Sadrian * Copyright remains Eric Young's, and as such any Copyright notices in
17300548Sadrian * the code are not to be removed.
18300548Sadrian * If this package is used in a product, Eric Young should be given attribution
19300548Sadrian * as the author of the parts of the library used.
20300548Sadrian * This can be in the form of a textual message at program startup or
21300548Sadrian * in documentation (online or textual) provided with the package.
22300548Sadrian *
23300548Sadrian * Redistribution and use in source and binary forms, with or without
24300548Sadrian * modification, are permitted provided that the following conditions
25300548Sadrian * are met:
26300548Sadrian * 1. Redistributions of source code must retain the copyright
27300548Sadrian *    notice, this list of conditions and the following disclaimer.
28300548Sadrian * 2. Redistributions in binary form must reproduce the above copyright
29300548Sadrian *    notice, this list of conditions and the following disclaimer in the
30300548Sadrian *    documentation and/or other materials provided with the distribution.
31300548Sadrian * 3. All advertising materials mentioning features or use of this software
32300548Sadrian *    must display the following acknowledgement:
33300548Sadrian *    "This product includes cryptographic software written by
34300548Sadrian *     Eric Young (eay@cryptsoft.com)"
35300548Sadrian *    The word 'cryptographic' can be left out if the rouines from the library
36300548Sadrian *    being used are not cryptographic related :-).
37300548Sadrian * 4. If you include any Windows specific code (or a derivative thereof) from
38300548Sadrian *    the apps directory (application code) you must include an acknowledgement:
39300548Sadrian *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40300548Sadrian *
41300548Sadrian * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42300548Sadrian * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43300548Sadrian * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44300548Sadrian * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45300548Sadrian * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46300548Sadrian * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47300548Sadrian * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48300548Sadrian * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49300548Sadrian * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50300548Sadrian * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51300702Sadrian * SUCH DAMAGE.
52300702Sadrian *
53300702Sadrian * The licence and distribution terms for any publically available version or
54300702Sadrian * derivative of this code cannot be changed.  i.e. this code cannot simply be
55300702Sadrian * copied and put under another distribution licence
56300702Sadrian * [including the GNU Public Licence.]
57300702Sadrian */
58300702Sadrian
59300702Sadrian#include <stdio.h>
60300702Sadrian#include "cryptlib.h"
61300702Sadrian#include <openssl/evp.h>
62300702Sadrian#include <openssl/objects.h>
63300702Sadrian#include <openssl/x509.h>
64300702Sadrian
65300702Sadrian#ifdef undef
66300548Sadrianvoid EVP_SignInit(EVP_MD_CTX *ctx, EVP_MD *type)
67300548Sadrian{
68300548Sadrian    EVP_DigestInit_ex(ctx, type);
69300548Sadrian}
70300548Sadrian
71300548Sadrianvoid EVP_SignUpdate(EVP_MD_CTX *ctx, unsigned char *data, unsigned int count)
72300548Sadrian{
73300548Sadrian    EVP_DigestUpdate(ctx, data, count);
74300548Sadrian}
75300548Sadrian#endif
76300548Sadrian
77300548Sadrianint EVP_SignFinal(EVP_MD_CTX *ctx, unsigned char *sigret,
78300548Sadrian                  unsigned int *siglen, EVP_PKEY *pkey)
79300548Sadrian{
80300548Sadrian    unsigned char m[EVP_MAX_MD_SIZE];
81300548Sadrian    unsigned int m_len;
82300548Sadrian    int i, ok = 0, v;
83300548Sadrian    EVP_MD_CTX tmp_ctx;
84300548Sadrian
85300548Sadrian    *siglen = 0;
86300548Sadrian    for (i = 0; i < 4; i++) {
87300548Sadrian        v = ctx->digest->required_pkey_type[i];
88300548Sadrian        if (v == 0)
89300548Sadrian            break;
90300548Sadrian        if (pkey->type == v) {
91300548Sadrian            ok = 1;
92300548Sadrian            break;
93300548Sadrian        }
94300548Sadrian    }
95300548Sadrian    if (!ok) {
96300548Sadrian        EVPerr(EVP_F_EVP_SIGNFINAL, EVP_R_WRONG_PUBLIC_KEY_TYPE);
97300548Sadrian        return (0);
98300548Sadrian    }
99300548Sadrian    if (ctx->digest->sign == NULL) {
100300548Sadrian        EVPerr(EVP_F_EVP_SIGNFINAL, EVP_R_NO_SIGN_FUNCTION_CONFIGURED);
101300548Sadrian        return (0);
102300548Sadrian    }
103300548Sadrian    EVP_MD_CTX_init(&tmp_ctx);
104300702Sadrian    EVP_MD_CTX_copy_ex(&tmp_ctx, ctx);
105300548Sadrian    if (ctx->digest->flags & EVP_MD_FLAG_SVCTX) {
106300548Sadrian        EVP_MD_SVCTX sctmp;
107300548Sadrian        sctmp.mctx = &tmp_ctx;
108300548Sadrian        sctmp.key = pkey->pkey.ptr;
109300548Sadrian        i = ctx->digest->sign(ctx->digest->type,
110300548Sadrian                              NULL, -1, sigret, siglen, &sctmp);
111300548Sadrian    } else {
112300548Sadrian        EVP_DigestFinal_ex(&tmp_ctx, &(m[0]), &m_len);
113300548Sadrian        i = ctx->digest->sign(ctx->digest->type, m, m_len, sigret, siglen,
114300548Sadrian                              pkey->pkey.ptr);
115300548Sadrian    }
116300548Sadrian    EVP_MD_CTX_cleanup(&tmp_ctx);
117    return i;
118}
119