rsa_asn1.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * Copyright 2000-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. /*
  10. * RSA low level APIs are deprecated for public use, but still ok for
  11. * internal use.
  12. */
  13. #include "internal/deprecated.h"
  14. #include <stdio.h>
  15. #include "internal/cryptlib.h"
  16. #include <openssl/bn.h>
  17. #include <openssl/x509.h>
  18. #include <openssl/asn1t.h>
  19. #include "rsa_local.h"
  20. /*
  21. * Override the default free and new methods,
  22. * and calculate helper products for multi-prime
  23. * RSA keys.
  24. */
  25. static int rsa_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it,
  26. void *exarg)
  27. {
  28. if (operation == ASN1_OP_NEW_PRE) {
  29. *pval = (ASN1_VALUE *)RSA_new();
  30. if (*pval != NULL)
  31. return 2;
  32. return 0;
  33. } else if (operation == ASN1_OP_FREE_PRE) {
  34. RSA_free((RSA *)*pval);
  35. *pval = NULL;
  36. return 2;
  37. } else if (operation == ASN1_OP_D2I_POST) {
  38. if (((RSA *)*pval)->version != RSA_ASN1_VERSION_MULTI) {
  39. /* not a multi-prime key, skip */
  40. return 1;
  41. }
  42. return (ossl_rsa_multip_calc_product((RSA *)*pval) == 1) ? 2 : 0;
  43. }
  44. return 1;
  45. }
  46. /* Based on definitions in RFC 8017 appendix A.1.2 */
  47. ASN1_SEQUENCE(RSA_PRIME_INFO) = {
  48. ASN1_SIMPLE(RSA_PRIME_INFO, r, CBIGNUM),
  49. ASN1_SIMPLE(RSA_PRIME_INFO, d, CBIGNUM),
  50. ASN1_SIMPLE(RSA_PRIME_INFO, t, CBIGNUM),
  51. } ASN1_SEQUENCE_END(RSA_PRIME_INFO)
  52. ASN1_SEQUENCE_cb(RSAPrivateKey, rsa_cb) = {
  53. ASN1_EMBED(RSA, version, INT32),
  54. ASN1_SIMPLE(RSA, n, BIGNUM),
  55. ASN1_SIMPLE(RSA, e, BIGNUM),
  56. ASN1_SIMPLE(RSA, d, CBIGNUM),
  57. ASN1_SIMPLE(RSA, p, CBIGNUM),
  58. ASN1_SIMPLE(RSA, q, CBIGNUM),
  59. ASN1_SIMPLE(RSA, dmp1, CBIGNUM),
  60. ASN1_SIMPLE(RSA, dmq1, CBIGNUM),
  61. ASN1_SIMPLE(RSA, iqmp, CBIGNUM),
  62. ASN1_SEQUENCE_OF_OPT(RSA, prime_infos, RSA_PRIME_INFO)
  63. } ASN1_SEQUENCE_END_cb(RSA, RSAPrivateKey)
  64. ASN1_SEQUENCE_cb(RSAPublicKey, rsa_cb) = {
  65. ASN1_SIMPLE(RSA, n, BIGNUM),
  66. ASN1_SIMPLE(RSA, e, BIGNUM),
  67. } ASN1_SEQUENCE_END_cb(RSA, RSAPublicKey)
  68. /* Free up maskHash */
  69. static int rsa_pss_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it,
  70. void *exarg)
  71. {
  72. if (operation == ASN1_OP_FREE_PRE) {
  73. RSA_PSS_PARAMS *pss = (RSA_PSS_PARAMS *)*pval;
  74. X509_ALGOR_free(pss->maskHash);
  75. }
  76. return 1;
  77. }
  78. ASN1_SEQUENCE_cb(RSA_PSS_PARAMS, rsa_pss_cb) = {
  79. ASN1_EXP_OPT(RSA_PSS_PARAMS, hashAlgorithm, X509_ALGOR,0),
  80. ASN1_EXP_OPT(RSA_PSS_PARAMS, maskGenAlgorithm, X509_ALGOR,1),
  81. ASN1_EXP_OPT(RSA_PSS_PARAMS, saltLength, ASN1_INTEGER,2),
  82. ASN1_EXP_OPT(RSA_PSS_PARAMS, trailerField, ASN1_INTEGER,3)
  83. } ASN1_SEQUENCE_END_cb(RSA_PSS_PARAMS, RSA_PSS_PARAMS)
  84. IMPLEMENT_ASN1_FUNCTIONS(RSA_PSS_PARAMS)
  85. IMPLEMENT_ASN1_DUP_FUNCTION(RSA_PSS_PARAMS)
  86. /* Free up maskHash */
  87. static int rsa_oaep_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it,
  88. void *exarg)
  89. {
  90. if (operation == ASN1_OP_FREE_PRE) {
  91. RSA_OAEP_PARAMS *oaep = (RSA_OAEP_PARAMS *)*pval;
  92. X509_ALGOR_free(oaep->maskHash);
  93. }
  94. return 1;
  95. }
  96. ASN1_SEQUENCE_cb(RSA_OAEP_PARAMS, rsa_oaep_cb) = {
  97. ASN1_EXP_OPT(RSA_OAEP_PARAMS, hashFunc, X509_ALGOR, 0),
  98. ASN1_EXP_OPT(RSA_OAEP_PARAMS, maskGenFunc, X509_ALGOR, 1),
  99. ASN1_EXP_OPT(RSA_OAEP_PARAMS, pSourceFunc, X509_ALGOR, 2),
  100. } ASN1_SEQUENCE_END_cb(RSA_OAEP_PARAMS, RSA_OAEP_PARAMS)
  101. IMPLEMENT_ASN1_FUNCTIONS(RSA_OAEP_PARAMS)
  102. IMPLEMENT_ASN1_ENCODE_FUNCTIONS_fname(RSA, RSAPrivateKey, RSAPrivateKey)
  103. IMPLEMENT_ASN1_ENCODE_FUNCTIONS_fname(RSA, RSAPublicKey, RSAPublicKey)
  104. RSA *RSAPublicKey_dup(const RSA *rsa)
  105. {
  106. return ASN1_item_dup(ASN1_ITEM_rptr(RSAPublicKey), rsa);
  107. }
  108. RSA *RSAPrivateKey_dup(const RSA *rsa)
  109. {
  110. return ASN1_item_dup(ASN1_ITEM_rptr(RSAPrivateKey), rsa);
  111. }