p5_pbe.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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/asn1t.h>
  12. #include <openssl/x509.h>
  13. #include <openssl/rand.h>
  14. /* PKCS#5 password based encryption structure */
  15. ASN1_SEQUENCE(PBEPARAM) = {
  16. ASN1_SIMPLE(PBEPARAM, salt, ASN1_OCTET_STRING),
  17. ASN1_SIMPLE(PBEPARAM, iter, ASN1_INTEGER)
  18. } ASN1_SEQUENCE_END(PBEPARAM)
  19. IMPLEMENT_ASN1_FUNCTIONS(PBEPARAM)
  20. /* Set an algorithm identifier for a PKCS#5 PBE algorithm */
  21. int PKCS5_pbe_set0_algor_ex(X509_ALGOR *algor, int alg, int iter,
  22. const unsigned char *salt, int saltlen,
  23. OSSL_LIB_CTX *ctx)
  24. {
  25. PBEPARAM *pbe = NULL;
  26. ASN1_STRING *pbe_str = NULL;
  27. unsigned char *sstr = NULL;
  28. pbe = PBEPARAM_new();
  29. if (pbe == NULL) {
  30. /* ERR_R_ASN1_LIB, because PBEPARAM_new() is defined in crypto/asn1 */
  31. ERR_raise(ERR_LIB_ASN1, ERR_R_ASN1_LIB);
  32. goto err;
  33. }
  34. if (iter <= 0)
  35. iter = PKCS5_DEFAULT_ITER;
  36. if (!ASN1_INTEGER_set(pbe->iter, iter)) {
  37. ERR_raise(ERR_LIB_ASN1, ERR_R_ASN1_LIB);
  38. goto err;
  39. }
  40. if (!saltlen)
  41. saltlen = PKCS5_SALT_LEN;
  42. if (saltlen < 0)
  43. goto err;
  44. sstr = OPENSSL_malloc(saltlen);
  45. if (sstr == NULL)
  46. goto err;
  47. if (salt)
  48. memcpy(sstr, salt, saltlen);
  49. else if (RAND_bytes_ex(ctx, sstr, saltlen, 0) <= 0)
  50. goto err;
  51. ASN1_STRING_set0(pbe->salt, sstr, saltlen);
  52. sstr = NULL;
  53. if (!ASN1_item_pack(pbe, ASN1_ITEM_rptr(PBEPARAM), &pbe_str)) {
  54. ERR_raise(ERR_LIB_ASN1, ERR_R_ASN1_LIB);
  55. goto err;
  56. }
  57. PBEPARAM_free(pbe);
  58. pbe = NULL;
  59. if (X509_ALGOR_set0(algor, OBJ_nid2obj(alg), V_ASN1_SEQUENCE, pbe_str))
  60. return 1;
  61. err:
  62. OPENSSL_free(sstr);
  63. PBEPARAM_free(pbe);
  64. ASN1_STRING_free(pbe_str);
  65. return 0;
  66. }
  67. int PKCS5_pbe_set0_algor(X509_ALGOR *algor, int alg, int iter,
  68. const unsigned char *salt, int saltlen)
  69. {
  70. return PKCS5_pbe_set0_algor_ex(algor, alg, iter, salt, saltlen, NULL);
  71. }
  72. /* Return an algorithm identifier for a PKCS#5 PBE algorithm */
  73. X509_ALGOR *PKCS5_pbe_set_ex(int alg, int iter,
  74. const unsigned char *salt, int saltlen,
  75. OSSL_LIB_CTX *ctx)
  76. {
  77. X509_ALGOR *ret;
  78. ret = X509_ALGOR_new();
  79. if (ret == NULL) {
  80. ERR_raise(ERR_LIB_ASN1, ERR_R_X509_LIB);
  81. return NULL;
  82. }
  83. if (PKCS5_pbe_set0_algor_ex(ret, alg, iter, salt, saltlen, ctx))
  84. return ret;
  85. X509_ALGOR_free(ret);
  86. return NULL;
  87. }
  88. X509_ALGOR *PKCS5_pbe_set(int alg, int iter,
  89. const unsigned char *salt, int saltlen)
  90. {
  91. return PKCS5_pbe_set_ex(alg, iter, salt, saltlen, NULL);
  92. }