/security/nss/lib/certdb/xauthkid.c

http://github.com/zpao/v8monkey · C · 160 lines · 92 code · 20 blank · 48 comment · 23 complexity · 53e699ebdad1b3e048f5885f23f8e694 MD5 · raw file

  1. /* ***** BEGIN LICENSE BLOCK *****
  2. * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  3. *
  4. * The contents of this file are subject to the Mozilla Public License Version
  5. * 1.1 (the "License"); you may not use this file except in compliance with
  6. * the License. You may obtain a copy of the License at
  7. * http://www.mozilla.org/MPL/
  8. *
  9. * Software distributed under the License is distributed on an "AS IS" basis,
  10. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  11. * for the specific language governing rights and limitations under the
  12. * License.
  13. *
  14. * The Original Code is the Netscape security libraries.
  15. *
  16. * The Initial Developer of the Original Code is
  17. * Netscape Communications Corporation.
  18. * Portions created by the Initial Developer are Copyright (C) 1994-2000
  19. * the Initial Developer. All Rights Reserved.
  20. *
  21. * Contributor(s):
  22. *
  23. * Alternatively, the contents of this file may be used under the terms of
  24. * either the GNU General Public License Version 2 or later (the "GPL"), or
  25. * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  26. * in which case the provisions of the GPL or the LGPL are applicable instead
  27. * of those above. If you wish to allow use of your version of this file only
  28. * under the terms of either the GPL or the LGPL, and not to allow others to
  29. * use your version of this file under the terms of the MPL, indicate your
  30. * decision by deleting the provisions above and replace them with the notice
  31. * and other provisions required by the GPL or the LGPL. If you do not delete
  32. * the provisions above, a recipient may use your version of this file under
  33. * the terms of any one of the MPL, the GPL or the LGPL.
  34. *
  35. * ***** END LICENSE BLOCK ***** */
  36. /*
  37. * X.509 v3 Subject Key Usage Extension
  38. *
  39. */
  40. #include "prtypes.h"
  41. #include "seccomon.h"
  42. #include "secdert.h"
  43. #include "secoidt.h"
  44. #include "secasn1t.h"
  45. #include "secasn1.h"
  46. #include "secport.h"
  47. #include "certt.h"
  48. #include "genname.h"
  49. #include "secerr.h"
  50. SEC_ASN1_MKSUB(SEC_IntegerTemplate)
  51. SEC_ASN1_MKSUB(SEC_OctetStringTemplate)
  52. const SEC_ASN1Template CERTAuthKeyIDTemplate[] = {
  53. { SEC_ASN1_SEQUENCE, 0, NULL, sizeof(CERTAuthKeyID) },
  54. { SEC_ASN1_OPTIONAL | SEC_ASN1_CONTEXT_SPECIFIC | SEC_ASN1_XTRN | 0,
  55. offsetof(CERTAuthKeyID,keyID), SEC_ASN1_SUB(SEC_OctetStringTemplate)},
  56. { SEC_ASN1_OPTIONAL | SEC_ASN1_CONSTRUCTED | SEC_ASN1_CONTEXT_SPECIFIC | 1,
  57. offsetof(CERTAuthKeyID, DERAuthCertIssuer), CERT_GeneralNamesTemplate},
  58. { SEC_ASN1_OPTIONAL | SEC_ASN1_CONTEXT_SPECIFIC | SEC_ASN1_XTRN | 2,
  59. offsetof(CERTAuthKeyID,authCertSerialNumber),
  60. SEC_ASN1_SUB(SEC_IntegerTemplate) },
  61. { 0 }
  62. };
  63. SECStatus CERT_EncodeAuthKeyID (PRArenaPool *arena, CERTAuthKeyID *value, SECItem *encodedValue)
  64. {
  65. SECStatus rv = SECFailure;
  66. PORT_Assert (value);
  67. PORT_Assert (arena);
  68. PORT_Assert (value->DERAuthCertIssuer == NULL);
  69. PORT_Assert (encodedValue);
  70. do {
  71. /* If both of the authCertIssuer and the serial number exist, encode
  72. the name first. Otherwise, it is an error if one exist and the other
  73. is not.
  74. */
  75. if (value->authCertIssuer) {
  76. if (!value->authCertSerialNumber.data) {
  77. PORT_SetError (SEC_ERROR_EXTENSION_VALUE_INVALID);
  78. break;
  79. }
  80. value->DERAuthCertIssuer = cert_EncodeGeneralNames
  81. (arena, value->authCertIssuer);
  82. if (!value->DERAuthCertIssuer) {
  83. PORT_SetError (SEC_ERROR_EXTENSION_VALUE_INVALID);
  84. break;
  85. }
  86. }
  87. else if (value->authCertSerialNumber.data) {
  88. PORT_SetError (SEC_ERROR_EXTENSION_VALUE_INVALID);
  89. break;
  90. }
  91. if (SEC_ASN1EncodeItem (arena, encodedValue, value,
  92. CERTAuthKeyIDTemplate) == NULL)
  93. break;
  94. rv = SECSuccess;
  95. } while (0);
  96. return(rv);
  97. }
  98. CERTAuthKeyID *
  99. CERT_DecodeAuthKeyID (PRArenaPool *arena, SECItem *encodedValue)
  100. {
  101. CERTAuthKeyID * value = NULL;
  102. SECStatus rv = SECFailure;
  103. void * mark;
  104. SECItem newEncodedValue;
  105. PORT_Assert (arena);
  106. do {
  107. mark = PORT_ArenaMark (arena);
  108. value = (CERTAuthKeyID*)PORT_ArenaZAlloc (arena, sizeof (*value));
  109. if (value == NULL)
  110. break;
  111. value->DERAuthCertIssuer = NULL;
  112. /* copy the DER into the arena, since Quick DER returns data that points
  113. into the DER input, which may get freed by the caller */
  114. rv = SECITEM_CopyItem(arena, &newEncodedValue, encodedValue);
  115. if ( rv != SECSuccess ) {
  116. break;
  117. }
  118. rv = SEC_QuickDERDecodeItem
  119. (arena, value, CERTAuthKeyIDTemplate, &newEncodedValue);
  120. if (rv != SECSuccess)
  121. break;
  122. value->authCertIssuer = cert_DecodeGeneralNames (arena, value->DERAuthCertIssuer);
  123. if (value->authCertIssuer == NULL)
  124. break;
  125. /* what if the general name contains other format but not URI ?
  126. hl
  127. */
  128. if ((value->authCertSerialNumber.data && !value->authCertIssuer) ||
  129. (!value->authCertSerialNumber.data && value->authCertIssuer)){
  130. PORT_SetError (SEC_ERROR_EXTENSION_VALUE_INVALID);
  131. break;
  132. }
  133. } while (0);
  134. if (rv != SECSuccess) {
  135. PORT_ArenaRelease (arena, mark);
  136. return ((CERTAuthKeyID *)NULL);
  137. }
  138. PORT_ArenaUnmark(arena, mark);
  139. return (value);
  140. }