p12_decr.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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_EVP_LIB);
  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. goto err;
  65. if (!EVP_CipherUpdate(ctx, out, &i, in, inlen)) {
  66. OPENSSL_free(out);
  67. out = NULL;
  68. ERR_raise(ERR_LIB_PKCS12, ERR_R_EVP_LIB);
  69. goto err;
  70. }
  71. outlen = i;
  72. if (!EVP_CipherFinal_ex(ctx, out + i, &i)) {
  73. OPENSSL_free(out);
  74. out = NULL;
  75. ERR_raise_data(ERR_LIB_PKCS12, PKCS12_R_PKCS12_CIPHERFINAL_ERROR,
  76. passlen == 0 ? "empty password"
  77. : "maybe wrong password");
  78. goto err;
  79. }
  80. outlen += i;
  81. if ((EVP_CIPHER_get_flags(EVP_CIPHER_CTX_get0_cipher(ctx))
  82. & EVP_CIPH_FLAG_CIPHER_WITH_MAC) != 0) {
  83. if (EVP_CIPHER_CTX_is_encrypting(ctx)) {
  84. if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG,
  85. (int)mac_len, out+outlen) < 0) {
  86. OPENSSL_free(out);
  87. out = NULL;
  88. ERR_raise(ERR_LIB_PKCS12, ERR_R_INTERNAL_ERROR);
  89. goto err;
  90. }
  91. outlen += mac_len;
  92. }
  93. }
  94. if (datalen)
  95. *datalen = outlen;
  96. if (data)
  97. *data = out;
  98. err:
  99. EVP_CIPHER_CTX_free(ctx);
  100. return out;
  101. }
  102. unsigned char *PKCS12_pbe_crypt(const X509_ALGOR *algor,
  103. const char *pass, int passlen,
  104. const unsigned char *in, int inlen,
  105. unsigned char **data, int *datalen, int en_de)
  106. {
  107. return PKCS12_pbe_crypt_ex(algor, pass, passlen, in, inlen, data, datalen,
  108. en_de, NULL, NULL);
  109. }
  110. /*
  111. * Decrypt an OCTET STRING and decode ASN1 structure if zbuf set zero buffer
  112. * after use.
  113. */
  114. void *PKCS12_item_decrypt_d2i_ex(const X509_ALGOR *algor, const ASN1_ITEM *it,
  115. const char *pass, int passlen,
  116. const ASN1_OCTET_STRING *oct, int zbuf,
  117. OSSL_LIB_CTX *libctx,
  118. const char *propq)
  119. {
  120. unsigned char *out = NULL;
  121. const unsigned char *p;
  122. void *ret;
  123. int outlen = 0;
  124. if (!PKCS12_pbe_crypt_ex(algor, pass, passlen, oct->data, oct->length,
  125. &out, &outlen, 0, libctx, propq))
  126. return NULL;
  127. p = out;
  128. OSSL_TRACE_BEGIN(PKCS12_DECRYPT) {
  129. BIO_printf(trc_out, "\n");
  130. BIO_dump(trc_out, out, outlen);
  131. BIO_printf(trc_out, "\n");
  132. } OSSL_TRACE_END(PKCS12_DECRYPT);
  133. ret = ASN1_item_d2i(NULL, &p, outlen, it);
  134. if (zbuf)
  135. OPENSSL_cleanse(out, outlen);
  136. if (!ret)
  137. ERR_raise(ERR_LIB_PKCS12, PKCS12_R_DECODE_ERROR);
  138. OPENSSL_free(out);
  139. return ret;
  140. }
  141. void *PKCS12_item_decrypt_d2i(const X509_ALGOR *algor, const ASN1_ITEM *it,
  142. const char *pass, int passlen,
  143. const ASN1_OCTET_STRING *oct, int zbuf)
  144. {
  145. return PKCS12_item_decrypt_d2i_ex(algor, it, pass, passlen, oct, zbuf,
  146. NULL, NULL);
  147. }
  148. /*
  149. * Encode ASN1 structure and encrypt, return OCTET STRING if zbuf set zero
  150. * encoding.
  151. */
  152. ASN1_OCTET_STRING *PKCS12_item_i2d_encrypt_ex(X509_ALGOR *algor,
  153. const ASN1_ITEM *it,
  154. const char *pass, int passlen,
  155. void *obj, int zbuf,
  156. OSSL_LIB_CTX *ctx,
  157. const char *propq)
  158. {
  159. ASN1_OCTET_STRING *oct = NULL;
  160. unsigned char *in = NULL;
  161. int inlen;
  162. if ((oct = ASN1_OCTET_STRING_new()) == NULL) {
  163. ERR_raise(ERR_LIB_PKCS12, ERR_R_ASN1_LIB);
  164. goto err;
  165. }
  166. inlen = ASN1_item_i2d(obj, &in, it);
  167. if (!in) {
  168. ERR_raise(ERR_LIB_PKCS12, PKCS12_R_ENCODE_ERROR);
  169. goto err;
  170. }
  171. if (!PKCS12_pbe_crypt_ex(algor, pass, passlen, in, inlen, &oct->data,
  172. &oct->length, 1, ctx, propq)) {
  173. ERR_raise(ERR_LIB_PKCS12, PKCS12_R_ENCRYPT_ERROR);
  174. OPENSSL_free(in);
  175. goto err;
  176. }
  177. if (zbuf)
  178. OPENSSL_cleanse(in, inlen);
  179. OPENSSL_free(in);
  180. return oct;
  181. err:
  182. ASN1_OCTET_STRING_free(oct);
  183. return NULL;
  184. }
  185. ASN1_OCTET_STRING *PKCS12_item_i2d_encrypt(X509_ALGOR *algor,
  186. const ASN1_ITEM *it,
  187. const char *pass, int passlen,
  188. void *obj, int zbuf)
  189. {
  190. return PKCS12_item_i2d_encrypt_ex(algor, it, pass, passlen, obj, zbuf, NULL, NULL);
  191. }