証明書から公開鍵を抽出するために使用するコードを次に示します。公開鍵のピン留めに使用するため、接続から を使用しSSL*
ます。
基本的にlen1 = i2d_X509_PUBKEY(X509_get_X509_PUBKEY(cert), NULL)
、長さを取得するために 1 回呼び出し、次にlen2 = i2d_X509_PUBKEY(X509_get_X509_PUBKEY(cert), buffer)
証明書を取得するために 2 回呼び出します。基本的なサニティ チェックとしてlen1
andを使用して、予想されるバイト数が返されたことを確認できます。len2
int pin_peer_pubkey(SSL* ssl)
{
if(NULL == ssl) return FALSE;
X509* cert = NULL;
/* Scratch */
int len1 = 0, len2 = 0;
unsigned char *buff1 = NULL;
/* Result is returned to caller */
int ret = 0, result = FALSE;
do
{
/* http://www.openssl.org/docs/ssl/SSL_get_peer_certificate.html */
cert = SSL_get_peer_certificate(ssl);
if(!(cert != NULL))
break; /* failed */
/* Begin Gyrations to get the subjectPublicKeyInfo */
/* Thanks to Viktor Dukhovni on the OpenSSL mailing list */
/* http://groups.google.com/group/mailing.openssl.users/browse_thread/thread/d61858dae102c6c7 */
len1 = i2d_X509_PUBKEY(X509_get_X509_PUBKEY(cert), NULL);
if(!(len1 > 0))
break; /* failed */
/* scratch */
unsigned char* temp = NULL;
/* http://www.openssl.org/docs/crypto/buffer.html */
buff1 = temp = OPENSSL_malloc(len1);
if(!(buff1 != NULL))
break; /* failed */
/* http://www.openssl.org/docs/crypto/d2i_X509.html */
len2 = i2d_X509_PUBKEY(X509_get_X509_PUBKEY(cert), &temp);
/* These checks are verifying we got back the same values as when we sized the buffer. */
/* Its pretty weak since they should always be the same. But it gives us something to test. */
if(!((len1 == len2) && (temp != NULL) && ((temp - buff1) == len1)))
break; /* failed */
/* End Gyrations */
/* Do something with the public key */
...
ret = TRUE;
} while(0);
/* http://www.openssl.org/docs/crypto/buffer.html */
if(NULL != buff1)
OPENSSL_free(buff1);
/* http://www.openssl.org/docs/crypto/X509_new.html */
if(NULL != cert)
X509_free(cert);
return result;
}
SOAP で WSSE を使用して、Ruby で Base 64 でエンコードされた公開鍵を送信する...
まあ、それは解決できるプロトコルの詳細です。生のバイトとして送信するか、Base64 エンコード ( RFC 4648、セクション 4) または Base64URL エンコード ( RFC 4648 、セクション 5) して送信できます。それはあなた次第です。