rsa_saos.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * Copyright 1995-2020 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/rsa.h>
  18. #include <openssl/objects.h>
  19. #include <openssl/x509.h>
  20. int RSA_sign_ASN1_OCTET_STRING(int type,
  21. const unsigned char *m, unsigned int m_len,
  22. unsigned char *sigret, unsigned int *siglen,
  23. RSA *rsa)
  24. {
  25. ASN1_OCTET_STRING sig;
  26. int i, j, ret = 1;
  27. unsigned char *p, *s;
  28. sig.type = V_ASN1_OCTET_STRING;
  29. sig.length = m_len;
  30. sig.data = (unsigned char *)m;
  31. i = i2d_ASN1_OCTET_STRING(&sig, NULL);
  32. j = RSA_size(rsa);
  33. if (i > (j - RSA_PKCS1_PADDING_SIZE)) {
  34. RSAerr(RSA_F_RSA_SIGN_ASN1_OCTET_STRING,
  35. RSA_R_DIGEST_TOO_BIG_FOR_RSA_KEY);
  36. return 0;
  37. }
  38. s = OPENSSL_malloc((unsigned int)j + 1);
  39. if (s == NULL) {
  40. RSAerr(RSA_F_RSA_SIGN_ASN1_OCTET_STRING, ERR_R_MALLOC_FAILURE);
  41. return 0;
  42. }
  43. p = s;
  44. i2d_ASN1_OCTET_STRING(&sig, &p);
  45. i = RSA_private_encrypt(i, s, sigret, rsa, RSA_PKCS1_PADDING);
  46. if (i <= 0)
  47. ret = 0;
  48. else
  49. *siglen = i;
  50. OPENSSL_clear_free(s, (unsigned int)j + 1);
  51. return ret;
  52. }
  53. int RSA_verify_ASN1_OCTET_STRING(int dtype,
  54. const unsigned char *m,
  55. unsigned int m_len, unsigned char *sigbuf,
  56. unsigned int siglen, RSA *rsa)
  57. {
  58. int i, ret = 0;
  59. unsigned char *s;
  60. const unsigned char *p;
  61. ASN1_OCTET_STRING *sig = NULL;
  62. if (siglen != (unsigned int)RSA_size(rsa)) {
  63. RSAerr(RSA_F_RSA_VERIFY_ASN1_OCTET_STRING,
  64. RSA_R_WRONG_SIGNATURE_LENGTH);
  65. return 0;
  66. }
  67. s = OPENSSL_malloc((unsigned int)siglen);
  68. if (s == NULL) {
  69. RSAerr(RSA_F_RSA_VERIFY_ASN1_OCTET_STRING, ERR_R_MALLOC_FAILURE);
  70. goto err;
  71. }
  72. i = RSA_public_decrypt((int)siglen, sigbuf, s, rsa, RSA_PKCS1_PADDING);
  73. if (i <= 0)
  74. goto err;
  75. p = s;
  76. sig = d2i_ASN1_OCTET_STRING(NULL, &p, (long)i);
  77. if (sig == NULL)
  78. goto err;
  79. if (((unsigned int)sig->length != m_len) ||
  80. (memcmp(m, sig->data, m_len) != 0)) {
  81. RSAerr(RSA_F_RSA_VERIFY_ASN1_OCTET_STRING, RSA_R_BAD_SIGNATURE);
  82. } else {
  83. ret = 1;
  84. }
  85. err:
  86. ASN1_OCTET_STRING_free(sig);
  87. OPENSSL_clear_free(s, (unsigned int)siglen);
  88. return ret;
  89. }