LCOV - code coverage report
Current view: top level - ballet/x509 - fd_x509_mock.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 31 31 100.0 %
Date: 2024-11-13 11:58:15 Functions: 4 4 100.0 %

          Line data    Source code
       1             : #define _GNU_SOURCE  /* memmem */
       2             : #include "fd_x509_mock.h"
       3             : #include <string.h>  /* memmem */
       4             : 
       5             : static uchar const
       6             : fd_x509_mock_tpl[ FD_X509_MOCK_CERT_SZ ] = {
       7             :   /* Certificate SEQUENCE (3 elem) */
       8             :   0x30, 0x81, 0xf6,
       9             : 
      10             :     /* tbsCertificate TBSCertificate SEQUENCE (8 elem) */
      11             :     0x30, 0x81, 0xa9,
      12             : 
      13             :       /* version [0] (1 elem)  */
      14             :       0xa0, 0x03,
      15             :         /* INTEGER  2 */
      16             :         0x02, 0x01, 0x02,
      17             : 
      18             :       /* serialNumber CertificateSerialNumber INTEGER (62 bit) */
      19             :       0x02, 0x08,
      20             :       0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
      21             : 
      22             :       /* signature AlgorithmIdentifier SEQUENCE (1 elem) */
      23             :       0x30, 0x05,
      24             :         /* algorithm OBJECT IDENTIFIER 1.3.101.112 curveEd25519 (EdDSA 25519 signature algorithm) */
      25             :         0x06, 0x03, 0x2b, 0x65, 0x70,
      26             : 
      27             :       /* issuer Name SEQUENCE (1 elem) */
      28             :       0x30, 0x16,
      29             :         /* RelativeDistinguishedName SET (1 elem) */
      30             :         0x31, 0x14,
      31             :           /* AttributeTypeAndValue SEQUENCE (2 elem) */
      32             :           0x30, 0x12,
      33             :             /* type AttributeType OBJECT IDENTIFIER 2.5.4.3 commonName (X.520 DN component) */
      34             :             0x06, 0x03, 0x55, 0x04, 0x03,
      35             :             /* value AttributeValue [?] UTF8String Solana node */
      36             :             0x0c, 0x0b, 0x53, 0x6f, 0x6c, 0x61, 0x6e, 0x61, 0x20, 0x6e, 0x6f, 0x64, 0x65,
      37             : 
      38             :       /* validity Validity SEQUENCE (2 elem) */
      39             :       0x30, 0x20,
      40             :         /* notBefore Time UTCTime 1970-01-01 00:00:00 UTC */
      41             :         0x17, 0x0d, 0x37, 0x30, 0x30, 0x31, 0x30, 0x31, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x5a,
      42             :         /* notAfter Time GeneralizedTime 4096-01-01 00:00:00 UTC */
      43             :         0x18, 0x0f, 0x34, 0x30, 0x39, 0x36, 0x30, 0x31, 0x30, 0x31, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x5a,
      44             : 
      45             :       /* subject Name SEQUENCE (0 elem) */
      46             :       0x30, 0x00,
      47             : 
      48             :       /* subjectPublicKeyInfo SubjectPublicKeyInfo SEQUENCE (2 elem) */
      49             :       0x30, 0x2a,
      50             :         /* algorithm AlgorithmIdentifier SEQUENCE (1 elem) */
      51             :         0x30, 0x05,
      52             :           /* algorithm OBJECT IDENTIFIER 1.3.101.112 curveEd25519 (EdDSA 25519 signature algorithm) */
      53             :           0x06, 0x03, 0x2b, 0x65, 0x70,
      54             :         /* subjectPublicKey BIT STRING (256 bit) */
      55             :         0x03, 0x21, 0x00,
      56    42137643 :         #define FD_X509_MOCK_PUBKEY_OFF (0x64)
      57             :         0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
      58             :         0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
      59             :         0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
      60             :         0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
      61             : 
      62             :       /* extensions [3] (1 elem) */
      63             :       0xa3, 0x29,
      64             :         /* Extensions SEQUENCE (2 elem) */
      65             :         0x30, 0x27,
      66             :           /* Extension SEQUENCE (3 elem) */
      67             :           0x30, 0x17,
      68             :             /* extnID OBJECT IDENTIFIER 2.5.29.17 subjectAltName (X.509 extension) */
      69             :             0x06, 0x03, 0x55, 0x1d, 0x11,
      70             :             /* critical BOOLEAN true */
      71             :             0x01, 0x01, 0xff,
      72             :             /* extnValue OCTET STRING (13 byte) encapsulating */
      73             :             0x04, 0x0d,
      74             :               /* SEQUENCE (1 elem) */
      75             :               0x30, 0x0b,
      76             :               /* [2] (9 byte) localhost */
      77             :               0x82, 0x09,
      78             :               0x6c, 0x6f, 0x63, 0x61, 0x6c, 0x68, 0x6f, 0x73, 0x74,
      79             : 
      80             :           /* Extension SEQUENCE (3 elem) */
      81             :           0x30, 0x0c,
      82             :             /* extnID OBJECT IDENTIFIER 2.5.29.19 basicConstraints (X.509 extension) */
      83             :             0x06, 0x03, 0x55, 0x1d, 0x13,
      84             :             /* critical BOOLEAN true */
      85             :             0x01, 0x01, 0xff,
      86             :             /* extnValue OCTET STRING (2 byte) encapsulating */
      87             :             0x04, 0x02,
      88             :               /* SEQUENCE (0 elem) */
      89             :               0x30, 0x00,
      90             : 
      91             :     /* signatureAlgorithm AlgorithmIdentifier SEQUENCE (1 elem) */
      92             :     0x30, 0x05,
      93             :       /* algorithm OBJECT IDENTIFIER 1.3.101.112 curveEd25519 (EdDSA 25519 signature algorithm) */
      94             :       0x06, 0x03, 0x2b, 0x65, 0x70,
      95             :       /* signature BIT STRING (512 bit) */
      96             :       0x03, 0x41, 0x00,
      97             :       0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
      98             :       0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
      99             :       0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
     100             :       0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
     101             :       0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
     102             :       0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
     103             :       0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
     104             :       0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
     105             : };
     106             : 
     107             : /* Agave v1.18 uses a different template.  It is less
     108             :    determistic due to a variable length serial number, so we match the
     109             :    prefix before the public key. */
     110             : 
     111             : static uchar const
     112             : fd_x509_mock_v1_prefix[] = {
     113             :   0x30, 0x05, 0x06, 0x03, 0x2b, 0x65, 0x70, 0x30,
     114             :   0x16, 0x31, 0x14, 0x30, 0x12, 0x06, 0x03, 0x55,
     115             :   0x04, 0x03, 0x0c, 0x0b, 0x53, 0x6f, 0x6c, 0x61,
     116             :   0x6e, 0x61, 0x20, 0x6e, 0x6f, 0x64, 0x65, 0x30,
     117             :   0x20, 0x17, 0x0d, 0x37, 0x35, 0x30, 0x31, 0x30,
     118             :   0x31, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x5a,
     119             :   0x18, 0x0f, 0x34, 0x30, 0x39, 0x36, 0x30, 0x31,
     120             :   0x30, 0x31, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
     121             :   0x5a, 0x30, 0x16, 0x31, 0x14, 0x30, 0x12, 0x06,
     122             :   0x03, 0x55, 0x04, 0x03, 0x0c, 0x0b, 0x53, 0x6f,
     123             :   0x6c, 0x61, 0x6e, 0x61, 0x20, 0x6e, 0x6f, 0x64,
     124             :   0x65, 0x30, 0x2a, 0x30, 0x05, 0x06, 0x03, 0x2b,
     125             :   0x65, 0x70, 0x03, 0x21, 0x00
     126             : };
     127             : 
     128             : void
     129             : fd_x509_mock_cert( uchar buf[ static FD_X509_MOCK_CERT_SZ ],
     130      303324 :                    uchar public_key[ static 32 ] ) {
     131      303324 :   fd_memcpy( buf, fd_x509_mock_tpl, FD_X509_MOCK_CERT_SZ );
     132      303324 :   fd_memcpy( buf+FD_X509_MOCK_PUBKEY_OFF, public_key, 32UL );
     133      303324 : }
     134             : 
     135             : static uchar const *
     136             : fd_x509_mock_pubkey_v1( uchar const * cert,
     137    19500105 :                         ulong         cert_sz ) {
     138    19500105 :   uchar const * end   = cert + cert_sz;
     139    19500105 :   uchar const * match = memmem( cert, cert_sz, fd_x509_mock_v1_prefix, sizeof(fd_x509_mock_v1_prefix) );
     140    19500105 :   if( !match ) return NULL;
     141         105 :   uchar const * pubkey = match + sizeof(fd_x509_mock_v1_prefix);
     142         105 :   if( FD_UNLIKELY( (ulong)pubkey+32UL > (ulong)end ) ) return NULL;
     143           9 :   return pubkey;
     144         105 : }
     145             : 
     146             : static uchar const *
     147             : fd_x509_mock_pubkey_v2( uchar const * cert,
     148    19500096 :                         ulong         cert_sz ) {
     149    19500096 :   if( cert_sz != FD_X509_MOCK_CERT_SZ ) return NULL;
     150             : 
     151    19500000 :   ulong off = 0UL;
     152    19500000 :   int match0 = (0==memcmp( cert+off, fd_x509_mock_tpl+off, FD_X509_MOCK_PUBKEY_OFF ) );
     153    19500000 :   off += FD_X509_MOCK_PUBKEY_OFF;
     154    19500000 :   off += 32UL;
     155    19500000 :   int match1 = (0==memcmp( cert+off, fd_x509_mock_tpl+off, FD_X509_MOCK_CERT_SZ-off ) );
     156             : 
     157    19500000 :   if( (!match0) | (!match1) ) return NULL;
     158             : 
     159     2834319 :   return cert + FD_X509_MOCK_PUBKEY_OFF;
     160    19500000 : }
     161             : 
     162             : uchar const *
     163             : fd_x509_mock_pubkey( uchar const * cert,
     164    19500105 :                      ulong         cert_sz ) {
     165    19500105 :   uchar const * match;
     166    19500105 :   match = fd_x509_mock_pubkey_v1( cert, cert_sz );
     167    19500105 :   if( match ) return match;
     168    19500096 :   match = fd_x509_mock_pubkey_v2( cert, cert_sz );
     169    19500096 :   if( match ) return match;
     170    16665777 :   return NULL;
     171    19500096 : }

Generated by: LCOV version 1.14