kconfig: remove redundant if_block rule
[linux/fpc-iii.git] / crypto / asymmetric_keys / pkcs8_parser.c
blob5f6a7ecc9765e91934b2ede0e056088109525a0f
1 /* PKCS#8 Private Key parser [RFC 5208].
3 * Copyright (C) 2016 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public Licence
8 * as published by the Free Software Foundation; either version
9 * 2 of the Licence, or (at your option) any later version.
12 #define pr_fmt(fmt) "PKCS8: "fmt
13 #include <linux/module.h>
14 #include <linux/kernel.h>
15 #include <linux/export.h>
16 #include <linux/slab.h>
17 #include <linux/err.h>
18 #include <linux/oid_registry.h>
19 #include <keys/asymmetric-subtype.h>
20 #include <keys/asymmetric-parser.h>
21 #include <crypto/public_key.h>
22 #include "pkcs8.asn1.h"
24 struct pkcs8_parse_context {
25 struct public_key *pub;
26 unsigned long data; /* Start of data */
27 enum OID last_oid; /* Last OID encountered */
28 enum OID algo_oid; /* Algorithm OID */
29 u32 key_size;
30 const void *key;
34 * Note an OID when we find one for later processing when we know how to
35 * interpret it.
37 int pkcs8_note_OID(void *context, size_t hdrlen,
38 unsigned char tag,
39 const void *value, size_t vlen)
41 struct pkcs8_parse_context *ctx = context;
43 ctx->last_oid = look_up_OID(value, vlen);
44 if (ctx->last_oid == OID__NR) {
45 char buffer[50];
47 sprint_oid(value, vlen, buffer, sizeof(buffer));
48 pr_info("Unknown OID: [%lu] %s\n",
49 (unsigned long)value - ctx->data, buffer);
51 return 0;
55 * Note the version number of the ASN.1 blob.
57 int pkcs8_note_version(void *context, size_t hdrlen,
58 unsigned char tag,
59 const void *value, size_t vlen)
61 if (vlen != 1 || ((const u8 *)value)[0] != 0) {
62 pr_warn("Unsupported PKCS#8 version\n");
63 return -EBADMSG;
65 return 0;
69 * Note the public algorithm.
71 int pkcs8_note_algo(void *context, size_t hdrlen,
72 unsigned char tag,
73 const void *value, size_t vlen)
75 struct pkcs8_parse_context *ctx = context;
77 if (ctx->last_oid != OID_rsaEncryption)
78 return -ENOPKG;
80 ctx->pub->pkey_algo = "rsa";
81 return 0;
85 * Note the key data of the ASN.1 blob.
87 int pkcs8_note_key(void *context, size_t hdrlen,
88 unsigned char tag,
89 const void *value, size_t vlen)
91 struct pkcs8_parse_context *ctx = context;
93 ctx->key = value;
94 ctx->key_size = vlen;
95 return 0;
99 * Parse a PKCS#8 private key blob.
101 static struct public_key *pkcs8_parse(const void *data, size_t datalen)
103 struct pkcs8_parse_context ctx;
104 struct public_key *pub;
105 long ret;
107 memset(&ctx, 0, sizeof(ctx));
109 ret = -ENOMEM;
110 ctx.pub = kzalloc(sizeof(struct public_key), GFP_KERNEL);
111 if (!ctx.pub)
112 goto error;
114 ctx.data = (unsigned long)data;
116 /* Attempt to decode the private key */
117 ret = asn1_ber_decoder(&pkcs8_decoder, &ctx, data, datalen);
118 if (ret < 0)
119 goto error_decode;
121 ret = -ENOMEM;
122 pub = ctx.pub;
123 pub->key = kmemdup(ctx.key, ctx.key_size, GFP_KERNEL);
124 if (!pub->key)
125 goto error_decode;
127 pub->keylen = ctx.key_size;
128 pub->key_is_private = true;
129 return pub;
131 error_decode:
132 kfree(ctx.pub);
133 error:
134 return ERR_PTR(ret);
138 * Attempt to parse a data blob for a key as a PKCS#8 private key.
140 static int pkcs8_key_preparse(struct key_preparsed_payload *prep)
142 struct public_key *pub;
144 pub = pkcs8_parse(prep->data, prep->datalen);
145 if (IS_ERR(pub))
146 return PTR_ERR(pub);
148 pr_devel("Cert Key Algo: %s\n", pub->pkey_algo);
149 pub->id_type = "PKCS8";
151 /* We're pinning the module by being linked against it */
152 __module_get(public_key_subtype.owner);
153 prep->payload.data[asym_subtype] = &public_key_subtype;
154 prep->payload.data[asym_key_ids] = NULL;
155 prep->payload.data[asym_crypto] = pub;
156 prep->payload.data[asym_auth] = NULL;
157 prep->quotalen = 100;
158 return 0;
161 static struct asymmetric_key_parser pkcs8_key_parser = {
162 .owner = THIS_MODULE,
163 .name = "pkcs8",
164 .parse = pkcs8_key_preparse,
168 * Module stuff
170 static int __init pkcs8_key_init(void)
172 return register_asymmetric_key_parser(&pkcs8_key_parser);
175 static void __exit pkcs8_key_exit(void)
177 unregister_asymmetric_key_parser(&pkcs8_key_parser);
180 module_init(pkcs8_key_init);
181 module_exit(pkcs8_key_exit);
183 MODULE_DESCRIPTION("PKCS#8 certificate parser");
184 MODULE_LICENSE("GPL");