parse_key_file.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * Copyright (C) 2011-2018 Intel Corporation. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * * Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * * Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in
  12. * the documentation and/or other materials provided with the
  13. * distribution.
  14. * * Neither the name of Intel Corporation nor the names of its
  15. * contributors may be used to endorse or promote products derived
  16. * from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. *
  30. */
  31. /**
  32. * File:
  33. * parse_key_file.cpp
  34. * Description:
  35. * Parse the RSA key file that user inputs
  36. * to get the key type and RSA structure.
  37. */
  38. #include "parse_key_file.h"
  39. #include "se_trace.h"
  40. #include "util_st.h"
  41. #include <stdio.h>
  42. #include <stdlib.h>
  43. #include <string.h>
  44. #include <assert.h>
  45. #include <openssl/pem.h>
  46. //parse_key_file():
  47. // parse the RSA key file
  48. //Return Value:
  49. // true: success
  50. // false: fail
  51. bool parse_key_file(int mode, const char *key_path, RSA **prsa, int *pkey_type)
  52. {
  53. assert(prsa != NULL && pkey_type != NULL);
  54. if(key_path == NULL)
  55. {
  56. *pkey_type = NO_KEY;
  57. return false;
  58. }
  59. FILE *fp = fopen(key_path, "rb");
  60. if(fp == NULL)
  61. {
  62. se_trace(SE_TRACE_ERROR, OPEN_FILE_ERROR, key_path);
  63. return false;
  64. }
  65. int key_type = UNIDENTIFIABLE_KEY;
  66. RSA *rsa = NULL;
  67. if(mode == SIGN)
  68. {
  69. rsa = PEM_read_RSAPrivateKey(fp, NULL, NULL, NULL);
  70. fclose(fp);
  71. if(!rsa)
  72. {
  73. se_trace(SE_TRACE_ERROR, KEY_FORMAT_ERROR);
  74. return false;
  75. }
  76. key_type = PRIVATE_KEY;
  77. }
  78. else if(mode == CATSIG)
  79. {
  80. rsa = PEM_read_RSA_PUBKEY(fp, NULL, NULL, NULL);
  81. fclose(fp);
  82. if(!rsa)
  83. {
  84. se_trace(SE_TRACE_ERROR, KEY_FORMAT_ERROR);
  85. return false;
  86. }
  87. key_type = PUBLIC_KEY;
  88. }
  89. else
  90. {
  91. se_trace(SE_TRACE_ERROR, "ERROR: Invalid command\n %s", USAGE_STRING);
  92. fclose(fp);
  93. return false;
  94. }
  95. // Check the key size and exponent
  96. if(BN_num_bytes(rsa->n) != N_SIZE_IN_BYTES)
  97. {
  98. se_trace(SE_TRACE_ERROR, INVALID_KEYSIZE_ERROR);
  99. RSA_free(rsa);
  100. return false;
  101. }
  102. char *p = BN_bn2dec(rsa->e);
  103. if(memcmp(p, "3", 2))
  104. {
  105. se_trace(SE_TRACE_ERROR, INVALID_EXPONENT_ERROR);
  106. OPENSSL_free(p);
  107. RSA_free(rsa);
  108. return false;
  109. }
  110. OPENSSL_free(p);
  111. *prsa = rsa;
  112. *pkey_type = key_type;
  113. return true;
  114. }