2 * Copyright 1995-2022 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (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
12 #include <sys/types.h>
14 #include "internal/cryptlib.h"
16 #include <openssl/bn.h>
17 #include <openssl/evp.h>
18 #include <openssl/x509.h>
19 #include <openssl/objects.h>
20 #include <openssl/buffer.h>
21 #include <openssl/core_names.h>
22 #include "crypto/asn1.h"
23 #include "crypto/evp.h"
25 #ifndef OPENSSL_NO_DEPRECATED_3_0
27 int ASN1_sign(i2d_of_void
*i2d
, X509_ALGOR
*algor1
, X509_ALGOR
*algor2
,
28 ASN1_BIT_STRING
*signature
, char *data
, EVP_PKEY
*pkey
,
31 EVP_MD_CTX
*ctx
= EVP_MD_CTX_new();
32 unsigned char *p
, *buf_in
= NULL
, *buf_out
= NULL
;
33 int i
, inl
= 0, outl
= 0;
34 size_t inll
= 0, outll
= 0;
38 ERR_raise(ERR_LIB_ASN1
, ERR_R_MALLOC_FAILURE
);
41 for (i
= 0; i
< 2; i
++) {
48 if (type
->pkey_type
== NID_dsaWithSHA1
) {
50 * special case: RFC 2459 tells us to omit 'parameters' with
53 ASN1_TYPE_free(a
->parameter
);
55 } else if ((a
->parameter
== NULL
) ||
56 (a
->parameter
->type
!= V_ASN1_NULL
)) {
57 ASN1_TYPE_free(a
->parameter
);
58 if ((a
->parameter
= ASN1_TYPE_new()) == NULL
)
60 a
->parameter
->type
= V_ASN1_NULL
;
62 ASN1_OBJECT_free(a
->algorithm
);
63 a
->algorithm
= OBJ_nid2obj(type
->pkey_type
);
64 if (a
->algorithm
== NULL
) {
65 ERR_raise(ERR_LIB_ASN1
, ASN1_R_UNKNOWN_OBJECT_TYPE
);
68 if (a
->algorithm
->length
== 0) {
69 ERR_raise(ERR_LIB_ASN1
,
70 ASN1_R_THE_ASN1_OBJECT_IDENTIFIER_IS_NOT_KNOWN_FOR_THIS_MD
);
74 inl
= i2d(data
, NULL
);
76 ERR_raise(ERR_LIB_ASN1
, ERR_R_INTERNAL_ERROR
);
80 buf_in
= OPENSSL_malloc(inll
);
81 outll
= outl
= EVP_PKEY_get_size(pkey
);
82 buf_out
= OPENSSL_malloc(outll
);
83 if (buf_in
== NULL
|| buf_out
== NULL
) {
85 ERR_raise(ERR_LIB_ASN1
, ERR_R_MALLOC_FAILURE
);
91 if (!EVP_SignInit_ex(ctx
, type
, NULL
)
92 || !EVP_SignUpdate(ctx
, (unsigned char *)buf_in
, inl
)
93 || !EVP_SignFinal(ctx
, (unsigned char *)buf_out
,
94 (unsigned int *)&outl
, pkey
)) {
96 ERR_raise(ERR_LIB_ASN1
, ERR_R_EVP_LIB
);
99 ASN1_STRING_set0(signature
, buf_out
, outl
);
102 * In the interests of compatibility, I'll make sure that the bit string
103 * has a 'not-used bits' value of 0
105 signature
->flags
&= ~(ASN1_STRING_FLAG_BITS_LEFT
| 0x07);
106 signature
->flags
|= ASN1_STRING_FLAG_BITS_LEFT
;
108 EVP_MD_CTX_free(ctx
);
109 OPENSSL_clear_free((char *)buf_in
, inll
);
110 OPENSSL_clear_free((char *)buf_out
, outll
);
116 int ASN1_item_sign(const ASN1_ITEM
*it
, X509_ALGOR
*algor1
, X509_ALGOR
*algor2
,
117 ASN1_BIT_STRING
*signature
, const void *data
,
118 EVP_PKEY
*pkey
, const EVP_MD
*md
)
120 return ASN1_item_sign_ex(it
, algor1
, algor2
, signature
, data
, NULL
, pkey
,
124 int ASN1_item_sign_ex(const ASN1_ITEM
*it
, X509_ALGOR
*algor1
,
125 X509_ALGOR
*algor2
, ASN1_BIT_STRING
*signature
,
126 const void *data
, const ASN1_OCTET_STRING
*id
,
127 EVP_PKEY
*pkey
, const EVP_MD
*md
, OSSL_LIB_CTX
*libctx
,
131 EVP_MD_CTX
*ctx
= evp_md_ctx_new_ex(pkey
, id
, libctx
, propq
);
134 ERR_raise(ERR_LIB_ASN1
, ERR_R_MALLOC_FAILURE
);
137 /* We can use the non _ex variant here since the pkey is already setup */
138 if (!EVP_DigestSignInit(ctx
, NULL
, md
, NULL
, pkey
))
141 rv
= ASN1_item_sign_ctx(it
, algor1
, algor2
, signature
, data
, ctx
);
144 EVP_PKEY_CTX_free(EVP_MD_CTX_get_pkey_ctx(ctx
));
145 EVP_MD_CTX_free(ctx
);
149 int ASN1_item_sign_ctx(const ASN1_ITEM
*it
, X509_ALGOR
*algor1
,
150 X509_ALGOR
*algor2
, ASN1_BIT_STRING
*signature
,
151 const void *data
, EVP_MD_CTX
*ctx
)
155 unsigned char *buf_in
= NULL
, *buf_out
= NULL
;
156 size_t inl
= 0, outl
= 0, outll
= 0;
157 int signid
, paramtype
, buf_len
= 0;
160 md
= EVP_MD_CTX_get0_md(ctx
);
161 pkey
= EVP_PKEY_CTX_get0_pkey(EVP_MD_CTX_get_pkey_ctx(ctx
));
164 ERR_raise(ERR_LIB_ASN1
, ASN1_R_CONTEXT_NOT_INITIALISED
);
168 if (pkey
->ameth
== NULL
) {
169 EVP_PKEY_CTX
*pctx
= EVP_MD_CTX_get_pkey_ctx(ctx
);
170 OSSL_PARAM params
[2];
171 unsigned char aid
[128];
175 || !EVP_PKEY_CTX_IS_SIGNATURE_OP(pctx
)) {
176 ERR_raise(ERR_LIB_ASN1
, ASN1_R_CONTEXT_NOT_INITIALISED
);
181 OSSL_PARAM_construct_octet_string(OSSL_SIGNATURE_PARAM_ALGORITHM_ID
,
183 params
[1] = OSSL_PARAM_construct_end();
185 if (EVP_PKEY_CTX_get_params(pctx
, params
) <= 0)
188 if ((aid_len
= params
[0].return_size
) == 0) {
189 ERR_raise(ERR_LIB_ASN1
, ASN1_R_DIGEST_AND_KEY_TYPE_NOT_SUPPORTED
);
193 if (algor1
!= NULL
) {
194 const unsigned char *pp
= aid
;
196 if (d2i_X509_ALGOR(&algor1
, &pp
, aid_len
) == NULL
) {
197 ERR_raise(ERR_LIB_ASN1
, ERR_R_INTERNAL_ERROR
);
202 if (algor2
!= NULL
) {
203 const unsigned char *pp
= aid
;
205 if (d2i_X509_ALGOR(&algor2
, &pp
, aid_len
) == NULL
) {
206 ERR_raise(ERR_LIB_ASN1
, ERR_R_INTERNAL_ERROR
);
212 } else if (pkey
->ameth
->item_sign
) {
213 rv
= pkey
->ameth
->item_sign(ctx
, it
, data
, algor1
, algor2
, signature
);
215 outl
= signature
->length
;
217 * Return value meanings:
219 * 1: method does everything.
220 * 2: carry on as normal.
221 * 3: ASN1 method sets algorithm identifiers: just sign.
224 ERR_raise(ERR_LIB_ASN1
, ERR_R_EVP_LIB
);
233 ERR_raise(ERR_LIB_ASN1
, ASN1_R_CONTEXT_NOT_INITIALISED
);
238 #ifndef OPENSSL_NO_SM2
239 EVP_PKEY_get_id(pkey
) == NID_sm2
? NID_sm2
:
241 pkey
->ameth
->pkey_id
;
243 if (!OBJ_find_sigid_by_algs(&signid
, EVP_MD_nid(md
), pkey_id
)) {
244 ERR_raise(ERR_LIB_ASN1
, ASN1_R_DIGEST_AND_KEY_TYPE_NOT_SUPPORTED
);
248 paramtype
= pkey
->ameth
->pkey_flags
& ASN1_PKEY_SIGPARAM_NULL
?
249 V_ASN1_NULL
: V_ASN1_UNDEF
;
251 && !X509_ALGOR_set0(algor1
, OBJ_nid2obj(signid
), paramtype
, NULL
))
254 && !X509_ALGOR_set0(algor2
, OBJ_nid2obj(signid
), paramtype
, NULL
))
258 buf_len
= ASN1_item_i2d(data
, &buf_in
, it
);
261 ERR_raise(ERR_LIB_ASN1
, ERR_R_INTERNAL_ERROR
);
265 if (!EVP_DigestSign(ctx
, NULL
, &outll
, buf_in
, inl
)) {
267 ERR_raise(ERR_LIB_ASN1
, ERR_R_EVP_LIB
);
271 buf_out
= OPENSSL_malloc(outll
);
272 if (buf_in
== NULL
|| buf_out
== NULL
) {
274 ERR_raise(ERR_LIB_ASN1
, ERR_R_MALLOC_FAILURE
);
278 if (!EVP_DigestSign(ctx
, buf_out
, &outl
, buf_in
, inl
)) {
280 ERR_raise(ERR_LIB_ASN1
, ERR_R_EVP_LIB
);
283 ASN1_STRING_set0(signature
, buf_out
, outl
);
286 * In the interests of compatibility, I'll make sure that the bit string
287 * has a 'not-used bits' value of 0
289 signature
->flags
&= ~(ASN1_STRING_FLAG_BITS_LEFT
| 0x07);
290 signature
->flags
|= ASN1_STRING_FLAG_BITS_LEFT
;
292 OPENSSL_clear_free((char *)buf_in
, inl
);
293 OPENSSL_clear_free((char *)buf_out
, outll
);