source: EcnlProtoTool/trunk/openssl-1.1.0e/crypto/dsa/dsa_pmeth.c@ 331

Last change on this file since 331 was 331, checked in by coas-nagasima, 6 years ago

prototoolに関連するプロジェクトをnewlibからmuslを使うよう変更・更新
ntshellをnewlibの下位の実装から、muslのsyscallの実装に変更・更新
以下のOSSをアップデート
・mruby-1.3.0
・musl-1.1.18
・onigmo-6.1.3
・tcc-0.9.27
以下のOSSを追加
・openssl-1.1.0e
・curl-7.57.0
・zlib-1.2.11
以下のmrbgemsを追加
・iij/mruby-digest
・iij/mruby-env
・iij/mruby-errno
・iij/mruby-iijson
・iij/mruby-ipaddr
・iij/mruby-mock
・iij/mruby-require
・iij/mruby-tls-openssl

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-csrc
File size: 6.9 KB
Line 
1/*
2 * Copyright 2006-2016 The OpenSSL Project Authors. All Rights Reserved.
3 *
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
8 */
9
10#include <stdio.h>
11#include "internal/cryptlib.h"
12#include <openssl/asn1t.h>
13#include <openssl/x509.h>
14#include <openssl/evp.h>
15#include <openssl/bn.h>
16#include "internal/evp_int.h"
17#include "dsa_locl.h"
18
19/* DSA pkey context structure */
20
21typedef struct {
22 /* Parameter gen parameters */
23 int nbits; /* size of p in bits (default: 1024) */
24 int qbits; /* size of q in bits (default: 160) */
25 const EVP_MD *pmd; /* MD for parameter generation */
26 /* Keygen callback info */
27 int gentmp[2];
28 /* message digest */
29 const EVP_MD *md; /* MD for the signature */
30} DSA_PKEY_CTX;
31
32static int pkey_dsa_init(EVP_PKEY_CTX *ctx)
33{
34 DSA_PKEY_CTX *dctx;
35 dctx = OPENSSL_malloc(sizeof(*dctx));
36 if (dctx == NULL)
37 return 0;
38 dctx->nbits = 1024;
39 dctx->qbits = 160;
40 dctx->pmd = NULL;
41 dctx->md = NULL;
42
43 ctx->data = dctx;
44 ctx->keygen_info = dctx->gentmp;
45 ctx->keygen_info_count = 2;
46
47 return 1;
48}
49
50static int pkey_dsa_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src)
51{
52 DSA_PKEY_CTX *dctx, *sctx;
53 if (!pkey_dsa_init(dst))
54 return 0;
55 sctx = src->data;
56 dctx = dst->data;
57 dctx->nbits = sctx->nbits;
58 dctx->qbits = sctx->qbits;
59 dctx->pmd = sctx->pmd;
60 dctx->md = sctx->md;
61 return 1;
62}
63
64static void pkey_dsa_cleanup(EVP_PKEY_CTX *ctx)
65{
66 DSA_PKEY_CTX *dctx = ctx->data;
67 OPENSSL_free(dctx);
68}
69
70static int pkey_dsa_sign(EVP_PKEY_CTX *ctx, unsigned char *sig,
71 size_t *siglen, const unsigned char *tbs,
72 size_t tbslen)
73{
74 int ret;
75 unsigned int sltmp;
76 DSA_PKEY_CTX *dctx = ctx->data;
77 DSA *dsa = ctx->pkey->pkey.dsa;
78
79 if (dctx->md) {
80 if (tbslen != (size_t)EVP_MD_size(dctx->md))
81 return 0;
82 } else {
83 if (tbslen != SHA_DIGEST_LENGTH)
84 return 0;
85 }
86
87 ret = DSA_sign(0, tbs, tbslen, sig, &sltmp, dsa);
88
89 if (ret <= 0)
90 return ret;
91 *siglen = sltmp;
92 return 1;
93}
94
95static int pkey_dsa_verify(EVP_PKEY_CTX *ctx,
96 const unsigned char *sig, size_t siglen,
97 const unsigned char *tbs, size_t tbslen)
98{
99 int ret;
100 DSA_PKEY_CTX *dctx = ctx->data;
101 DSA *dsa = ctx->pkey->pkey.dsa;
102
103 if (dctx->md) {
104 if (tbslen != (size_t)EVP_MD_size(dctx->md))
105 return 0;
106 } else {
107 if (tbslen != SHA_DIGEST_LENGTH)
108 return 0;
109 }
110
111 ret = DSA_verify(0, tbs, tbslen, sig, siglen, dsa);
112
113 return ret;
114}
115
116static int pkey_dsa_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
117{
118 DSA_PKEY_CTX *dctx = ctx->data;
119 switch (type) {
120 case EVP_PKEY_CTRL_DSA_PARAMGEN_BITS:
121 if (p1 < 256)
122 return -2;
123 dctx->nbits = p1;
124 return 1;
125
126 case EVP_PKEY_CTRL_DSA_PARAMGEN_Q_BITS:
127 if (p1 != 160 && p1 != 224 && p1 && p1 != 256)
128 return -2;
129 dctx->qbits = p1;
130 return 1;
131
132 case EVP_PKEY_CTRL_DSA_PARAMGEN_MD:
133 if (EVP_MD_type((const EVP_MD *)p2) != NID_sha1 &&
134 EVP_MD_type((const EVP_MD *)p2) != NID_sha224 &&
135 EVP_MD_type((const EVP_MD *)p2) != NID_sha256) {
136 DSAerr(DSA_F_PKEY_DSA_CTRL, DSA_R_INVALID_DIGEST_TYPE);
137 return 0;
138 }
139 dctx->pmd = p2;
140 return 1;
141
142 case EVP_PKEY_CTRL_MD:
143 if (EVP_MD_type((const EVP_MD *)p2) != NID_sha1 &&
144 EVP_MD_type((const EVP_MD *)p2) != NID_dsa &&
145 EVP_MD_type((const EVP_MD *)p2) != NID_dsaWithSHA &&
146 EVP_MD_type((const EVP_MD *)p2) != NID_sha224 &&
147 EVP_MD_type((const EVP_MD *)p2) != NID_sha256 &&
148 EVP_MD_type((const EVP_MD *)p2) != NID_sha384 &&
149 EVP_MD_type((const EVP_MD *)p2) != NID_sha512) {
150 DSAerr(DSA_F_PKEY_DSA_CTRL, DSA_R_INVALID_DIGEST_TYPE);
151 return 0;
152 }
153 dctx->md = p2;
154 return 1;
155
156 case EVP_PKEY_CTRL_GET_MD:
157 *(const EVP_MD **)p2 = dctx->md;
158 return 1;
159
160 case EVP_PKEY_CTRL_DIGESTINIT:
161 case EVP_PKEY_CTRL_PKCS7_SIGN:
162 case EVP_PKEY_CTRL_CMS_SIGN:
163 return 1;
164
165 case EVP_PKEY_CTRL_PEER_KEY:
166 DSAerr(DSA_F_PKEY_DSA_CTRL,
167 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
168 return -2;
169 default:
170 return -2;
171
172 }
173}
174
175static int pkey_dsa_ctrl_str(EVP_PKEY_CTX *ctx,
176 const char *type, const char *value)
177{
178 if (strcmp(type, "dsa_paramgen_bits") == 0) {
179 int nbits;
180 nbits = atoi(value);
181 return EVP_PKEY_CTX_set_dsa_paramgen_bits(ctx, nbits);
182 }
183 if (strcmp(type, "dsa_paramgen_q_bits") == 0) {
184 int qbits = atoi(value);
185 return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DSA, EVP_PKEY_OP_PARAMGEN,
186 EVP_PKEY_CTRL_DSA_PARAMGEN_Q_BITS, qbits,
187 NULL);
188 }
189 if (strcmp(type, "dsa_paramgen_md") == 0) {
190 return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DSA, EVP_PKEY_OP_PARAMGEN,
191 EVP_PKEY_CTRL_DSA_PARAMGEN_MD, 0,
192 (void *)EVP_get_digestbyname(value));
193 }
194 return -2;
195}
196
197static int pkey_dsa_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
198{
199 DSA *dsa = NULL;
200 DSA_PKEY_CTX *dctx = ctx->data;
201 BN_GENCB *pcb;
202 int ret;
203 if (ctx->pkey_gencb) {
204 pcb = BN_GENCB_new();
205 if (pcb == NULL)
206 return 0;
207 evp_pkey_set_cb_translate(pcb, ctx);
208 } else
209 pcb = NULL;
210 dsa = DSA_new();
211 if (dsa == NULL) {
212 BN_GENCB_free(pcb);
213 return 0;
214 }
215 ret = dsa_builtin_paramgen(dsa, dctx->nbits, dctx->qbits, dctx->pmd,
216 NULL, 0, NULL, NULL, NULL, pcb);
217 BN_GENCB_free(pcb);
218 if (ret)
219 EVP_PKEY_assign_DSA(pkey, dsa);
220 else
221 DSA_free(dsa);
222 return ret;
223}
224
225static int pkey_dsa_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
226{
227 DSA *dsa = NULL;
228 if (ctx->pkey == NULL) {
229 DSAerr(DSA_F_PKEY_DSA_KEYGEN, DSA_R_NO_PARAMETERS_SET);
230 return 0;
231 }
232 dsa = DSA_new();
233 if (dsa == NULL)
234 return 0;
235 EVP_PKEY_assign_DSA(pkey, dsa);
236 /* Note: if error return, pkey is freed by parent routine */
237 if (!EVP_PKEY_copy_parameters(pkey, ctx->pkey))
238 return 0;
239 return DSA_generate_key(pkey->pkey.dsa);
240}
241
242const EVP_PKEY_METHOD dsa_pkey_meth = {
243 EVP_PKEY_DSA,
244 EVP_PKEY_FLAG_AUTOARGLEN,
245 pkey_dsa_init,
246 pkey_dsa_copy,
247 pkey_dsa_cleanup,
248
249 0,
250 pkey_dsa_paramgen,
251
252 0,
253 pkey_dsa_keygen,
254
255 0,
256 pkey_dsa_sign,
257
258 0,
259 pkey_dsa_verify,
260
261 0, 0,
262
263 0, 0, 0, 0,
264
265 0, 0,
266
267 0, 0,
268
269 0, 0,
270
271 pkey_dsa_ctrl,
272 pkey_dsa_ctrl_str
273};
Note: See TracBrowser for help on using the repository browser.