00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 FILE_LICENCE ( GPL2_OR_LATER );
00020
00021 #include <stdlib.h>
00022 #include <string.h>
00023 #include <errno.h>
00024 #include <gpxe/asn1.h>
00025 #include <gpxe/x509.h>
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037 static const uint8_t oid_rsa_encryption[] = { 0x2a, 0x86, 0x48, 0x86, 0xf7,
00038 0x0d, 0x01, 0x01, 0x01 };
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048 static int x509_public_key ( const struct asn1_cursor *certificate,
00049 struct asn1_cursor *algorithm,
00050 struct asn1_cursor *pubkey ) {
00051 struct asn1_cursor cursor;
00052 int rc;
00053
00054
00055 memcpy ( &cursor, certificate, sizeof ( cursor ) );
00056 rc = ( asn1_enter ( &cursor, ASN1_SEQUENCE ),
00057 asn1_enter ( &cursor, ASN1_SEQUENCE ),
00058 asn1_skip ( &cursor, ASN1_EXPLICIT_TAG ),
00059 asn1_skip ( &cursor, ASN1_INTEGER ),
00060 asn1_skip ( &cursor, ASN1_SEQUENCE ),
00061 asn1_skip ( &cursor, ASN1_SEQUENCE ),
00062 asn1_skip ( &cursor, ASN1_SEQUENCE ),
00063 asn1_skip ( &cursor, ASN1_SEQUENCE ),
00064 asn1_enter ( &cursor, ASN1_SEQUENCE ));
00065 if ( rc != 0 ) {
00066 DBG ( "Cannot locate subjectPublicKeyInfo in:\n" );
00067 DBG_HDA ( 0, certificate->data, certificate->len );
00068 return rc;
00069 }
00070
00071
00072 memcpy ( algorithm, &cursor, sizeof ( *algorithm ) );
00073 rc = ( asn1_enter ( algorithm, ASN1_SEQUENCE ) );
00074 if ( rc != 0 ) {
00075 DBG ( "Cannot locate algorithm in:\n" );
00076 DBG_HDA ( 0, certificate->data, certificate->len );
00077 return rc;
00078 }
00079
00080
00081 memcpy ( pubkey, &cursor, sizeof ( *pubkey ) );
00082 rc = ( asn1_skip ( pubkey, ASN1_SEQUENCE ),
00083 asn1_enter ( pubkey, ASN1_BIT_STRING ) );
00084 if ( rc != 0 ) {
00085 DBG ( "Cannot locate subjectPublicKey in:\n" );
00086 DBG_HDA ( 0, certificate->data, certificate->len );
00087 return rc;
00088 }
00089
00090 return 0;
00091 }
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104 int x509_rsa_public_key ( const struct asn1_cursor *certificate,
00105 struct x509_rsa_public_key *rsa_pubkey ) {
00106 struct asn1_cursor algorithm;
00107 struct asn1_cursor pubkey;
00108 struct asn1_cursor modulus;
00109 struct asn1_cursor exponent;
00110 int rc;
00111
00112
00113 if ( ( rc = x509_public_key ( certificate, &algorithm,
00114 &pubkey ) ) != 0 )
00115 return rc;
00116
00117
00118 rc = ( asn1_enter ( &algorithm, ASN1_OID ) );
00119 if ( rc != 0 ) {
00120 DBG ( "Cannot locate algorithm:\n" );
00121 DBG_HDA ( 0, certificate->data, certificate->len );
00122 return rc;
00123 }
00124 if ( ( algorithm.len != sizeof ( oid_rsa_encryption ) ) ||
00125 ( memcmp ( algorithm.data, &oid_rsa_encryption,
00126 sizeof ( oid_rsa_encryption ) ) != 0 ) ) {
00127 DBG ( "algorithm is not rsaEncryption in:\n" );
00128 DBG_HDA ( 0, certificate->data, certificate->len );
00129 return -ENOTSUP;
00130 }
00131
00132
00133
00134
00135 if ( ( pubkey.len < 1 ) ||
00136 ( ( *( uint8_t * ) pubkey.data ) != 0 ) ) {
00137 DBG ( "subjectPublicKey is not a byte string in:\n" );
00138 DBG_HDA ( 0, certificate->data, certificate->len );
00139 return -ENOTSUP;
00140 }
00141 pubkey.data++;
00142 pubkey.len--;
00143
00144
00145 rc = ( asn1_enter ( &pubkey, ASN1_SEQUENCE ) );
00146 if ( rc != 0 ) {
00147 DBG ( "Cannot locate RSAPublicKey in:\n" );
00148 DBG_HDA ( 0, certificate->data, certificate->len );
00149 return -ENOTSUP;
00150 }
00151 memcpy ( &modulus, &pubkey, sizeof ( modulus ) );
00152 rc = ( asn1_enter ( &modulus, ASN1_INTEGER ) );
00153 if ( rc != 0 ) {
00154 DBG ( "Cannot locate modulus in:\n" );
00155 DBG_HDA ( 0, certificate->data, certificate->len );
00156 return -ENOTSUP;
00157 }
00158 memcpy ( &exponent, &pubkey, sizeof ( exponent ) );
00159 rc = ( asn1_skip ( &exponent, ASN1_INTEGER ),
00160 asn1_enter ( &exponent, ASN1_INTEGER ) );
00161 if ( rc != 0 ) {
00162 DBG ( "Cannot locate publicExponent in:\n" );
00163 DBG_HDA ( 0, certificate->data, certificate->len );
00164 return -ENOTSUP;
00165 }
00166
00167
00168 rsa_pubkey->modulus = malloc ( modulus.len + exponent.len );
00169 if ( ! rsa_pubkey->modulus )
00170 return -ENOMEM;
00171 rsa_pubkey->exponent = ( rsa_pubkey->modulus + modulus.len );
00172 memcpy ( rsa_pubkey->modulus, modulus.data, modulus.len );
00173 rsa_pubkey->modulus_len = modulus.len;
00174 memcpy ( rsa_pubkey->exponent, exponent.data, exponent.len );
00175 rsa_pubkey->exponent_len = exponent.len;
00176
00177 DBG2 ( "RSA modulus:\n" );
00178 DBG2_HDA ( 0, rsa_pubkey->modulus, rsa_pubkey->modulus_len );
00179 DBG2 ( "RSA exponent:\n" );
00180 DBG2_HDA ( 0, rsa_pubkey->exponent, rsa_pubkey->exponent_len );
00181
00182 return 0;
00183 }