You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

x509.c 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*
  2. * Copyright (C) 2007 Michael Brown <mbrown@fensystems.co.uk>.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <errno.h>
  21. #include <gpxe/asn1.h>
  22. #include <gpxe/x509.h>
  23. /** @file
  24. *
  25. * X.509 certificates
  26. *
  27. * The structure of X.509v3 certificates is concisely documented in
  28. * RFC5280 section 4.1. The structure of RSA public keys is
  29. * documented in RFC2313.
  30. */
  31. /** Object Identifier for "rsaEncryption" (1.2.840.113549.1.1.1) */
  32. static const uint8_t oid_rsa_encryption[] = { 0x2a, 0x86, 0x48, 0x86, 0xf7,
  33. 0x0d, 0x01, 0x01, 0x01 };
  34. /**
  35. * Identify X.509 certificate public key
  36. *
  37. * @v certificate Certificate
  38. * @v algorithm Public key algorithm to fill in
  39. * @v pubkey Public key value to fill in
  40. * @ret rc Return status code
  41. */
  42. static int x509_public_key ( const struct asn1_cursor *certificate,
  43. struct asn1_cursor *algorithm,
  44. struct asn1_cursor *pubkey ) {
  45. struct asn1_cursor cursor;
  46. int rc;
  47. /* Locate subjectPublicKeyInfo */
  48. memcpy ( &cursor, certificate, sizeof ( cursor ) );
  49. rc = ( asn1_enter ( &cursor, ASN1_SEQUENCE ), /* Certificate */
  50. asn1_enter ( &cursor, ASN1_SEQUENCE ), /* tbsCertificate */
  51. asn1_skip ( &cursor, ASN1_EXPLICIT_TAG ), /* version */
  52. asn1_skip ( &cursor, ASN1_INTEGER ), /* serialNumber */
  53. asn1_skip ( &cursor, ASN1_SEQUENCE ), /* signature */
  54. asn1_skip ( &cursor, ASN1_SEQUENCE ), /* issuer */
  55. asn1_skip ( &cursor, ASN1_SEQUENCE ), /* validity */
  56. asn1_skip ( &cursor, ASN1_SEQUENCE ), /* name */
  57. asn1_enter ( &cursor, ASN1_SEQUENCE )/* subjectPublicKeyInfo*/);
  58. if ( rc != 0 ) {
  59. DBG ( "Cannot locate subjectPublicKeyInfo in:\n" );
  60. DBG_HDA ( 0, certificate->data, certificate->len );
  61. return rc;
  62. }
  63. /* Locate algorithm */
  64. memcpy ( algorithm, &cursor, sizeof ( *algorithm ) );
  65. rc = ( asn1_enter ( algorithm, ASN1_SEQUENCE ) /* algorithm */ );
  66. if ( rc != 0 ) {
  67. DBG ( "Cannot locate algorithm in:\n" );
  68. DBG_HDA ( 0, certificate->data, certificate->len );
  69. return rc;
  70. }
  71. /* Locate subjectPublicKey */
  72. memcpy ( pubkey, &cursor, sizeof ( *pubkey ) );
  73. rc = ( asn1_skip ( pubkey, ASN1_SEQUENCE ), /* algorithm */
  74. asn1_enter ( pubkey, ASN1_BIT_STRING ) /* subjectPublicKey*/ );
  75. if ( rc != 0 ) {
  76. DBG ( "Cannot locate subjectPublicKey in:\n" );
  77. DBG_HDA ( 0, certificate->data, certificate->len );
  78. return rc;
  79. }
  80. return 0;
  81. }
  82. /**
  83. * Identify X.509 certificate RSA modulus and public exponent
  84. *
  85. * @v certificate Certificate
  86. * @v rsa RSA public key to fill in
  87. * @ret rc Return status code
  88. *
  89. * The caller is responsible for eventually calling
  90. * x509_free_rsa_public_key() to free the storage allocated to hold
  91. * the RSA modulus and exponent.
  92. */
  93. int x509_rsa_public_key ( const struct asn1_cursor *certificate,
  94. struct x509_rsa_public_key *rsa_pubkey ) {
  95. struct asn1_cursor algorithm;
  96. struct asn1_cursor pubkey;
  97. struct asn1_cursor modulus;
  98. struct asn1_cursor exponent;
  99. int rc;
  100. /* First, extract the public key algorithm and key data */
  101. if ( ( rc = x509_public_key ( certificate, &algorithm,
  102. &pubkey ) ) != 0 )
  103. return rc;
  104. /* Check that algorithm is RSA */
  105. rc = ( asn1_enter ( &algorithm, ASN1_OID ) /* algorithm */ );
  106. if ( rc != 0 ) {
  107. DBG ( "Cannot locate algorithm:\n" );
  108. DBG_HDA ( 0, certificate->data, certificate->len );
  109. return rc;
  110. }
  111. if ( ( algorithm.len != sizeof ( oid_rsa_encryption ) ) ||
  112. ( memcmp ( algorithm.data, &oid_rsa_encryption,
  113. sizeof ( oid_rsa_encryption ) ) != 0 ) ) {
  114. DBG ( "algorithm is not rsaEncryption in:\n" );
  115. DBG_HDA ( 0, certificate->data, certificate->len );
  116. return -ENOTSUP;
  117. }
  118. /* Check that public key is a byte string, i.e. that the
  119. * "unused bits" byte contains zero.
  120. */
  121. if ( ( pubkey.len < 1 ) ||
  122. ( ( *( uint8_t * ) pubkey.data ) != 0 ) ) {
  123. DBG ( "subjectPublicKey is not a byte string in:\n" );
  124. DBG_HDA ( 0, certificate->data, certificate->len );
  125. return -ENOTSUP;
  126. }
  127. pubkey.data++;
  128. pubkey.len--;
  129. /* Pick out the modulus and exponent */
  130. rc = ( asn1_enter ( &pubkey, ASN1_SEQUENCE ) /* RSAPublicKey */ );
  131. if ( rc != 0 ) {
  132. DBG ( "Cannot locate RSAPublicKey in:\n" );
  133. DBG_HDA ( 0, certificate->data, certificate->len );
  134. return -ENOTSUP;
  135. }
  136. memcpy ( &modulus, &pubkey, sizeof ( modulus ) );
  137. rc = ( asn1_enter ( &modulus, ASN1_INTEGER ) /* modulus */ );
  138. if ( rc != 0 ) {
  139. DBG ( "Cannot locate modulus in:\n" );
  140. DBG_HDA ( 0, certificate->data, certificate->len );
  141. return -ENOTSUP;
  142. }
  143. memcpy ( &exponent, &pubkey, sizeof ( exponent ) );
  144. rc = ( asn1_skip ( &exponent, ASN1_INTEGER ), /* modulus */
  145. asn1_enter ( &exponent, ASN1_INTEGER ) /* publicExponent */ );
  146. if ( rc != 0 ) {
  147. DBG ( "Cannot locate publicExponent in:\n" );
  148. DBG_HDA ( 0, certificate->data, certificate->len );
  149. return -ENOTSUP;
  150. }
  151. /* Allocate space and copy out modulus and exponent */
  152. rsa_pubkey->modulus = malloc ( modulus.len + exponent.len );
  153. if ( ! rsa_pubkey->modulus )
  154. return -ENOMEM;
  155. rsa_pubkey->exponent = ( rsa_pubkey->modulus + modulus.len );
  156. memcpy ( rsa_pubkey->modulus, modulus.data, modulus.len );
  157. rsa_pubkey->modulus_len = modulus.len;
  158. memcpy ( rsa_pubkey->exponent, exponent.data, exponent.len );
  159. rsa_pubkey->exponent_len = exponent.len;
  160. DBG2 ( "RSA modulus:\n" );
  161. DBG2_HDA ( 0, rsa_pubkey->modulus, rsa_pubkey->modulus_len );
  162. DBG2 ( "RSA exponent:\n" );
  163. DBG2_HDA ( 0, rsa_pubkey->exponent, rsa_pubkey->exponent_len );
  164. return 0;
  165. }