summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/asn1/a_sign.c
diff options
context:
space:
mode:
authordjm <>2012-10-13 21:25:14 +0000
committerdjm <>2012-10-13 21:25:14 +0000
commit93723b50b639d8dc717bc1bf463fd46e1b321239 (patch)
tree281e0a29ae8f87a8c47fbd4deaa1f3d48b8cc5c1 /src/lib/libcrypto/asn1/a_sign.c
parent65e72ac55a6405783db7a12d7e35a7561d46005b (diff)
downloadopenbsd-93723b50b639d8dc717bc1bf463fd46e1b321239.tar.gz
openbsd-93723b50b639d8dc717bc1bf463fd46e1b321239.tar.bz2
openbsd-93723b50b639d8dc717bc1bf463fd46e1b321239.zip
resolve conflicts
Diffstat (limited to 'src/lib/libcrypto/asn1/a_sign.c')
-rw-r--r--src/lib/libcrypto/asn1/a_sign.c111
1 files changed, 73 insertions, 38 deletions
diff --git a/src/lib/libcrypto/asn1/a_sign.c b/src/lib/libcrypto/asn1/a_sign.c
index ff63bfc7be..7b4a193d6b 100644
--- a/src/lib/libcrypto/asn1/a_sign.c
+++ b/src/lib/libcrypto/asn1/a_sign.c
@@ -184,9 +184,9 @@ int ASN1_sign(i2d_of_void *i2d, X509_ALGOR *algor1, X509_ALGOR *algor2,
184 p=buf_in; 184 p=buf_in;
185 185
186 i2d(data,&p); 186 i2d(data,&p);
187 EVP_SignInit_ex(&ctx,type, NULL); 187 if (!EVP_SignInit_ex(&ctx,type, NULL)
188 EVP_SignUpdate(&ctx,(unsigned char *)buf_in,inl); 188 || !EVP_SignUpdate(&ctx,(unsigned char *)buf_in,inl)
189 if (!EVP_SignFinal(&ctx,(unsigned char *)buf_out, 189 || !EVP_SignFinal(&ctx,(unsigned char *)buf_out,
190 (unsigned int *)&outl,pkey)) 190 (unsigned int *)&outl,pkey))
191 { 191 {
192 outl=0; 192 outl=0;
@@ -218,65 +218,100 @@ int ASN1_item_sign(const ASN1_ITEM *it, X509_ALGOR *algor1, X509_ALGOR *algor2,
218 const EVP_MD *type) 218 const EVP_MD *type)
219 { 219 {
220 EVP_MD_CTX ctx; 220 EVP_MD_CTX ctx;
221 EVP_MD_CTX_init(&ctx);
222 if (!EVP_DigestSignInit(&ctx, NULL, type, NULL, pkey))
223 {
224 EVP_MD_CTX_cleanup(&ctx);
225 return 0;
226 }
227 return ASN1_item_sign_ctx(it, algor1, algor2, signature, asn, &ctx);
228 }
229
230
231int ASN1_item_sign_ctx(const ASN1_ITEM *it,
232 X509_ALGOR *algor1, X509_ALGOR *algor2,
233 ASN1_BIT_STRING *signature, void *asn, EVP_MD_CTX *ctx)
234 {
235 const EVP_MD *type;
236 EVP_PKEY *pkey;
221 unsigned char *buf_in=NULL,*buf_out=NULL; 237 unsigned char *buf_in=NULL,*buf_out=NULL;
222 int inl=0,outl=0,outll=0; 238 size_t inl=0,outl=0,outll=0;
223 int signid, paramtype; 239 int signid, paramtype;
240 int rv;
241
242 type = EVP_MD_CTX_md(ctx);
243 pkey = EVP_PKEY_CTX_get0_pkey(ctx->pctx);
224 244
225 if (type == NULL) 245 if (!type || !pkey)
226 { 246 {
227 int def_nid; 247 ASN1err(ASN1_F_ASN1_ITEM_SIGN_CTX, ASN1_R_CONTEXT_NOT_INITIALISED);
228 if (EVP_PKEY_get_default_digest_nid(pkey, &def_nid) > 0) 248 return 0;
229 type = EVP_get_digestbynid(def_nid);
230 } 249 }
231 250
232 if (type == NULL) 251 if (pkey->ameth->item_sign)
233 { 252 {
234 ASN1err(ASN1_F_ASN1_ITEM_SIGN, ASN1_R_NO_DEFAULT_DIGEST); 253 rv = pkey->ameth->item_sign(ctx, it, asn, algor1, algor2,
235 return 0; 254 signature);
255 if (rv == 1)
256 outl = signature->length;
257 /* Return value meanings:
258 * <=0: error.
259 * 1: method does everything.
260 * 2: carry on as normal.
261 * 3: ASN1 method sets algorithm identifiers: just sign.
262 */
263 if (rv <= 0)
264 ASN1err(ASN1_F_ASN1_ITEM_SIGN_CTX, ERR_R_EVP_LIB);
265 if (rv <= 1)
266 goto err;
236 } 267 }
268 else
269 rv = 2;
237 270
238 if (type->flags & EVP_MD_FLAG_PKEY_METHOD_SIGNATURE) 271 if (rv == 2)
239 { 272 {
240 if (!pkey->ameth || 273 if (type->flags & EVP_MD_FLAG_PKEY_METHOD_SIGNATURE)
241 !OBJ_find_sigid_by_algs(&signid, EVP_MD_nid(type),
242 pkey->ameth->pkey_id))
243 { 274 {
244 ASN1err(ASN1_F_ASN1_ITEM_SIGN, 275 if (!pkey->ameth ||
245 ASN1_R_DIGEST_AND_KEY_TYPE_NOT_SUPPORTED); 276 !OBJ_find_sigid_by_algs(&signid,
246 return 0; 277 EVP_MD_nid(type),
278 pkey->ameth->pkey_id))
279 {
280 ASN1err(ASN1_F_ASN1_ITEM_SIGN_CTX,
281 ASN1_R_DIGEST_AND_KEY_TYPE_NOT_SUPPORTED);
282 return 0;
283 }
247 } 284 }
248 } 285 else
249 else 286 signid = type->pkey_type;
250 signid = type->pkey_type;
251 287
252 if (pkey->ameth->pkey_flags & ASN1_PKEY_SIGPARAM_NULL) 288 if (pkey->ameth->pkey_flags & ASN1_PKEY_SIGPARAM_NULL)
253 paramtype = V_ASN1_NULL; 289 paramtype = V_ASN1_NULL;
254 else 290 else
255 paramtype = V_ASN1_UNDEF; 291 paramtype = V_ASN1_UNDEF;
256 292
257 if (algor1) 293 if (algor1)
258 X509_ALGOR_set0(algor1, OBJ_nid2obj(signid), paramtype, NULL); 294 X509_ALGOR_set0(algor1, OBJ_nid2obj(signid), paramtype, NULL);
259 if (algor2) 295 if (algor2)
260 X509_ALGOR_set0(algor2, OBJ_nid2obj(signid), paramtype, NULL); 296 X509_ALGOR_set0(algor2, OBJ_nid2obj(signid), paramtype, NULL);
297
298 }
261 299
262 EVP_MD_CTX_init(&ctx);
263 inl=ASN1_item_i2d(asn,&buf_in, it); 300 inl=ASN1_item_i2d(asn,&buf_in, it);
264 outll=outl=EVP_PKEY_size(pkey); 301 outll=outl=EVP_PKEY_size(pkey);
265 buf_out=(unsigned char *)OPENSSL_malloc((unsigned int)outl); 302 buf_out=OPENSSL_malloc((unsigned int)outl);
266 if ((buf_in == NULL) || (buf_out == NULL)) 303 if ((buf_in == NULL) || (buf_out == NULL))
267 { 304 {
268 outl=0; 305 outl=0;
269 ASN1err(ASN1_F_ASN1_ITEM_SIGN,ERR_R_MALLOC_FAILURE); 306 ASN1err(ASN1_F_ASN1_ITEM_SIGN_CTX,ERR_R_MALLOC_FAILURE);
270 goto err; 307 goto err;
271 } 308 }
272 309
273 EVP_SignInit_ex(&ctx,type, NULL); 310 if (!EVP_DigestSignUpdate(ctx, buf_in, inl)
274 EVP_SignUpdate(&ctx,(unsigned char *)buf_in,inl); 311 || !EVP_DigestSignFinal(ctx, buf_out, &outl))
275 if (!EVP_SignFinal(&ctx,(unsigned char *)buf_out,
276 (unsigned int *)&outl,pkey))
277 { 312 {
278 outl=0; 313 outl=0;
279 ASN1err(ASN1_F_ASN1_ITEM_SIGN,ERR_R_EVP_LIB); 314 ASN1err(ASN1_F_ASN1_ITEM_SIGN_CTX,ERR_R_EVP_LIB);
280 goto err; 315 goto err;
281 } 316 }
282 if (signature->data != NULL) OPENSSL_free(signature->data); 317 if (signature->data != NULL) OPENSSL_free(signature->data);
@@ -289,7 +324,7 @@ int ASN1_item_sign(const ASN1_ITEM *it, X509_ALGOR *algor1, X509_ALGOR *algor2,
289 signature->flags&= ~(ASN1_STRING_FLAG_BITS_LEFT|0x07); 324 signature->flags&= ~(ASN1_STRING_FLAG_BITS_LEFT|0x07);
290 signature->flags|=ASN1_STRING_FLAG_BITS_LEFT; 325 signature->flags|=ASN1_STRING_FLAG_BITS_LEFT;
291err: 326err:
292 EVP_MD_CTX_cleanup(&ctx); 327 EVP_MD_CTX_cleanup(ctx);
293 if (buf_in != NULL) 328 if (buf_in != NULL)
294 { OPENSSL_cleanse((char *)buf_in,(unsigned int)inl); OPENSSL_free(buf_in); } 329 { OPENSSL_cleanse((char *)buf_in,(unsigned int)inl); OPENSSL_free(buf_in); }
295 if (buf_out != NULL) 330 if (buf_out != NULL)