PageRenderTime 61ms CodeModel.GetById 6ms RepoModel.GetById 1ms app.codeStats 0ms

/security/nss/lib/pkcs12/p12plcy.c

https://bitbucket.org/soko/mozilla-central
C | 157 lines | 97 code | 23 blank | 37 comment | 22 complexity | 67f0a4d27bf1896ac65f25fba3da1416 MD5 | raw file
Possible License(s): GPL-2.0, JSON, 0BSD, LGPL-3.0, AGPL-1.0, MIT, MPL-2.0-no-copyleft-exception, BSD-3-Clause, LGPL-2.1, Apache-2.0
  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. #include "p12plcy.h"
  37. #include "secoid.h"
  38. #include "secport.h"
  39. #include "secpkcs5.h"
  40. #define PKCS12_NULL 0x0000
  41. typedef struct pkcs12SuiteMapStr {
  42. SECOidTag algTag;
  43. unsigned int keyLengthBits; /* in bits */
  44. unsigned long suite;
  45. PRBool allowed;
  46. PRBool preferred;
  47. } pkcs12SuiteMap;
  48. static pkcs12SuiteMap pkcs12SuiteMaps[] = {
  49. { SEC_OID_RC4, 40, PKCS12_RC4_40, PR_FALSE, PR_FALSE},
  50. { SEC_OID_RC4, 128, PKCS12_RC4_128, PR_FALSE, PR_FALSE},
  51. { SEC_OID_RC2_CBC, 40, PKCS12_RC2_CBC_40, PR_FALSE, PR_TRUE},
  52. { SEC_OID_RC2_CBC, 128, PKCS12_RC2_CBC_128, PR_FALSE, PR_FALSE},
  53. { SEC_OID_DES_CBC, 64, PKCS12_DES_56, PR_FALSE, PR_FALSE},
  54. { SEC_OID_DES_EDE3_CBC, 192, PKCS12_DES_EDE3_168, PR_FALSE, PR_FALSE},
  55. { SEC_OID_UNKNOWN, 0, PKCS12_NULL, PR_FALSE, PR_FALSE},
  56. { SEC_OID_UNKNOWN, 0, 0L, PR_FALSE, PR_FALSE}
  57. };
  58. /* determine if algid is an algorithm which is allowed */
  59. PRBool
  60. SEC_PKCS12DecryptionAllowed(SECAlgorithmID *algid)
  61. {
  62. unsigned int keyLengthBits;
  63. SECOidTag algId;
  64. int i;
  65. algId = SEC_PKCS5GetCryptoAlgorithm(algid);
  66. if(algId == SEC_OID_UNKNOWN) {
  67. return PR_FALSE;
  68. }
  69. keyLengthBits = (unsigned int)(SEC_PKCS5GetKeyLength(algid) * 8);
  70. i = 0;
  71. while(pkcs12SuiteMaps[i].algTag != SEC_OID_UNKNOWN) {
  72. if((pkcs12SuiteMaps[i].algTag == algId) &&
  73. (pkcs12SuiteMaps[i].keyLengthBits == keyLengthBits)) {
  74. return pkcs12SuiteMaps[i].allowed;
  75. }
  76. i++;
  77. }
  78. return PR_FALSE;
  79. }
  80. /* is any encryption allowed? */
  81. PRBool
  82. SEC_PKCS12IsEncryptionAllowed(void)
  83. {
  84. int i;
  85. i = 0;
  86. while(pkcs12SuiteMaps[i].algTag != SEC_OID_UNKNOWN) {
  87. if(pkcs12SuiteMaps[i].allowed == PR_TRUE) {
  88. return PR_TRUE;
  89. }
  90. i++;
  91. }
  92. return PR_FALSE;
  93. }
  94. SECStatus
  95. SEC_PKCS12EnableCipher(long which, int on)
  96. {
  97. int i;
  98. i = 0;
  99. while(pkcs12SuiteMaps[i].suite != 0L) {
  100. if(pkcs12SuiteMaps[i].suite == (unsigned long)which) {
  101. if(on) {
  102. pkcs12SuiteMaps[i].allowed = PR_TRUE;
  103. } else {
  104. pkcs12SuiteMaps[i].allowed = PR_FALSE;
  105. }
  106. return SECSuccess;
  107. }
  108. i++;
  109. }
  110. return SECFailure;
  111. }
  112. SECStatus
  113. SEC_PKCS12SetPreferredCipher(long which, int on)
  114. {
  115. int i;
  116. PRBool turnedOff = PR_FALSE;
  117. PRBool turnedOn = PR_FALSE;
  118. i = 0;
  119. while(pkcs12SuiteMaps[i].suite != 0L) {
  120. if(pkcs12SuiteMaps[i].preferred == PR_TRUE) {
  121. pkcs12SuiteMaps[i].preferred = PR_FALSE;
  122. turnedOff = PR_TRUE;
  123. }
  124. if(pkcs12SuiteMaps[i].suite == (unsigned long)which) {
  125. pkcs12SuiteMaps[i].preferred = PR_TRUE;
  126. turnedOn = PR_TRUE;
  127. }
  128. i++;
  129. }
  130. if((turnedOn) && (turnedOff)) {
  131. return SECSuccess;
  132. }
  133. return SECFailure;
  134. }