Coverage Report

Created: 2018-08-29 13:53

/src/openssl/crypto/evp/evp_pkey.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 1999-2016 The OpenSSL Project Authors. All Rights Reserved.
3
 *
4
 * Licensed under the OpenSSL license (the "License").  You may not use
5
 * this file except in compliance with the License.  You can obtain a copy
6
 * in the file LICENSE in the source distribution or at
7
 * https://www.openssl.org/source/license.html
8
 */
9
10
#include <stdio.h>
11
#include <stdlib.h>
12
#include "internal/cryptlib.h"
13
#include <openssl/x509.h>
14
#include <openssl/rand.h>
15
#include "internal/asn1_int.h"
16
#include "internal/evp_int.h"
17
#include "internal/x509_int.h"
18
19
/* Extract a private key from a PKCS8 structure */
20
21
EVP_PKEY *EVP_PKCS82PKEY(const PKCS8_PRIV_KEY_INFO *p8)
22
12.8k
{
23
12.8k
    EVP_PKEY *pkey = NULL;
24
12.8k
    const ASN1_OBJECT *algoid;
25
12.8k
    char obj_tmp[80];
26
12.8k
27
12.8k
    if (!PKCS8_pkey_get0(&algoid, NULL, NULL, NULL, p8))
28
0
        return NULL;
29
12.8k
30
12.8k
    if ((pkey = EVP_PKEY_new()) == NULL) {
31
0
        EVPerr(EVP_F_EVP_PKCS82PKEY, ERR_R_MALLOC_FAILURE);
32
0
        return NULL;
33
0
    }
34
12.8k
35
12.8k
    if (!EVP_PKEY_set_type(pkey, OBJ_obj2nid(algoid))) {
36
12.8k
        EVPerr(EVP_F_EVP_PKCS82PKEY, EVP_R_UNSUPPORTED_PRIVATE_KEY_ALGORITHM);
37
12.8k
        i2t_ASN1_OBJECT(obj_tmp, 80, algoid);
38
12.8k
        ERR_add_error_data(2, "TYPE=", obj_tmp);
39
12.8k
        goto error;
40
12.8k
    }
41
17
42
17
    if (pkey->ameth->priv_decode) {
43
17
        if (!pkey->ameth->priv_decode(pkey, p8)) {
44
17
            EVPerr(EVP_F_EVP_PKCS82PKEY, EVP_R_PRIVATE_KEY_DECODE_ERROR);
45
17
            goto error;
46
17
        }
47
0
    } else {
48
0
        EVPerr(EVP_F_EVP_PKCS82PKEY, EVP_R_METHOD_NOT_SUPPORTED);
49
0
        goto error;
50
0
    }
51
0
52
0
    return pkey;
53
12.8k
54
12.8k
 error:
55
12.8k
    EVP_PKEY_free(pkey);
56
12.8k
    return NULL;
57
0
}
58
59
/* Turn a private key into a PKCS8 structure */
60
61
PKCS8_PRIV_KEY_INFO *EVP_PKEY2PKCS8(EVP_PKEY *pkey)
62
0
{
63
0
    PKCS8_PRIV_KEY_INFO *p8 = PKCS8_PRIV_KEY_INFO_new();
64
0
    if (p8  == NULL) {
65
0
        EVPerr(EVP_F_EVP_PKEY2PKCS8, ERR_R_MALLOC_FAILURE);
66
0
        return NULL;
67
0
    }
68
0
69
0
    if (pkey->ameth) {
70
0
        if (pkey->ameth->priv_encode) {
71
0
            if (!pkey->ameth->priv_encode(p8, pkey)) {
72
0
                EVPerr(EVP_F_EVP_PKEY2PKCS8, EVP_R_PRIVATE_KEY_ENCODE_ERROR);
73
0
                goto error;
74
0
            }
75
0
        } else {
76
0
            EVPerr(EVP_F_EVP_PKEY2PKCS8, EVP_R_METHOD_NOT_SUPPORTED);
77
0
            goto error;
78
0
        }
79
0
    } else {
80
0
        EVPerr(EVP_F_EVP_PKEY2PKCS8, EVP_R_UNSUPPORTED_PRIVATE_KEY_ALGORITHM);
81
0
        goto error;
82
0
    }
83
0
    return p8;
84
0
 error:
85
0
    PKCS8_PRIV_KEY_INFO_free(p8);
86
0
    return NULL;
87
0
}
88
89
/* EVP_PKEY attribute functions */
90
91
int EVP_PKEY_get_attr_count(const EVP_PKEY *key)
92
0
{
93
0
    return X509at_get_attr_count(key->attributes);
94
0
}
95
96
int EVP_PKEY_get_attr_by_NID(const EVP_PKEY *key, int nid, int lastpos)
97
0
{
98
0
    return X509at_get_attr_by_NID(key->attributes, nid, lastpos);
99
0
}
100
101
int EVP_PKEY_get_attr_by_OBJ(const EVP_PKEY *key, const ASN1_OBJECT *obj,
102
                             int lastpos)
103
0
{
104
0
    return X509at_get_attr_by_OBJ(key->attributes, obj, lastpos);
105
0
}
106
107
X509_ATTRIBUTE *EVP_PKEY_get_attr(const EVP_PKEY *key, int loc)
108
0
{
109
0
    return X509at_get_attr(key->attributes, loc);
110
0
}
111
112
X509_ATTRIBUTE *EVP_PKEY_delete_attr(EVP_PKEY *key, int loc)
113
0
{
114
0
    return X509at_delete_attr(key->attributes, loc);
115
0
}
116
117
int EVP_PKEY_add1_attr(EVP_PKEY *key, X509_ATTRIBUTE *attr)
118
0
{
119
0
    if (X509at_add1_attr(&key->attributes, attr))
120
0
        return 1;
121
0
    return 0;
122
0
}
123
124
int EVP_PKEY_add1_attr_by_OBJ(EVP_PKEY *key,
125
                              const ASN1_OBJECT *obj, int type,
126
                              const unsigned char *bytes, int len)
127
0
{
128
0
    if (X509at_add1_attr_by_OBJ(&key->attributes, obj, type, bytes, len))
129
0
        return 1;
130
0
    return 0;
131
0
}
132
133
int EVP_PKEY_add1_attr_by_NID(EVP_PKEY *key,
134
                              int nid, int type,
135
                              const unsigned char *bytes, int len)
136
0
{
137
0
    if (X509at_add1_attr_by_NID(&key->attributes, nid, type, bytes, len))
138
0
        return 1;
139
0
    return 0;
140
0
}
141
142
int EVP_PKEY_add1_attr_by_txt(EVP_PKEY *key,
143
                              const char *attrname, int type,
144
                              const unsigned char *bytes, int len)
145
0
{
146
0
    if (X509at_add1_attr_by_txt(&key->attributes, attrname, type, bytes, len))
147
0
        return 1;
148
0
    return 0;
149
0
}