2 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
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
13 #include "internal/cryptlib.h"
15 #ifndef NO_SYS_TYPES_H
16 # include <sys/types.h>
19 #include <openssl/bn.h>
20 #include <openssl/evp.h>
21 #include <openssl/x509.h>
22 #include <openssl/objects.h>
23 #include <openssl/buffer.h>
24 #include "internal/asn1_int.h"
25 #include "internal/evp_int.h"
29 int ASN1_sign(i2d_of_void *i2d, X509_ALGOR *algor1, X509_ALGOR *algor2,
30 ASN1_BIT_STRING *signature, char *data, EVP_PKEY *pkey,
33 EVP_MD_CTX *ctx = EVP_MD_CTX_new();
34 unsigned char *p, *buf_in = NULL, *buf_out = NULL;
35 int i, inl = 0, outl = 0, outll = 0;
39 ASN1err(ASN1_F_ASN1_SIGN, ERR_R_MALLOC_FAILURE);
42 for (i = 0; i < 2; i++) {
49 if (type->pkey_type == NID_dsaWithSHA1) {
51 * special case: RFC 2459 tells us to omit 'parameters' with
54 ASN1_TYPE_free(a->parameter);
56 } else if ((a->parameter == NULL) ||
57 (a->parameter->type != V_ASN1_NULL)) {
58 ASN1_TYPE_free(a->parameter);
59 if ((a->parameter = ASN1_TYPE_new()) == NULL)
61 a->parameter->type = V_ASN1_NULL;
63 ASN1_OBJECT_free(a->algorithm);
64 a->algorithm = OBJ_nid2obj(type->pkey_type);
65 if (a->algorithm == NULL) {
66 ASN1err(ASN1_F_ASN1_SIGN, ASN1_R_UNKNOWN_OBJECT_TYPE);
69 if (a->algorithm->length == 0) {
70 ASN1err(ASN1_F_ASN1_SIGN,
71 ASN1_R_THE_ASN1_OBJECT_IDENTIFIER_IS_NOT_KNOWN_FOR_THIS_MD);
75 inl = i2d(data, NULL);
76 buf_in = OPENSSL_malloc((unsigned int)inl);
77 outll = outl = EVP_PKEY_size(pkey);
78 buf_out = OPENSSL_malloc((unsigned int)outl);
79 if ((buf_in == NULL) || (buf_out == NULL)) {
81 ASN1err(ASN1_F_ASN1_SIGN, ERR_R_MALLOC_FAILURE);
87 if (!EVP_SignInit_ex(ctx, type, NULL)
88 || !EVP_SignUpdate(ctx, (unsigned char *)buf_in, inl)
89 || !EVP_SignFinal(ctx, (unsigned char *)buf_out,
90 (unsigned int *)&outl, pkey)) {
92 ASN1err(ASN1_F_ASN1_SIGN, ERR_R_EVP_LIB);
95 OPENSSL_free(signature->data);
96 signature->data = buf_out;
98 signature->length = outl;
100 * In the interests of compatibility, I'll make sure that the bit string
101 * has a 'not-used bits' value of 0
103 signature->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
104 signature->flags |= ASN1_STRING_FLAG_BITS_LEFT;
106 EVP_MD_CTX_free(ctx);
107 OPENSSL_clear_free((char *)buf_in, (unsigned int)inl);
108 OPENSSL_clear_free((char *)buf_out, outll);
114 int ASN1_item_sign(const ASN1_ITEM *it, X509_ALGOR *algor1,
115 X509_ALGOR *algor2, ASN1_BIT_STRING *signature, void *asn,
116 EVP_PKEY *pkey, const EVP_MD *type)
119 EVP_MD_CTX *ctx = EVP_MD_CTX_new();
122 ASN1err(ASN1_F_ASN1_ITEM_SIGN, ERR_R_MALLOC_FAILURE);
125 if (!EVP_DigestSignInit(ctx, NULL, type, NULL, pkey)) {
126 EVP_MD_CTX_free(ctx);
130 rv = ASN1_item_sign_ctx(it, algor1, algor2, signature, asn, ctx);
132 EVP_MD_CTX_free(ctx);
136 int ASN1_item_sign_ctx(const ASN1_ITEM *it,
137 X509_ALGOR *algor1, X509_ALGOR *algor2,
138 ASN1_BIT_STRING *signature, void *asn, EVP_MD_CTX *ctx)
142 unsigned char *buf_in = NULL, *buf_out = NULL;
143 size_t inl = 0, outl = 0, outll = 0;
144 int signid, paramtype;
147 type = EVP_MD_CTX_md(ctx);
148 pkey = EVP_PKEY_CTX_get0_pkey(EVP_MD_CTX_pkey_ctx(ctx));
150 if (type == NULL || pkey == NULL) {
151 ASN1err(ASN1_F_ASN1_ITEM_SIGN_CTX, ASN1_R_CONTEXT_NOT_INITIALISED);
155 if (pkey->ameth == NULL) {
156 ASN1err(ASN1_F_ASN1_ITEM_SIGN_CTX, ASN1_R_DIGEST_AND_KEY_TYPE_NOT_SUPPORTED);
160 if (pkey->ameth->item_sign) {
161 rv = pkey->ameth->item_sign(ctx, it, asn, algor1, algor2, signature);
163 outl = signature->length;
165 * Return value meanings:
167 * 1: method does everything.
168 * 2: carry on as normal.
169 * 3: ASN1 method sets algorithm identifiers: just sign.
172 ASN1err(ASN1_F_ASN1_ITEM_SIGN_CTX, ERR_R_EVP_LIB);
179 if (!OBJ_find_sigid_by_algs(&signid,
181 pkey->ameth->pkey_id)) {
182 ASN1err(ASN1_F_ASN1_ITEM_SIGN_CTX,
183 ASN1_R_DIGEST_AND_KEY_TYPE_NOT_SUPPORTED);
187 if (pkey->ameth->pkey_flags & ASN1_PKEY_SIGPARAM_NULL)
188 paramtype = V_ASN1_NULL;
190 paramtype = V_ASN1_UNDEF;
193 X509_ALGOR_set0(algor1, OBJ_nid2obj(signid), paramtype, NULL);
195 X509_ALGOR_set0(algor2, OBJ_nid2obj(signid), paramtype, NULL);
199 inl = ASN1_item_i2d(asn, &buf_in, it);
200 outll = outl = EVP_PKEY_size(pkey);
201 buf_out = OPENSSL_malloc((unsigned int)outl);
202 if ((buf_in == NULL) || (buf_out == NULL)) {
204 ASN1err(ASN1_F_ASN1_ITEM_SIGN_CTX, ERR_R_MALLOC_FAILURE);
208 if (!EVP_DigestSignUpdate(ctx, buf_in, inl)
209 || !EVP_DigestSignFinal(ctx, buf_out, &outl)) {
211 ASN1err(ASN1_F_ASN1_ITEM_SIGN_CTX, ERR_R_EVP_LIB);
214 OPENSSL_free(signature->data);
215 signature->data = buf_out;
217 signature->length = outl;
219 * In the interests of compatibility, I'll make sure that the bit string
220 * has a 'not-used bits' value of 0
222 signature->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
223 signature->flags |= ASN1_STRING_FLAG_BITS_LEFT;
225 OPENSSL_clear_free((char *)buf_in, (unsigned int)inl);
226 OPENSSL_clear_free((char *)buf_out, outll);