secalgid.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. #include "secoid.h"
  5. #include "secder.h" /* XXX remove this when remove the DERTemplate */
  6. #include "secasn1.h"
  7. #include "secitem.h"
  8. #include "secerr.h"
  9. SECOidTag
  10. SECOID_GetAlgorithmTag(const SECAlgorithmID *id)
  11. {
  12. if (id == NULL || id->algorithm.data == NULL)
  13. return SEC_OID_UNKNOWN;
  14. return SECOID_FindOIDTag(&(id->algorithm));
  15. }
  16. SECStatus
  17. SECOID_SetAlgorithmID(PLArenaPool *arena, SECAlgorithmID *id, SECOidTag which,
  18. SECItem *params)
  19. {
  20. SECOidData *oiddata;
  21. PRBool add_null_param;
  22. oiddata = SECOID_FindOIDByTag(which);
  23. if (!oiddata) {
  24. PORT_SetError(SEC_ERROR_INVALID_ALGORITHM);
  25. return SECFailure;
  26. }
  27. if (SECITEM_CopyItem(arena, &id->algorithm, &oiddata->oid))
  28. return SECFailure;
  29. switch (which) {
  30. case SEC_OID_MD2:
  31. case SEC_OID_MD4:
  32. case SEC_OID_MD5:
  33. case SEC_OID_SHA1:
  34. case SEC_OID_SHA224:
  35. case SEC_OID_SHA256:
  36. case SEC_OID_SHA384:
  37. case SEC_OID_SHA512:
  38. case SEC_OID_PKCS1_RSA_ENCRYPTION:
  39. case SEC_OID_PKCS1_MD2_WITH_RSA_ENCRYPTION:
  40. case SEC_OID_PKCS1_MD4_WITH_RSA_ENCRYPTION:
  41. case SEC_OID_PKCS1_MD5_WITH_RSA_ENCRYPTION:
  42. case SEC_OID_PKCS1_SHA1_WITH_RSA_ENCRYPTION:
  43. case SEC_OID_PKCS1_SHA224_WITH_RSA_ENCRYPTION:
  44. case SEC_OID_PKCS1_SHA256_WITH_RSA_ENCRYPTION:
  45. case SEC_OID_PKCS1_SHA384_WITH_RSA_ENCRYPTION:
  46. case SEC_OID_PKCS1_SHA512_WITH_RSA_ENCRYPTION:
  47. add_null_param = PR_TRUE;
  48. break;
  49. default:
  50. add_null_param = PR_FALSE;
  51. break;
  52. }
  53. if (params) {
  54. /*
  55. * I am specifically *not* enforcing the following assertion
  56. * (by following it up with an error and a return of failure)
  57. * because I do not want to introduce any change in the current
  58. * behavior. But I do want for us to notice if the following is
  59. * ever true, because I do not think it should be so and probably
  60. * signifies an error/bug somewhere.
  61. */
  62. PORT_Assert(!add_null_param || (params->len == 2 && params->data[0] == SEC_ASN1_NULL && params->data[1] == 0));
  63. if (SECITEM_CopyItem(arena, &id->parameters, params)) {
  64. return SECFailure;
  65. }
  66. } else {
  67. /*
  68. * Again, this is not considered an error. But if we assume
  69. * that nobody tries to set the parameters field themselves
  70. * (but always uses this routine to do that), then we should
  71. * not hit the following assertion. Unless they forgot to zero
  72. * the structure, which could also be a bad (and wrong) thing.
  73. */
  74. PORT_Assert(id->parameters.data == NULL);
  75. if (add_null_param) {
  76. (void)SECITEM_AllocItem(arena, &id->parameters, 2);
  77. if (id->parameters.data == NULL) {
  78. return SECFailure;
  79. }
  80. id->parameters.data[0] = SEC_ASN1_NULL;
  81. id->parameters.data[1] = 0;
  82. }
  83. }
  84. return SECSuccess;
  85. }
  86. SECStatus
  87. SECOID_CopyAlgorithmID(PLArenaPool *arena, SECAlgorithmID *to,
  88. const SECAlgorithmID *from)
  89. {
  90. SECStatus rv;
  91. rv = SECITEM_CopyItem(arena, &to->algorithm, &from->algorithm);
  92. if (rv)
  93. return rv;
  94. rv = SECITEM_CopyItem(arena, &to->parameters, &from->parameters);
  95. return rv;
  96. }
  97. void
  98. SECOID_DestroyAlgorithmID(SECAlgorithmID *algid, PRBool freeit)
  99. {
  100. SECITEM_FreeItem(&algid->parameters, PR_FALSE);
  101. SECITEM_FreeItem(&algid->algorithm, PR_FALSE);
  102. if (freeit == PR_TRUE)
  103. PORT_Free(algid);
  104. }
  105. SECComparison
  106. SECOID_CompareAlgorithmID(SECAlgorithmID *a, SECAlgorithmID *b)
  107. {
  108. SECComparison rv;
  109. rv = SECITEM_CompareItem(&a->algorithm, &b->algorithm);
  110. if (rv)
  111. return rv;
  112. rv = SECITEM_CompareItem(&a->parameters, &b->parameters);
  113. return rv;
  114. }