host_signature.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /* Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
  2. * Use of this source code is governed by a BSD-style license that can be
  3. * found in the LICENSE file.
  4. *
  5. * Host functions for signature generation.
  6. */
  7. /* TODO: change all 'return 0', 'return 1' into meaningful return codes */
  8. #include <openssl/rsa.h>
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <sys/types.h>
  12. #include <sys/wait.h>
  13. #include <unistd.h>
  14. #include "2sysincludes.h"
  15. #include "2common.h"
  16. #include "2rsa.h"
  17. #include "2sha.h"
  18. #include "host_common.h"
  19. #include "host_signature2.h"
  20. #include "vb2_common.h"
  21. /* Invoke [external_signer] command with [pem_file] as an argument, contents of
  22. * [inbuf] passed redirected to stdin, and the stdout of the command is put
  23. * back into [outbuf]. Returns -1 on error, 0 on success.
  24. */
  25. static int sign_external(uint32_t size,
  26. const uint8_t *inbuf,
  27. uint8_t *outbuf,
  28. uint32_t outbufsize,
  29. const char *pem_file,
  30. const char *external_signer)
  31. {
  32. int rv = 0, n;
  33. int p_to_c[2], c_to_p[2]; /* pipe descriptors */
  34. pid_t pid;
  35. VB2_DEBUG("Will invoke \"%s %s\" to perform signing.\n"
  36. "Input to the signer will be provided on standard in.\n"
  37. "Output of the signer will be read from standard out.\n",
  38. external_signer, pem_file);
  39. /* Need two pipes since we want to invoke the external_signer as
  40. * a co-process writing to its stdin and reading from its stdout. */
  41. if (pipe(p_to_c) < 0 || pipe(c_to_p) < 0) {
  42. VB2_DEBUG("pipe() error\n");
  43. return -1;
  44. }
  45. if ((pid = fork()) < 0) {
  46. VB2_DEBUG("fork() error\n");
  47. return -1;
  48. } else if (pid > 0) { /* Parent. */
  49. close(p_to_c[STDIN_FILENO]);
  50. close(c_to_p[STDOUT_FILENO]);
  51. /* We provide input to the child process (external signer). */
  52. if (write(p_to_c[STDOUT_FILENO], inbuf, size) != size) {
  53. VB2_DEBUG("write() error\n");
  54. rv = -1;
  55. } else {
  56. /* Send EOF to child (signer process). */
  57. close(p_to_c[STDOUT_FILENO]);
  58. do {
  59. n = read(c_to_p[STDIN_FILENO], outbuf,
  60. outbufsize);
  61. outbuf += n;
  62. outbufsize -= n;
  63. } while (n > 0 && outbufsize);
  64. if (n < 0) {
  65. VB2_DEBUG("read() error\n");
  66. rv = -1;
  67. }
  68. }
  69. if (waitpid(pid, NULL, 0) < 0) {
  70. VB2_DEBUG("waitpid() error\n");
  71. rv = -1;
  72. }
  73. } else { /* Child. */
  74. close (p_to_c[STDOUT_FILENO]);
  75. close (c_to_p[STDIN_FILENO]);
  76. /* Map the stdin to the first pipe (this pipe gets input
  77. * from the parent) */
  78. if (STDIN_FILENO != p_to_c[STDIN_FILENO]) {
  79. if (dup2(p_to_c[STDIN_FILENO], STDIN_FILENO) !=
  80. STDIN_FILENO) {
  81. VB2_DEBUG("stdin dup2() failed\n");
  82. close(p_to_c[0]);
  83. return -1;
  84. }
  85. }
  86. /* Map the stdout to the second pipe (this pipe sends back
  87. * signer output to the parent) */
  88. if (STDOUT_FILENO != c_to_p[STDOUT_FILENO]) {
  89. if (dup2(c_to_p[STDOUT_FILENO], STDOUT_FILENO) !=
  90. STDOUT_FILENO) {
  91. VB2_DEBUG("stdout dup2() failed\n");
  92. close(c_to_p[STDOUT_FILENO]);
  93. return -1;
  94. }
  95. }
  96. /* External signer is invoked here. */
  97. if (execl(external_signer, external_signer, pem_file,
  98. (char *) 0) < 0) {
  99. VB2_DEBUG("execl() of external signer failed\n");
  100. }
  101. }
  102. return rv;
  103. }
  104. struct vb2_signature *vb2_external_signature(const uint8_t *data,
  105. uint32_t size,
  106. const char *key_file,
  107. uint32_t key_algorithm,
  108. const char *external_signer)
  109. {
  110. int vb2_alg = vb2_crypto_to_hash(key_algorithm);
  111. uint8_t digest[VB2_MAX_DIGEST_SIZE];
  112. int digest_size = vb2_digest_size(vb2_alg);
  113. uint32_t digest_info_size = 0;
  114. const uint8_t *digest_info = NULL;
  115. if (VB2_SUCCESS != vb2_digest_info(vb2_alg,
  116. &digest_info, &digest_info_size))
  117. return NULL;
  118. uint8_t *signature_digest;
  119. uint64_t signature_digest_len = digest_size + digest_info_size;
  120. int rv;
  121. /* Calculate the digest */
  122. if (VB2_SUCCESS != vb2_digest_buffer(data, size, vb2_alg,
  123. digest, sizeof(digest)))
  124. return NULL;
  125. /* Prepend the digest info to the digest */
  126. signature_digest = calloc(signature_digest_len, 1);
  127. if (!signature_digest)
  128. return NULL;
  129. memcpy(signature_digest, digest_info, digest_info_size);
  130. memcpy(signature_digest + digest_info_size, digest, digest_size);
  131. /* Allocate output signature */
  132. uint32_t sig_size =
  133. vb2_rsa_sig_size(vb2_crypto_to_signature(key_algorithm));
  134. struct vb2_signature *sig = vb2_alloc_signature(sig_size, size);
  135. if (!sig) {
  136. free(signature_digest);
  137. return NULL;
  138. }
  139. /* Sign the signature_digest into our output buffer */
  140. rv = sign_external(signature_digest_len, /* Input length */
  141. signature_digest, /* Input data */
  142. vb2_signature_data(sig), /* Output sig */
  143. sig_size, /* Max Output sig size */
  144. key_file, /* Key file to use */
  145. external_signer); /* External cmd to invoke */
  146. free(signature_digest);
  147. if (-1 == rv) {
  148. VB2_DEBUG("RSA_private_encrypt() failed.\n");
  149. free(sig);
  150. return NULL;
  151. }
  152. /* Return the signature */
  153. return sig;
  154. }