2
0

p12_key.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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/bn.h>
  13. #include <openssl/trace.h>
  14. #include <openssl/kdf.h>
  15. #include <openssl/core_names.h>
  16. #include "internal/provider.h"
  17. int PKCS12_key_gen_asc_ex(const char *pass, int passlen, unsigned char *salt,
  18. int saltlen, int id, int iter, int n,
  19. unsigned char *out, const EVP_MD *md_type,
  20. OSSL_LIB_CTX *ctx, const char *propq)
  21. {
  22. int ret;
  23. unsigned char *unipass;
  24. int uniplen;
  25. if (pass == NULL) {
  26. unipass = NULL;
  27. uniplen = 0;
  28. } else if (!OPENSSL_asc2uni(pass, passlen, &unipass, &uniplen)) {
  29. ERR_raise(ERR_LIB_PKCS12, ERR_R_MALLOC_FAILURE);
  30. return 0;
  31. }
  32. ret = PKCS12_key_gen_uni_ex(unipass, uniplen, salt, saltlen, id, iter,
  33. n, out, md_type, ctx, propq);
  34. OPENSSL_clear_free(unipass, uniplen);
  35. return ret > 0;
  36. }
  37. int PKCS12_key_gen_asc(const char *pass, int passlen, unsigned char *salt,
  38. int saltlen, int id, int iter, int n,
  39. unsigned char *out, const EVP_MD *md_type)
  40. {
  41. return PKCS12_key_gen_asc_ex(pass, passlen, salt, saltlen, id, iter, n,
  42. out, md_type, NULL, NULL);
  43. }
  44. int PKCS12_key_gen_utf8_ex(const char *pass, int passlen, unsigned char *salt,
  45. int saltlen, int id, int iter, int n,
  46. unsigned char *out, const EVP_MD *md_type,
  47. OSSL_LIB_CTX *ctx, const char *propq)
  48. {
  49. int ret;
  50. unsigned char *unipass;
  51. int uniplen;
  52. if (pass == NULL) {
  53. unipass = NULL;
  54. uniplen = 0;
  55. } else if (!OPENSSL_utf82uni(pass, passlen, &unipass, &uniplen)) {
  56. ERR_raise(ERR_LIB_PKCS12, ERR_R_MALLOC_FAILURE);
  57. return 0;
  58. }
  59. ret = PKCS12_key_gen_uni_ex(unipass, uniplen, salt, saltlen, id, iter,
  60. n, out, md_type, ctx, propq);
  61. OPENSSL_clear_free(unipass, uniplen);
  62. return ret > 0;
  63. }
  64. int PKCS12_key_gen_utf8(const char *pass, int passlen, unsigned char *salt,
  65. int saltlen, int id, int iter, int n,
  66. unsigned char *out, const EVP_MD *md_type)
  67. {
  68. return PKCS12_key_gen_utf8_ex(pass, passlen, salt, saltlen, id, iter, n,
  69. out, md_type, NULL, NULL);
  70. }
  71. int PKCS12_key_gen_uni_ex(unsigned char *pass, int passlen, unsigned char *salt,
  72. int saltlen, int id, int iter, int n,
  73. unsigned char *out, const EVP_MD *md_type,
  74. OSSL_LIB_CTX *libctx, const char *propq)
  75. {
  76. int res = 0;
  77. EVP_KDF *kdf;
  78. EVP_KDF_CTX *ctx;
  79. OSSL_PARAM params[6], *p = params;
  80. if (n <= 0)
  81. return 0;
  82. kdf = EVP_KDF_fetch(libctx, "PKCS12KDF", propq);
  83. if (kdf == NULL)
  84. return 0;
  85. ctx = EVP_KDF_CTX_new(kdf);
  86. EVP_KDF_free(kdf);
  87. if (ctx == NULL)
  88. return 0;
  89. *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST,
  90. (char *)EVP_MD_get0_name(md_type),
  91. 0);
  92. *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_PASSWORD,
  93. pass, passlen);
  94. *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SALT,
  95. salt, saltlen);
  96. *p++ = OSSL_PARAM_construct_int(OSSL_KDF_PARAM_PKCS12_ID, &id);
  97. *p++ = OSSL_PARAM_construct_int(OSSL_KDF_PARAM_ITER, &iter);
  98. *p = OSSL_PARAM_construct_end();
  99. OSSL_TRACE_BEGIN(PKCS12_KEYGEN) {
  100. BIO_printf(trc_out, "PKCS12_key_gen_uni_ex(): ID %d, ITER %d\n", id, iter);
  101. BIO_printf(trc_out, "Password (length %d):\n", passlen);
  102. BIO_hex_string(trc_out, 0, passlen, pass, passlen);
  103. BIO_printf(trc_out, "\n");
  104. BIO_printf(trc_out, "Salt (length %d):\n", saltlen);
  105. BIO_hex_string(trc_out, 0, saltlen, salt, saltlen);
  106. BIO_printf(trc_out, "\n");
  107. } OSSL_TRACE_END(PKCS12_KEYGEN);
  108. if (EVP_KDF_derive(ctx, out, (size_t)n, params)) {
  109. res = 1;
  110. OSSL_TRACE_BEGIN(PKCS12_KEYGEN) {
  111. BIO_printf(trc_out, "Output KEY (length %d)\n", n);
  112. BIO_hex_string(trc_out, 0, n, out, n);
  113. BIO_printf(trc_out, "\n");
  114. } OSSL_TRACE_END(PKCS12_KEYGEN);
  115. }
  116. EVP_KDF_CTX_free(ctx);
  117. return res;
  118. }
  119. int PKCS12_key_gen_uni(unsigned char *pass, int passlen, unsigned char *salt,
  120. int saltlen, int id, int iter, int n,
  121. unsigned char *out, const EVP_MD *md_type)
  122. {
  123. return PKCS12_key_gen_uni_ex(pass, passlen, salt, saltlen, id, iter, n, out, md_type, NULL, NULL);
  124. }