OLD | NEW |
1 /* crypto/evp/p_sign.c */ | 1 /* crypto/evp/p_sign.c */ |
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) | 2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) |
3 * All rights reserved. | 3 * All rights reserved. |
4 * | 4 * |
5 * This package is an SSL implementation written | 5 * This package is an SSL implementation written |
6 * by Eric Young (eay@cryptsoft.com). | 6 * by Eric Young (eay@cryptsoft.com). |
7 * The implementation was written so as to conform with Netscapes SSL. | 7 * The implementation was written so as to conform with Netscapes SSL. |
8 * | 8 * |
9 * This library is free for commercial and non-commercial use as long as | 9 * This library is free for commercial and non-commercial use as long as |
10 * the following conditions are aheared to. The following conditions | 10 * the following conditions are aheared to. The following conditions |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
74 EVP_DigestUpdate(ctx,data,count); | 74 EVP_DigestUpdate(ctx,data,count); |
75 } | 75 } |
76 #endif | 76 #endif |
77 | 77 |
78 int EVP_SignFinal(EVP_MD_CTX *ctx, unsigned char *sigret, unsigned int *siglen, | 78 int EVP_SignFinal(EVP_MD_CTX *ctx, unsigned char *sigret, unsigned int *siglen, |
79 EVP_PKEY *pkey) | 79 EVP_PKEY *pkey) |
80 { | 80 { |
81 unsigned char m[EVP_MAX_MD_SIZE]; | 81 unsigned char m[EVP_MAX_MD_SIZE]; |
82 unsigned int m_len; | 82 unsigned int m_len; |
83 int i,ok=0,v; | 83 int i,ok=0,v; |
84 » MS_STATIC EVP_MD_CTX tmp_ctx; | 84 » EVP_MD_CTX tmp_ctx; |
85 | 85 |
86 *siglen=0; | 86 *siglen=0; |
| 87 EVP_MD_CTX_init(&tmp_ctx); |
| 88 EVP_MD_CTX_copy_ex(&tmp_ctx,ctx); |
| 89 EVP_DigestFinal_ex(&tmp_ctx,&(m[0]),&m_len); |
| 90 EVP_MD_CTX_cleanup(&tmp_ctx); |
| 91 |
| 92 if (ctx->digest->flags & EVP_MD_FLAG_PKEY_METHOD_SIGNATURE) |
| 93 { |
| 94 EVP_PKEY_CTX *pkctx = NULL; |
| 95 size_t sltmp = (size_t)EVP_PKEY_size(pkey); |
| 96 i = 0; |
| 97 pkctx = EVP_PKEY_CTX_new(pkey, NULL); |
| 98 if (!pkctx) |
| 99 goto err; |
| 100 if (EVP_PKEY_sign_init(pkctx) <= 0) |
| 101 goto err; |
| 102 if (EVP_PKEY_CTX_set_signature_md(pkctx, ctx->digest) <= 0) |
| 103 goto err; |
| 104 if (EVP_PKEY_sign(pkctx, sigret, &sltmp, m, m_len) <= 0) |
| 105 goto err; |
| 106 *siglen = sltmp; |
| 107 i = 1; |
| 108 err: |
| 109 EVP_PKEY_CTX_free(pkctx); |
| 110 return i; |
| 111 } |
| 112 |
87 for (i=0; i<4; i++) | 113 for (i=0; i<4; i++) |
88 { | 114 { |
89 v=ctx->digest->required_pkey_type[i]; | 115 v=ctx->digest->required_pkey_type[i]; |
90 if (v == 0) break; | 116 if (v == 0) break; |
91 if (pkey->type == v) | 117 if (pkey->type == v) |
92 { | 118 { |
93 ok=1; | 119 ok=1; |
94 break; | 120 break; |
95 } | 121 } |
96 } | 122 } |
97 if (!ok) | 123 if (!ok) |
98 { | 124 { |
99 EVPerr(EVP_F_EVP_SIGNFINAL,EVP_R_WRONG_PUBLIC_KEY_TYPE); | 125 EVPerr(EVP_F_EVP_SIGNFINAL,EVP_R_WRONG_PUBLIC_KEY_TYPE); |
100 return(0); | 126 return(0); |
101 } | 127 } |
| 128 |
102 if (ctx->digest->sign == NULL) | 129 if (ctx->digest->sign == NULL) |
103 { | 130 { |
104 EVPerr(EVP_F_EVP_SIGNFINAL,EVP_R_NO_SIGN_FUNCTION_CONFIGURED); | 131 EVPerr(EVP_F_EVP_SIGNFINAL,EVP_R_NO_SIGN_FUNCTION_CONFIGURED); |
105 return(0); | 132 return(0); |
106 } | 133 } |
107 » EVP_MD_CTX_init(&tmp_ctx); | 134 » return(ctx->digest->sign(ctx->digest->type,m,m_len,sigret,siglen, |
108 » EVP_MD_CTX_copy_ex(&tmp_ctx,ctx); | 135 » » pkey->pkey.ptr)); |
109 » if (ctx->digest->flags & EVP_MD_FLAG_SVCTX) | |
110 » » { | |
111 » » EVP_MD_SVCTX sctmp; | |
112 » » sctmp.mctx = &tmp_ctx; | |
113 » » sctmp.key = pkey->pkey.ptr; | |
114 » » i = ctx->digest->sign(ctx->digest->type, | |
115 » » » NULL, -1, sigret, siglen, &sctmp); | |
116 » » } | |
117 » else | |
118 » » { | |
119 » » EVP_DigestFinal_ex(&tmp_ctx,&(m[0]),&m_len); | |
120 » » i = ctx->digest->sign(ctx->digest->type,m,m_len,sigret,siglen, | |
121 » » » » » pkey->pkey.ptr); | |
122 » » } | |
123 » EVP_MD_CTX_cleanup(&tmp_ctx); | |
124 » return i; | |
125 } | 136 } |
126 | 137 |
OLD | NEW |