2
0

p12_decr.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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(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. {
  22. unsigned char *out = NULL;
  23. int outlen, i;
  24. EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
  25. int max_out_len, mac_len = 0;
  26. if (ctx == NULL) {
  27. ERR_raise(ERR_LIB_PKCS12, ERR_R_MALLOC_FAILURE);
  28. goto err;
  29. }
  30. /* Process data */
  31. if (!EVP_PBE_CipherInit(algor->algorithm, pass, passlen,
  32. algor->parameter, ctx, en_de))
  33. goto err;
  34. /*
  35. * GOST algorithm specifics:
  36. * OMAC algorithm calculate and encrypt MAC of the encrypted objects
  37. * It's appended to encrypted text on encrypting
  38. * MAC should be processed on decrypting separately from plain text
  39. */
  40. max_out_len = inlen + EVP_CIPHER_CTX_block_size(ctx);
  41. if (EVP_CIPHER_flags(EVP_CIPHER_CTX_cipher(ctx)) & EVP_CIPH_FLAG_CIPHER_WITH_MAC) {
  42. if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_TLS1_AAD, 0, &mac_len) < 0) {
  43. ERR_raise(ERR_LIB_PKCS12, ERR_R_INTERNAL_ERROR);
  44. goto err;
  45. }
  46. if (EVP_CIPHER_CTX_encrypting(ctx)) {
  47. max_out_len += mac_len;
  48. } else {
  49. if (inlen < mac_len) {
  50. ERR_raise(ERR_LIB_PKCS12, PKCS12_R_UNSUPPORTED_PKCS12_MODE);
  51. goto err;
  52. }
  53. inlen -= mac_len;
  54. if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
  55. (int)mac_len, (unsigned char *)in+inlen) < 0) {
  56. ERR_raise(ERR_LIB_PKCS12, ERR_R_INTERNAL_ERROR);
  57. goto err;
  58. }
  59. }
  60. }
  61. if ((out = OPENSSL_malloc(max_out_len)) == NULL) {
  62. ERR_raise(ERR_LIB_PKCS12, ERR_R_MALLOC_FAILURE);
  63. goto err;
  64. }
  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_flags(EVP_CIPHER_CTX_cipher(ctx)) & EVP_CIPH_FLAG_CIPHER_WITH_MAC) {
  82. if (EVP_CIPHER_CTX_encrypting(ctx)) {
  83. if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG,
  84. (int)mac_len, out+outlen) < 0) {
  85. ERR_raise(ERR_LIB_PKCS12, ERR_R_INTERNAL_ERROR);
  86. goto err;
  87. }
  88. outlen += mac_len;
  89. }
  90. }
  91. if (datalen)
  92. *datalen = outlen;
  93. if (data)
  94. *data = out;
  95. err:
  96. EVP_CIPHER_CTX_free(ctx);
  97. return out;
  98. }
  99. /*
  100. * Decrypt an OCTET STRING and decode ASN1 structure if zbuf set zero buffer
  101. * after use.
  102. */
  103. void *PKCS12_item_decrypt_d2i(const X509_ALGOR *algor, const ASN1_ITEM *it,
  104. const char *pass, int passlen,
  105. const ASN1_OCTET_STRING *oct, int zbuf)
  106. {
  107. unsigned char *out = NULL;
  108. const unsigned char *p;
  109. void *ret;
  110. int outlen = 0;
  111. if (!PKCS12_pbe_crypt(algor, pass, passlen, oct->data, oct->length,
  112. &out, &outlen, 0))
  113. return NULL;
  114. p = out;
  115. OSSL_TRACE_BEGIN(PKCS12_DECRYPT) {
  116. BIO_printf(trc_out, "\n");
  117. BIO_dump(trc_out, out, outlen);
  118. BIO_printf(trc_out, "\n");
  119. } OSSL_TRACE_END(PKCS12_DECRYPT);
  120. ret = ASN1_item_d2i(NULL, &p, outlen, it);
  121. if (zbuf)
  122. OPENSSL_cleanse(out, outlen);
  123. if (!ret)
  124. ERR_raise(ERR_LIB_PKCS12, PKCS12_R_DECODE_ERROR);
  125. OPENSSL_free(out);
  126. return ret;
  127. }
  128. /*
  129. * Encode ASN1 structure and encrypt, return OCTET STRING if zbuf set zero
  130. * encoding.
  131. */
  132. ASN1_OCTET_STRING *PKCS12_item_i2d_encrypt(X509_ALGOR *algor,
  133. const ASN1_ITEM *it,
  134. const char *pass, int passlen,
  135. void *obj, int zbuf)
  136. {
  137. ASN1_OCTET_STRING *oct = NULL;
  138. unsigned char *in = NULL;
  139. int inlen;
  140. if ((oct = ASN1_OCTET_STRING_new()) == NULL) {
  141. ERR_raise(ERR_LIB_PKCS12, ERR_R_MALLOC_FAILURE);
  142. goto err;
  143. }
  144. inlen = ASN1_item_i2d(obj, &in, it);
  145. if (!in) {
  146. ERR_raise(ERR_LIB_PKCS12, PKCS12_R_ENCODE_ERROR);
  147. goto err;
  148. }
  149. if (!PKCS12_pbe_crypt(algor, pass, passlen, in, inlen, &oct->data,
  150. &oct->length, 1)) {
  151. ERR_raise(ERR_LIB_PKCS12, PKCS12_R_ENCRYPT_ERROR);
  152. OPENSSL_free(in);
  153. goto err;
  154. }
  155. if (zbuf)
  156. OPENSSL_cleanse(in, inlen);
  157. OPENSSL_free(in);
  158. return oct;
  159. err:
  160. ASN1_OCTET_STRING_free(oct);
  161. return NULL;
  162. }