📄 keydbx.c
字号:
CRYPT_KEYSET cryptKeyset;
static const char *ldapErrorString = \
"LDAP directory read failed, probably because the standard being "
"used by the\ndirectory server differs from the one used by the "
"LDAP client software (pick\na standard, any standard). If you "
"know how the directory being used is\nconfigured, you can try "
"changing the CRYPT_OPTION_KEYS_LDAP_xxx settings to\nmatch those "
"used by the server. Processing will continue without treating\n"
"this as a fatal error.\n";
const C_STR ldapKeysetName = ldapUrlInfo[ LDAP_SERVER_NO ].url;
char ldapAttribute1[ CRYPT_MAX_TEXTSIZE + 1 ];
char ldapAttribute2[ CRYPT_MAX_TEXTSIZE + 1 ];
char certName[ CRYPT_MAX_TEXTSIZE ], caCertName[ CRYPT_MAX_TEXTSIZE ];
char crlName[ CRYPT_MAX_TEXTSIZE ];
int length, status;
/* LDAP directories come and go, check to see which one is currently
around */
puts( "Testing LDAP directory availability..." );
status = cryptKeysetOpen( &cryptKeyset, CRYPT_UNUSED, CRYPT_KEYSET_LDAP,
ldapKeysetName, CRYPT_KEYOPT_READONLY );
if( status == CRYPT_ERROR_PARAM3 )
/* LDAP keyset access not available */
return( CRYPT_ERROR_NOTAVAIL );
if( status == CRYPT_ERROR_OPEN )
{
printf( "%s not available, trying alternative directory...\n",
ldapUrlInfo[ LDAP_SERVER_NO ].asciiURL );
ldapKeysetName = ldapUrlInfo[ LDAP_ALT_SERVER_NO ].url;
status = cryptKeysetOpen( &cryptKeyset, CRYPT_UNUSED,
CRYPT_KEYSET_LDAP, ldapKeysetName,
CRYPT_KEYOPT_READONLY );
}
if( status == CRYPT_ERROR_OPEN )
{
printf( "%s not available either.\n",
ldapUrlInfo[ LDAP_ALT_SERVER_NO ].asciiURL );
puts( "None of the test LDAP directories are available. If you need "
"to test the\nLDAP capabilities, you need to set up an LDAP "
"directory that can be used\nfor the certificate store. You "
"can configure the LDAP directory using the\nLDAP_KEYSET_xxx "
"settings in test/test.h. Alternatively, if this message\n"
"took a long time to appear you may be behind a firewall that "
"blocks LDAP\ntraffic.\n" );
return( FALSE );
}
if( cryptStatusError( status ) )
{
printf( "cryptKeysetOpen() failed with error code %d, line %d.\n",
status, __LINE__ );
return( FALSE );
}
status = cryptGetAttributeString( CRYPT_UNUSED,
CRYPT_OPTION_KEYS_LDAP_OBJECTCLASS,
ldapAttribute1, &length );
if( cryptStatusOK( status ) )
{
#ifdef UNICODE_STRINGS
length /= sizeof( wchar_t );
#endif /* UNICODE_STRINGS */
ldapAttribute1[ length ] = TEXT( '\0' );
status = cryptGetAttributeString( cryptKeyset,
CRYPT_OPTION_KEYS_LDAP_OBJECTCLASS,
ldapAttribute2, &length );
}
if( cryptStatusOK( status ) )
{
#ifdef UNICODE_STRINGS
length /= sizeof( wchar_t );
#endif /* UNICODE_STRINGS */
ldapAttribute2[ length ] = TEXT( '\0' );
}
if( cryptStatusError( status ) || \
strcmp( ldapAttribute1, ldapAttribute2 ) )
{
printf( "Failed to get/set keyset attribute via equivalent global "
"attribute, error\ncode %d, value '%s', should be\n'%s', "
"line %d.\n", status, ldapAttribute2, ldapAttribute1,
__LINE__ );
return( FALSE );
}
cryptKeysetClose( cryptKeyset );
printf( " LDAP directory %s seems to be up, using that for read test.\n",
ldapKeysetName );
/* Now it gets tricky, we have to jump through all sorts of hoops to
run the tests in an automated manner since the innate incompatibility
of LDAP directory setups means that even though cryptlib's LDAP access
code retries failed queries with various options, we still need to
manually override some settings here. The simplest option is a direct
read with no special-case handling */
if( !paramStrcmp( ldapKeysetName, ldapUrlInfo[ LDAP_SERVER_NO ].url ) )
{
puts( "Testing LDAP certificate read..." );
status = testKeysetRead( CRYPT_KEYSET_LDAP, ldapKeysetName,
CRYPT_KEYID_NAME,
ldapUrlInfo[ LDAP_SERVER_NO ].certName,
CRYPT_CERTTYPE_CERTIFICATE,
READ_OPTION_NORMAL );
if( !status )
{
/* Since we can never be sure about the LDAP schema du jour, we
don't treat a failure as a fatal error */
puts( ldapErrorString );
return( FALSE );
}
/* This directory doesn't contain CRLs (or at least not at any known
location) so we skip the CRL read test */
puts( "LDAP certificate read succeeded (CRL read skipped).\n" );
return( TRUE );
}
/* The secondary LDAP directory that we're using for these tests doesn't
recognise the ';binary' modifier which is required by LDAP servers in
order to get them to work properly, we have to change the attribute
name around the read calls to the format expected by the server.
In addition because the magic formula for fetching a CRL doesn't seem
to work for certificates, the CRL read is done first */
puts( "Testing LDAP CRL read..." );
cryptGetAttributeString( CRYPT_UNUSED, CRYPT_OPTION_KEYS_LDAP_CRLNAME,
crlName, &length );
#ifdef UNICODE_STRINGS
length /= sizeof( wchar_t );
#endif /* UNICODE_STRINGS */
certName[ length ] = TEXT( '\0' );
cryptSetAttributeString( CRYPT_UNUSED, CRYPT_OPTION_KEYS_LDAP_CRLNAME,
"certificateRevocationList", 25 );
status = testKeysetRead( CRYPT_KEYSET_LDAP, ldapKeysetName,
CRYPT_KEYID_NAME,
ldapUrlInfo[ LDAP_ALT_SERVER_NO ].crlName,
CRYPT_CERTTYPE_CRL, READ_OPTION_NORMAL );
cryptSetAttributeString( CRYPT_UNUSED, CRYPT_OPTION_KEYS_LDAP_CRLNAME,
crlName, strlen( crlName ) );
if( !status )
{
/* Since we can never be sure about the LDAP schema du jour, we
don't treat a failure as a fatal error */
puts( ldapErrorString );
return( FALSE );
}
puts( "Testing LDAP certificate read..." );
cryptGetAttributeString( CRYPT_UNUSED, CRYPT_OPTION_KEYS_LDAP_CERTNAME,
certName, &length );
#ifdef UNICODE_STRINGS
length /= sizeof( wchar_t );
#endif /* UNICODE_STRINGS */
certName[ length ] = TEXT( '\0' );
cryptSetAttributeString( CRYPT_UNUSED, CRYPT_OPTION_KEYS_LDAP_CERTNAME,
"userCertificate", 15 );
cryptGetAttributeString( CRYPT_UNUSED, CRYPT_OPTION_KEYS_LDAP_CACERTNAME,
caCertName, &length );
#ifdef UNICODE_STRINGS
length /= sizeof( wchar_t );
#endif /* UNICODE_STRINGS */
certName[ length ] = TEXT( '\0' );
cryptSetAttributeString( CRYPT_UNUSED, CRYPT_OPTION_KEYS_LDAP_CACERTNAME,
"cACertificate", 13 );
status = testKeysetRead( CRYPT_KEYSET_LDAP, ldapKeysetName,
CRYPT_KEYID_NAME,
ldapUrlInfo[ LDAP_ALT_SERVER_NO ].certName,
CRYPT_CERTTYPE_CERTIFICATE, READ_OPTION_NORMAL );
cryptSetAttributeString( CRYPT_UNUSED, CRYPT_OPTION_KEYS_LDAP_CERTNAME,
certName, strlen( certName ) );
cryptSetAttributeString( CRYPT_UNUSED, CRYPT_OPTION_KEYS_LDAP_CACERTNAME,
caCertName, strlen( caCertName ) );
if( !status )
{
/* Since we can never be sure about the LDAP schema du jour, we
don't treat a failure as a fatal error */
puts( "LDAP directory read failed, probably due to the magic "
"incantatation to fetch\na certificate from this server not "
"matching the one used to fetch a CRL.\nProcessing will "
"continue without treating this as a fatal error.\n" );
return( FALSE );
}
puts( "LDAP certificate/CRL read succeeded.\n" );
return( TRUE );
}
int testWriteCertLDAP( void )
{
int status;
puts( "Testing LDAP directory write..." );
status = testKeysetWrite( CRYPT_KEYSET_LDAP,
ldapUrlInfo[ LDAP_SERVER_NO ].url );
if( status == CRYPT_ERROR_NOTAVAIL )
/* LDAP keyset access not available */
return( CRYPT_ERROR_NOTAVAIL );
if( status == CRYPT_ERROR_FAILED )
{
printf( "This is probably because you haven't set up an LDAP "
"directory for use as the\nkey store. For this test to "
"work,you need to set up a directory with the\nname "
"'%s'.\n\n", ldapUrlInfo[ LDAP_SERVER_NO ].asciiURL );
return( FALSE );
}
if( !status )
{
/* Since we can never be sure about the LDAP schema du jour, we
don't treat a failure as a fatal error */
puts( "LDAP directory write failed, probably due to the standard "
"being used by the\ndirectory differing from the one used "
"by cryptlib (pick a standard, any\nstandard). Processing "
"will continue without treating this as a fatal error.\n" );
return( FALSE );
}
puts( "LDAP directory write succeeded.\n" );
return( TRUE );
}
/* Read a certificate from a web page */
int testReadCertURL( void )
{
int status;
/* Test fetching a cert from a URL via an HTTP proxy. This requires
a random open proxy for testing (unless the site happens to be running
an HTTP proxy), www.openproxies.com will provide this */
#if 0 /* This is usually disabled because most people aren't behind HTTP
proxies, and abusing other people's misconfigured HTTP caches/
proxies for testing isn't very nice */
puts( "Testing HTTP certificate read from URL via proxy..." );
cryptSetAttributeString( CRYPT_UNUSED, CRYPT_OPTION_NET_HTTP_PROXY,
"proxy.zetwuinwest.com.pl:80", 27 );
status = testKeysetRead( CRYPT_KEYSET_HTTP, HTTP_KEYSET_CERT_NAME,
CRYPT_KEYID_NAME, "[none]",
CRYPT_CERTTYPE_CERTIFICATE, READ_OPTION_NORMAL );
if( status == CRYPT_ERROR_NOTAVAIL ) /* HTTP keyset access not avail.*/
return( CRYPT_ERROR_NOTAVAIL );
if( !status )
return( FALSE );
#endif /* 0 */
/* Test fetching a cert from a URL */
puts( "Testing HTTP certificate read from URL..." );
status = testKeysetRead( CRYPT_KEYSET_HTTP, HTTP_KEYSET_CERT_NAME,
CRYPT_KEYID_NAME, TEXT( "[none]" ),
CRYPT_CERTTYPE_CERTIFICATE, READ_OPTION_NORMAL );
if( status == CRYPT_ERROR_NOTAVAIL ) /* HTTP keyset access not avail.*/
return( CRYPT_ERROR_NOTAVAIL );
if( status == CRYPT_ERROR_FAILED )
{
puts( "This is probably because the server isn't available or "
"inaccessible.\n" );
return( TRUE );
}
if( !status )
{
puts( "If this message took a long time to appear, you may be "
"behind a firewall\nthat blocks HTTP traffic.\n" );
return( FALSE );
}
/* Test fetching a CRL from a URL */
puts( "Testing HTTP CRL read from URL..." );
status = testKeysetRead( CRYPT_KEYSET_HTTP, HTTP_KEYSET_CRL_NAME,
CRYPT_KEYID_NAME, TEXT( "[none]" ),
CRYPT_CERTTYPE_CRL, READ_OPTION_NORMAL );
if( status == CRYPT_ERROR_NOTAVAIL ) /* HTTP keyset access not avail.*/
return( CRYPT_ERROR_NOTAVAIL );
if( !status )
return( FALSE );
/* Test fetching a huge CRL from a URL, to check the ability to read
arbitrary-length HTTP data */
#if 0 /* This is usually disabled because of the CRL size */
puts( "Testing HTTP mega-CRL read from URL..." );
status = testKeysetRead( CRYPT_KEYSET_HTTP, HTTP_KEYSET_HUGECRL_NAME,
CRYPT_KEYID_NAME, "[none]", CRYPT_CERTTYPE_CRL,
READ_OPTION_NORMAL );
if( status == CRYPT_ERROR_NOTAVAIL ) /* HTTP keyset access not avail.*/
return( CRYPT_ERROR_NOTAVAIL );
if( !status )
return( FALSE );
#endif /* 0 */
puts( "HTTP certificate/CRL read from URL succeeded.\n" );
return( TRUE );
}
/* Read a certificate from an HTTP certificate store */
int testReadCertHTTP( void )
{
int status;
puts( "Testing HTTP certificate store read..." );
status = testKeysetRead( CRYPT_KEYSET_HTTP, HTTP_KEYSET_CERT_NAME,
CRYPT_KEYID_NAME, TEXT( "Verisign" ),
CRYPT_CERTTYPE_CERTIFICATE, READ_OPTION_NORMAL );
if( status == CRYPT_ERROR_NOTAVAIL ) /* HTTP keyset access not avail.*/
return( CRYPT_ERROR_NOTAVAIL );
if( status == CRYPT_ERROR_FAILED )
{
puts( "This is probably because the server isn't available or "
"inaccessible.\n" );
return( TRUE );
}
if( !status )
{
puts( "If this message took a long time to appear, you may be "
"behind a firewall\nthat blocks HTTP traffic.\n" );
return( FALSE );
}
puts( "HTTP certificate store read succeeded.\n" );
return( TRUE );
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -