x509type.c revision 296341
1132751Skan/* crypto/x509/x509type.c */
2132751Skan/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3169718Skan * All rights reserved.
445299Sobrien *
5132751Skan * This package is an SSL implementation written
6169718Skan * by Eric Young (eay@cryptsoft.com).
7132751Skan * The implementation was written so as to conform with Netscapes SSL.
8169718Skan *
952914Sobrien * This library is free for commercial and non-commercial use as long as
10169718Skan * the following conditions are aheared to.  The following conditions
11132751Skan * apply to all code found in this distribution, be it the RC4, RSA,
12169718Skan * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13132751Skan * included with this distribution is covered by the same copyright terms
14169718Skan * except that the holder is Tim Hudson (tjh@cryptsoft.com).
1552914Sobrien *
16169718Skan * Copyright remains Eric Young's, and as such any Copyright notices in
17132751Skan * the code are not to be removed.
18169718Skan * If this package is used in a product, Eric Young should be given attribution
19132751Skan * as the author of the parts of the library used.
20169718Skan * This can be in the form of a textual message at program startup or
2196340Sobrien * in documentation (online or textual) provided with the package.
22169718Skan *
23132751Skan * Redistribution and use in source and binary forms, with or without
24169718Skan * modification, are permitted provided that the following conditions
25132751Skan * are met:
26169718Skan * 1. Redistributions of source code must retain the copyright
2796340Sobrien *    notice, this list of conditions and the following disclaimer.
28169718Skan * 2. Redistributions in binary form must reproduce the above copyright
29132751Skan *    notice, this list of conditions and the following disclaimer in the
30132751Skan *    documentation and/or other materials provided with the distribution.
31132751Skan * 3. All advertising materials mentioning features or use of this software
32132751Skan *    must display the following acknowledgement:
33169718Skan *    "This product includes cryptographic software written by
34132751Skan *     Eric Young (eay@cryptsoft.com)"
35169718Skan *    The word 'cryptographic' can be left out if the rouines from the library
3652914Sobrien *    being used are not cryptographic related :-).
3752914Sobrien * 4. If you include any Windows specific code (or a derivative thereof) from
38169718Skan *    the apps directory (application code) you must include an acknowledgement:
39169718Skan *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40169718Skan *
41169718Skan * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
4245299Sobrien * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43169718Skan * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44132751Skan * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45132751Skan * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46169718Skan * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47169718Skan * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48169718Skan * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
4952914Sobrien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50169718Skan * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51169718Skan * SUCH DAMAGE.
52169718Skan *
53169718Skan * The licence and distribution terms for any publically available version or
54169718Skan * derivative of this code cannot be changed.  i.e. this code cannot simply be
55169718Skan * copied and put under another distribution licence
56169718Skan * [including the GNU Public Licence.]
57132751Skan */
58132751Skan
59169718Skan#include <stdio.h>
60169718Skan#include "cryptlib.h"
61169718Skan#include <openssl/evp.h>
6252914Sobrien#include <openssl/objects.h>
63169718Skan#include <openssl/x509.h>
64132751Skan
65132751Skanint X509_certificate_type(X509 *x, EVP_PKEY *pkey)
66132751Skan{
67169718Skan    EVP_PKEY *pk;
68169718Skan    int ret = 0, i;
69169718Skan
7052914Sobrien    if (x == NULL)
71169718Skan        return (0);
72132751Skan
73132751Skan    if (pkey == NULL)
74169718Skan        pk = X509_get_pubkey(x);
75169718Skan    else
76169718Skan        pk = pkey;
7796340Sobrien
78169718Skan    if (pk == NULL)
79169718Skan        return (0);
80169718Skan
81169718Skan    switch (pk->type) {
82169718Skan    case EVP_PKEY_RSA:
83169718Skan        ret = EVP_PK_RSA | EVP_PKT_SIGN;
84169718Skan/*              if (!sign only extension) */
85169718Skan        ret |= EVP_PKT_ENC;
86132751Skan        break;
87132751Skan    case EVP_PKEY_DSA:
88132751Skan        ret = EVP_PK_DSA | EVP_PKT_SIGN;
89169718Skan        break;
90169718Skan    case EVP_PKEY_EC:
91169718Skan        ret = EVP_PK_EC | EVP_PKT_SIGN | EVP_PKT_EXCH;
9252914Sobrien        break;
93169718Skan    case EVP_PKEY_DH:
94132751Skan        ret = EVP_PK_DH | EVP_PKT_EXCH;
95132751Skan        break;
96169718Skan    case NID_id_GostR3410_94:
97169718Skan    case NID_id_GostR3410_2001:
98169718Skan        ret = EVP_PKT_EXCH | EVP_PKT_SIGN;
9952914Sobrien        break;
100169718Skan    default:
101169718Skan        break;
102169718Skan    }
103169718Skan
104132751Skan    i = OBJ_obj2nid(x->sig_alg->algorithm);
105132751Skan    if (i && OBJ_find_sigid_algs(i, NULL, &i)) {
106169718Skan
107169718Skan        switch (i) {
108169718Skan        case NID_rsaEncryption:
109169718Skan        case NID_rsa:
110169718Skan            ret |= EVP_PKS_RSA;
11152914Sobrien            break;
112169718Skan        case NID_dsa:
113132751Skan        case NID_dsa_2:
114132751Skan            ret |= EVP_PKS_DSA;
115169718Skan            break;
116132751Skan        case NID_X9_62_id_ecPublicKey:
117169718Skan            ret |= EVP_PKS_EC;
11852914Sobrien            break;
11945299Sobrien        default:
120169718Skan            break;
121169718Skan        }
122169718Skan    }
123132751Skan
124169718Skan    if (pkey == NULL)
12545299Sobrien        EVP_PKEY_free(pk);
126169718Skan    return (ret);
127169718Skan}
128169718Skan