p12_decr.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /*
  2. * Copyright 1999-2024 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  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
  8. */
  9. #include <stdio.h>
  10. #include "internal/cryptlib.h"
  11. #include <openssl/pkcs12.h>
  12. #include <openssl/trace.h>
  13. /*
  14. * Encrypt/Decrypt a buffer based on password and algor, result in a
  15. * OPENSSL_malloc'ed buffer
  16. */
  17. unsigned char *PKCS12_pbe_crypt_ex(const X509_ALGOR *algor,
  18. const char *pass, int passlen,
  19. const unsigned char *in, int inlen,
  20. unsigned char **data, int *datalen, int en_de,
  21. OSSL_LIB_CTX *libctx, const char *propq)
  22. {
  23. unsigned char *out = NULL;
  24. int outlen, i;
  25. EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
  26. int max_out_len, mac_len = 0;
  27. int block_size;
  28. if (ctx == NULL) {
  29. ERR_raise(ERR_LIB_PKCS12, ERR_R_EVP_LIB);
  30. goto err;
  31. }
  32. /* Process data */
  33. if (!EVP_PBE_CipherInit_ex(algor->algorithm, pass, passlen,
  34. algor->parameter, ctx, en_de, libctx, propq))
  35. goto err;
  36. /*
  37. * GOST algorithm specifics:
  38. * OMAC algorithm calculate and encrypt MAC of the encrypted objects
  39. * It's appended to encrypted text on encrypting
  40. * MAC should be processed on decrypting separately from plain text
  41. */
  42. block_size = EVP_CIPHER_CTX_get_block_size(ctx);
  43. if (block_size == 0) {
  44. ERR_raise(ERR_LIB_PKCS12, ERR_R_PASSED_NULL_PARAMETER);
  45. goto err;
  46. }
  47. max_out_len = inlen + block_size;
  48. if ((EVP_CIPHER_get_flags(EVP_CIPHER_CTX_get0_cipher(ctx))
  49. & EVP_CIPH_FLAG_CIPHER_WITH_MAC) != 0) {
  50. if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_TLS1_AAD, 0, &mac_len) < 0) {
  51. ERR_raise(ERR_LIB_PKCS12, ERR_R_INTERNAL_ERROR);
  52. goto err;
  53. }
  54. if (EVP_CIPHER_CTX_is_encrypting(ctx)) {
  55. max_out_len += mac_len;
  56. } else {
  57. if (inlen < mac_len) {
  58. ERR_raise(ERR_LIB_PKCS12, PKCS12_R_UNSUPPORTED_PKCS12_MODE);
  59. goto err;
  60. }
  61. inlen -= mac_len;
  62. if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
  63. (int)mac_len, (unsigned char *)in+inlen) < 0) {
  64. ERR_raise(ERR_LIB_PKCS12, ERR_R_INTERNAL_ERROR);
  65. goto err;
  66. }
  67. }
  68. }
  69. if ((out = OPENSSL_malloc(max_out_len)) == NULL)
  70. goto err;
  71. if (!EVP_CipherUpdate(ctx, out, &i, in, inlen)) {
  72. OPENSSL_free(out);
  73. out = NULL;
  74. ERR_raise(ERR_LIB_PKCS12, ERR_R_EVP_LIB);
  75. goto err;
  76. }
  77. outlen = i;
  78. if (!EVP_CipherFinal_ex(ctx, out + i, &i)) {
  79. OPENSSL_free(out);
  80. out = NULL;
  81. ERR_raise_data(ERR_LIB_PKCS12, PKCS12_R_PKCS12_CIPHERFINAL_ERROR,
  82. passlen == 0 ? "empty password"
  83. : "maybe wrong password");
  84. goto err;
  85. }
  86. outlen += i;
  87. if ((EVP_CIPHER_get_flags(EVP_CIPHER_CTX_get0_cipher(ctx))
  88. & EVP_CIPH_FLAG_CIPHER_WITH_MAC) != 0) {
  89. if (EVP_CIPHER_CTX_is_encrypting(ctx)) {
  90. if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG,
  91. (int)mac_len, out+outlen) < 0) {
  92. OPENSSL_free(out);
  93. out = NULL;
  94. ERR_raise(ERR_LIB_PKCS12, ERR_R_INTERNAL_ERROR);
  95. goto err;
  96. }
  97. outlen += mac_len;
  98. }
  99. }
  100. if (datalen)
  101. *datalen = outlen;
  102. if (data)
  103. *data = out;
  104. err:
  105. EVP_CIPHER_CTX_free(ctx);
  106. return out;
  107. }
  108. unsigned char *PKCS12_pbe_crypt(const X509_ALGOR *algor,
  109. const char *pass, int passlen,
  110. const unsigned char *in, int inlen,
  111. unsigned char **data, int *datalen, int en_de)
  112. {
  113. return PKCS12_pbe_crypt_ex(algor, pass, passlen, in, inlen, data, datalen,
  114. en_de, NULL, NULL);
  115. }
  116. /*
  117. * Decrypt an OCTET STRING and decode ASN1 structure if zbuf set zero buffer
  118. * after use.
  119. */
  120. void *PKCS12_item_decrypt_d2i_ex(const X509_ALGOR *algor, const ASN1_ITEM *it,
  121. const char *pass, int passlen,
  122. const ASN1_OCTET_STRING *oct, int zbuf,
  123. OSSL_LIB_CTX *libctx,
  124. const char *propq)
  125. {
  126. unsigned char *out = NULL;
  127. const unsigned char *p;
  128. void *ret;
  129. int outlen = 0;
  130. if (!PKCS12_pbe_crypt_ex(algor, pass, passlen, oct->data, oct->length,
  131. &out, &outlen, 0, libctx, propq))
  132. return NULL;
  133. p = out;
  134. OSSL_TRACE_BEGIN(PKCS12_DECRYPT) {
  135. BIO_printf(trc_out, "\n");
  136. BIO_dump(trc_out, out, outlen);
  137. BIO_printf(trc_out, "\n");
  138. } OSSL_TRACE_END(PKCS12_DECRYPT);
  139. ret = ASN1_item_d2i(NULL, &p, outlen, it);
  140. if (zbuf)
  141. OPENSSL_cleanse(out, outlen);
  142. if (!ret)
  143. ERR_raise(ERR_LIB_PKCS12, PKCS12_R_DECODE_ERROR);
  144. OPENSSL_free(out);
  145. return ret;
  146. }
  147. void *PKCS12_item_decrypt_d2i(const X509_ALGOR *algor, const ASN1_ITEM *it,
  148. const char *pass, int passlen,
  149. const ASN1_OCTET_STRING *oct, int zbuf)
  150. {
  151. return PKCS12_item_decrypt_d2i_ex(algor, it, pass, passlen, oct, zbuf,
  152. NULL, NULL);
  153. }
  154. /*
  155. * Encode ASN1 structure and encrypt, return OCTET STRING if zbuf set zero
  156. * encoding.
  157. */
  158. ASN1_OCTET_STRING *PKCS12_item_i2d_encrypt_ex(X509_ALGOR *algor,
  159. const ASN1_ITEM *it,
  160. const char *pass, int passlen,
  161. void *obj, int zbuf,
  162. OSSL_LIB_CTX *ctx,
  163. const char *propq)
  164. {
  165. ASN1_OCTET_STRING *oct = NULL;
  166. unsigned char *in = NULL;
  167. int inlen;
  168. if ((oct = ASN1_OCTET_STRING_new()) == NULL) {
  169. ERR_raise(ERR_LIB_PKCS12, ERR_R_ASN1_LIB);
  170. goto err;
  171. }
  172. inlen = ASN1_item_i2d(obj, &in, it);
  173. if (!in) {
  174. ERR_raise(ERR_LIB_PKCS12, PKCS12_R_ENCODE_ERROR);
  175. goto err;
  176. }
  177. if (!PKCS12_pbe_crypt_ex(algor, pass, passlen, in, inlen, &oct->data,
  178. &oct->length, 1, ctx, propq)) {
  179. ERR_raise(ERR_LIB_PKCS12, PKCS12_R_ENCRYPT_ERROR);
  180. OPENSSL_free(in);
  181. goto err;
  182. }
  183. if (zbuf)
  184. OPENSSL_cleanse(in, inlen);
  185. OPENSSL_free(in);
  186. return oct;
  187. err:
  188. ASN1_OCTET_STRING_free(oct);
  189. return NULL;
  190. }
  191. ASN1_OCTET_STRING *PKCS12_item_i2d_encrypt(X509_ALGOR *algor,
  192. const ASN1_ITEM *it,
  193. const char *pass, int passlen,
  194. void *obj, int zbuf)
  195. {
  196. return PKCS12_item_i2d_encrypt_ex(algor, it, pass, passlen, obj, zbuf, NULL, NULL);
  197. }