| 6 | // |
| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 |
| 15 | #include <openssl/asn1.h> |
| 16 | #include <openssl/digest.h> |
| 17 | #include <openssl/err.h> |
| 18 | #include <openssl/evp.h> |
| 19 | #include <openssl/mem.h> |
| 20 | #include <openssl/obj.h> |
| 21 | #include <openssl/x509.h> |
| 22 |
| 23 | #include <limits.h> |
| 24 |
| 25 | #include "internal.h" |
| 26 |
| 27 | int ASN1_item_sign(const ASN1_ITEM *it, X509_ALGOR *algor1, X509_ALGOR *algor2, |
| 28 | ASN1_BIT_STRING *signature, void *asn, EVP_PKEY *pkey, |
| 29 | const EVP_MD *type) { |
| 30 | if (signature->type != V_ASN1_BIT_STRING) { |
| 31 | OPENSSL_PUT_ERROR(ASN1, ASN1_R_WRONG_TYPE); |
| 32 | return 0; |
| 33 | } |
| 34 | EVP_MD_CTX ctx; |
| 35 | EVP_MD_CTX_init(&ctx); |
| 36 | if (!EVP_DigestSignInit(&ctx, NULL, type, NULL, pkey)) { |
| 37 | EVP_MD_CTX_cleanup(&ctx); |
| 38 | return 0; |
| 39 | } |
| 40 | return ASN1_item_sign_ctx(it, algor1, algor2, signature, asn, &ctx); |
| 41 | } |
| 42 |
| 43 | int ASN1_item_sign_ctx(const ASN1_ITEM *it, X509_ALGOR *algor1, |
| 44 | X509_ALGOR *algor2, ASN1_BIT_STRING *signature, |
| 45 | void *asn, EVP_MD_CTX *ctx) { |
| 46 | int ret = 0; |
| 47 | uint8_t *in = NULL, *out = NULL; |
| 48 |
| 49 | { |
| 50 | if (signature->type != V_ASN1_BIT_STRING) { |
| 51 | OPENSSL_PUT_ERROR(ASN1, ASN1_R_WRONG_TYPE); |
| 52 | goto err; |
| 53 | } |
| 54 |
| 55 | // Write out the requested copies of the AlgorithmIdentifier. |
| 56 | if (algor1 && !x509_digest_sign_algorithm(ctx, algor1)) { |
| 57 | goto err; |
| 58 | } |
| 59 | if (algor2 && !x509_digest_sign_algorithm(ctx, algor2)) { |
| 60 | goto err; |
| 61 | } |
| 62 |
| 63 | int in_len = ASN1_item_i2d(reinterpret_cast<ASN1_VALUE *>(asn), &in, it); |
| 64 | if (in_len < 0) { |
| 65 | goto err; |
| 66 | } |
| 67 |
| 68 | EVP_PKEY *pkey = EVP_PKEY_CTX_get0_pkey(ctx->pctx); |
| 69 | size_t out_len = EVP_PKEY_size(pkey); |
| 70 | if (out_len > INT_MAX) { |
| 71 | OPENSSL_PUT_ERROR(X509, ERR_R_OVERFLOW); |
| 72 | goto err; |
| 73 | } |
| 74 |
| 75 | out = reinterpret_cast<uint8_t *>(OPENSSL_malloc(out_len)); |
| 76 | if (out == NULL) { |
| 77 | goto err; |
| 78 | } |
| 79 |
| 80 | if (!EVP_DigestSign(ctx, out, &out_len, in, in_len)) { |
| 81 | OPENSSL_PUT_ERROR(X509, ERR_R_EVP_LIB); |
| 82 | goto err; |
| 83 | } |
| 84 |
| 85 | ASN1_STRING_set0(signature, out, (int)out_len); |
| 86 | out = NULL; |
| 87 | signature->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07); |
| 88 | signature->flags |= ASN1_STRING_FLAG_BITS_LEFT; |
| 89 | ret = (int)out_len; |
| 90 | } |
| 91 |
| 92 | err: |
| 93 | EVP_MD_CTX_cleanup(ctx); |
| 94 | OPENSSL_free(in); |
| 95 | OPENSSL_free(out); |
| 96 | return ret; |
| 97 | } |
| 98 |