1656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project/* v3_skey.c */ 2e45f106cb6b47af1f21efe76e933bdea2f5dd1caNagendra Modadugu/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL 3656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * project 1999. 4656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project */ 5656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project/* ==================================================================== 6656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * Copyright (c) 1999 The OpenSSL Project. All rights reserved. 7656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * 8656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * Redistribution and use in source and binary forms, with or without 9656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * modification, are permitted provided that the following conditions 10656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * are met: 11656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * 12656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * 1. Redistributions of source code must retain the above copyright 13656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * notice, this list of conditions and the following disclaimer. 14656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * 15656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * 2. Redistributions in binary form must reproduce the above copyright 16656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * notice, this list of conditions and the following disclaimer in 17656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * the documentation and/or other materials provided with the 18656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * distribution. 19656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * 20656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * 3. All advertising materials mentioning features or use of this 21656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * software must display the following acknowledgment: 22656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * "This product includes software developed by the OpenSSL Project 23656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" 24656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * 25656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 26656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * endorse or promote products derived from this software without 27656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * prior written permission. For written permission, please contact 28656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * licensing@OpenSSL.org. 29656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * 30656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * 5. Products derived from this software may not be called "OpenSSL" 31656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * nor may "OpenSSL" appear in their names without prior written 32656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * permission of the OpenSSL Project. 33656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * 34656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * 6. Redistributions of any form whatsoever must retain the following 35656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * acknowledgment: 36656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * "This product includes software developed by the OpenSSL Project 37656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" 38656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * 39656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 40656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 41656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 42656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 43656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 44656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 45656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 46656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 47656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 48656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 49656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 50656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * OF THE POSSIBILITY OF SUCH DAMAGE. 51656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * ==================================================================== 52656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * 53656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * This product includes cryptographic software written by Eric Young 54656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * (eay@cryptsoft.com). This product includes software written by Tim 55656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * Hudson (tjh@cryptsoft.com). 56656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * 57656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project */ 58656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project 59656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project 60656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#include <stdio.h> 61656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#include "cryptlib.h" 62656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#include <openssl/x509v3.h> 63656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project 64656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Projectstatic ASN1_OCTET_STRING *s2i_skey_id(X509V3_EXT_METHOD *method, X509V3_CTX *ctx, char *str); 65656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Projectconst X509V3_EXT_METHOD v3_skey_id = { 66656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source ProjectNID_subject_key_identifier, 0, ASN1_ITEM_ref(ASN1_OCTET_STRING), 67656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project0,0,0,0, 68656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project(X509V3_EXT_I2S)i2s_ASN1_OCTET_STRING, 69656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project(X509V3_EXT_S2I)s2i_skey_id, 70656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project0,0,0,0, 71656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source ProjectNULL}; 72656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project 73656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Projectchar *i2s_ASN1_OCTET_STRING(X509V3_EXT_METHOD *method, 74656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project ASN1_OCTET_STRING *oct) 75656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project{ 76656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project return hex_to_string(oct->data, oct->length); 77656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project} 78656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project 79656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source ProjectASN1_OCTET_STRING *s2i_ASN1_OCTET_STRING(X509V3_EXT_METHOD *method, 80656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project X509V3_CTX *ctx, char *str) 81656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project{ 82656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project ASN1_OCTET_STRING *oct; 83656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project long length; 84656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project 85656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project if(!(oct = M_ASN1_OCTET_STRING_new())) { 86656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project X509V3err(X509V3_F_S2I_ASN1_OCTET_STRING,ERR_R_MALLOC_FAILURE); 87656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project return NULL; 88656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project } 89656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project 90656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project if(!(oct->data = string_to_hex(str, &length))) { 91656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project M_ASN1_OCTET_STRING_free(oct); 92656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project return NULL; 93656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project } 94656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project 95656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project oct->length = length; 96656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project 97656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project return oct; 98656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project 99656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project} 100656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project 101656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Projectstatic ASN1_OCTET_STRING *s2i_skey_id(X509V3_EXT_METHOD *method, 102656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project X509V3_CTX *ctx, char *str) 103656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project{ 104656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project ASN1_OCTET_STRING *oct; 105656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project ASN1_BIT_STRING *pk; 106656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project unsigned char pkey_dig[EVP_MAX_MD_SIZE]; 107656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project unsigned int diglen; 108656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project 109656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project if(strcmp(str, "hash")) return s2i_ASN1_OCTET_STRING(method, ctx, str); 110656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project 111656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project if(!(oct = M_ASN1_OCTET_STRING_new())) { 112656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project X509V3err(X509V3_F_S2I_SKEY_ID,ERR_R_MALLOC_FAILURE); 113656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project return NULL; 114656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project } 115656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project 116656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project if(ctx && (ctx->flags == CTX_TEST)) return oct; 117656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project 118656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project if(!ctx || (!ctx->subject_req && !ctx->subject_cert)) { 119656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project X509V3err(X509V3_F_S2I_SKEY_ID,X509V3_R_NO_PUBLIC_KEY); 120656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project goto err; 121656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project } 122656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project 123656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project if(ctx->subject_req) 124656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project pk = ctx->subject_req->req_info->pubkey->public_key; 125656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project else pk = ctx->subject_cert->cert_info->key->public_key; 126656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project 127656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project if(!pk) { 128656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project X509V3err(X509V3_F_S2I_SKEY_ID,X509V3_R_NO_PUBLIC_KEY); 129656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project goto err; 130656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project } 131656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project 132392aa7cc7d2b122614c5393c3e357da07fd07af3Brian Carlstrom if (!EVP_Digest(pk->data, pk->length, pkey_dig, &diglen, EVP_sha1(), NULL)) 133392aa7cc7d2b122614c5393c3e357da07fd07af3Brian Carlstrom goto err; 134656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project 135656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project if(!M_ASN1_OCTET_STRING_set(oct, pkey_dig, diglen)) { 136656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project X509V3err(X509V3_F_S2I_SKEY_ID,ERR_R_MALLOC_FAILURE); 137656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project goto err; 138656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project } 139656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project 140656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project return oct; 141656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project 142656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project err: 143656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project M_ASN1_OCTET_STRING_free(oct); 144656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project return NULL; 145656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project} 146