p12_decr.c 7.0 KB

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