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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. FILE_LICENCE ( GPL2_OR_LATER );
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <errno.h>
  22. #include <gpxe/asn1.h>
  23. #include <gpxe/x509.h>
  24. /** @file
  25. *
  26. * X.509 certificates
  27. *
  28. * The structure of X.509v3 certificates is concisely documented in
  29. * RFC5280 section 4.1. The structure of RSA public keys is
  30. * documented in RFC2313.
  31. */
  32. /** Object Identifier for "rsaEncryption" (1.2.840.113549.1.1.1) */
  33. static const uint8_t oid_rsa_encryption[] = { 0x2a, 0x86, 0x48, 0x86, 0xf7,
  34. 0x0d, 0x01, 0x01, 0x01 };
  35. /**
  36. * Identify X.509 certificate public key
  37. *
  38. * @v certificate Certificate
  39. * @v algorithm Public key algorithm to fill in
  40. * @v pubkey Public key value to fill in
  41. * @ret rc Return status code
  42. */
  43. static int x509_public_key ( const struct asn1_cursor *certificate,
  44. struct asn1_cursor *algorithm,
  45. struct asn1_cursor *pubkey ) {
  46. struct asn1_cursor cursor;
  47. int rc;
  48. /* Locate subjectPublicKeyInfo */
  49. memcpy ( &cursor, certificate, sizeof ( cursor ) );
  50. rc = ( asn1_enter ( &cursor, ASN1_SEQUENCE ), /* Certificate */
  51. asn1_enter ( &cursor, ASN1_SEQUENCE ), /* tbsCertificate */
  52. asn1_skip ( &cursor, ASN1_EXPLICIT_TAG ), /* version */
  53. asn1_skip ( &cursor, ASN1_INTEGER ), /* serialNumber */
  54. asn1_skip ( &cursor, ASN1_SEQUENCE ), /* signature */
  55. asn1_skip ( &cursor, ASN1_SEQUENCE ), /* issuer */
  56. asn1_skip ( &cursor, ASN1_SEQUENCE ), /* validity */
  57. asn1_skip ( &cursor, ASN1_SEQUENCE ), /* name */
  58. asn1_enter ( &cursor, ASN1_SEQUENCE )/* subjectPublicKeyInfo*/);
  59. if ( rc != 0 ) {
  60. DBG ( "Cannot locate subjectPublicKeyInfo in:\n" );
  61. DBG_HDA ( 0, certificate->data, certificate->len );
  62. return rc;
  63. }
  64. /* Locate algorithm */
  65. memcpy ( algorithm, &cursor, sizeof ( *algorithm ) );
  66. rc = ( asn1_enter ( algorithm, ASN1_SEQUENCE ) /* algorithm */ );
  67. if ( rc != 0 ) {
  68. DBG ( "Cannot locate algorithm in:\n" );
  69. DBG_HDA ( 0, certificate->data, certificate->len );
  70. return rc;
  71. }
  72. /* Locate subjectPublicKey */
  73. memcpy ( pubkey, &cursor, sizeof ( *pubkey ) );
  74. rc = ( asn1_skip ( pubkey, ASN1_SEQUENCE ), /* algorithm */
  75. asn1_enter ( pubkey, ASN1_BIT_STRING ) /* subjectPublicKey*/ );
  76. if ( rc != 0 ) {
  77. DBG ( "Cannot locate subjectPublicKey in:\n" );
  78. DBG_HDA ( 0, certificate->data, certificate->len );
  79. return rc;
  80. }
  81. return 0;
  82. }
  83. /**
  84. * Identify X.509 certificate RSA modulus and public exponent
  85. *
  86. * @v certificate Certificate
  87. * @v rsa RSA public key to fill in
  88. * @ret rc Return status code
  89. *
  90. * The caller is responsible for eventually calling
  91. * x509_free_rsa_public_key() to free the storage allocated to hold
  92. * the RSA modulus and exponent.
  93. */
  94. int x509_rsa_public_key ( const struct asn1_cursor *certificate,
  95. struct x509_rsa_public_key *rsa_pubkey ) {
  96. struct asn1_cursor algorithm;
  97. struct asn1_cursor pubkey;
  98. struct asn1_cursor modulus;
  99. struct asn1_cursor exponent;
  100. int rc;
  101. /* First, extract the public key algorithm and key data */
  102. if ( ( rc = x509_public_key ( certificate, &algorithm,
  103. &pubkey ) ) != 0 )
  104. return rc;
  105. /* Check that algorithm is RSA */
  106. rc = ( asn1_enter ( &algorithm, ASN1_OID ) /* algorithm */ );
  107. if ( rc != 0 ) {
  108. DBG ( "Cannot locate algorithm:\n" );
  109. DBG_HDA ( 0, certificate->data, certificate->len );
  110. return rc;
  111. }
  112. if ( ( algorithm.len != sizeof ( oid_rsa_encryption ) ) ||
  113. ( memcmp ( algorithm.data, &oid_rsa_encryption,
  114. sizeof ( oid_rsa_encryption ) ) != 0 ) ) {
  115. DBG ( "algorithm is not rsaEncryption in:\n" );
  116. DBG_HDA ( 0, certificate->data, certificate->len );
  117. return -ENOTSUP;
  118. }
  119. /* Check that public key is a byte string, i.e. that the
  120. * "unused bits" byte contains zero.
  121. */
  122. if ( ( pubkey.len < 1 ) ||
  123. ( ( *( uint8_t * ) pubkey.data ) != 0 ) ) {
  124. DBG ( "subjectPublicKey is not a byte string in:\n" );
  125. DBG_HDA ( 0, certificate->data, certificate->len );
  126. return -ENOTSUP;
  127. }
  128. pubkey.data++;
  129. pubkey.len--;
  130. /* Pick out the modulus and exponent */
  131. rc = ( asn1_enter ( &pubkey, ASN1_SEQUENCE ) /* RSAPublicKey */ );
  132. if ( rc != 0 ) {
  133. DBG ( "Cannot locate RSAPublicKey in:\n" );
  134. DBG_HDA ( 0, certificate->data, certificate->len );
  135. return -ENOTSUP;
  136. }
  137. memcpy ( &modulus, &pubkey, sizeof ( modulus ) );
  138. rc = ( asn1_enter ( &modulus, ASN1_INTEGER ) /* modulus */ );
  139. if ( rc != 0 ) {
  140. DBG ( "Cannot locate modulus in:\n" );
  141. DBG_HDA ( 0, certificate->data, certificate->len );
  142. return -ENOTSUP;
  143. }
  144. memcpy ( &exponent, &pubkey, sizeof ( exponent ) );
  145. rc = ( asn1_skip ( &exponent, ASN1_INTEGER ), /* modulus */
  146. asn1_enter ( &exponent, ASN1_INTEGER ) /* publicExponent */ );
  147. if ( rc != 0 ) {
  148. DBG ( "Cannot locate publicExponent in:\n" );
  149. DBG_HDA ( 0, certificate->data, certificate->len );
  150. return -ENOTSUP;
  151. }
  152. /* Allocate space and copy out modulus and exponent */
  153. rsa_pubkey->modulus = malloc ( modulus.len + exponent.len );
  154. if ( ! rsa_pubkey->modulus )
  155. return -ENOMEM;
  156. rsa_pubkey->exponent = ( rsa_pubkey->modulus + modulus.len );
  157. memcpy ( rsa_pubkey->modulus, modulus.data, modulus.len );
  158. rsa_pubkey->modulus_len = modulus.len;
  159. memcpy ( rsa_pubkey->exponent, exponent.data, exponent.len );
  160. rsa_pubkey->exponent_len = exponent.len;
  161. DBG2 ( "RSA modulus:\n" );
  162. DBG2_HDA ( 0, rsa_pubkey->modulus, rsa_pubkey->modulus_len );
  163. DBG2 ( "RSA exponent:\n" );
  164. DBG2_HDA ( 0, rsa_pubkey->exponent, rsa_pubkey->exponent_len );
  165. return 0;
  166. }