rsa_asn1.c 3.8 KB

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